Skip to content

Commit c917c22

Browse files
authored
Merge pull request #1455 from weaviate/rbac/add-helper-method-to-role-dataclass
Add `role.permissions` helper method, tidy export files for rbac classes
2 parents 2b463fa + 2ae369f commit c917c22

File tree

7 files changed

+65
-18
lines changed

7 files changed

+65
-18
lines changed

.github/workflows/main.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ env:
1717
WEAVIATE_125: 1.25.24
1818
WEAVIATE_126: 1.26.8
1919
WEAVIATE_127: 1.27.1
20-
WEAVIATE_128: 1.28.0-dev-223bafc
21-
20+
WEAVIATE_128: 1.28.0-rc.0
2221
jobs:
2322
lint-and-format:
2423
name: Run Linter and Formatter

integration/test_rbac.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ def test_create_role(client_factory: ClientFactory, permissions, expected) -> No
161161
permissions=permissions,
162162
)
163163
role = client.roles.by_name(expected.name)
164+
assert role is not None
164165
assert role == expected
166+
assert len(role.permissions) == 1
165167
finally:
166168
client.roles.delete(expected.name)
167169

@@ -181,6 +183,7 @@ def test_add_permissions_to_existing(client_factory: ClientFactory) -> None:
181183
assert role is not None
182184
assert role.collections_permissions is not None
183185
assert len(role.collections_permissions) == 1
186+
assert len(role.permissions) == 1
184187
assert role.collections_permissions[0].action == Actions.Collections.CREATE
185188

