ra-data-django-rest-framework icon indicating copy to clipboard operation
ra-data-django-rest-framework copied to clipboard

Are there permissions available?

Open rafaellehmkuhl opened this issue 5 years ago • 4 comments

On the example client (example/client/src/index.js) you use permissions for displaying or not the users section:

{permissions => [
    <Resource name="posts" {...posts} />,
    <Resource name="comments" {...comments} />,
    permissions ? <Resource name="users" {...users} /> : null,
    <Resource name="tags" {...tags} />,
]}

Are those permissions available? Because as far as I could see on the tokenAuthProvider.ts, only the authentication token is stored locally.

rafaellehmkuhl avatar Oct 30 '20 13:10 rafaellehmkuhl

If you like the idea, I'm thinking about submitting a PR adding the user groups to the token response, like this:

from rest_framework.authtoken.views import ObtainAuthToken
from rest_framework.authtoken.models import Token
from rest_framework.response import Response

class CustomAuthToken(ObtainAuthToken):

    def post(self, request, *args, **kwargs):
        serializer = self.serializer_class(data=request.data,
                                           context={'request': request})
        serializer.is_valid(raise_exception=True)
        user = serializer.validated_data['user']
        token, created = Token.objects.get_or_create(user=user)
        return Response({
            'token': token.key,
            'roles': [g.name for g in user.groups.all()]
        })

which would substitute the default ObtainAuthToken view, and making the corresponding changes on the tokenAuthProvider to make the roles available at the permissions.

rafaellehmkuhl avatar Oct 30 '20 14:10 rafaellehmkuhl

Would love to have this.

mmodenesi avatar Aug 02 '21 21:08 mmodenesi

@rafaellehmkuhl could you please explain your method for permission more?

vahidqo avatar Apr 06 '22 13:04 vahidqo

Sorry, but it has been a long time since I've worked with this and I honest have no clue 😅

@rafaellehmkuhl could you please explain your method for permission more?

rafaellehmkuhl avatar Apr 06 '22 16:04 rafaellehmkuhl