]> Frank Brehm's Git Trees - pixelpark/admin-tools.git/commitdiff
Obviously finished
authorFrank Brehm <frank.brehm@pixelpark.com>
Fri, 12 Jan 2018 12:51:22 +0000 (13:51 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Fri, 12 Jan 2018 12:51:22 +0000 (13:51 +0100)
pp_lib/dnsui_users.py

index 0c04368b5de34e464fd1229b1641d6c09d3acae9..aec44b069c8c49fcd78fe22eabedab550945ee60 100644 (file)
@@ -25,7 +25,7 @@ from .common import pp
 
 from .ldap_app import PpLdapAppError, PpLdapApplication
 
-__version__ = '0.4.2'
+__version__ = '0.4.3'
 LOG = logging.getLogger(__name__)
 
 
@@ -492,9 +492,13 @@ class DnsuiUsersApp(PpLdapApplication):
                     LOG.debug("Data uf user {n!r} ({u}) are still correct.".format(
                         n=db_user['name'], u=uid))
             else:
-                LOG.warn("DB user {n!r} ({u}) does not exists anymore, will be dectivated.".format(
-                    n=db_user['name'], u=uid))
-                self.db_users_deactivate.append(db_id)
+                if db_user['active'] != 0:
+                    LOG.warn("DB user {n!r} ({u}) does not exists anymore, will be dectivated.".format(
+                        n=db_user['name'], u=uid))
+                    self.db_users_deactivate.append(db_id)
+                else:
+                    LOG.debug("User {n!r} ({u}) is already dectivated.".format(
+                        n=db_user['name'], u=uid))
 
     # -------------------------------------------------------------------------
     def insert_db_users(self):
@@ -508,8 +512,8 @@ class DnsuiUsersApp(PpLdapApplication):
             LOG.debug("User data to insert:\n{}".format(pp(self.users_to_add)))
 
         sql = textwrap.dedent('''\
-            INSERT INTO public.user (uid, name, email, active, admin, developer)
-                VALUES (%(uid)s, %(name)s, %(email)s, %(active)s, %(admin)s, %(developer)s)
+            INSERT INTO public.user (uid, name, email, auth_realm, active, admin, developer)
+                VALUES (%(uid)s, %(name)s, %(email)s, 'LDAP', %(active)s, %(admin)s, %(developer)s)
             ''').strip()
         if self.verbose > 1:
             LOG.debug("Insert SQL:\n{}".format(sql))
@@ -518,12 +522,17 @@ class DnsuiUsersApp(PpLdapApplication):
 
             for db_user in self.users_to_add:
 
+                LOG.warn("Adding user {n!r} ({u}) ...".format(n=db_user['name'], u=db_user['uid']))
+
                 if self.verbose > 1:
                     show_sql = db_cursor.mogrify(sql, db_user)
                     LOG.debug("Executing:\n{}".format(show_sql))
                 if not self.simulate:
                     db_cursor.execute(sql, db_user)
 
+        LOG.debug("Commiting changes ...")
+        self.db_connection.commit()
+
     # -------------------------------------------------------------------------
     def change_db_users(self):
 
@@ -535,6 +544,46 @@ class DnsuiUsersApp(PpLdapApplication):
         if self.verbose > 1:
             LOG.debug("User data to update:\n{}".format(pp(self.users_to_update)))
 
+        with self.db_connection.cursor() as db_cursor:
+
+            for db_user in self.users_to_update:
+
+                #LOG.warn("Updating user {n!r} ({u}) ...".format(n=db_user['name'], u=db_user['uid']))
+                msg = "Udating user db id {}:".format(db_user['id'])
+
+                sql = 'UPDATE public.user SET'
+                updates = []
+                msg_list = []
+                if 'name' in db_user:
+                    updates.append(' name = %(name)s')
+                    msg_list.append("name = {!r}".format(db_user['name']))
+                if 'email' in db_user:
+                    updates.append(' email = %(email)s')
+                    msg_list.append("email = {!r}".format(db_user['email']))
+                if 'active' in db_user:
+                    updates.append(' active = %(active)s')
+                    msg_list.append("active = {!r}".format(db_user['active']))
+                if 'admin' in db_user:
+                    updates.append(' admin = %(admin)s')
+                    msg_list.append("admin = {!r}".format(db_user['admin']))
+                if 'developer' in db_user:
+                    updates.append(' developer = %(developer)s')
+                    msg_list.append("developer = {!r}".format(db_user['developer']))
+                sql += ', '.join(updates)
+                sql += ' WHERE id = %(id)s'
+                msg += ' ' + ', '.join(msg_list)
+
+                LOG.warn(msg)
+
+                if self.verbose > 1:
+                    show_sql = db_cursor.mogrify(sql, db_user)
+                    LOG.debug("Executing:\n{}".format(show_sql))
+                if not self.simulate:
+                    db_cursor.execute(sql, db_user)
+
+        LOG.debug("Commiting changes ...")
+        self.db_connection.commit()
+
     # -------------------------------------------------------------------------
     def deactivate_db_users(self):
 
@@ -546,6 +595,20 @@ class DnsuiUsersApp(PpLdapApplication):
         if self.verbose > 1:
             LOG.debug("User Ids to deactivate:\n{}".format(pp(self.db_users_deactivate)))
 
+        sql = "UPDATE public.user SET active = 0 WHERE id = %s"
+
+        with self.db_connection.cursor() as db_cursor:
+
+            for db_id in self.db_users_deactivate:
+                if self.verbose > 1:
+                    show_sql = db_cursor.mogrify(sql, (db_id, ))
+                    LOG.debug("Executing:\n{}".format(show_sql))
+                if not self.simulate:
+                    db_cursor.execute(sql, (db_id, ))
+
+        LOG.debug("Commiting changes ...")
+        self.db_connection.commit()
+
     # -------------------------------------------------------------------------
     def _close_db(self):