From: Frank Brehm Date: Thu, 11 Jan 2018 17:35:00 +0000 (+0100) Subject: Get list of current users from DB X-Git-Tag: 0.1.2~54 X-Git-Url: https://git.uhu-banane.de/?a=commitdiff_plain;h=f68080022c739b18fba5b4ce1364d6c6fbe828c9;p=pixelpark%2Fadmin-tools.git Get list of current users from DB --- diff --git a/pp_lib/dnsui_users.py b/pp_lib/dnsui_users.py index f81b31d..368202f 100644 --- a/pp_lib/dnsui_users.py +++ b/pp_lib/dnsui_users.py @@ -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):