|
4 | 4 | from typing import List, Optional, Sequence, TypedDict, Union
|
5 | 5 |
|
6 | 6 | from pydantic import BaseModel
|
| 7 | +from typing_extensions import NotRequired |
7 | 8 |
|
8 | 9 | from weaviate.cluster.types import Verbosity
|
9 | 10 | from weaviate.util import _capitalize_first_letter
|
@@ -36,6 +37,7 @@ class PermissionBackup(TypedDict):
|
36 | 37 |
|
37 | 38 | class PermissionRoles(TypedDict):
|
38 | 39 | role: str
|
| 40 | + scope: NotRequired[str] |
39 | 41 |
|
40 | 42 |
|
41 | 43 | # action is always present in WeaviatePermission
|
@@ -191,14 +193,16 @@ def _to_weaviate(self) -> WeaviatePermission:
|
191 | 193 |
|
192 | 194 | class _RolesPermission(_Permission):
|
193 | 195 | role: str
|
| 196 | + scope: Optional[str] = None |
194 | 197 | action: RolesAction
|
195 | 198 |
|
196 | 199 | def _to_weaviate(self) -> WeaviatePermission:
|
| 200 | + roles: PermissionRoles = {"role": self.role} |
| 201 | + if self.scope is not None: |
| 202 | + roles["scope"] = self.scope |
197 | 203 | return {
|
198 | 204 | "action": self.action,
|
199 |
| - "roles": { |
200 |
| - "role": self.role, |
201 |
| - }, |
| 205 | + "roles": roles, |
202 | 206 | }
|
203 | 207 |
|
204 | 208 |
|
@@ -554,8 +558,8 @@ def delete(*, collection: Optional[str] = None) -> TenantsPermission:
|
554 | 558 |
|
555 | 559 | class _RolesFactory:
|
556 | 560 | @staticmethod
|
557 |
| - def manage(*, role: Optional[str] = None) -> _RolesPermission: |
558 |
| - return _RolesPermission(role=role or "*", action=RolesAction.MANAGE) |
| 561 | + def manage(*, role: Optional[str] = None, scope: Optional[str] = None) -> _RolesPermission: |
| 562 | + return _RolesPermission(role=role or "*", action=RolesAction.MANAGE, scope=scope) |
559 | 563 |
|
560 | 564 | @staticmethod
|
561 | 565 | def read(*, role: Optional[str] = None) -> _RolesPermission:
|
@@ -673,16 +677,22 @@ def tenants(
|
673 | 677 |
|
674 | 678 | @staticmethod
|
675 | 679 | def roles(
|
676 |
| - *, role: Union[str, Sequence[str]], read: bool = False, manage: bool = False |
| 680 | + *, |
| 681 | + role: Union[str, Sequence[str]], |
| 682 | + read: bool = False, |
| 683 | + manage: Optional[Union[str, bool]] = None, |
677 | 684 | ) -> PermissionsCreateType:
|
678 | 685 | permissions: List[_Permission] = []
|
679 | 686 | if isinstance(role, str):
|
680 | 687 | role = [role]
|
681 | 688 | for r in role:
|
682 | 689 | if read:
|
683 | 690 | permissions.append(_RolesFactory.read(role=r))
|
684 |
| - if manage: |
685 |
| - permissions.append(_RolesFactory.manage(role=r)) |
| 691 | + if manage is not None: |
| 692 | + if isinstance(manage, bool): |
| 693 | + permissions.append(_RolesFactory.manage(role=r)) |
| 694 | + else: |
| 695 | + permissions.append(_RolesFactory.manage(role=r, scope=manage)) |
686 | 696 | return permissions
|
687 | 697 |
|
688 | 698 | @staticmethod
|
|
0 commit comments