Skip to content

Commit

Permalink
extract lib.user.blocking
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasjuhrich committed Sep 20, 2024
1 parent c6c728d commit 325a20e
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 83 deletions.
8 changes: 5 additions & 3 deletions pycroft/lib/user/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
from ._old import (
get_blocked_groups,
block,
unblock,
send_password_reset_mail,
)
from .user_id import (
Expand All @@ -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,
Expand Down
80 changes: 0 additions & 80 deletions pycroft/lib/user/_old.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
96 changes: 96 additions & 0 deletions pycroft/lib/user/blocking.py
Original file line number Diff line number Diff line change
@@ -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]

0 comments on commit 325a20e

Please sign in to comment.