from .ldap_app import PpLdapAppError, PpLdapApplication
-__version__ = '0.4.2'
+__version__ = '0.4.3'
LOG = logging.getLogger(__name__)
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):
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))
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):
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):
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):