Skip to content

Commit

Permalink
chore(api): merge user role api with user api (#77)
Browse files Browse the repository at this point in the history
UserRoles are an attribute of Users so this PR helps manage UsersRoles
completely under Users

Fixes #74
  • Loading branch information
mawandm authored May 10, 2024
1 parent d2aaabf commit b5f03a7
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 70 deletions.
8 changes: 4 additions & 4 deletions nesis/api/core/controllers/management.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ def operate_user_roles(user_id):
token = get_bearer_token(request.headers.get("Authorization"))
try:
if request.method == POST:
result = services.user_role_service.create(
token=token, user_id=user_id, role=request.json
result = services.user_service.update(
token=token, user_id=user_id, user={"roles": [request.json.get("id")]}
)
return jsonify(result.to_dict())
else:
results = services.user_role_service.get(token=token, user_id=user_id)
results = services.user_service.get_roles(token=token, user_id=user_id)
return jsonify({"items": [item.to_dict() for item in results]})
except util.ServiceException as se:
return jsonify(error_message(str(se))), 400
Expand All @@ -101,7 +101,7 @@ def operate_user_roles(user_id):
def operate_user_role(user_id, role_id):
token = get_bearer_token(request.headers.get("Authorization"))
try:
services.user_service.delete(token=token, user_id=user_id, role_id=role_id)
services.user_service.delete_role(token=token, user_id=user_id, role_id=role_id)
return jsonify(success=True)
except util.ServiceException as se:
return jsonify(error_message(str(se))), 400
Expand Down
5 changes: 1 addition & 4 deletions nesis/api/core/services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,11 @@
user_service: UserService
user_session_service: UserSessionService
role_service: RoleService
user_role_service: UserRoleService
task_service: TaskService


def init_services(config, http_client=None):
global datasource_service, qanda_prediction_service, settings_service, user_service, user_session_service, role_service, user_role_service, task_service
global datasource_service, qanda_prediction_service, settings_service, user_service, user_session_service, role_service, task_service

user_session_service = UserSessionService(config=config)

Expand All @@ -46,11 +45,9 @@ def init_services(config, http_client=None):
config=config, session_service=user_session_service
)

user_role_service = UserRoleService(config, session_service=user_session_service)
user_service = UserService(
config=config,
session_service=user_session_service,
user_role_service=user_role_service,
)
role_service = RoleService(config=config, session_service=user_session_service)
datasource_service = DatasourceService(
Expand Down
41 changes: 37 additions & 4 deletions nesis/api/core/services/management.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,14 @@ class UserService(ServiceOperation):
Manage system users
"""

def __init__(
self, config: dict, session_service: UserSessionService, user_role_service
) -> None:
def __init__(self, config: dict, session_service: UserSessionService) -> None:
self._config = config
self._resource_type = ResourceType.USERS
self._session_service = session_service
self._user_role_service = user_role_service
self._user_role_service = UserRoleService(
config, session_service=session_service
)

self._LOG = logging.getLogger(self.__module__ + "." + self.__class__.__name__)

self._LOG.info("Initializing service...")
Expand Down Expand Up @@ -345,6 +346,38 @@ def update(self, **kwargs):
if session:
session.close()

def get_roles(self, **kwargs):
uuid = kwargs["user_id"]

session = DBSession()

services.authorized(
session_service=self._session_service,
session=session,
token=kwargs.get("token"),
action=Action.READ,
resource_type=self._resource_type,
resource=self.__get_rbac_resource(user_id=uuid),
)

return self._user_role_service.get(**kwargs)

def delete_role(self, **kwargs):
uuid = kwargs["user_id"]

session = DBSession()

services.authorized(
session_service=self._session_service,
session=session,
token=kwargs.get("token"),
action=Action.DELETE,
resource_type=self._resource_type,
resource=self.__get_rbac_resource(user_id=uuid),
)

return self._user_role_service.delete(**kwargs)


class RoleService(ServiceOperation):
"""
Expand Down
Binary file removed nesis/api/tests/bbk-2021.pdf
Binary file not shown.
6 changes: 2 additions & 4 deletions nesis/api/tests/core/services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,5 @@ def create_role(
return role_record


def assign_role(
service: UserRoleService, token: str, role: dict, user_id: str
) -> UserRole:
return service.create(token=token, user_id=user_id, role=role)
def assign_role(service: UserService, token: str, role: dict, user_id: str):
service.update(token=token, user_id=user_id, user={"roles": [role["id"]]})
2 changes: 1 addition & 1 deletion nesis/api/tests/core/services/test_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def test_datasource_permissions(http_client, tc):
service=services.role_service, role=role, token=admin_user.token
)
assign_role(
service=services.user_role_service,
service=services.user_service,
token=admin_user.token,
role=role_record.to_dict(),
user_id=given_user_record.uuid,
Expand Down
53 changes: 0 additions & 53 deletions nesis/api/tests/qanda-result.json

This file was deleted.

0 comments on commit b5f03a7

Please sign in to comment.