]> Frank Brehm's Git Trees - pixelpark/admin-tools.git/commitdiff
Get list of current users from DB
authorFrank Brehm <frank.brehm@pixelpark.com>
Thu, 11 Jan 2018 17:35:00 +0000 (18:35 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Thu, 11 Jan 2018 17:35:00 +0000 (18:35 +0100)
pp_lib/dnsui_users.py

index f81b31de2af680ba6df1d8c87fc23543deb3b957..368202f703daec79f7453ca29785b7662922c3ad 100644 (file)
@@ -24,7 +24,7 @@ from .common import pp
 
 from .ldap_app import PpLdapAppError, PpLdapApplication
 
-__version__ = '0.3.1'
+__version__ = '0.3.2'
 LOG = logging.getLogger(__name__)
 
 
@@ -61,6 +61,8 @@ class DnsuiUsersApp(PpLdapApplication):
         self.db_user = self.default_db_user
         self.db_pass = None
 
+        self.db_users = []
+
         self.db_connection = None
 
         self._show_simulate_opt = True
@@ -199,6 +201,7 @@ class DnsuiUsersApp(PpLdapApplication):
 
             self.get_admin_user_dns()
             self.get_admin_users()
+            self.get_db_users()
 
         finally:
             self._close_db()
@@ -279,7 +282,45 @@ class DnsuiUsersApp(PpLdapApplication):
             }
             self.admin_users.append(user)
 
-        LOG.debug("Found admin user:\n{}".format(pp(self.admin_users)))
+        LOG.debug("Found admin users:\n{}".format(pp(self.admin_users)))
+
+    # -------------------------------------------------------------------------
+    def get_db_users(self):
+
+        LOG.info("Get list of current users in DB.")
+
+        self.db_users = []
+
+        sql = textwrap.dedent('''\
+            SELECT id, uid, name, email, active, admin, developer
+              FROM public.user
+             WHERE auth_realm = 'LDAP'
+             ORDER BY uid
+            ''').strip()
+        if self.verbose > 1:
+            LOG.debug("SQL:\n{}".format(sql))
+
+        with self.db_connection.cursor() as db_cursor:
+
+            db_cursor.execute(sql)
+            results = db_cursor.fetchall()
+
+            if self.verbose > 2:
+                LOG.debug("Got users:\n{}".format(pp(results)))
+
+            for result in results:
+                user = {
+                    'id': result[0],
+                    'uid': result[1],
+                    'name': result[2],
+                    'email': result[3],
+                    'active': result[4],
+                    'admin': result[5],
+                    'developer': result[6],
+                }
+                self.db_users.append(user)
+
+        LOG.debug("Found database users:\n{}".format(pp(self.db_users)))
 
     # -------------------------------------------------------------------------
     def _close_db(self):