- Roles are assigned to users or a group of users. Any user can have more than one role, like
editororowner. - Permissions allow users to access certain objects based on their specific roles, like
device_renamerorchannel_archiver.
viewer of a trip can have permissions to view bookings, while the role owners can have permissions to add or view trip bookings.
When to use a Roles and Permissions modelRole and permissions models in OpenFGA can both directly assign roles to users and assign permissions through relations users receive downstream from other relations. For example, you can:
- Grant someone an
adminrole that caneditandreadadocument - Grant someone a
security_guardrole that canlive_video_vieweron adevice - Grant someone a
viewerrole that canview_productson ashop
check('bob', 'booking_adder', 'trip:Europe') instead of check('bob', 'owner', 'trip:Europe'), and you later decide owners can no longer add bookings to a trip, you can remove the relation within the trip type with no code changes in your application and all permissions will automatically honor the change.Before you start
Familiarize yourself with the basics of OpenFGA Concepts. Assume that you have the following authorization model and a type calledtrip that users can be related to as viewer and/or an owner.
Step by step
The Roles and Permissions example below is a trip booking system that hasowners and/or viewers, both of which can have more granular permissions like adding bookings to a trip or viewing a trip’s bookings.
To represent this in an OpenFGA environment, you need to:
- Understand how roles are related to direct relations for the trip booking system
- Add implied relations to the existing authorization model to define permissions for bookings
- Check user roles and their permissions based on relationship tuples for direct and implied relations
01. Understand how roles work within the trip booking system
Roles are relations that are directly assigned to users. Below, the stated roles that a given user can be assigned areowner and viewer.
02. Add permissions for bookings
Permissions are relations that users get through other relations. To avoid adding a direct relationship type restriction to the relation in the authorization model while representing permissions, they instead define the relation via other relations in the model, which indicates that it is a permission granted to and implied from a different relation. To add permissions related to bookings, add new relations to thetrip object type denoting the various actions a user can take on trips, like view, edit, delete, or rename.
To allow viewers of a trip to have permissions to view bookings and owners to have permissions to add/view bookings, you modify the type:
Note: bothbooking_viewerandbooking_adderdon’t have direct relationship type restrictions, which ensures that the relation can only be assigned through the role and not directly.
03. Check user roles and their permissions
Your type definitions reflects the roles and permissions on how bookings can be viewed/added, so you can create relationship tuples to assign roles to users, then check if users have the proper permissions. Create two relationship tuples:- give
bobthe role ofviewerontripcalled Europe. - give
alicethe role ofownerontripcalled Europe.
bob allowed to view bookings on trip Europe?
bob is a booking_viewer because of the following chain of resolution:
bobis aviewerontrip: Europe- Any user related to the object
trip:Europeasvieweris also related as abooking_viewer(i.eusersRelatedToObjectAs: viewer) - Therefore, all
viewerson a giventriparebooking_viewers
bob is not allowed to add bookings on trip Europe, run the following check:
You also check: is alice allowed to view and add bookings on trip Europe?
alice is a booking_viewer and booking_adder because of the following chain of resolution:
aliceis aownerontrip: Europe- Any user related to the object
trip:Europeasowneris also related as abooking_viewer - Any user related to the object
trip:Europeasowneris also related as abooking_adder - Therefore, all
ownerson a giventriparebooking_viewersandbooking_adderson that trip
Related sections
See following sections for more on how to model for roles and permissions.Modeling Concepts: Concentric Relationships
Learn about how to represent a concentric relationships in OpenFGA.
Modeling Google Drive
See how to indicate that editors are commenters and viewers in Google Drive.
Modeling GitHub
See how to indicate that repository admins are writers and readers in GitHub.