|
| 1 | +from datetime import datetime, timezone |
| 2 | +from typing import List, Optional |
| 3 | +import logging |
| 4 | +from bson import ObjectId |
| 5 | + |
| 6 | +from todo.models.user_role import UserRoleModel |
| 7 | +from todo.repositories.common.mongo_repository import MongoRepository |
| 8 | +from todo.constants.role import RoleScope, RoleName |
| 9 | + |
| 10 | +logger = logging.getLogger(__name__) |
| 11 | + |
| 12 | + |
| 13 | +class UserRoleRepository(MongoRepository): |
| 14 | + collection_name = UserRoleModel.collection_name |
| 15 | + |
| 16 | + @classmethod |
| 17 | + def create(cls, user_role: UserRoleModel) -> UserRoleModel: |
| 18 | + collection = cls.get_collection() |
| 19 | + |
| 20 | + role_name_value = user_role.role_name.value if hasattr(user_role.role_name, "value") else user_role.role_name |
| 21 | + scope_value = user_role.scope.value if hasattr(user_role.scope, "value") else user_role.scope |
| 22 | + |
| 23 | + # Check if already exists and is active |
| 24 | + existing = collection.find_one( |
| 25 | + { |
| 26 | + "user_id": user_role.user_id, |
| 27 | + "role_name": role_name_value, |
| 28 | + "scope": scope_value, |
| 29 | + "team_id": user_role.team_id, |
| 30 | + "is_active": True, |
| 31 | + } |
| 32 | + ) |
| 33 | + |
| 34 | + if existing: |
| 35 | + return UserRoleModel(**existing) |
| 36 | + |
| 37 | + user_role.created_at = datetime.now(timezone.utc) |
| 38 | + user_role_dict = user_role.model_dump(mode="json", by_alias=True, exclude_none=True) |
| 39 | + result = collection.insert_one(user_role_dict) |
| 40 | + user_role.id = result.inserted_id |
| 41 | + return user_role |
| 42 | + |
| 43 | + @classmethod |
| 44 | + def get_user_roles( |
| 45 | + cls, user_id: Optional[str] = None, scope: Optional["RoleScope"] = None, team_id: Optional[str] = None |
| 46 | + ) -> List[UserRoleModel]: |
| 47 | + collection = cls.get_collection() |
| 48 | + |
| 49 | + query = {"is_active": True} |
| 50 | + |
| 51 | + if user_id: |
| 52 | + query["user_id"] = user_id |
| 53 | + |
| 54 | + if scope: |
| 55 | + scope_value = scope.value if hasattr(scope, "value") else scope |
| 56 | + query["scope"] = scope_value |
| 57 | + |
| 58 | + if team_id: |
| 59 | + query["team_id"] = team_id |
| 60 | + elif scope and (scope.value if hasattr(scope, "value") else scope) == "GLOBAL": |
| 61 | + query["team_id"] = None |
| 62 | + |
| 63 | + roles = [] |
| 64 | + for doc in collection.find(query): |
| 65 | + roles.append(UserRoleModel(**doc)) |
| 66 | + return roles |
| 67 | + |
| 68 | + @classmethod |
| 69 | + def assign_role( |
| 70 | + cls, user_id: str, role_name: "RoleName", scope: "RoleScope", team_id: Optional[str] = None |
| 71 | + ) -> UserRoleModel: |
| 72 | + """Assign a role to a user - simple and clean.""" |
| 73 | + user_role = UserRoleModel(user_id=user_id, role_name=role_name, scope=scope, team_id=team_id, is_active=True) |
| 74 | + return cls.create(user_role) |
| 75 | + |
| 76 | + @classmethod |
| 77 | + def remove_role_by_id(cls, user_id: str, role_id: str, scope: str, team_id: Optional[str] = None) -> bool: |
| 78 | + """Remove a role from a user by role_id - simple deactivation.""" |
| 79 | + collection = cls.get_collection() |
| 80 | + |
| 81 | + try: |
| 82 | + object_id = ObjectId(role_id) |
| 83 | + except Exception: |
| 84 | + return False |
| 85 | + |
| 86 | + query = {"_id": object_id, "user_id": user_id, "scope": scope, "is_active": True} |
| 87 | + |
| 88 | + if scope == "TEAM" and team_id: |
| 89 | + query["team_id"] = team_id |
| 90 | + elif scope == "GLOBAL": |
| 91 | + query["team_id"] = None |
| 92 | + |
| 93 | + result = collection.update_one(query, {"$set": {"is_active": False}}) |
| 94 | + |
| 95 | + return result.modified_count > 0 |
0 commit comments