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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Add basic introduction to permissions
Oliver Günther34ddc79fff9a
diff --git a/docs/development/concepts/README.md b/docs/development/concepts/README.mdindex 9067ed18709..dd272ba5dcb 100644--- a/docs/development/concepts/README.md+++ b/docs/development/concepts/README.md@@ -18,6 +18,7 @@ Please choose an area that you would like to read about:| [Application architecture](application-architecture) | An introduction of the application architecture used at OpenProject. || [State management](state-management) | How does the frontend hanble state and react to changes? || [HAL resources](hal-resources) | What are HAL resources and how are they used in the frontend? |+| [Permissions](permissions) | How are roles and permissions handled in OpenProject code ? || [Resource schemas](resource-schemas) | What is a schema and how is it tied to an editable resource? || [Resource changesets](resource-changesets) | How is change tracked to resources in the frontend and how to save the changes? || [Inline editing](inline-editing) | How does inline editing and the edit field functionality work in OpenProject? |diff --git a/docs/development/concepts/permissions/README.md b/docs/development/concepts/permissions/README.mdnew file mode 100644index 00000000000..06f73e5ec37--- /dev/null+++ b/docs/development/concepts/permissions/README.md@@ -0,0 +1,121 @@+---+sidebar_navigation:+ title: Permissiions+description: Get an overview of how roles and permissions are handled in the OpenProject application code+robots: index, follow+keywords: permissions, roles, RBAC+---++# Development Concept: Permissions++OpenProject is very flexible when it comes to authorization and granting permissions to users. The OpenProject application uses a Role-based access control (RBAC) approach to grant individual users permissions to projects.++With RBAC, the application defines a set of roles that users and groups can be individually assigned to. In OpenProject, the roles and permissions contained within are freely configurable. There can be an arbitrary number of roles defined globally and assigned roles to users on a project level.++++## Key takeaways++*Permissions in OpenProject...*++- use the Role-based access control (RBAC) approach to allow fine-grained access to authorized resources+- are assigned to users and groups through roles on a per-project or global level+- are often communicated to the frontend through the presence of action links in HAL resources++++## Definition of roles++Roles in OpenProject can be defined in the global administration. If you would read about roles from a user experience, please see the [Roles & Permissions guide](https://docs.openproject.org/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.++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*.+- *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 allow non-members of the public projects to allow grant 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.++++In the following screenshot, you can see the builtin, non-deletable roles *Non member* and *Anonymous*, as well as three additional, user-created roles.++++++Scrolling through the list of available permissions, you will begin to see the flexibility (and complexity) of the potential user permissions that are available:++++++## Definition of Permissions++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.++++## Checking of permissions in Backend++The way a developer can check for permissions obviously depends on whether the backend or frontend is doing the check. We will go through some of the possible ways to check for authrization.++++### Controller `before_action`++If the permission should be tested for a specific controller action, it will suffice to call the `before_action :authorize` to handle authorization.++As an example, the permissions `:manage_members`, `:view_members` is defined as follows:++```ruby+permission :manage_members, { members: %i[index new create update destroy autocomplete_for_member] }+permission :view_members, { members: [:index] }+```++This means if a user has only the `:view_members` permission, the `authorize` check in the [`MembersController`](https://github.com/opf/openproject/tree/dev/app/controllers/members_controller.rb) will allow the user to pass through for the `index` action (Overview of the members), but not the CRUD actions. These will only pass if the user has the `manage_members` (or both) permissions.++++### Explicitly testing for permissions++If you have a user and a project, you can explicitly ask for a permission like so:++```ruby+project = Project.find_by(name: 'My project')+user = User.find_by(login: 'foobar')++user.allowed_to?(:view_members, project) # true or false+```++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.++++## Checking of permissions in Frontend++In the frontend, we have to rely on the API to tell us what actions the user is allowed to do. With [`HAL+JSON resources`](../hal-resources), we can do that by checking for the presence or absence of an action link in responses.++For example, if the user has the permission to create work packages in the OpenProject project on the community, [the collection response of the work packages API](https://community.openproject.com/api/v3/projects/openproject/work_packages?pageSize=0) of it will contain a link `createWorkPackage` that contains the link to the create form API endpoint.++To check these links, one can use the [`ModelAuthService`](https://github.com/opf/openproject/tree/dev/frontend/src/app/modules/common/model-auth/model-auth.service.ts) that gets initialized with the resources being loaded:++```typescript+const modelAuth = injector.get(ModelAuthService);+modelAuth.initModelAuth('work_packages', { createWorkPackage: () => 'foo' });++modelAuth.can('work_packages', 'createWorkPackage'); // true+modelAuth.can('work_packages', 'someOtherAction'); // false+```++The service doesn't care for what's inside the links but just looks for their presence.++The fact that permissions are implicitly checked through links has the advantage that the frontend doesn't need to care about whether a functionality has been disabled in the project (i.e., the project module is not enabled there) or if the user has no permission to access it.++The downside to this approach is that it is impossible to check a multitiude of permissions upfront without loading the necessary requests. In some cases, we thus load requests just for the sake of permission testing, even though we do not depend on the API response itself.\ No newline at end of filediff --git a/docs/development/concepts/permissions/roles-administration.png b/docs/development/concepts/permissions/roles-administration.pngnew file mode 100644index 00000000000..c50684d2420Binary files /dev/null and b/docs/development/concepts/permissions/roles-administration.png differdiff --git a/docs/development/concepts/permissions/roles-permissions-overview.gif b/docs/development/concepts/permissions/roles-permissions-overview.gifnew file mode 100644index 00000000000..739385bc6deBinary files /dev/null and b/docs/development/concepts/permissions/roles-permissions-overview.gif differ