186189
client.roles.add_permissions(
@@ -194,6 +197,7 @@ def test_add_permissions_to_existing(client_factory: ClientFactory) -> None:
194197
assert role is not None
195198
assert role.collections_permissions is not None
196199
assert len(role.collections_permissions) == 2
200+
assert len(role.permissions) == 2
197201
assert role.collections_permissions[0].action == Actions.Collections.CREATE
198202
assert role.collections_permissions[1].action == Actions.Collections.DELETE
199203
finally:
@@ -215,6 +219,7 @@ def test_upsert_permissions(client_factory: ClientFactory) -> None:
215219
assert role is not None
216220
assert role.collections_permissions is not None
217221
assert len(role.collections_permissions) == 1
222+
assert len(role.permissions) == 1
218223
assert role.collections_permissions[0].action == Actions.Collections.CREATE
219224
finally:
220225
client.roles.delete(role_name)
@@ -237,6 +242,7 @@ def test_downsert_permissions(client_factory: ClientFactory) -> None:
237242
assert role is not None
238243
assert role.collections_permissions is not None
239244
assert len(role.collections_permissions) == 2
245+
assert len(role.permissions) == 2
240246
assert role.collections_permissions[0].action == Actions.Collections.CREATE
241247
assert role.collections_permissions[1].action == Actions.Collections.DELETE
242248

@@ -249,6 +255,7 @@ def test_downsert_permissions(client_factory: ClientFactory) -> None:
249255
assert role is not None
250256
assert role.collections_permissions is not None
251257
assert len(role.collections_permissions) == 1
258+
assert len(role.permissions) == 1
252259
assert role.collections_permissions[0].action == Actions.Collections.CREATE
253260

254261
client.roles.remove_permissions(
@@ -287,6 +294,7 @@ def test_multiple_permissions(client_factory: ClientFactory) -> None:
287294

288295
role = client.roles.by_name(role_name)
289296
assert role is not None
297+
assert len(role.permissions) == 3
290298
assert role.collections_permissions is not None
291299
assert len(role.collections_permissions) == 1
292300
assert role.collections_permissions[0].action == Actions.Collections.READ

weaviate/classes/rbac.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
from weaviate.rbac.models import Permissions, Actions
1+
from weaviate.rbac.models import Permissions, Actions, PermissionsInputType
22

3-
__all__ = ["Actions", "Permissions"]
3+
__all__ = ["Actions", "Permissions", "PermissionsInputType"]

weaviate/outputs/rbac.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from weaviate.rbac.models import (
2+
PermissionsOutputType,
3+
BackupsPermission,
4+
ClusterPermission,
5+
CollectionsPermission,
6+
DataPermission,
7+
NodesPermission,
8+
RolesPermission,
9+
UsersPermission,
10+
)
11+
12+
__all__ = [
13+
"PermissionsOutputType",
14+
"BackupsPermission",
15+
"ClusterPermission",
16+
"CollectionsPermission",
17+
"DataPermission",
18+
"NodesPermission",
19+
"RolesPermission",
20+
"UsersPermission",
21+
]

weaviate/rbac/models.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,17 @@ class NodesPermission:
257257
action: NodesAction
258258

259259

260+
PermissionsOutputType = Union[
261+
ClusterPermission,
262+
CollectionsPermission,
263+
DataPermission,
264+
RolesPermission,
265+
UsersPermission,
266+
BackupsPermission,
267+
NodesPermission,
268+
]
269+
270+
260271
@dataclass
261272
class Role:
262273
name: str
@@ -268,6 +279,18 @@ class Role:
268279
backups_permissions: List[BackupsPermission]
269280
nodes_permissions: List[NodesPermission]
270281

282+
@property
283+
def permissions(self) -> List[PermissionsOutputType]:
284+
permissions: List[PermissionsOutputType] = []
285+
permissions.extend(self.cluster_permissions)
286+
permissions.extend(self.collections_permissions)
287+
permissions.extend(self.data_permissions)
288+
permissions.extend(self.roles_permissions)
289+
permissions.extend(self.users_permissions)
290+
permissions.extend(self.backups_permissions)
291+
permissions.extend(self.nodes_permissions)
292+
return permissions
293+
271294
@classmethod
272295
def _from_weaviate_role(cls, role: WeaviateRole) -> "Role":
273296
cluster_permissions: List[ClusterPermission] = []
@@ -354,7 +377,7 @@ class User:
354377
ActionsType = Union[_Action, Sequence[_Action]]
355378

356379

357-
PermissionsType = Union[
380+
PermissionsInputType = Union[
358381
_Permission,
359382
Sequence[_Permission],
360383
Sequence[Sequence[_Permission]],
@@ -575,7 +598,3 @@ def cluster(*, read: bool = False) -> PermissionsCreateType:
575598
if read:
576599
permissions.append(_ClusterFactory.read())
577600
return permissions
578-
579-
580-
class RBAC:
581-
permissions = Permissions

weaviate/rbac/roles.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from weaviate.connect.v4 import _ExpectedStatusCodes
66
from weaviate.rbac.models import (
77
_Permission,
8-
PermissionsType,
8+
PermissionsInputType,
99
Role,
1010
User,
1111
WeaviatePermission,
@@ -213,7 +213,7 @@ async def delete(self, role: str) -> None:
213213
"""
214214
return await self._delete_role(role)
215215

216-
async def create(self, *, name: str, permissions: PermissionsType) -> Role:
216+
async def create(self, *, name: str, permissions: PermissionsInputType) -> Role:
217217
"""Create a new role.
218218
219219
Args:
@@ -249,7 +249,7 @@ async def revoke(self, *, roles: Union[str, List[str]], user: str) -> None:
249249
"""
250250
await self._revoke_roles_from_user([roles] if isinstance(roles, str) else roles, user)
251251

252-
async def add_permissions(self, *, permissions: PermissionsType, role: str) -> None:
252+
async def add_permissions(self, *, permissions: PermissionsInputType, role: str) -> None:
253253
"""Add permissions to a role.
254254
255255
Note: This method is an upsert operation. If the permission already exists, it will be updated. If it does not exist, it will be created.
@@ -264,7 +264,7 @@ async def add_permissions(self, *, permissions: PermissionsType, role: str) -> N
264264
[permission._to_weaviate() for permission in _flatten_permissions(permissions)], role
265265
)
266266

267-
async def remove_permissions(self, *, permissions: PermissionsType, role: str) -> None:
267+
async def remove_permissions(self, *, permissions: PermissionsInputType, role: str) -> None:
268268
"""Remove permissions from a role.
269269
270270
Note: This method is a downsert operation. If the permission does not exist, it will be ignored. If these permissions are the only permissions of the role, the role will be deleted.
@@ -280,7 +280,7 @@ async def remove_permissions(self, *, permissions: PermissionsType, role: str) -
280280
)
281281

282282

283-
def _flatten_permissions(permissions: PermissionsType) -> List[_Permission]:
283+
def _flatten_permissions(permissions: PermissionsInputType) -> List[_Permission]:
284284
if isinstance(permissions, _Permission):
285285
return [permissions]
286286
flattened_permissions: List[_Permission] = []

weaviate/rbac/sync.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Dict, List, Optional, Union
22

3-
from weaviate.rbac.models import PermissionsType, Role, User
3+
from weaviate.rbac.models import PermissionsInputType, Role, User
44
from weaviate.rbac.roles import _RolesBase
55

66
class _Roles(_RolesBase):
@@ -10,8 +10,8 @@ class _Roles(_RolesBase):
1010
def by_user(self, user: str) -> Dict[str, Role]: ...
1111
def users(self, role: str) -> Dict[str, User]: ...
1212
def delete(self, role: str) -> None: ...
13-
def create(self, *, name: str, permissions: PermissionsType) -> Role: ...
13+
def create(self, *, name: str, permissions: PermissionsInputType) -> Role: ...
1414
def assign(self, *, roles: Union[str, List[str]], user: str) -> None: ...
1515
def revoke(self, *, roles: Union[str, List[str]], user: str) -> None: ...
16-
def add_permissions(self, *, permissions: PermissionsType, role: str) -> None: ...
17-
def remove_permissions(self, *, permissions: PermissionsType, role: str) -> None: ...
16+
def add_permissions(self, *, permissions: PermissionsInputType, role: str) -> None: ...
17+
def remove_permissions(self, *, permissions: PermissionsInputType, role: str) -> None: ...

0 commit comments

Comments
 (0)