1- from typing import Optional , Set , Union , List , Dict , Any
2- from open_webui .models .users import Users , UserModel
3- from open_webui .models .groups import Groups
4-
1+ import json
2+ from typing import Any
53
4+ from open_webui .models .users import UserModel
5+ from open_webui .models .groups import Groups
6+ from open_webui .models .access_grants import (
7+ has_public_read_access_grant ,
8+ has_user_access_grant ,
9+ strip_user_access_grants ,
10+ )
611from open_webui .config import DEFAULT_USER_PERMISSIONS
7- import json
12+
13+ from sqlalchemy .orm import Session
814
915
1016def fill_missing_permissions (
11- permissions : Dict [str , Any ], default_permissions : Dict [str , Any ]
12- ) -> Dict [str , Any ]:
17+ permissions : dict [str , Any ], default_permissions : dict [str , Any ]
18+ ) -> dict [str , Any ]:
1319 """
1420 Recursively fills in missing properties in the permissions dictionary
1521 using the default permissions as a template.
@@ -27,18 +33,18 @@ def fill_missing_permissions(
2733
2834def get_permissions (
2935 user_id : str ,
30- default_permissions : Dict [str , Any ],
31- db : Optional [ Any ] = None ,
32- ) -> Dict [str , Any ]:
36+ default_permissions : dict [str , Any ],
37+ db : Session | None = None ,
38+ ) -> dict [str , Any ]:
3339 """
3440 Get all permissions for a user by combining the permissions of all groups the user is a member of.
3541 If a permission is defined in multiple groups, the most permissive value is used (True > False).
3642 Permissions are nested in a dict with the permission key as the key and a boolean as the value.
3743 """
3844
3945 def combine_permissions (
40- permissions : Dict [str , Any ], group_permissions : Dict [str , Any ]
41- ) -> Dict [str , Any ]:
46+ permissions : dict [str , Any ], group_permissions : dict [str , Any ]
47+ ) -> dict [str , Any ]:
4248 """Combine permissions from multiple groups by taking the most permissive value."""
4349 for key , value in group_permissions .items ():
4450 if isinstance (value , dict ):
@@ -72,8 +78,8 @@ def combine_permissions(
7278def has_permission (
7379 user_id : str ,
7480 permission_key : str ,
75- default_permissions : Dict [str , Any ] = {},
76- db : Optional [ Any ] = None ,
81+ default_permissions : dict [str , Any ] = {},
82+ db : Session | None = None ,
7783) -> bool :
7884 """
7985 Check if a user has a specific permission by checking the group permissions
@@ -82,7 +88,7 @@ def has_permission(
8288 Permission keys can be hierarchical and separated by dots ('.').
8389 """
8490
85- def get_permission (permissions : Dict [str , Any ], keys : List [str ]) -> bool :
91+ def get_permission (permissions : dict [str , Any ], keys : list [str ]) -> bool :
8692 """Traverse permissions dict using a list of keys (from dot-split permission_key)."""
8793 for key in keys :
8894 if key not in permissions :
@@ -110,9 +116,9 @@ def get_permission(permissions: Dict[str, Any], keys: List[str]) -> bool:
110116def has_access (
111117 user_id : str ,
112118 permission : str = "read" ,
113- access_grants : Optional [ list ] = None ,
114- user_group_ids : Optional [ Set [ str ]] = None ,
115- db : Optional [ Any ] = None ,
119+ access_grants : list | None = None ,
120+ user_group_ids : set [ str ] | None = None ,
121+ db : Session | None = None ,
116122) -> bool :
117123 """
118124 Check if a user has the specified permission using an in-memory access_grants list.
@@ -156,7 +162,7 @@ def has_access(
156162def has_connection_access (
157163 user : UserModel ,
158164 connection : dict ,
159- user_group_ids : Optional [ Set [ str ]] = None ,
165+ user_group_ids : set [ str ] | None = None ,
160166) -> bool :
161167 """
162168 Check if a user can access a server connection (tool server, terminal, etc.)
@@ -194,7 +200,7 @@ def migrate_access_control(
194200 if access_control is None and ac_key not in data :
195201 return
196202
197- grants : List [ Dict [str , str ]] = []
203+ grants : list [ dict [str , str ]] = []
198204 if access_control and isinstance (access_control , dict ):
199205 for perm in ["read" , "write" ]:
200206 perm_data = access_control .get (perm , {})
@@ -221,20 +227,13 @@ def migrate_access_control(
221227 data .pop (ac_key , None )
222228
223229
224- from open_webui .models .access_grants import (
225- has_public_read_access_grant ,
226- has_user_access_grant ,
227- strip_user_access_grants ,
228- )
229-
230-
231230def filter_allowed_access_grants (
232- default_permissions : Dict [str , Any ],
231+ default_permissions : dict [str , Any ],
233232 user_id : str ,
234233 user_role : str ,
235234 access_grants : list ,
236235 public_permission_key : str ,
237- db : Optional [ Any ] = None ,
236+ db : Session | None = None ,
238237) -> list :
239238 """
240239 Checks if the user has the required permissions to grant access to a resource.
0 commit comments