Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Update developer docs about permissions

Klaus Zanders8cf461fff913
diff --git a/docs/development/concepts/permissions/README.md b/docs/development/concepts/permissions/README.md
index b6e36b7e3c1..418add5121a 100644
--- a/docs/development/concepts/permissions/README.md
+++ b/docs/development/concepts/permissions/README.md
@@ -23,14 +23,16 @@ With RBAC, the application defines a set of roles that users and groups can be i
Roles in OpenProject can be defined in the global administration. If you want to read about roles from a user experience, please see the [Roles & Permissions guide](../../../system-admin-guide/users-permissions/roles-permissions/#roles-and-permissions)
-In the backend, roles are a Rails model [`Role`](https://github.com/opf/openproject/tree/dev/app/models/role.rb) that holds a set of permissions associated in a `RolePermission` lookup table.
+In the backend, roles are a polymorphic Rails model [`Role`](https://github.com/opf/openproject/tree/dev/app/models/role.rb)
+that holds a set of permissions associated in a `RolePermission` lookup table.
There are multiple types of roles:
-- [Global roles](https://github.com/opf/openproject/tree/dev/modules/global_roles/app/models/global_role.rb) that are granted to user on a global level, i.e. they are not assigned per project. They can contain the permissions to e.g., *Create new projects*. *Please note, that the global_roles module is a relict of the past and is to be merged into the core*
-- *Non member* roles that is a special role that applies to any _authenticated_ user and all public projects that this user is not a member of. On the OpenProject community, it is configured to grant non-members of the public projects, i.e. all logged in users, the permissions *Add forum posts* and *Create new work package*.
-- *Anonymous* roles that is a special role, similar to the *non member* role but applying to non-authenticated users.
-- All other roles, which are saved in the database and contain a user-defined set of permissions that this role will grant.
+- [Global roles](https://github.com/opf/openproject/blob/dev/app/models/global_role.rb) that are granted to user on a global level, i.e. they are not assigned per project. They can contain the permissions to e.g., *Create new projects* or *Create* or *Manage users*
+- Entity specific roles like [WorkPackage Role](https://github.com/opf/openproject/blob/dev/app/models/work_package_role.rb) that allows a user to get certain permissions on a single entity. *Currently, those roles are only seeded and cannot be manually edited or created by administrators.*
+- All other roles (and the de facto default) are [Project roles](https://github.com/opf/openproject/blob/dev/app/models/project_role.rb). They grant users permission on an entire project and all resources belonging to that project. There are 2 special types of project roles:
+ - *Non member* roles that is a special role that applies to any _authenticated_ user and all public projects that this user is not a member of. On the OpenProject community, it is configured to grant non-members of the public projects, i.e. all logged in users, the permissions *Add forum posts* and *Create new work package*.
+ - *Anonymous* roles that is a special role, similar to the *non member* role but applying to non-authenticated users.
In the following screenshot, you can see the builtin, non-deletable roles *Non member* and *Anonymous*, as well as three additional, user-created roles.
@@ -47,7 +49,9 @@ The permissions are defined in two places:
1. The core [`config/initializers/permissions.rb`](https://github.com/opf/openproject/tree/dev/config/initializers/permissions.rb) initializer file. It defines the available project modules and its associated permissions
2. Module permissions defined in the `engine.rb` of modules under `module/` folder. For example, the definitions for budgets are defined in[`modules/budgets/lib/budgets/engine.rb`](https://github.com/opf/openproject/tree/dev/modules/budgets/lib/budgets/engine.rb).
-These definitions determine the name of the permission and the Rails controller actions that are this permission unlocks. In some cases, the permissions do not define a controller action and then is only used for authorization checks in contracts.
+These definitions determine the name of the permission and the Rails controller actions that this permission unlocks. In some cases, the permissions do not define a controller action and then is only used for authorization checks in contracts.
+
+The definition also specifies what context a permission can be given to. The `permissible_on:` specifies those allowed contexts. The permission system will than take care of hinting the developer when they are checking a permission in a wrong context (i.e. checking for `create_user` permission on a specific project, but it only being a global permission).
## Checking of permissions in Backend
@@ -92,13 +96,16 @@ When a set of records is to be returned, e.g. for an index action, it is best to
| Scenario | Scope | Example |
|--------- | ------| ------- |
-| All projects a user is allowed a permission in | `Authorization.projects(permission, user)` | `Authorization.projects(:view_work_packages, User.current)` |
+| All projects a user is allowed a permission in | `Project.allowed_to(user, permission)` | `Project.allowed_to(User.current, :view_work_packages)` |
+| All work packges a user is allowed a permission in | `WorkPackage.allowed_to(user, permission)` | `WorkPackage.allowed_to(User.current, :view_work_packages)` |
| All users granted a permission in a project | `Authorization.users(permission, project: project)` | `Authorization.users(:view_work_packages, project: project)` |
| All roles a user has in a project | `Authorization.roles(user, project)` | `Authorization.roles(User.current, project)` |
| All roles a user has for a specific resource | `Authorization.roles(user, entity)` | `Authorization.roles(User.current, work_package)` |
| All roles a user has globally | `Authorization.roles(user)` | `Authorization.roles(User.current)` |
-Most of the time, a developer will not witness those queries as they are the embedded deeply within the existing scopes. E.g. the `visible` scopes defined for most AR models, under the hood rely on `Authorization.projects(permission, user)` by checking that the `project_id` attribute of the record is within that set of projects.
+Most of the time, a developer will not witness those queries as they are the embedded deeply within the existing scopes. E.g. the `visible` scopes defined for most AR models.
+
+The developer should mostly rely on the `allowed_to` scopes defined on the models that permissions can be checked for.
### Explicitly testing for permissions
@@ -109,14 +116,41 @@ project = Project.find_by(name: 'My project')
work_package = project.work_packages.find_by(subject: 'My task')
user = User.find_by(login: 'foobar')
-user.allowed_to?(:view_members, project) # true or false
-# or
-user.allowed_to?(:view_work_package, work_package) # true or false
+# Checks if the user has the permission in the given project
+user.allowed_in_project?(:view_members, project)
+
+# Checks if the user has the permission in *any* project
+user.allowed_in_any_project?(:view_members)
+
+# Checks if the user has the permission in the given work package *or* the project it belongs to
+user.allowed_in_work_package?(:view_work_package, work_package)
+
+# Checks if the user has the permission in any work package *or* project
+user.allowed_in_any_work_package?(:view_work_package)
+
+# Checks if the user has the permission in the given project *or* any work package that belongs to the given project
+user.allowed_in_any_work_package?(:view_work_package, in_project: project)
+```
+
+The same is true for global permissions using `user.allowed_globally?(permission)`. This will either test a global permission such as `:add_project`.
+
+Those methods will also aid the developer, when they are trying to request a permission in the wrong context. For example, when you try to check a global permission on a project, it will raise an `IllegalPermissionContextError`
+
+```ruby
+user.allowed_in_project(:create_user, project)
+# => Authorization::IllegalPermissionContextError: Used permission "create_user" which maps to create_user in project context. Correct contexts for this permission are: global.
```
-The same is true for permissions outside a project using `user.allowed_to_globally?(permission)`. This will either test a global permission such as `:add_project` or return `true` whenever the user has such a permission in any project.
+It also checks, that you are checking for a permission that really exists. This prevents accidentally adding a typo to a permission name, that previously just silently returned `false`:
+
+```ruby
+user.allowed_globally?(:create_users)
+# => Authorization::UnknownPermissionError: Used permission "create_users" that is not defined. It will never return true.
+```
+### Deprecated methods
+Previously, we used to check permissions using the `allowed_to?`, `allowed_to_globally?` and `allowed_to_in_project?` methods to check permissions. Because of some ambiguity in them (i.e. you could check for project permissions globally, which then returned what is now `allowed_in_any_project?`) and the lack of the checks that are mentioned above, those methods have been deprecated and will be removed in a future version of OpenProject. If you are using those methods in a module, please migrate to the new methods.
## Checking of permissions in Frontend