-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/consumer-permissions' of https://github.com/wen…
…i-ai/weni-integrations-engine into develop
- Loading branch information
Showing
6 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 @@ | ||
from .update_permissions import UpdatePermissionConsumer |
This file contains 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,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}") |
This file contains 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,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 | ||
) |
This file contains 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 @@ | ||
from .permission_update import update_permission |
This file contains 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,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 |
This file contains 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 |
---|---|---|
@@ -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) |