Skip to content

Commit 3a6b5eb

Browse files
authored
refac: modernize type hints and imports in access_control module (open-webui#22594)
1 parent cee6450 commit 3a6b5eb

2 files changed

Lines changed: 32 additions & 32 deletions

File tree

backend/open_webui/utils/access_control/__init__.py

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
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+
)
611
from open_webui.config import DEFAULT_USER_PERMISSIONS
7-
import json
12+
13+
from sqlalchemy.orm import Session
814

915

1016
def 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

2834
def 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(
7278
def 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:
110116
def 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(
156162
def 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-
231230
def 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.

backend/open_webui/utils/access_control/files.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import logging
2-
from typing import Optional, Any
32

43
from open_webui.models.users import UserModel
54
from open_webui.models.files import Files
@@ -10,14 +9,16 @@
109
from open_webui.models.models import Models
1110
from open_webui.models.access_grants import AccessGrants
1211

12+
from sqlalchemy.orm import Session
13+
1314
log = logging.getLogger(__name__)
1415

1516

1617
def has_access_to_file(
17-
file_id: Optional[str],
18+
file_id: str | None,
1819
access_type: str,
1920
user: UserModel,
20-
db: Optional[Any] = None,
21+
db: Session | None = None,
2122
) -> bool:
2223
"""
2324
Check if a user has the specified access to a file through any of:

0 commit comments

Comments
 (0)