Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update partial update from User permission endpoint #222

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[flake8]
ignore=E501,F405,T003,E203,W503
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ fail_fast: true

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
rev: v4.4.0
hooks:
- id: check-merge-conflict
- id: end-of-file-fixer

- repo: https://github.com/psf/black
rev: 21.12b0
rev: 23.3.0
hooks:
- id: black

- repo: https://github.com/pycqa/flake8
rev: 4.0.1
rev: 6.0.0
hooks:
- id: flake8
16 changes: 0 additions & 16 deletions weni/auth/backends.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import logging

import pytz
from django.conf import settings

from mozilla_django_oidc.auth import OIDCAuthenticationBackend
from temba.orgs.models import Org

LOGGER = logging.getLogger("weni_django_oidc")

Expand Down Expand Up @@ -33,18 +29,6 @@ def create_user(self, claims):

user.save()

org = Org.objects.create(
lucaslinhares marked this conversation as resolved.
Show resolved Hide resolved
name="Temba New",
timezone=pytz.timezone("America/Sao_Paulo"),
brand=settings.DEFAULT_BRAND,
created_by=user,
modified_by=user,
)
org.administrators.add(user)

# initialize our org, but without any credits
org.initialize(branding=org.get_branding(), topup_size=0)

return user

def update_user(self, user, claims):
Expand Down
50 changes: 38 additions & 12 deletions weni/internal/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
from rest_framework.exceptions import ValidationError

from weni.internal.views import InternalGenericViewSet
from weni.internal.users.serializers import UserAPITokenSerializer, UserSerializer, UserPermissionSerializer
from weni.internal.users.serializers import (
UserAPITokenSerializer,
UserSerializer,
UserPermissionSerializer,
)
from temba.api.models import APIToken
from temba.orgs.models import Org

Expand All @@ -24,9 +28,13 @@


class UserViewSet(InternalGenericViewSet):
@action(detail=False, methods=["GET"], url_path="api-token", serializer_class=UserAPITokenSerializer)
@action(
detail=False,
methods=["GET"],
url_path="api-token",
serializer_class=UserAPITokenSerializer,
)
def api_token(self, request: "Request", **kwargs):

serializer = self.get_serializer(data=request.query_params)
serializer.is_valid(raise_exception=True)

Expand All @@ -35,15 +43,25 @@ def api_token(self, request: "Request", **kwargs):
except APIToken.DoesNotExist:
raise exceptions.PermissionDenied()

return Response(dict(user=api_token.user.email, org=api_token.org.uuid, api_token=api_token.key))
return Response(
dict(
user=api_token.user.email,
org=api_token.org.uuid,
api_token=api_token.key,
)
)


class UserPermissionEndpoint(InternalGenericViewSet):
serializer_class = UserPermissionSerializer

def retrieve(self, request):
org = get_object_or_404(Org, uuid=request.query_params.get("org_uuid"))
user = get_object_or_404(User, email=request.query_params.get("user_email"), is_active=request.query_params.get("is_active", True))
user = get_object_or_404(
User,
email=request.query_params.get("user_email"),
is_active=request.query_params.get("is_active", True),
)

permissions = self._get_user_permissions(org, user)
serializer = self.get_serializer(permissions)
Expand All @@ -52,7 +70,11 @@ def retrieve(self, request):

def partial_update(self, request):
org = get_object_or_404(Org, uuid=request.data.get("org_uuid"))
user = get_object_or_404(User, email=request.data.get("user_email"), is_active=request.query_params.get("is_active", True))
user, created = User.objects.get_or_create(
email=request.data.get("user_email"),
defaults={"username": request.data.get("user_email")},
is_active=request.query_params.get("is_active", True),
)

self._validate_permission(org, request.data.get("permission", ""))
self._set_user_permission(org, user, request.data.get("permission", ""))
Expand All @@ -64,7 +86,11 @@ def partial_update(self, request):

def destroy(self, request):
org = get_object_or_404(Org, uuid=request.data.get("org_uuid"))
user = get_object_or_404(User, email=request.data.get("user_email"), is_active=request.query_params.get("is_active", True))
user = get_object_or_404(
User,
email=request.data.get("user_email"),
is_active=request.query_params.get("is_active", True),
)

self._validate_permission(org, request.data.get("permission", ""))
self._remove_user_permission(org, user, request.data.get("permission", ""))
Expand All @@ -89,7 +115,6 @@ def _set_user_permission(self, org: Org, user: User, permission: str):

def _validate_permission(self, org: Org, permission: str):
permissions_keys = self._get_permissions(org).keys()

if permission not in permissions_keys:
raise ValidationError(detail=f"{permission} is not a valid permission!")

Expand All @@ -113,16 +138,16 @@ def _get_user_permissions(self, org: Org, user: User) -> dict:


class UserEndpoint(InternalGenericViewSet, mixins.RetrieveModelMixin):

serializer_class = UserSerializer
queryset = User.objects.all()
lookup_field = "uuid"


def partial_update(self, request):
instance = get_object_or_404(User, email=request.query_params.get("email"))

if request.data.get("language") not in [language[0] for language in settings.LANGUAGES]:
if request.data.get("language") not in [
language[0] for language in settings.LANGUAGES
]:
raise ValidationError("Invalid argument: language")

user_settings = instance.get_settings()
Expand All @@ -136,7 +161,8 @@ def retrieve(self, request):
raise ValidationError(detail="empty email")

user = User.objects.get_or_create(
email=request.query_params.get("email"), defaults={"username": request.query_params.get("email")}
email=request.query_params.get("email"),
defaults={"username": request.query_params.get("email")},
)

serializer = self.get_serializer(user[0])
Expand Down