A user token is a holder of the request's authentication state. Using user tokens you can get an instance of the current user, test if the request is authenticated, and get original user details if an impersonation is active.
During request handling the authentication middleware sets user token to request.auth making it available in every
view. If the request is not authenticated the anonymous user token is set.
Here is a list of attributes:
is_authenticatedreturns True if current user is authenticatedis_anonymousreturns True if current user is not authenticatedoriginal_user_idreturns ID of original user when the impersonation is activescopesreturns list of permissions that current user is assigneduser_idreturns ID of current useruserreturns a current user instance or instance ofAnonymousUserdisplay_namereturns a string representation of current user
Use regular if-checks:
if request.auth:
print('authenticated')Leverage "in" operator to check if the user has a specific permission assigned:
if 'auth:impersonate_others' in request.auth:
print('user can activate impersonation')