How can I let the superadmin user and user itself to access a API?

fanhualuojin154873

I have a UserUpdateAPIView, in it I can edit the user information:

class UserUpdateAPIView(RetrieveUpdateAPIView):
    queryset = User.objects.filter(is_admin=False, is_staff=False, is_superuser=False).exclude(status=4)
    serializer_class = UserDetailSerializer  
    lookup_field = "username"
    def perform_update(self, serializer):
        serializer.save()

The UserDetailSerializer:

class UserDetailSerializer(ModelSerializer):
"""
user detail
"""
class Meta:
    model = User
    exclude = [
        'password',
    ]
    depth = 1

Now, every user can access the UserUpdateAPIView, so its a bad design. I just want the super admin and the user itself can access the APIView, how to implement it?

I know I can use permissions = [IsAdminUser] to allow the admin users to access this API, but I just want to let the super admin user and the user itself to access.

Ykh
from rest_framework import permissions
from rest_framework.compat import is_authenticated

class IsAdminUserOrSelf(permissions.BasePermission):

    def has_object_permission(self, request, view, obj):
        # this methid is called in get_object method.
        # obj mean the object you retrieve.Here you retrieved is User instance.
        # It's can be any model instance,depend on the Molde you Retrieve in views.

        # if you want everyone can see user info
        if request.method in permissions.SAFE_METHODS:
            return True
        # if you use Django2.0 is_authenticated(request.user) should be changed to request.user.is_authenticated
        if request.user and is_authenticated(request.user):
            # is self or is superuser
            return obj == request.user or request.user.is_superuser
        else:
            return False

class UserUpdateAPIView(RetrieveUpdateAPIView):
    permissions = [IsAdminUserOrSelf,]
    queryset = User.objects.filter(is_admin=False, is_staff=False, is_superuser=False).exclude(status=4)
    serializer_class = UserDetailSerializer  
    lookup_field = "username"
    def perform_update(self, serializer):
        serializer.save()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I access the instantiated user within the user model itself (without creating another relationship)?

From Dev

How can I let the user fill in an array?

From Dev

How can I let the user load/play their own songs in XNA?

From Dev

How can I find out what scope a user has based off their access token in the Instagram api?

From Dev

iOS client: I can have user sign into Active Directory, but how to access MS Graph API?

From Dev

How can I allow a user to get access to survey results using the Google Consumer Surveys API?

From Dev

iOS client: I can have user sign into Active Directory, but how to access MS Graph API?

From Dev

How can I test that a user can't access a page with access reserved to another user?

From Dev

How can I test that a user can't access a page with access reserved to another user?

From Dev

how can i access control in user control from master page?

From Dev

How do I create a SSH user that can only access MySQL?

From Dev

How can I disable network access for specific user?

From Dev

How can I prevent access to an Angular page if the user IS logged in with AngularFire?

From Dev

How can I get access to user folder from another computer?

From Dev

How can I install software on an user computer (no root access)?

From Dev

How can I access user type from Razor html (Umbraco)

From Dev

How can I get object data by itself without user_id key?

From Dev

Can a user also be a group itself?

From Java

How can I offer default values for SwiftUI config but let the user override them?

From Dev

How can I let a user download multiple files when a button is clicked?

From Dev

How can I let user paste image data from the clipboard into a canvas element in Firefox in pure Javascript?

From Dev

When presenting a popover view, how can I let the user select cell in parent collection view?

From Dev

(Python) How can I let a user open a text file and then change an integer / number

From Dev

How can i run a service for my android app to let the user return to the same activity and bypass login

From Dev

How can I let an iOS Teams App user use my Bot without inform the MicrosoftAppId?

From Dev

SoftLayer API: can a user access multiple accounts?

From Dev

How can I give a new user to access file/folders in an existing user

From Dev

How can I give an access user01 to only reset password for user02 in CentOS 7?

From Dev

How can I use JavaMail API without the user having to install it?

Related Related

  1. 1

    How can I access the instantiated user within the user model itself (without creating another relationship)?

  2. 2

    How can I let the user fill in an array?

  3. 3

    How can I let the user load/play their own songs in XNA?

  4. 4

    How can I find out what scope a user has based off their access token in the Instagram api?

  5. 5

    iOS client: I can have user sign into Active Directory, but how to access MS Graph API?

  6. 6

    How can I allow a user to get access to survey results using the Google Consumer Surveys API?

  7. 7

    iOS client: I can have user sign into Active Directory, but how to access MS Graph API?

  8. 8

    How can I test that a user can't access a page with access reserved to another user?

  9. 9

    How can I test that a user can't access a page with access reserved to another user?

  10. 10

    how can i access control in user control from master page?

  11. 11

    How do I create a SSH user that can only access MySQL?

  12. 12

    How can I disable network access for specific user?

  13. 13

    How can I prevent access to an Angular page if the user IS logged in with AngularFire?

  14. 14

    How can I get access to user folder from another computer?

  15. 15

    How can I install software on an user computer (no root access)?

  16. 16

    How can I access user type from Razor html (Umbraco)

  17. 17

    How can I get object data by itself without user_id key?

  18. 18

    Can a user also be a group itself?

  19. 19

    How can I offer default values for SwiftUI config but let the user override them?

  20. 20

    How can I let a user download multiple files when a button is clicked?

  21. 21

    How can I let user paste image data from the clipboard into a canvas element in Firefox in pure Javascript?

  22. 22

    When presenting a popover view, how can I let the user select cell in parent collection view?

  23. 23

    (Python) How can I let a user open a text file and then change an integer / number

  24. 24

    How can i run a service for my android app to let the user return to the same activity and bypass login

  25. 25

    How can I let an iOS Teams App user use my Bot without inform the MicrosoftAppId?

  26. 26

    SoftLayer API: can a user access multiple accounts?

  27. 27

    How can I give a new user to access file/folders in an existing user

  28. 28

    How can I give an access user01 to only reset password for user02 in CentOS 7?

  29. 29

    How can I use JavaMail API without the user having to install it?

HotTag

Archive