diff --git a/marketplace/accounts/consumers/__init__.py b/marketplace/accounts/consumers/__init__.py new file mode 100644 index 00000000..da950859 --- /dev/null +++ b/marketplace/accounts/consumers/__init__.py @@ -0,0 +1 @@ +from .update_permissions import UpdatePermissionConsumer diff --git a/marketplace/accounts/consumers/update_permissions.py b/marketplace/accounts/consumers/update_permissions.py new file mode 100644 index 00000000..26e1e2c3 --- /dev/null +++ b/marketplace/accounts/consumers/update_permissions.py @@ -0,0 +1,27 @@ +import amqp +from sentry_sdk import capture_exception + +from ..usecases import update_permission +from marketplace.event_driven.parsers import JSONParser +from marketplace.event_driven.consumers import EDAConsumer + + +class UpdatePermissionConsumer(EDAConsumer): + def consume(self, message: amqp.Message): # pragma: no cover + print(f"[UpdatePermission] - Consuming a message. Body: {message.body}") + try: + body = JSONParser.parse(message.body) + + update_permission( + project_uuid=body.get("project"), + action=body.get("action"), + user_email=body.get("user"), + role=body.get("role"), + ) + + message.channel.basic_ack(message.delivery_tag) + + except Exception as exception: + capture_exception(exception) + message.channel.basic_reject(message.delivery_tag, requeue=False) + print(f"[UpdatePermission] - Message rejected by: {exception}") diff --git a/marketplace/accounts/handle.py b/marketplace/accounts/handle.py new file mode 100644 index 00000000..f3770b47 --- /dev/null +++ b/marketplace/accounts/handle.py @@ -0,0 +1,9 @@ +from amqp.channel import Channel + +from .consumers import UpdatePermissionConsumer + + +def handle_consumers(channel: Channel) -> None: + channel.basic_consume( + "integrations.update-permission", callback=UpdatePermissionConsumer().handle + ) diff --git a/marketplace/accounts/usecases/__init__.py b/marketplace/accounts/usecases/__init__.py new file mode 100644 index 00000000..992aa77f --- /dev/null +++ b/marketplace/accounts/usecases/__init__.py @@ -0,0 +1 @@ +from .permission_update import update_permission diff --git a/marketplace/accounts/usecases/permission_update.py b/marketplace/accounts/usecases/permission_update.py new file mode 100644 index 00000000..d67d798d --- /dev/null +++ b/marketplace/accounts/usecases/permission_update.py @@ -0,0 +1,69 @@ +from marketplace.accounts.models import ProjectAuthorization + + +from django.contrib.auth import get_user_model + +from marketplace.projects.models import Project + +User = get_user_model() + + +def get_or_create_user_by_email(email: str) -> tuple: # pragma: no cover + return User.objects.get_or_create(email=email) + + +def set_user_project_authorization_role( + user: User, project: Project, role: int +): # pragma: no cover + + project_authorization, created = ProjectAuthorization.objects.get_or_create( + user=user, project_uuid=project.uuid + ) + + project_authorization.role = role + project_authorization.save(update_fields=["role"]) + + +def update_user_permission(role: int, project: Project, user: User): # pragma: no cover + if role == 1: + set_user_project_authorization_role( + user=user, project=project, role=ProjectAuthorization.ROLE_VIEWER + ) + + elif role == 2 or role == 5: + set_user_project_authorization_role( + user=user, project=project, role=ProjectAuthorization.ROLE_CONTRIBUTOR + ) + + elif role == 3 or role == 4: + set_user_project_authorization_role( + user=user, project=project, role=ProjectAuthorization.ROLE_ADMIN + ) + + else: + set_user_project_authorization_role( + user=user, project=project, role=ProjectAuthorization.ROLE_NOT_SETTED + ) + + +def delete_permisison(role, project, user): # pragma: no cover + project_authorization = ProjectAuthorization.objects.get( + user=user, project_uuid=project.uuid, role=role + ) + + project_authorization.delete() + + +def update_permission( + project_uuid: Project, action: str, user_email: str, role: int +) -> Project: # pragma: no cover + project = Project.objects.get(uuid=project_uuid) + user, _ = get_or_create_user_by_email(user_email) + + if action == "create" or action == "update": # OK - Verificar no shell + update_user_permission(role, project, user) + + if action == "delete": + delete_permisison(role, project, user) + + return project diff --git a/marketplace/event_driven/handle.py b/marketplace/event_driven/handle.py index 8ba70da5..bf725237 100644 --- a/marketplace/event_driven/handle.py +++ b/marketplace/event_driven/handle.py @@ -1,7 +1,9 @@ from amqp.channel import Channel from marketplace.projects.handle import handle_consumers as project_handle_consumers +from marketplace.accounts.handle import handle_consumers as update_permission_consumers def handle_consumers(channel: Channel) -> None: project_handle_consumers(channel) + update_permission_consumers(channel)