|
60 | 60 | from pycroft.model.types import IPAddress, InvalidMACAddressException
|
61 | 61 | from pycroft.model.user import User, IllegalEmailError, IllegalLoginError
|
62 | 62 | from web.blueprints.mpskclient import get_mpsk_client_or_404
|
| 63 | +from web.template_filters import require |
63 | 64 |
|
64 | 65 | api = Api()
|
65 | 66 |
|
@@ -940,3 +941,44 @@ def patch(self, token: str, password: str) -> ResponseReturnValue:
|
940 | 941 |
|
941 | 942 |
|
942 | 943 | 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