Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions seacatauth/authz/role/handler/roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import asab
import asab.web.rest
import asab.exceptions
import seacatauth.exceptions

from ....decorators import access_control

Expand Down Expand Up @@ -123,10 +124,30 @@ async def assign_role(self, request, *, tenant):
status=403
)

await self.RoleService.assign_role(
credentials_id=request.match_info["credentials_id"],
role_id=role_id
)
credentials_id = request.match_info["credentials_id"]
try:
await self.RoleService.assign_role(credentials_id, role_id)
except seacatauth.exceptions.TenantNotAssignedError:
L.log(asab.LOG_NOTICE, "Failed to assign role: Tenant not assigned", struct_data={
"tenant": tenant, "cid": credentials_id})
return asab.web.rest.json_response(request, status=400, data={
"result": "FAILED",
"message": "Tenant not assigned",
})
except seacatauth.exceptions.CredentialsNotFoundError:
L.log(asab.LOG_NOTICE, "Failed to assign role: Credentials not found", struct_data={
"cid": credentials_id})
return asab.web.rest.json_response(request, status=404, data={
"result": "NOT-FOUND",
"message": "Credentials not found",
})
except seacatauth.exceptions.RoleNotFoundError as e:
L.log(asab.LOG_NOTICE, "Failed to assign role: Role not found", struct_data={
"role_id": role_id})
return asab.web.rest.json_response(request, status=404, data={
"result": "NOT-FOUND",
"message": "Role not found",
})

return asab.web.rest.json_response(request, data={"result": "OK"})

Expand Down
2 changes: 1 addition & 1 deletion seacatauth/authz/role/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ async def assign_role(
try:
await self.TenantService.get_tenant(tenant)
except KeyError:
raise exceptions.TenantNotFoundError(tenant)
raise exceptions.IntegrityError("Role's tenant not found", tenant_id=tenant, role_id=role_id)

if verify_credentials:
try:
Expand Down
9 changes: 9 additions & 0 deletions seacatauth/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,12 @@ def __init__(self, client_id=None, *args):
else:
message = "Request contains no root session cookie"
super().__init__(message, *args)


class IntegrityError(SeacatAuthError):
"""
Database is in an unexpected state; on object is missing its dependencies.
"""
def __init__(self, message, **struct_data):
self.StructData = struct_data
super().__init__(message)