can I assign fine-grained permissions (like `finan...
# general
t
can I assign fine-grained permissions (like
finance:view
) directly to a user instead of just assigning roles? For example, if a user already has the
agent
role but also needs the
finance:view
permission, how can I handle this?
o
it’s better to ask in #permit-saas 😉 (for next time) For now @Ask Inkeep
✅ 1
a
Thanks for tagging me @Or Weis. I'll look through relevant sources and get back to you shortly.
Yes, you can handle fine-grained permissions in Permit.io through multiple approaches: Direct Permission Assignment via Roles In Permit.io, permissions are typically granted through roles (1) . You can create a specific role that contains just the
finance:view
permission and assign it to the user alongside their existing
agent
role (2) . Resource-Level Roles For more granular control, you can use resource-specific roles (formatted as
Resource#Role
) (3) (4) . For example, you could create a
Finance#Viewer
role that grants view permissions only on finance-related resources (5) . Dynamic Role Assignment Here's how you can programmatically assign additional roles to users:
Copy code
from permit import Permit
from dotenv import load_dotenv
from django.http import JsonResponse
from django.views import View
load_dotenv()
permit = Permit(
    pdp= os.getenv("permit_pdp_url") ,
    token= os.getenv("permit_sdk_key")
)
class AssignRoleView(View) :
    def post(self, request) :
        user = request.user
        role = request.POST.get('role')
        tenant = request.POST.get('tenant')
        # Assuming you have a function to assign roles
        ra = await permit.api.users.assign_role(user, role, tenant)
        return JsonResponse({"message": "Role assigned successfully"}, status=200)
(6) Multiple Role Assignment Users can have multiple roles simultaneously in Permit.io (2) . A user can be both an
agent
and have a
finance_viewer
role (or any other role containing the
finance:view
permission) within the same tenant . The recommended approach is to create focused roles for specific permissions and assign multiple roles to users as needed, rather than trying to assign individual permissions directly.
To learn more, see the sources I considered: [[1] Policy Basics](https://docs.permit.io/how-to/build-policies/policy-basics) [[2] Best Practices for Multi-Tenant Authorization](https://www.permit.io/blog/best-practices-for-multi-tenant-authorization) [[3] What is ReBAC?](https://docs.permit.io/how-to/build-policies/rebac/overview) [[4] Build Authorization Like Google](https://www.permit.io/blog/build-authorization-like-google) [[5] From RBAC to ReBAC and ABAC with Next.js and Permit.io](https://www.permit.io/blog/from-rbac-to-rebac-and-abac-with-nextjs-and-permitio) [[6] How to Implement Fine-Grained Authorization with Django](https://www.permit.io/blog/fine-grained-authorization-with-django)
o
Short answer: • As a best practice, and avoiding permissions sprawl - we encourage you to use Roles as boxes for Permissions • You can create roles on the fly with whichever combos of permissions you want ◦ FYI You’d actually gain a lot in sense of auditing by picking good names for these roles
t
Thanks for the clarification, @Or Weis. Could we discuss this over a call? We’d like to learn more about this and ABAC.
o
Hi Teccas - can you shed some light on who’s we? (feel free to DM me)