Skip to content

Commit 6e704b7

Browse files
committed
added active member endpoint
added api endpoint for validating if user is an active member
1 parent ff08052 commit 6e704b7

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

pycroft/lib/user/info.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class UserStatus(t.NamedTuple):
1919
member: bool
2020
traffic_exceeded: bool
2121
network_access: bool
22+
is_active: bool
2223
wifi_access: bool
2324
account_balanced: bool
2425
violation: bool
@@ -29,10 +30,12 @@ class UserStatus(t.NamedTuple):
2930
def status(user: User) -> UserStatus:
3031
has_interface = any(h.interfaces for h in user.hosts)
3132
has_access = user.has_property("network_access")
33+
is_active = user.has_property("active_member")
3234
return UserStatus(
3335
member=user.has_property("member"),
3436
traffic_exceeded=user.has_property("traffic_limit_exceeded"),
3537
network_access=has_access and has_interface,
38+
is_active=is_active,
3639
wifi_access=user.has_wifi_access and has_access,
3740
account_balanced=user_has_paid(user),
3841
violation=user.has_property("violation"),

web/api/v0/__init__.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
from pycroft.model.types import IPAddress, InvalidMACAddressException
6161
from pycroft.model.user import User, IllegalEmailError, IllegalLoginError
6262
from web.blueprints.mpskclient import get_mpsk_client_or_404
63+
from web.template_filters import require
6364

6465
api = Api()
6566

@@ -940,3 +941,44 @@ def patch(self, token: str, password: str) -> ResponseReturnValue:
940941

941942

942943
api.add_resource(ResetPasswordResource, '/user/reset-password')
944+
945+
946+
class UserActiveResource(Resource):
947+
@use_kwargs(
948+
{
949+
"name": fields.Str(required=True),
950+
"byear": fields.Int(required=False, missing=None),
951+
"uid": fields.Int(required=True),
952+
},
953+
location="form",
954+
)
955+
def get(self, name: str, byear: int, uid: int) -> ResponseReturnValue:
956+
"""
957+
Validates whether the given combination of username, user ID, and optional birth year belongs to a user who is part of the `active_member` group.
958+
959+
Parameters:
960+
name (str): The username to validate.
961+
byear (int, optional): The user's birth year. Can be omitted.
962+
uid (int): User ID.
963+
964+
Returns:
965+
ResponseReturnValue: A JSON-encoded string with {"response": True} if the user belongs to the
966+
`active_member` group, otherwise {"response": False}.
967+
"""
968+
user = session.session.get(User, uid)
969+
970+
if user is None:
971+
return jsonify({"response": False})
972+
user_status = status(user)
973+
has_birthday = not bool(byear)
974+
if user.birthdate and byear:
975+
if user.birthdate.year == byear:
976+
has_birthday = True
977+
978+
if user_status.is_active and name == user.name and has_birthday:
979+
return jsonify({"response": True})
980+
981+
return jsonify({"response": False})
982+
983+
984+
api.add_resource(UserActiveResource, "/user/active")

0 commit comments

Comments
 (0)