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

Add method to check is user either staff or superuser. #1409

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

adambirds
Copy link

Add method to check is user either staff or superuser.

@adambirds
Copy link
Author

Would be gerat if this method could be added @vitalik, let me know if it needs any amendments.

@vitalik
Copy link
Owner

vitalik commented Feb 11, 2025

I think the check of is_superuser (and naming it so) is obsolete

in django world if user is superuser that it's automatically is staff and has all the permissions

https://github.com/django/django/blob/41239fe34d64e801212dccaa4585e4802d0fac68/django/contrib/auth/models.py#L384-L397

I would just call it SessionAuthIsStaff :

class SessionAuthIsStaff(SessionAuthSuperUser):
    
    def authenticate(self, request: HttpRequest, key: Optional[str]) -> Optional[Any]:
        result = super().authenticate(request, key)
        if result:
              return result
        if getattr(request.user, "is_staff", None):
            return request.user

        return None

django_auth_is_staff = SessionAuthIsStaff()

Also added tests for new staff or superuser method.
@adambirds adambirds force-pushed the add-staff-auth-method branch from 0557ac8 to a9a680b Compare February 14, 2025 23:01
@adambirds
Copy link
Author

@vitalik I've made those changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants