...
 
Commits (2)
#!/usr/bin/env python
import sys
import os
base = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
if os.path.isdir(base):
sys.path.insert(0, base)
from argparse import ArgumentParser
import logging
logger = logging.getLogger(__name__)
parser = ArgumentParser()
parser.add_argument('-c', '--config-dir', help='Directory containing configuration files.')
parser.add_argument('-d', '--data-dir', help='Directory containing server.')
parser.add_argument('-v', '--verbose', action="store_true", help="Verbose output.")
parser.add_argument('--yes-delete', action="store_true", help='Yes, actually delete stuff.')
parser.add_argument("user", help="User identifier (name/email)")
args = parser.parse_args()
logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO)
import vispa
import vispa.models
from sqlalchemy.orm import scoped_session, sessionmaker
vispa.setup_config(args.config_dir)
session = scoped_session(sessionmaker(autoflush=True, autocommit=False))
session.configure(bind=vispa.models.open_database(args.data_dir))
from vispa.models.user import User
from vispa.models.group import group_manager_association, Group_User_Assoc
from vispa.models.project import project_manager_association, project_user_assoc_role_association
from vispa.models.workgroup import workgroup_manager_association, workgroup_user_association
from vispa.models.workspace import Workspace
from vispa.models.jsondata import JSONData
user = User.get(session, args.user)
for obj in [
Workspace,
workgroup_manager_association,
workgroup_user_association,
project_manager_association,
project_user_assoc_role_association,
JSONData,
group_manager_association,
Group_User_Assoc,
]:
tab = getattr(obj, "__table__", obj)
q = session.query(tab).filter_by(user_id=user.id)
n = q.count()
logger.info("%3d entries in %s", n, tab)
if n:
logger.debug("%r", q.all())
if args.yes_delete:
q.delete(synchronize_session=False)
print(user.name)
if args.yes_delete:
session.delete(user)
try:
session.commit()
except Exception as e:
session.rollback()
raise e
......@@ -50,16 +50,16 @@ class LDAPExport(object):
self.connection.search(self.user_base, "(objectClass=posixAccount)", attributes=['uid'])
for ldap_user in self.connection.entries:
if ldap_user.uid not in active_users:
logger.info("Deleting unknown ldap user: %s", ldap_user.entry_get_dn())
self.connection.delete(ldap_user.entry_get_dn())
logger.info("Deleting unknown ldap user: %s", ldap_user.entry_dn)
self.connection.delete(ldap_user.entry_dn)
self.connection.search(self.group_base, "(objectClass=posixGroup)", attributes=['cn'])
for ldap_group in self.connection.entries:
private_group = self.private_group and ldap_group.cn in active_users
public_group = ldap_group.cn in groups
if not private_group and not public_group:
logger.info("Deleting unknown ldap group: %s", ldap_group.entry_get_dn())
self.connection.delete(ldap_group.entry_get_dn())
logger.info("Deleting unknown ldap group: %s", ldap_group.entry_dn)
self.connection.delete(ldap_group.entry_dn)
except:
logger.exception("remove_invalid_ldap_entries")
......