# 🔐 Permissions system guide The permission system is used to control access to resources or functionality within Baserow. It determines who is allowed to perform certain operations or access certain data. The permission system is pluggable and allows for the easy addition or replacement of different components that handle the authorization of user operations. It provides flexibility and modularity in the way that access to resources or functionality is controlled within Baserow. This allows for different authorization strategies to be easily implemented and swapped out as needed without having to make significant changes to the overall code. ## 📖 Glossary Before going any further, we need to agree on the definition of some terms: **Object**: represents a piece of data in Baserow. One of Field, Row, Table, Database, Workspace, User, Team, Role, Webhook, … **Hierarchical Objects**: in Basrow, _Objects_ are related to each others and we can have a parent <-> children dependency between two _Objects_. A full hierarchy tree can be created with all the Baserow objects. For instance, The `Table` _Object_ is a child of a `Database`. **Actor**: A generic term grouping anything that can perform _Operations_ on an _Object_ in Baserow. Can be a `User` but also a `Personal API Token` or an `AnonymousUser`... **Operation**: an action an _Actor_ do on an _Object_. Some examples: - `database.list_tables`: the operation to list the `Tables` related to a `Database`. - `database_table.create_row`: the action to create a `Row` in a `Table`. - `database_table.update`: the action to update a `Table` _Object_. **Context**: the `Object` on which the an _Operation_ is applied. For instance: - for the _Operation_ `database.list_tables` the context object is the `Database` we want the `Table` list for. - for the _Operation_ `database_table.create_row` the context object is the `Table` we want to create the `Row` for. - for the _Operation_ `database_table.update` the context object is also the `Table` we want to update. **Permission request**: a _Permission request_ is represented by a triplet consisting of an _Actor_, an _Operation_, and a _Context_, which is used to determine if access to a specific resource or functionality is granted or denied by the _Permission system_. The _Context_ can be omitted if the _Operation_ doesn't need one. **Permission system**: the whole mechanism in Baserow that decides if a _Permission request_ is allowed or not. The _Permission system_ relies on _Permission managers_ to take a decision regarding a specific _Permission request_ in a given _Workspace_. **Permission manager**: a _Permission manager_ is a pluggable part of the _Permission system_ that can decide if a _Permission Request_ is allowed or not when some criteria are met. Each _Permission manager_ is responsible to take a decision in some situation. For instance the `StaffOnlyPermissionManager` can decide to disallow an _Operation_ if the given _Actor_ is not part of the staff. **Workspace**: An _Operation_ can take place in a specific _Workspace_ (Formerly Group). **Subject**: includes all _Actors_ but also groups of _Actors_ like `Teams`. Users and identities are both `auth.User` subjects. The immutable `UserProfile.user_type` distinguishes them; their roles and grants use the same workspace memberships, teams and field permissions. `User.objects` and `WorkspaceUser.objects` list humans; permission and shared User resolution code opts into `User.objects_and_identities` and `WorkspaceUser.objects_and_identities`. An identity must have one workspace membership and be active and untrashed to execute, even for trash-inclusive checks. Human account grants and authentication explicitly require `is_human(actor)`; `isinstance(actor, User)` does not establish that. **Personal API Token**: An authentication token which can be created by users in their settings area in Baserow. It is owned by a `User`, for a _Workspace_, allowing access to some of our API endpoints. ## 📃 Main principles For every _Operation_ an _Actor_ wants to perform on a _Context_, a _Permission request_ is checked by the permission system. Behind the scene, the _Permission request_ is tested by each _Permission manager_ one by one always in same order. Each _Permission manager_ can: - Allow the Permission Request. Then the operation can be executed. - Disallow the Permission Request. The operation is canceled and an error is raised. - Passthrough the Permission Request. The Permission Request is then tested by the next _Permission manager_ If none of the Permission managers have allowed or disallowed the Permission, then the permission is disallowed by default. ![Permission diagram](https://raw.githubusercontent.com/baserow/baserow/refs/heads/master/docs/assets/diagrams/permissionProcessus.jpg) [source](https://viewer.diagrams.net/?tags=%7B%7D&highlight=0000ff&edit=_blank&layers=1&nav=1#R7VnZVtswEP2aPNLjJXbIIwESKEvbAxR4FLZiq5GtVFYW9%2Bs7iiXvJUkhUCgvOZrxWMvcO4uVjn0YLUccTcML5mPasQx%2F2bGPOpbVd7rwKxVppnBNO1MEnPiZyiwUV%2BQXVkpDaWfEx0nFUDBGBZlWlR6LY%2ByJig5xzhZVszGj1VWnKMANxZWHaFN7S3wRZtp9q1foTzAJQr2y6fazJxHSxuokSYh8tiip7OOOfcgZE9koWh5iKn2n%2FXJ7mt7S84k7%2Bvwt%2BYluBmfXl9%2F3ssmG27ySH4HjWDzv1FY29RzRmfKXOqtItQM5m8U%2BlpMYHXsQiojC0IThDyxEqgBHM8FAxbgIWcBiRM8Zmyq7MYuFMjOljGP%2FQAIL8gNl3iRTDQmlag2QlP0%2BSIngbJJjJyfIgZDGFD1gOkDeJFht9JBRxuFRzGIsp%2FKBDOosxeaOC%2B1gQ98qDBI24x5%2BxE4Fh0A8wI%2FN52Z2cn8lnirkRphFWPAUDDimSJB5lcxIxUSQ2xW4w0BBvwUN7AYNRrBmLJeSzsE8IklCWPwIOSQwi5AIfDVFKw8tIJ1UCVMmApx1EFCUJArGNShvh9Icc4GXj%2FpVPbUNFdypljNxUaQKU8d%2FWEoTXWNHSHRbAtKlsOrAJ3MYBnJ4ybQS1ijpP2L3SbHrbhi7urStDV5Fsj3jk%2BM4boVomlQbx7ea%2FSsjcNbChI3HCWy2Trt8E3%2FPRKfBxHuo43WGrQfpTXPwlRjW25BgabXX%2BnfJ5DbIdJpIR4S4Vl%2Bgi0SxALpYxkOaW3gzvkKgZhuhGNo3DqOOPWwmv5BFD7NkfV2q8EGybYgiQqVvTzCdY0E81FK9ECVBDIIHG4M9tBINliRxAJJbSNcrYkOi311Vs%2FrVqmb2Wspat6Wsmeau6lqvQYDrFbIMAAXOruAkkhKIUraQ%2BL%2BzTqNbw8QynFduNfYbkMi%2B4qODeEJ%2B1%2BGzvoUwt24hrDfWP%2FQ%2F%2BodX5Jf13joIvcMNWgifJHkV%2BegMqt%2B7Zu1712ypQkZLFdp%2FhirEjfDCvDm4pQejL2dWgnu20dc3UE9ICa1B35IcSriCV3l6l4c7CPdS%2BORo8WhZfniUamlJxF1pXHoLpOIlKeh3njsF6EvUF79hWr0KmROlJYOpTABJS0bQrahbJVy3V7ulXGNvWU6NYtkOnje36DgqcssVpvI2WueXGC%2F%2F%2FPXxzjrV%2Bp1Yt7thp%2BrsqlPV1Wz910M59b8zWJz6VaX9gh8Qram7GTXXaILzmBkTnvy3QZMX0R2gA2Lxr1OWA4u%2F7uzj3w%3D%3D) Example: 1. If a non admin user wants to create `Table` in a `Database` of his `Workspace`, the following permission is tested: `(user, "database.create_table", database)` by the Permission system. 2) The Permission request is first given to the `CorePermissionManager` that can't take a decision, because it's not a core operation. Then it is given to the `StaffOnlyPermissionManager` that also can't decide because this is not a staff only operation. 3) The last permission manager the permission will be tested is the `BasicPermissionManager` that will allows the Permission request because it's not an Admin only operation so the user can execute it. ## ⚙️ The backend Most of the Permission system is driven by the backend. The main components of the Permissions system are the following: - `OperationType`: you need an `OperationType` for every _Operation_ you want to check. - `PermissionManagerType`: the _Permission managers_ are responsible for allowing or not the Permission requests. - `SubjectType`: every _Actor_ you want to use in the _Permission system_ must belongs to a `SubjectType`. - `ObjectScopeType`: every _Context_ object must be part of the Baserow _Object_ hierarchy. By now it's implemented by having a related `ObjectScopeType` for each _Object_ type. All of them are objects you can register in their related registry to extend the core functionnalities of Baserow Permission system. ### Check a Permission on the backend When you want to test a permission on the backend, you'll have to use the `CoreHandler.check_permission` method. ```python CoreHandler().check_permission( # The actor can be the user who did the request: actor = request.user. actor, # CreateRowDatabaseTable is an `OperationType` class and `.type` is its name. CreateRowDatabaseTable.type, context=table, workspace=workspace ) ``` If the permission request is allowed then this method will return `True` if not, it will raise a `PermissionException`. The workspace (formerly known as a group) is optional if the operation is a core operation that does not belong to any specific workspace. ### Filter a Django Queryset Another common use case related to permissions is to filter a django queryset based on the permissions of the user. You can acheive queryset filtering with this method call. ```python CoreHandler().filter_queryset( # The actor can be the user who did the request: actor = request.user. actor, # CreateRowDatabaseTable is an `OperationType` class and `.type` is its name. ListTablesDatabaseTableOperationType.type, queryset, workspace=workspace ) ``` Here the context is the database because we are listing the tables of this database but the queryset is a `Table` queryset. This is consistent with the `object_scope` property of the `ListTablesDatabaseTableOperationType` which is `TableObjectScope`. This is the purpose of `object_scope` property it helps to determine what kind of objects the operation targets. ### Declare a new operation An `OperationType` instance must be registered for each Operation you want to check. It can be declared this way: ```python from baserow.core.registries import OperationType class ListTablesDatabaseTableOperationType(OperationType): type = "database.list_tables" # Type context_scope_name = "database" # The name of the type of context needed to check permissions object_scope_name = "database_table" # The name of the type of the objects handled by the operation ``` For most of the operation the `context_scope_name` and the `object_scope_name` are the same, so the last can be omitted. However regarding all "list" operations, the object*scope_name is in general one of the children of the context object in the hierarchy of the \_Objects*. When you want to list all `Tables` of a `Database`, the context is a `Database` and the objects are the `Tables`. When you list all `Databases` of an `Application`, the context is an `Application` and the objects are the related `Databases`. This class must be registered in the `operation_type_registry` in order to be used. ```python from baserow.core.registries import operation_type_registry operation_type_registry.register(ListTablesDatabaseTableOperationType()) ``` An `Operation` instance is saved in database for each registered operation. You can use them later in your permission manager code if necessary. ### Create a backend Permission manager A permission manager is responsible for deciding whether or not a Permission Request is allowed for a certain application area. To ensure proper separation of concerns, a good permission manager should only handle one permission checking use case. The permission managers are then stacked to create a complex and powerful permission checking algorithm. You can think of them like being a Django middleware, but instead for a Permission Request instead. To create a new permission manager you have to create a new `PermissionManagerType` and implement the required methods. ```python from baserow.core.registries import PermissionManagerType class OwnedTablePermissionManagerType(PermissionManagerType): type = "owned_table" def check_multiple_permissions(self, check, workspace=None, include_trash=False): ... def get_permissions_object(self, actor, workspace=None): ... def filter_queryset(self, actor, operation_name, queryset, workspace=None) ... ``` A quick summary of these methods: - `.check_multiple_permissions` is the permissions checking method itself. It takes multiple checks at once for better performances. For each check the result dict should have the value `True` if the permission manager can accept the permission or an instance of `PermissionException` if not or it shouldn't include the check at all if the permission manager cannot make a decision about this check. - `get_permissions_object` should return any value that will be helpfull to the frontend permission manager to check a frontend permission. The data returned should be sufficient for the frontend to make a decision without having to request further data from the backend. - `filter_queryset` is used to filter a queryset regarding the permissions the actor has on the Objects returned by the queryset. The method should exclude the same Objects that the `.check_permission` would exclude if it was called for each Objects of the queryset. You can read the related docstring to learn more about these methods. Then, you can register it in the `permission_manager_type_registry`. ```python from baserow.core.registries import permission_manager_type_registry permission_manager_type_registry.register(OwnedTablePermissionManagerType()) ``` You'll have to add your permission manager in the enabled permission manager list in the Django settings: ```python PERMISSION_MANAGERS = [ 'core', 'staff_only', ... 'owned_table', # <- here ... 'basic' ] ``` The position of the permission manager in the list depends on its priority over the other permission managers. In our case we want the permission manager to answer before the basic permission manager has a chance to refuse it. Now you can check a permission that is handled by your permission manager 🎯. Remember that you probably need a frontend permission manager for each backend permision manager. See frontend section for more information. ## 📺 The frontend ### How to check a Permission On the frontend you can check a permission with the `$hasPermission` method available on the Vue instance: ```js // Inside a Vue component // this.$hasPermission(, , ) this.$hasPermission("database.create_table", database, workspace.id); ``` This call returns `true` if the operation is granted `false` otherwise. ### The permissions object The frontend permissions are calculated with the permission object sent by the backend at login for each workspace the user has access to. Check the `.get_permissions_object` method from each backend permission manager. The permission object looks like this: ```json [ { "name": "core", "permissions": ["list_workspaces"] }, { "name": "staff", "permissions": { "staff_only_operations": ["settings.update"], "is_staff": true } }, { "name": "basic", "permissions": { "admin_only_operations": [ "workspace.list_invitations", "...", "workspace.delete" ], "is_admin": true } } ] ``` Each entry of the list has been generated by a permission manager on the backend. The `name` property is the `.type` of the permission manager itself and the `permissions` property can be any value that helps the frontend to decide of the permission can be granted or not. For each backend permission manager a frontend permission manager should also be registered to handle it's value. To check the permissions, the frontend `$hasPermission` plugin asks to each permission manager for which the name is listed in this object, in the list order, if the permission is granted or not given the data from the `permissions` property. For instance the `BasicPermissionManagerType.hasPermission(permissions, operation, context, workspaceId)` method will be called with the following object: ```json { "admin_only_operations": [ "workspace.list_invitations", "...", "workspace_user.delete" ], "is_admin": true } ``` See next section to learn how to create the frontend permission manager. ### Creating a permission manager For each backend permission manager you probably need a frontend permission manager (some permission managers don't need one). You can create a frontend permission manager this way: ```js import { PermissionManagerType } from "@baserow/modules/core/permissionManagerTypes"; export class OwnedTablePermissionManagerType extends PermissionManagerType { static getType() { return "owned_table"; } hasPermission(permissions, operation, context) { // ... } } ``` Check out the documentation of the `PermissionManagerType` methods to figure out how to implement `hasPermission` for your permission manager. Then you need to register it during the Vue plugin initialisation phase in the `plugin.js` frontend file of your project. ```js app.$registry.register( "permissionManager", new OwnedTablePermissionManagerType(context), ); ``` And that's it, you have a fully functionnal frontend permission manager. ## 👑 Admins can't be locked out (enterprise RBAC) The enterprise `role` permission manager resolves an actor's roles per scope, from the workspace down to the application, table and view, and normally the most specific scope wins. One rule takes precedence over that, applied by `RoleAssignmentHandler.apply_admin_precedence` when the per scope list is built, so every consumer of the list sees the same result: the admin role an actor holds directly, as their stored workspace role or as a role assignment of their own, holds on that scope and on everything inside it. Any lower role on a descendant scope, directly assigned or through a team, has no effect. An admin of the workspace is therefore an admin everywhere in it. To restrict them, lower the admin role itself. Roles held through a team don't get that treatment. A team's admin role on a database applies on the database like any other team role, and a role on a table inside still wins there. On one scope the actor's own role keeps winning over the roles of their teams, whatever they are. A team's admin role on the workspace applies to the members whose stored role is "No role", as any team role does: they resolve to admin on the workspace, but without the precedence, so a lower role on a scope inside still wins for them. Existing role assignments that the rule makes inert are kept as they are; the rule applies the moment the code runs, without a data migration. The `role` permission manager also returns early for admins of the workspace when building the permissions object and the queryset filters: both come from the admin role alone, with no exceptions. Permission checks still walk the scope list, since that walk is what denies a context that doesn't belong to the workspace of the check. Nobody can change their own role through the API, neither on the workspace (`ERROR_CANNOT_CHANGE_YOUR_OWN_ROLE` on the workspace user endpoint and `ERROR_CANT_ASSIGN_ROLE_TO_SELF` on the role assignment endpoints) nor on an application, table or view. Another admin has to do it, and the last admin check still applies to them. The reasoning is recorded in [ADR 010](../decisions/010-admin-role-precedence.md), and [its role matrix](../decisions/010-role-matrix-after.md) lists how every combination of roles resolves before and after the rule. ## 📝 Conclusion If you want to create a new way to validate Permissions, you'll have to: - [ ] Create a backend permission manager - [ ] Implement its methods - [ ] Register the permission manager - [ ] Add missing operations if any - [ ] Create a frontend permission manager - [ ] Implement its methods - [ ] register the frontend permisison manager - [ ] Test everything ## 🤔 A few considerations The Permission system has been designed with these constraints in mind: - Must be extensible (to support RBAC from enterprise folder) - Must be as much compatible with the previous system (The `.has_user` method) as possible - Must play well with realtime - Must be able to work with object but also a collection of objects - Must be performant - Must avoid code duplication between backend and frontend That may explain some of the decisions that has been made. **More technically**: - The parent of a `Database` is not the workspace has we could imagine first but the "more generic" type which is the `Application`. It solves a lot of issues (but also creates some if we don't pay attention). - For a `User` Actor, the Basic permission manager has two "roles", `ADMIN` and `MEMBER` which is compatible with the previous permission system. The Role name is stored in the `WorkspaceUser.permissions` field. The idea is to make the other Role based system using this field to make them compatible and avoid duplication of data or synchronisation when switching from one system to another. For the `BasicPermissionManagerType`, The `ADMIN` value in this property means the user is `ADMIN`. For any other values the `User` is treated as a simple `MEMBER` of the Workspace. - The current Personnal API Token has been partially migrated to the current permission system. - The `AnonymousUser` is a `SubjectType` that can be handled by some permission manager. **TBD**: - Add a new Authentication Token to replace the old one that really use the permission system. - Renaming the `.permissions` field to something more understantable. - Handle Public views with a new Permission manager. - Create a few more Roles