Hi there,
I am creating a Django App in which I am using django-rules library to define and add permissions.
I have followed the documentation and created the predicates in a separate file called permissions.py
. I have also added the permissions in the same file. My permissions.py
looks like below:
from .models import WorkspaceMembership
import rules
@rules.predicate
def is_workspace_member(user, workspace):
workspace_membership = WorkspaceMembership.objects.get_by_member_and_workspace(user, workspace)
if workspace_membership is not None:
return workspace_membership.is_workspace_member
return False
@rules.predicate
def is_workspace_admin(user, workspace):
workspace_membership = WorkspaceMembership.objects.get_by_member_and_workspace(user, workspace)
if workspace_membership is not None:
return workspace_membership.is_workspace_admin
return False
@rules.predicate
def is_active_member(user, workspace):
workspace_membership = WorkspaceMembership.objects.get_by_member_and_workspace(user, workspace)
if workspace_membership is not None:
return workspace_membership.is_active
return False
rules.add_perm("accounts.view_workspace", is_workspace_member)
rules.add_perm("accounts.view_workspace", is_workspace_admin)
rules.add_perm("accounts.view_workspace", is_workspace_member | is_workspace_admin)
Now, I want to use these permissions in my class-based views and programmatically like checking below:
if user.has_permissions("any-permission")
I am unable to figure out that how to use the permissions. Because the predicates will required two objects, one is User instance and another is Workspace instance, in order to work, which are not present in permissions.py
file. So, how do I use the permissions, can anybody guide me?