Univention Bugzilla – Attachment 10986 Details for
Bug 55179
ucs-school-purge-expired-users can't handle school_admins
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
MWE for a working script without using the ImportUser factory
dataport-purge-expired-users (text/x-python), 5.54 KB, created by
Oliver Friedrich
on 2022-09-07 16:33:18 CEST
(
hide
)
Description:
MWE for a working script without using the ImportUser factory
Filename:
MIME Type:
Creator:
Oliver Friedrich
Created:
2022-09-07 16:33:18 CEST
Size:
5.54 KB
patch
obsolete
>#!/usr/bin/python3 ># -*- coding: utf-8 -*- ># ># Univention UCS@school ># Copyright 2017-2022 Univention GmbH ># ># https://www.univention.de/ ># ># All rights reserved. ># ># The source code of this program is made available ># under the terms of the GNU Affero General Public License version 3 ># (GNU AGPL V3) as published by the Free Software Foundation. ># ># Binary versions of this program provided by Univention to you as ># well as other copyrighted, protected or trademarked materials like ># Logos, graphics, fonts, specific documentations and configurations, ># cryptographic keys etc. are subject to a license agreement between ># you and Univention and not subject to the GNU AGPL V3. ># ># In the case you use this program under the terms of the GNU AGPL V3, ># the program is provided in the hope that it will be useful, ># but WITHOUT ANY WARRANTY; without even the implied warranty of ># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ># GNU Affero General Public License for more details. ># ># You should have received a copy of the GNU Affero General Public ># License with the Debian GNU/Linux or Univention distribution in file ># /usr/share/common-licenses/AGPL-3; if not, see ># <http://www.gnu.org/licenses/>. > >""" >Tool to delete users whose deletion grace time (ucsschoolPurgeTimestamp) has passed. >""" > >import datetime >import logging >import pprint >import sys >from argparse import ArgumentParser > >from ldap.filter import filter_format >from ucsschool.importer.frontend.user_import_cmdline import \ > UserImportCommandLine >from ucsschool.importer.utils.ldap_connection import get_admin_connection >from ucsschool.lib.models.user import User >from ucsschool.lib.models.utils import UniStreamHandler, ucr > > >def parse_cmdline(): > defaults = dict(dry_run=False, logfile=None, verbose=False) > parser = ArgumentParser( > description="UCS@school delete tool for expired user accounts (ucsschoolPurgeTimestamp)" > ) > parser.add_argument( > "-c", > "--conffile", > help="Configuration file to use (see /usr/share/doc/ucs-school-import for an explanation on " > "configuration file stacking).", > ) > parser.add_argument("-l", "--logfile", help="Write to additional logfile (always verbose).") > parser.add_argument( > "-n", > "--dry-run", > dest="dry_run", > action="store_true", > help="Dry-run: don't actually commit changes to LDAP [default: %(default)s].", > ) > parser.add_argument( > "-q", > "--quiet", > action="store_true", > help="Disable output on the console except errors [default: %(default)s].", > ) > parser.add_argument( > "-v", > "--verbose", > action="store_true", > help="Enable debugging output on the console (overwrites setting from configuration files) " > "[default: %(default)s].", > ) > parser.set_defaults(**defaults) > > args = parser.parse_args() > if args.quiet and args.verbose: > parser.error('Options "quiet" and "verbose" are mutually exclusive.') > return args > > >def purge_timestamp2date(purge_timestamp): > return datetime.datetime.strptime(purge_timestamp, "%Y%m%d%H%M%SZ") > > >def shadow_expire2date(shadow_expire): > return datetime.datetime.utcfromtimestamp(int(shadow_expire) * 3600 * 24).date() > > >def main(): > # this closely follows ucsschool.importer.frontend.cmdline.CommandLine.main() > uic = UserImportCommandLine() > uic.args = parse_cmdline() > # overwrite 'verbose' setting from configuration files > uic.args.settings = { > "disabled_checks": ["test_00_required_config_keys"], > "dry_run": uic.args.dry_run, > "verbose": uic.args.verbose, > } > # set logging configured by cmdline > uic.setup_logging(uic.args.verbose, uic.args.logfile) > logger = uic.logger > if uic.args.quiet: > loglevel = logging.ERROR > elif uic.args.verbose: > loglevel = logging.DEBUG > else: > loglevel = logging.INFO > logger.setLevel(loglevel) > for handler in logger.handlers: > if isinstance(handler, UniStreamHandler): > handler.setLevel(loglevel) > > logger.info("------ UCS@school import tool starting ------") > if uic.args.conffile: > uic.configuration_files.append(uic.args.conffile) > else: > # prevent InitialisationError > uic.args.settings["source_uid"] = "PurgeExpiredUsers" > config = uic.setup_config() > > logger.info("------ UCS@school import tool configured ------") > logger.debug("Configuration is:\n%s", pprint.pformat(config)) > lo, po = get_admin_connection() > > > today_s = datetime.datetime.today().strftime("%Y%m%d%H%M%SZ") > filter_s = filter_format("(&(objectClass=ucsschoolType)(ucsschoolPurgeTimestamp<=%s))", (today_s,)) > expired_accounts = lo.search( > filter_s, attr=["ucsschoolPurgeTimestamp", "shadowExpire", "objectClass"] > ) > > logger.info("Found %d expired accounts.", len(expired_accounts)) > for dn, attr in expired_accounts: > logger.debug( > "dn=%r ucsschoolPurgeTimestamp=%r shadowExpire=%r", > dn, > purge_timestamp2date(attr["ucsschoolPurgeTimestamp"][0].decode("UTF-8")).strftime( > "%Y-%m-%d" > ), > shadow_expire2date(attr["shadowExpire"][0].decode("ASCII")).strftime("%Y-%m-%d") > if attr.get("shadowExpire") > else None, > ) > logger.info("%sDeleting %r...", "(dry-run) " if uic.args.dry_run else "", dn) > > obj = User.from_dn(dn, None, lo) > if not uic.args.dry_run: > obj.remove_without_hooks(lo) > > logger.info("Finished.") > > >if __name__ == "__main__": > sys.exit(main())
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 55179
: 10986