11from datetime import datetime
2- from typing import List
2+ from typing import Any , List
33from uuid import UUID
44
55from app import scope_utils
66from app .db .db import Database
77from app .db .models .organization import OrganizationEntity
88from app .db .repository .organization import OrganizationRepository
99from app .models .ura import UraNumber
10- from app .services .exceptions import ScopesNotGrantedError
10+ from app .services .exceptions import ScopeNotAllowedError , ScopesNotGrantedError
1111
1212
1313class OrganizationService :
14- def __init__ (self , db : Database ) -> None :
14+ def __init__ (self , db : Database , allowed_scopes : List [ str ] ) -> None :
1515 self .db = db
16+ self .allowed_scopes = allowed_scopes
1617
1718 def create_one (
1819 self ,
1920 register_id : UraNumber ,
2021 name : str ,
2122 scopes : str | None = None ,
2223 ) -> OrganizationEntity :
24+ if scopes :
25+ scope_allowed = scope_utils .check_allowed (self .allowed_scopes , scopes )
26+ if not scope_allowed :
27+ raise ScopeNotAllowedError (scopes )
28+
2329 with self .db .get_db_session () as session :
2430 repo = session .get_repository (OrganizationRepository )
2531 entity = OrganizationEntity (register_id = register_id , name = name , scopes = scopes )
@@ -45,10 +51,21 @@ def get_many(
4551 with self .db .get_db_session () as session :
4652 repo = session .get_repository (OrganizationRepository )
4753 return list (
48- repo .get_many (register_id = register_id , name = name , scopes = scopes , include_deleted = include_deleted )
54+ repo .get_many (
55+ register_id = register_id ,
56+ name = name ,
57+ scopes = scopes ,
58+ include_deleted = include_deleted ,
59+ )
4960 )
5061
51- def update_one (self , id : UUID , ** kwargs : object ) -> OrganizationEntity | None :
62+ def update_one (self , id : UUID , ** kwargs : Any ) -> OrganizationEntity | None :
63+ if "scopes" in kwargs :
64+ scopes : str = kwargs ["scopes" ]
65+ valid_scope = scope_utils .check_allowed (self .allowed_scopes , scopes )
66+ if not valid_scope :
67+ raise ScopeNotAllowedError (scopes )
68+
5269 with self .db .get_db_session () as session :
5370 repo = session .get_repository (OrganizationRepository )
5471 return repo .update (id , ** kwargs )
0 commit comments