@@ -39,24 +39,51 @@ def get_session():
39
39
yield session
40
40
41
41
42
- def create_roles (session ):
42
+ def create_default_roles (session , organization_id : int , check_first : bool = True ):
43
43
"""
44
- Create default roles in the database if they do not exist.
44
+ Create default roles for an organization in the database if they do not exist.
45
45
"""
46
46
roles_in_db = []
47
47
for role_name in default_roles :
48
48
db_role = session .exec (select (Role ).where (
49
- Role .name == role_name )).first ()
49
+ Role .name == role_name ,
50
+ Role .organization_id == organization_id
51
+ )).first ()
50
52
if not db_role :
51
- db_role = Role (name = role_name )
53
+ db_role = Role (name = role_name , organization_id = organization_id )
52
54
session .add (db_role )
53
55
roles_in_db .append (db_role )
56
+
57
+ # Create RolePermissionLink for Owner and Administrator roles
58
+ for role in roles_in_db [:2 ]:
59
+ permissions = session .exec (select (Permission )).all ()
60
+ for permission in permissions :
61
+ # Check if the role already has the permission
62
+ if check_first :
63
+ db_role_permission_link : RolePermissionLink | None = session .exec (select (RolePermissionLink ).where (
64
+ RolePermissionLink .role_id == role .id ,
65
+ RolePermissionLink .permission_id == permission .id
66
+ )).first ()
67
+ else :
68
+ db_role_permission_link = None
69
+
70
+ # Skip giving DELETE_ORGANIZATION permission to Administrator
71
+ if not db_role_permission_link and not (
72
+ permission == ValidPermissions .DELETE_ORGANIZATION and
73
+ role .name == "Administrator"
74
+ ):
75
+ role_permission_link = RolePermissionLink (
76
+ role_id = role .id ,
77
+ permission_id = permission .id
78
+ )
79
+ session .add (role_permission_link )
80
+
54
81
return roles_in_db
55
82
56
83
57
- def create_permissions (session , roles_in_db ):
84
+ def create_permissions (session ):
58
85
"""
59
- Create default permissions and link them to roles in the database .
86
+ Create default permissions.
60
87
"""
61
88
for permission in ValidPermissions :
62
89
db_permission = session .exec (select (Permission ).where (
@@ -65,17 +92,6 @@ def create_permissions(session, roles_in_db):
65
92
db_permission = Permission (name = permission )
66
93
session .add (db_permission )
67
94
68
- # Create RolePermissionLink for Owner and Administrator
69
- for role in roles_in_db [:2 ]:
70
- db_role_permission_link = session .exec (select (RolePermissionLink ).where (
71
- RolePermissionLink .role_id == role .id ,
72
- RolePermissionLink .permission_id == db_permission .id )).first ()
73
- if not db_role_permission_link :
74
- if not (permission == ValidPermissions .DELETE_ORGANIZATION and role .name == "Administrator" ):
75
- role_permission_link = RolePermissionLink (
76
- role_id = role .id , permission_id = db_permission .id )
77
- session .add (role_permission_link )
78
-
79
95
80
96
def set_up_db (drop : bool = False ):
81
97
"""
@@ -85,10 +101,9 @@ def set_up_db(drop: bool = False):
85
101
if drop :
86
102
SQLModel .metadata .drop_all (engine )
87
103
SQLModel .metadata .create_all (engine )
104
+ # Create default permissions
88
105
with Session (engine ) as session :
89
- roles_in_db = create_roles (session )
90
- session .commit ()
91
- create_permissions (session , roles_in_db )
106
+ create_permissions (session )
92
107
session .commit ()
93
108
engine .dispose ()
94
109
0 commit comments