def user_roles(request): """Return a map of the current roles assigned to the user. Roles are denoted by their simple names, and then the permission type. Permissions can be access as follows: - roles.part.view - roles.build.delete Each value will return a boolean True / False """ user = request.user roles = { } for role in RuleSet.RULESET_MODELS.keys(): permissions = {} for perm in ['view', 'add', 'change', 'delete']: permissions[perm] = user.is_superuser or check_user_role(user, role, perm) roles[role] = permissions return {'roles': roles}
def has_permission(self): """ Determine if the current user """ roles_required = [] if type(self.role_required) is str: roles_required.append(self.role_required) elif type(self.role_required) in [list, tuple]: roles_required = self.role_required user = self.request.user # Superuser can have any permissions they desire if user.is_superuser: return True for required in roles_required: (role, permission) = required.split('.') if role not in RuleSet.RULESET_NAMES: raise ValueError(f"Role '{role}' is not a valid role") if permission not in RuleSet.RULESET_PERMISSIONS: raise ValueError( f"Permission '{permission}' is not a valid permission") # Return False if the user does not have *any* of the required roles if not check_user_role(user, role, permission): return False # We did not fail any required checks return True
def get(self, request, *args, **kwargs): """Return the list of roles / permissions available to the current user""" user = request.user roles = {} for ruleset in RuleSet.RULESET_CHOICES: role, text = ruleset permissions = [] for permission in RuleSet.RULESET_PERMISSIONS: if check_user_role(user, role, permission): permissions.append(permission) if len(permissions) > 0: roles[role] = permissions else: roles[role] = None # pragma: no cover data = { 'user': user.pk, 'username': user.username, 'roles': roles, 'is_staff': user.is_staff, 'is_superuser': user.is_superuser, } return Response(data)
def get(self, request, *args, **kwargs): user = request.user roles = {} for ruleset in RuleSet.RULESET_CHOICES: role, text = ruleset permissions = [] for permission in RuleSet.RULESET_PERMISSIONS: if check_user_role(user, role, permission): permissions.append(permission) if len(permissions) > 0: roles[role] = permissions else: roles[role] = None data = { 'user': user.pk, 'username': user.username, 'roles': roles, 'is_staff': user.is_staff, 'is_superuser': user.is_superuser, } return Response(data)
def has_permission(self): """Determine if the current user has specified permissions.""" roles_required = [] if type(self.role_required) is str: roles_required.append(self.role_required) elif type(self.role_required) in [list, tuple]: roles_required = self.role_required user = self.request.user # Superuser can have any permissions they desire if user.is_superuser: return True for required in roles_required: (role, permission) = required.split('.') if role not in RuleSet.RULESET_NAMES: raise ValueError(f"Role '{role}' is not a valid role") if permission not in RuleSet.RULESET_PERMISSIONS: raise ValueError( f"Permission '{permission}' is not a valid permission") # Return False if the user does not have *any* of the required roles if not check_user_role(user, role, permission): return False # If a permission_required is specified, use that! if self.permission_required: # Ignore role-based permissions return super().has_permission() # Ok, so at this point we have not explicitly require a "role" or a "permission" # Instead, we will use the model to introspect the data we need model = getattr(self, 'model', None) if not model: queryset = getattr(self, 'queryset', None) if queryset is not None: model = queryset.model # We were able to introspect a database model if model is not None: app_label = model._meta.app_label model_name = model._meta.model_name table = f"{app_label}_{model_name}" permission = self.get_permission_class() if not permission: raise AttributeError( f"permission_class not defined for {type(self).__name__}") # Check if the user has the required permission return RuleSet.check_table_permission(user, table, permission) # We did not fail any required checks return True