Skip to content

Commit

Permalink
Merge branch 'feature/consumer-permissions' of https://github.com/wen…
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaslinhares committed Apr 12, 2024
2 parents 45540d0 + 36e77f9 commit 8f4f74a
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 0 deletions.
1 change: 1 addition & 0 deletions marketplace/accounts/consumers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .update_permissions import UpdatePermissionConsumer
27 changes: 27 additions & 0 deletions marketplace/accounts/consumers/update_permissions.py
Original file line number Diff line number Diff line change
@@ -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}")
9 changes: 9 additions & 0 deletions marketplace/accounts/handle.py
Original file line number Diff line number Diff line change
@@ -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
)
1 change: 1 addition & 0 deletions marketplace/accounts/usecases/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .permission_update import update_permission
69 changes: 69 additions & 0 deletions marketplace/accounts/usecases/permission_update.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions marketplace/event_driven/handle.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 8f4f74a

Please sign in to comment.