Skip to content

Commit

Permalink
fix: Reading role permissions generates 500 error backend (#3079)
Browse files Browse the repository at this point in the history
  • Loading branch information
novakzaballa committed Nov 30, 2023
1 parent 2880ef5 commit cee607a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
12 changes: 12 additions & 0 deletions api/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.conf import settings
from django.contrib.auth.base_user import BaseUserManager
from django.contrib.auth.models import AbstractUser
from django.core.exceptions import ImproperlyConfigured
from django.core.mail import send_mail
from django.db import models
from django.db.models import Count, QuerySet
Expand Down Expand Up @@ -42,6 +43,9 @@
InviteLink,
)

if settings.IS_RBAC_INSTALLED:
from rbac.models import UserRole

logger = logging.getLogger(__name__)
mailer_lite = MailerLite()

Expand Down Expand Up @@ -255,6 +259,14 @@ def get_user_organisation(
"User %d is not part of organisation %d" % (self.id, organisation_id)
)

def get_user_roles(self):
if not settings.IS_RBAC_INSTALLED:
raise ImproperlyConfigured(
"RBAC is not installed. Unable to retrieve user roles."
)

return UserRole.objects.filter(user=self)

def get_permitted_projects(
self, permission_key: str, tag_ids: typing.List[int] = None
) -> QuerySet[Project]:
Expand Down
12 changes: 11 additions & 1 deletion api/users/serializers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from django.conf import settings
from djoser.serializers import UserSerializer as DjoserUserSerializer
from rest_framework import serializers
from rest_framework.exceptions import ValidationError

from organisations.models import Organisation
from organisations.serializers import UserOrganisationSerializer

if settings.IS_RBAC_INSTALLED:
from rbac.serializers import UserRoleSerializer

from .models import FFAdminUser, UserPermissionGroup


Expand Down Expand Up @@ -56,9 +60,15 @@ class Meta:
class UserListSerializer(serializers.ModelSerializer):
role = serializers.SerializerMethodField(read_only=True)
join_date = serializers.SerializerMethodField(read_only=True)
if settings.IS_RBAC_INSTALLED:
roles = UserRoleSerializer(many=True, read_only=True, source="get_user_roles")

default_fields = ("id", "email", "first_name", "last_name", "last_login")
organisation_users_fields = ("role", "date_joined")
organisation_users_fields = (
"role",
"date_joined",
*([] if not settings.IS_RBAC_INSTALLED else ["roles"]),
)

class Meta:
model = FFAdminUser
Expand Down

3 comments on commit cee607a

@vercel
Copy link

@vercel vercel bot commented on cee607a Nov 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

docs – ./docs

docs-git-main-flagsmith.vercel.app
docs-flagsmith.vercel.app
docs.flagsmith.com
docs.bullet-train.io

@vercel
Copy link

@vercel vercel bot commented on cee607a Nov 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on cee607a Nov 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.