original_docstring: Use this decorator to authenticate the user by their `role`- attribute. Requires the use of another authentication decorator before this one.
docstring: Use this decorator to authenticate the user by their `role`- attribute. Requires the use of another authentication decorator before this one.
docstring_tokens: Use this decorator to authenticate the user by their ` role ` - attribute . Requires the use of another authentication decorator before this one .
code: def requires_role(role: Union[str, list]):
      """Use this decorator to authenticate the user by their `role`- attribute. Requires the use of another authentication decorator before this one."""
      role = [r.lower() for r in role] if type(role) == list else role.lower()
  
      def my_wrap(func):
          @wraps(func)
          def wrapper(request: Request, *args, **kwargs):
              user_roles = _role_hierarchy.get(request.user.role, set())
              if type(role) == list:
                  for r in role:
                      if r == request.user.role or r in user_roles:
                          return func(request, *args, **kwargs)
              if type(role) == str:
                  if role == request.user.role or role in user_roles:
                      return func(request, *args, **kwargs)
              return Response(401, body=string.Template(UNAUTHORIZED_TMPLT).safe_substitute(**request.user.to_dict()))
          return wrapper
      return my_wrap
code_tokens: def requires_role ( role : Union [ str , list ] ) : role = [ r . lower ( ) for r in role ] if type ( role ) == list else role . lower ( ) def my_wrap ( func ) : @ wraps ( func ) def wrapper ( request : Request , * args , ** kwargs ) : user_roles = _role_hierarchy . get ( request . user . role , set ( ) ) if type ( role ) == list : for r in role : if r == request . user . role or r in user_roles : return func ( request , * args , ** kwargs ) if type ( role ) == str : if role == request . user . role or role in user_roles : return func ( request , * args , ** kwargs ) return Response ( 401 , body = string . Template ( UNAUTHORIZED_TMPLT ) . safe_substitute ( ** request . user . to_dict ( ) ) ) return wrapper return my_wrap
short_docstring: Use this decorator to authenticate the user by their `role`- attribute.
short_docstring_tokens: Use this decorator to authenticate the user by their ` role ` - attribute .
comment: """Use this decorator to authenticate the user by their `role`- attribute. Requires the use of another authentication decorator before this one."""