diff --git a/pycroft/lib/user/__init__.py b/pycroft/lib/user/__init__.py index b08c99dc8..f202f58cf 100644 --- a/pycroft/lib/user/__init__.py +++ b/pycroft/lib/user/__init__.py @@ -1,7 +1,4 @@ from ._old import ( - get_blocked_groups, - block, - unblock, send_password_reset_mail, ) from .user_id import ( @@ -11,6 +8,11 @@ decode_type2_user_id, check_user_id, ) +from .blocking import ( + block, + unblock, + get_blocked_groups, +) from .edit import ( edit_name, edit_email, diff --git a/pycroft/lib/user/_old.py b/pycroft/lib/user/_old.py index 15ebc13c8..2f26df4d5 100644 --- a/pycroft/lib/user/_old.py +++ b/pycroft/lib/user/_old.py @@ -12,21 +12,13 @@ import os -from pycroft import config -from pycroft.helpers.i18n import deferred_gettext -from pycroft.helpers.interval import Interval, starting_from from pycroft.helpers.user import generate_random_str -from pycroft.helpers.utc import DateTimeTz -from pycroft.lib.logging import log_user_event from pycroft.lib.mail import ( UserResetPasswordTemplate, ) -from pycroft.lib.membership import make_member_of, remove_member_of -from pycroft.model import session from pycroft.model.session import with_transaction from pycroft.model.user import ( User, - PropertyGroup, ) from .mail import user_send_mail @@ -35,78 +27,6 @@ password_reset_url = os.getenv('PASSWORD_RESET_URL') -def get_blocked_groups() -> list[PropertyGroup]: - return [config.violation_group, config.payment_in_default_group, - config.blocked_group] - - -@with_transaction -def block( - user: User, - reason: str, - processor: User, - during: Interval[DateTimeTz] = None, - violation: bool = True, -) -> User: - """Suspend a user during a given interval. - - The user is added to violation_group or blocked_group in a given - interval. A reason needs to be provided. - - :param user: The user to be suspended. - :param reason: The reason for suspending. - :param processor: The admin who suspended the user. - :param during: The interval in which the user is - suspended. If None the user will be suspendeded from now on - without an upper bound. - :param violation: If the user should be added to the violation group - - :return: The suspended user. - """ - if during is None: - during = starting_from(session.utcnow()) - - if violation: - make_member_of(user, config.violation_group, processor, during) - else: - make_member_of(user, config.blocked_group, processor, during) - - message = deferred_gettext("Suspended during {during}. Reason: {reason}.") - log_user_event(message=message.format(during=during, reason=reason) - .to_json(), author=processor, user=user) - return user - - -@with_transaction -def unblock(user: User, processor: User, when: DateTimeTz | None = None) -> User: - """Unblocks a user. - - This removes his membership of the violation, blocken and payment_in_default - group. - - Note that for unblocking, no further asynchronous action has to be - triggered, as opposed to e.g. membership termination. - - :param user: The user to be unblocked. - :param processor: The admin who unblocked the user. - :param when: The time of membership termination. Note - that in comparison to :py:func:`suspend`, you don't provide an - _interval_, but a point in time, defaulting to the current - time. Will be converted to ``starting_from(when)``. - - :return: The unblocked user. - """ - if when is None: - when = session.utcnow() - - during = starting_from(when) - for group in get_blocked_groups(): - if user.member_of(group, when=during): - remove_member_of(user=user, group=group, processor=processor, during=during) - - return user - - @with_transaction def send_password_reset_mail(user: User) -> bool: user.password_reset_token = generate_random_str(64) diff --git a/pycroft/lib/user/blocking.py b/pycroft/lib/user/blocking.py new file mode 100644 index 000000000..052db4a90 --- /dev/null +++ b/pycroft/lib/user/blocking.py @@ -0,0 +1,96 @@ +# Copyright (c) 2015 The Pycroft Authors. See the AUTHORS file. +# This file is part of the Pycroft project and licensed under the terms of +# the Apache License, Version 2.0. See the LICENSE file for details. +""" +pycroft.lib.user +~~~~~~~~~~~~~~~~ + +This module contains. + +:copyright: (c) 2012 by AG DSN. +""" + +from pycroft import config +from pycroft.helpers.i18n import deferred_gettext +from pycroft.helpers.interval import Interval, starting_from +from pycroft.helpers.utc import DateTimeTz +from pycroft.lib.logging import log_user_event +from pycroft.lib.membership import make_member_of, remove_member_of +from pycroft.model import session +from pycroft.model.session import with_transaction +from pycroft.model.user import ( + User, + PropertyGroup, +) + + +@with_transaction +def block( + user: User, + reason: str, + processor: User, + during: Interval[DateTimeTz] = None, + violation: bool = True, +) -> User: + """Suspend a user during a given interval. + + The user is added to violation_group or blocked_group in a given + interval. A reason needs to be provided. + + :param user: The user to be suspended. + :param reason: The reason for suspending. + :param processor: The admin who suspended the user. + :param during: The interval in which the user is + suspended. If None the user will be suspendeded from now on + without an upper bound. + :param violation: If the user should be added to the violation group + + :return: The suspended user. + """ + if during is None: + during = starting_from(session.utcnow()) + + if violation: + make_member_of(user, config.violation_group, processor, during) + else: + make_member_of(user, config.blocked_group, processor, during) + + message = deferred_gettext("Suspended during {during}. Reason: {reason}.") + log_user_event( + message=message.format(during=during, reason=reason).to_json(), author=processor, user=user + ) + return user + + +@with_transaction +def unblock(user: User, processor: User, when: DateTimeTz | None = None) -> User: + """Unblocks a user. + + This removes his membership of the violation, blocken and payment_in_default + group. + + Note that for unblocking, no further asynchronous action has to be + triggered, as opposed to e.g. membership termination. + + :param user: The user to be unblocked. + :param processor: The admin who unblocked the user. + :param when: The time of membership termination. Note + that in comparison to :py:func:`suspend`, you don't provide an + _interval_, but a point in time, defaulting to the current + time. Will be converted to ``starting_from(when)``. + + :return: The unblocked user. + """ + if when is None: + when = session.utcnow() + + during = starting_from(when) + for group in get_blocked_groups(): + if user.member_of(group, when=during): + remove_member_of(user=user, group=group, processor=processor, during=during) + + return user + + +def get_blocked_groups() -> list[PropertyGroup]: + return [config.violation_group, config.payment_in_default_group, config.blocked_group]