-
Notifications
You must be signed in to change notification settings - Fork 164
feat: Add RBAC DB source #5756
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fregataa
wants to merge
2
commits into
feat/add-processors-based-on-action-types
Choose a base branch
from
feat/add-rbac-db-source
base: feat/add-processors-based-on-action-types
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+233
−224
Open
feat: Add RBAC DB source #5756
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 0 additions & 39 deletions
39
src/ai/backend/manager/data/permission/scope_permission.py
This file was deleted.
Oops, something went wrong.
144 changes: 144 additions & 0 deletions
144
src/ai/backend/manager/repositories/permission_controller/db_source.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import uuid | ||
from typing import Optional, cast | ||
|
||
import sqlalchemy as sa | ||
from sqlalchemy.ext.asyncio import AsyncSession as SASession | ||
from sqlalchemy.orm import contains_eager | ||
|
||
from ...data.permission.id import ObjectId | ||
from ...data.permission.role import ( | ||
RoleCreateInput, | ||
RoleDeleteInput, | ||
RoleUpdateInput, | ||
UserRoleAssignmentInput, | ||
) | ||
from ...data.permission.status import ( | ||
RoleStatus, | ||
) | ||
from ...errors.common import ObjectNotFound | ||
from ...models.rbac_models.association_scopes_entities import AssociationScopesEntitiesRow | ||
from ...models.rbac_models.permission.object_permission import ObjectPermissionRow | ||
from ...models.rbac_models.permission.permission import PermissionRow | ||
from ...models.rbac_models.permission.permission_group import PermissionGroupRow | ||
from ...models.rbac_models.role import RoleRow | ||
from ...models.rbac_models.user_role import UserRoleRow | ||
from ...models.utils import ExtendedAsyncSAEngine | ||
|
||
|
||
class PermissionDBSource: | ||
_db: ExtendedAsyncSAEngine | ||
|
||
def __init__(self, db: ExtendedAsyncSAEngine) -> None: | ||
self._db = db | ||
|
||
async def create_role(self, data: RoleCreateInput) -> RoleRow: | ||
""" | ||
Create a new role in the database. | ||
|
||
Returns the ID of the created role. | ||
""" | ||
async with self._db.begin_session() as db_session: | ||
role_row = RoleRow.from_input(data) | ||
db_session.add(role_row) # type: ignore[arg-type] | ||
await db_session.flush() | ||
role_id = role_row.id | ||
for permission_group in data.permission_groups: | ||
permission_group_row = PermissionGroupRow.from_input( | ||
permission_group.to_input(role_id) | ||
) | ||
db_session.add(permission_group_row) # type: ignore[arg-type] | ||
for object_permission in data.object_permissions: | ||
object_permission_row = ObjectPermissionRow.from_input( | ||
object_permission.to_input(role_id) | ||
) | ||
db_session.add(object_permission_row) # type: ignore[arg-type] | ||
await db_session.flush() | ||
await db_session.refresh(role_row) | ||
return role_row | ||
|
||
async def _get_role(self, role_id: uuid.UUID, db_session: SASession) -> RoleRow: | ||
stmt = sa.select(RoleRow).where(RoleRow.id == role_id) | ||
role_row = await db_session.scalar(stmt) | ||
result = cast(Optional[RoleRow], role_row) | ||
if result is None: | ||
raise ObjectNotFound(f"Role with ID {role_id} does not exist.") | ||
return result | ||
|
||
async def update_role(self, data: RoleUpdateInput) -> RoleRow: | ||
to_update = data.fields_to_update() | ||
async with self._db.begin_session() as db_session: | ||
stmt = sa.update(RoleRow).where(RoleRow.id == data.id).values(**to_update) | ||
await db_session.execute(stmt) | ||
role_row = await self._get_role(data.id, db_session) | ||
return role_row | ||
|
||
async def delete_role(self, data: RoleDeleteInput) -> RoleRow: | ||
async with self._db.begin_session() as db_session: | ||
role_row = await self._get_role(data.id, db_session) | ||
role_row.status = RoleStatus.DELETED | ||
await db_session.flush() | ||
await db_session.refresh(role_row) | ||
return role_row | ||
|
||
async def assign_role(self, data: UserRoleAssignmentInput) -> UserRoleRow: | ||
async with self._db.begin_session() as db_session: | ||
user_role_row = UserRoleRow.from_input(data) | ||
db_session.add(user_role_row) # type: ignore[arg-type] | ||
await db_session.flush() | ||
await db_session.refresh(user_role_row) | ||
return user_role_row | ||
|
||
async def get_role(self, role_id: uuid.UUID) -> Optional[RoleRow]: | ||
async with self._db.begin_readonly_session() as db_session: | ||
try: | ||
result = await self._get_role(role_id, db_session) | ||
except ObjectNotFound: | ||
return None | ||
return result | ||
|
||
async def get_user_roles(self, user_id: uuid.UUID) -> list[RoleRow]: | ||
async with self._db.begin_readonly_session() as db_session: | ||
j = ( | ||
sa.join( | ||
RoleRow, | ||
UserRoleRow, | ||
RoleRow.id == UserRoleRow.role_id, | ||
) | ||
.join( | ||
ObjectPermissionRow, | ||
RoleRow.id == ObjectPermissionRow.role_id, | ||
) | ||
.join( | ||
PermissionGroupRow, | ||
RoleRow.id == PermissionGroupRow.role_id, | ||
) | ||
.join( | ||
PermissionRow, | ||
PermissionGroupRow.id == PermissionRow.permission_group_id, | ||
) | ||
) | ||
stmt = ( | ||
sa.select(RoleRow) | ||
.select_from(j) | ||
.where(UserRoleRow.user_id == user_id) | ||
.options( | ||
contains_eager(RoleRow.permission_group_rows).options( | ||
contains_eager(PermissionGroupRow.permission_rows) | ||
), | ||
contains_eager(RoleRow.object_permission_rows), | ||
) | ||
) | ||
|
||
result = await db_session.scalars(stmt) | ||
return result.all() | ||
|
||
async def get_entity_mapped_scopes( | ||
self, target_object_id: ObjectId | ||
) -> list[AssociationScopesEntitiesRow]: | ||
async with self._db.begin_readonly_session() as db_session: | ||
stmt = sa.select(AssociationScopesEntitiesRow.scope_id).where( | ||
AssociationScopesEntitiesRow.entity_id == target_object_id.entity_id, | ||
AssociationScopesEntitiesRow.entity_type == target_object_id.entity_type.value, | ||
) | ||
result = await db_session.scalars(stmt) | ||
return result.all() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😭