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
introduce permission governing whether work packages can be assigned
ulfertsa775e9e5d80e
diff --git a/app/models/actions/scopes/default.rb b/app/models/actions/scopes/default.rbindex 381ec6c2dbb..f4d868a1d0c 100644--- a/app/models/actions/scopes/default.rb+++ b/app/models/actions/scopes/default.rb@@ -32,12 +32,13 @@ module Actions::Scopesclass_methods dodef default- capabilities_sql = <<~SQL- (SELECT id, permission, global, module FROM (VALUES #{action_map}) AS t(id, permission, global, module)) actions+ actions_sql = <<~SQL.squish+ (SELECT id, permission, global, module, grant_to_admin+ FROM (VALUES #{action_map}) AS t(id, permission, global, module, grant_to_admin)) actionsSQLselect('actions.*')- .from(capabilities_sql)+ .from(actions_sql)endprivate@@ -45,18 +46,19 @@ module Actions::Scopesdef action_mapOpenProject::AccessControl.contract_actions_map- .map { |permission, v| map_actions(permission, v[:actions], v[:global], v[:module]) }+ .map { |permission, v| map_actions(permission, **v) }.flatten.join(', ')end- def map_actions(permission, actions, global, module_name)+ def map_actions(permission, actions:, global:, module_name:, grant_to_admin:)actions.map do |namespace, actions|actions.map do |action|values = [quote_string("#{action_v3_name(namespace)}/#{action}"),quote_string(permission),global,- module_name ? quote_string(module_name) : 'NULL'].join(', ')+ module_name ? quote_string(module_name) : 'NULL',+ grant_to_admin].join(', ')"(#{values})"enddiff --git a/app/models/capabilities/scopes/default.rb b/app/models/capabilities/scopes/default.rbindex 322b081644e..9c95e65d24d 100644--- a/app/models/capabilities/scopes/default.rb+++ b/app/models/capabilities/scopes/default.rb@@ -36,7 +36,7 @@ module Capabilities::Scopes# Currently, this does not reflect the behaviour present in the backend that every permission in at least one project# leads to having that permission in the global context as well. Hopefully, this is not necessary to be added.def default- capabilities_sql = <<~SQL+ capabilities_sql = <<~SQL.squish(#{default_sql_by_member}UNION@@ -53,7 +53,7 @@ module Capabilities::Scopesprivatedef default_sql_by_member- <<~SQL+ <<~SQL.squishSELECT DISTINCTactions.id "action",users.id principal_id,@@ -76,14 +76,14 @@ module Capabilities::Scopesenddef default_sql_by_admin- <<~SQL+ <<~SQL.squishSELECT DISTINCTactions.id "action",users.id principal_id,projects.id context_idFROM (#{Action.default.to_sql}) actionsJOIN (#{Principal.visible.not_builtin.not_locked.to_sql}) users- ON "users".admin = true+ ON "users".admin = true AND actions.grant_to_admin = trueLEFT OUTER JOIN "projects"ON "projects".active = trueAND NOT "actions".global@@ -95,7 +95,7 @@ module Capabilities::Scopesenddef default_sql_by_non_member- <<~SQL+ <<~SQL.squishSELECT DISTINCTactions.id "action",users.id principal_id,diff --git a/app/models/queries/capabilities/filters/action_filter.rb b/app/models/queries/capabilities/filters/action_filter.rbindex fb0e4187f6c..9df8ebd1e30 100644--- a/app/models/queries/capabilities/filters/action_filter.rb+++ b/app/models/queries/capabilities/filters/action_filter.rb@@ -33,7 +33,7 @@ class Queries::Capabilities::Filters::ActionFilter < Queries::Capabilities::Filtdef split_valuesvalues.map do |value|- if (matches = value.match(/\A([a-z]+\/[a-z]+)\z/))+ if (matches = value.match(/\A([a-z_]+\/[a-z_]+)\z/)){action: matches[1]}diff --git a/app/services/authorization/project_query.rb b/app/services/authorization/project_query.rbindex a4b10f5fdb5..3d9eee60619 100644--- a/app/services/authorization/project_query.rb+++ b/app/services/authorization/project_query.rb@@ -147,9 +147,13 @@ class Authorization::ProjectQuery < Authorization::AbstractQuerypermissions(action).all?(&:public?)end+ def self.granted_to_admin?(user, action)+ user.admin? && OpenProject::AccessControl.grant_to_admin?(action)+ end+transformations.register :all,- :members_join do |statement, user|- if user.admin?+ :members_join do |statement, user, action|+ if granted_to_admin?(user, action)statementelsestatement@@ -172,7 +176,7 @@ class Authorization::ProjectQuery < Authorization::AbstractQuerytransformations.register :all,:role_permissions_join,after: [:enabled_modules_join] do |statement, user, action|- if action_public?(action) || user.admin?+ if action_public?(action) || granted_to_admin?(user, action)statementelsestatement.join(role_permissions_table)@@ -182,8 +186,8 @@ class Authorization::ProjectQuery < Authorization::AbstractQuerytransformations.register :all,:members_member_roles_join,- after: [:members_join] do |statement, user|- if user.admin?+ after: [:members_join] do |statement, user, action|+ if granted_to_admin?(user, action)statementelsestatement.outer_join(member_roles_table)@@ -194,7 +198,7 @@ class Authorization::ProjectQuery < Authorization::AbstractQuerytransformations.register :all,:permission_roles_join,after: [:role_permissions_join] do |statement, user, action|- if action_public?(action) || user.admin?+ if action_public?(action) || granted_to_admin?(user, action)statementelsestatement.join(permission_roles_table)@@ -206,7 +210,7 @@ class Authorization::ProjectQuery < Authorization::AbstractQuery:assigned_roles_join,after: %i[permission_roles_joinmembers_member_roles_join] do |statement, user, action|- if user.admin?+ if granted_to_admin?(user, action)statementelsestatement.outer_join(assigned_roles_table)@@ -215,8 +219,8 @@ class Authorization::ProjectQuery < Authorization::AbstractQueryendtransformations.register :all,- :assigned_role_exists_condition do |statement, user|- if user.admin?+ :assigned_role_exists_condition do |statement, user, action|+ if granted_to_admin?(user, action)statement.where(project_active_condition)elsestatement.where(assigned_roles_table[:id].not_eq(nil))diff --git a/app/services/authorization/user_allowed_query.rb b/app/services/authorization/user_allowed_query.rbindex 81ea67abd56..c3f3844b3a6 100644--- a/app/services/authorization/user_allowed_query.rb+++ b/app/services/authorization/user_allowed_query.rb@@ -53,7 +53,11 @@ class Authorization::UserAllowedQuery < Authorization::AbstractUserQueryhas_role.and(has_permission)end- is_admin = users_table[:admin].eq(true)+ is_admin = if OpenProject::AccessControl.grant_to_admin?(action)+ users_table[:admin].eq(true)+ else+ Arel::Nodes::Equality.new(1, 0)+ endstatement.where(has_role_and_permission.or(is_admin))elsediff --git a/app/services/authorization/user_allowed_service.rb b/app/services/authorization/user_allowed_service.rbindex aad36b510a5..72c5544e2f3 100644--- a/app/services/authorization/user_allowed_service.rb+++ b/app/services/authorization/user_allowed_service.rb@@ -93,7 +93,8 @@ class Authorization::UserAllowedService# Inactive users are never authorizedreturn false unless authorizable_user?# Admin users are authorized for anything else- return true if user.admin?+ # unless the permission is explicitly flagged not to be granted to admins.+ return true if granted_to_admin?(action)has_authorized_role?(action, project)end@@ -104,7 +105,7 @@ class Authorization::UserAllowedService# Inactive users are never authorizedreturn false unless authorizable_user?# Admin users are always authorized- return true if user.admin?+ return true if granted_to_admin?(action)has_authorized_role?(action)end@@ -116,6 +117,12 @@ class Authorization::UserAllowedService!user.locked? || user.is_a?(SystemUser)end+ # Admin users are granted every permission unless the+ # permission explicitly disables it.+ def granted_to_admin?(action)+ user.admin? && OpenProject::AccessControl.grant_to_admin?(action)+ end+def has_authorized_role?(action, project = nil)project_role_cache.fetch(project)diff --git a/config/initializers/permissions.rb b/config/initializers/permissions.rbindex c28224fbbe3..5a3cf974774 100644--- a/config/initializers/permissions.rb+++ b/config/initializers/permissions.rb@@ -230,6 +230,15 @@ OpenProject::AccessControl.map do |map|wpt.permission :assign_versions,{},dependencies: :view_work_packages++ # A user having the following permission can become assignee and/or responsible of a work package.+ # This is a passive permission in the sense that a user having the permission isn't eligible to perform+ # actions but rather to have actions taken together with him/her.+ wpt.permission :work_package_assigned,+ {},+ require: :member,+ contract_actions: { work_packages: %i[assigned] },+ grant_to_admin: falseendmap.project_module :news do |news|@@ -245,12 +254,12 @@ OpenProject::AccessControl.map do |map|require: :membernews.permission :comment_news,- 'news/comments': :create+ { 'news/comments': :create }endmap.project_module :wiki do |wiki|wiki.permission :view_wiki_pages,- wiki: %i[index show special date_index]+ { wiki: %i[index show special date_index] }wiki.permission :list_attachments,{ wiki: :list_attachments },@@ -277,13 +286,13 @@ OpenProject::AccessControl.map do |map|require: :memberwiki.permission :export_wiki_pages,- wiki: [:export]+ { wiki: [:export] }wiki.permission :view_wiki_edits,- wiki: %i[history diff annotate]+ { wiki: %i[history diff annotate] }wiki.permission :edit_wiki_pages,- wiki: %i[edit update preview add_attachment new new_child create]+ { wiki: %i[edit update preview add_attachment new new_child create] }wiki.permission :delete_wiki_pages_attachments,{}@@ -295,7 +304,7 @@ OpenProject::AccessControl.map do |map|map.project_module :repository do |repo|repo.permission :browse_repository,- repositories: %i[show browse entry annotate changes diff stats graph]+ { repositories: %i[show browse entry annotate changes diff stats graph] }repo.permission :commit_access,{}@@ -308,7 +317,7 @@ OpenProject::AccessControl.map do |map|require: :memberrepo.permission :view_changesets,- repositories: %i[show revisions revision]+ { repositories: %i[show revisions revision] }repo.permission :view_commit_author_statistics,{}@@ -325,7 +334,7 @@ OpenProject::AccessControl.map do |map|public: trueforum.permission :add_messages,- messages: %i[new create reply quote preview]+ { messages: %i[new create reply quote preview] }forum.permission :edit_messages,{ messages: %i[edit update preview] },diff --git a/lib/open_project/access_control.rb b/lib/open_project/access_control.rbindex 31a30f92484..21bacedab17 100644--- a/lib/open_project/access_control.rb+++ b/lib/open_project/access_control.rb@@ -64,8 +64,12 @@ module OpenProject# Returns the permission of given name or nil if it wasn't found# Argument should be a symbol- def permission(name)- permissions.detect { |p| p.name == name }+ def permission(action)+ if action.is_a?(Hash)+ permissions.detect { |p| p.controller_actions.include?("#{action[:controller]}/#{action[:action]}") }+ else+ permissions.detect { |p| p.name == action }+ endend# Returns the actions that are allowed by the permission of given name@@ -119,10 +123,19 @@ module OpenProject@contract_actions_map ||= permissions.each_with_object({}) do |p, hash|next unless p.contract_actions.any?- hash[p.name] = { actions: p.contract_actions, global: p.global?, module: p.project_module }+ hash[p.name] = { actions: p.contract_actions,+ global: p.global?,+ module_name: p.project_module,+ grant_to_admin: p.grant_to_admin? }endend+ def grant_to_admin?(permission_name)+ # Parts of the application currently rely on granting not defined permissions,+ # e.g. :edit_attribute_help_texts to administrators.+ permission(permission_name).nil? || permission(permission_name).grant_to_admin?+ end+def remove_modules_permissions(module_name)permissions = @permissionsdiff --git a/lib/open_project/access_control/mapper.rb b/lib/open_project/access_control/mapper.rbindex 283fcde76d3..3afdf59aa08 100644--- a/lib/open_project/access_control/mapper.rb+++ b/lib/open_project/access_control/mapper.rb@@ -31,9 +31,8 @@module OpenProjectmodule AccessControlclass Mapper- def permission(name, hash, options = {})- options[:project_module] = @project_module- mapped_permissions << Permission.new(name, hash, options)+ def permission(name, hash, **options)+ mapped_permissions << Permission.new(name, hash, project_module: @project_module, **options)enddef project_module(name, options = {})diff --git a/lib/open_project/access_control/permission.rb b/lib/open_project/access_control/permission.rbindex 4cae12ccaa2..b2e13dec4e3 100644--- a/lib/open_project/access_control/permission.rb+++ b/lib/open_project/access_control/permission.rb@@ -37,24 +37,33 @@ module OpenProject:project_module,:dependencies- def initialize(name, hash, options)+ def initialize(name,+ hash,+ public: false,+ require: nil,+ global: false,+ enabled: true,+ project_module: nil,+ contract_actions: [],+ grant_to_admin: true,+ dependencies: nil)@name = name- @controller_actions = []- @public = options[:public] || false- @require = options[:require]- @global = options[:global] || false- @enabled = options.include?(:enabled) ? options[:enabled] : true- @dependencies = Array(options[:dependencies]) || []- @project_module = options[:project_module]- @contract_actions = options[:contract_actions] || []- hash.each do |controller, actions|- @controller_actions << if actions.is_a? Array- actions.map { |action| "#{controller}/#{action}" }- else- "#{controller}/#{actions}"- end- end- @controller_actions.flatten!+ @public = public+ @require = require+ @global = global+ @enabled = enabled+ @project_module = project_module+ @contract_actions = contract_actions+ @grant_to_admin = grant_to_admin+ @dependencies = Array(dependencies)++ @controller_actions = hash.map do |controller, actions|+ if actions.is_a? Array+ actions.map { |action| "#{controller}/#{action}" }+ else+ "#{controller}/#{actions}"+ end+ end.flattenenddef public?@@ -65,6 +74,10 @@ module OpenProject@globalend+ def grant_to_admin?+ @grant_to_admin+ end+def require_member?@require && @require == :memberenddiff --git a/lib/redmine/plugin.rb b/lib/redmine/plugin.rbindex 7aaf512ef22..552eca66b94 100644--- a/lib/redmine/plugin.rb+++ b/lib/redmine/plugin.rb@@ -315,11 +315,11 @@ module Redmine #:nodoc:mod, mod_options = @project_scopeOpenProject::AccessControl.map do |map|map.project_module(mod, mod_options) do |map|- map.permission(name, actions, options)+ map.permission(name, actions, **options)endendelse- OpenProject::AccessControl.map { |map| map.permission(name, actions, options) }+ OpenProject::AccessControl.map { |map| map.permission(name, actions, **options) }endenddiff --git a/modules/dashboards/lib/dashboards/engine.rb b/modules/dashboards/lib/dashboards/engine.rbindex a945a2f09f1..cebe8b212fb 100644--- a/modules/dashboards/lib/dashboards/engine.rb+++ b/modules/dashboards/lib/dashboards/engine.rb@@ -25,8 +25,8 @@ module DashboardsOpenProject::AccessControl.map do |ac_map|ac_map.project_module(:dashboards) do |pm_map|- pm_map.permission(:view_dashboards, 'dashboards/dashboards': ['show'])- pm_map.permission(:manage_dashboards, 'dashboards/dashboards': ['show'])+ pm_map.permission(:view_dashboards, { 'dashboards/dashboards': ['show'] })+ pm_map.permission(:manage_dashboards, { 'dashboards/dashboards': ['show'] })endendenddiff --git a/modules/github_integration/lib/open_project/github_integration/engine.rb b/modules/github_integration/lib/open_project/github_integration/engine.rbindex 22478a19f5d..66919d73873 100644--- a/modules/github_integration/lib/open_project/github_integration/engine.rb+++ b/modules/github_integration/lib/open_project/github_integration/engine.rb@@ -61,7 +61,7 @@ module OpenProject::GithubIntegrationinitializer 'github.permissions' doOpenProject::AccessControl.map do |ac_map|ac_map.project_module(:github, dependencies: :work_package_tracking) do |pm_map|- pm_map.permission(:show_github_content, {}, {})+ pm_map.permission(:show_github_content, {})endendenddiff --git a/modules/overviews/lib/overviews/engine.rb b/modules/overviews/lib/overviews/engine.rbindex ffef67dd2fa..bfc4c6a7ec4 100644--- a/modules/overviews/lib/overviews/engine.rb+++ b/modules/overviews/lib/overviews/engine.rb@@ -22,7 +22,7 @@ module OverviewsOpenProject::AccessControl.map do |ac_map|ac_map.project_module nil do |map|map.permission :manage_overview,- 'overviews/overviews': ['show'],+ { 'overviews/overviews': ['show'] },public: trueendenddiff --git a/modules/overviews/spec/requests/api/v3/grids/grids_resource_spec.rb b/modules/overviews/spec/requests/api/v3/grids/grids_resource_spec.rbindex 0a84770a610..6e247f5b25c 100644--- a/modules/overviews/spec/requests/api/v3/grids/grids_resource_spec.rb+++ b/modules/overviews/spec/requests/api/v3/grids/grids_resource_spec.rb@@ -33,7 +33,7 @@ describe 'API v3 Grids resource', type: :request, content_type: :json doinclude Rack::Test::Methodsinclude API::V3::Utilities::PathHelper- let(:current_user) do+ current_user docreate(:user,member_in_project: project,member_with_permissions: permissions)@@ -58,21 +58,13 @@ describe 'API v3 Grids resource', type: :request, content_type: :json doendlet(:custom_text) { "Some text a user wrote" }- before do- login_as(current_user)- end-subject(:response) { last_response }describe '#get api/v3/grids/:id' dolet(:path) { api_v3_paths.grid(grid.id) }- let(:stored_grids) do- grid- end-before do- stored_grids+ gridget pathend@@ -107,19 +99,36 @@ describe 'API v3 Grids resource', type: :request, content_type: :json dolet(:path) { api_v3_paths.grid(grid.id + 1) }it 'responds with 404 NOT FOUND' do- expect(subject.status).to eql 404+ expect(subject.status).to be 404endendend+ shared_examples_for 'creates a grid resource' do+ it 'responds with 201 CREATED' do+ expect(subject.status).to eq(201)+ end++ it 'returns the created grid block' do+ expect(subject.body)+ .to be_json_eql('Grid'.to_json)+ .at_path('_type')+ expect(subject.body)+ .to be_json_eql(params['rowCount'].to_json)+ .at_path('rowCount')+ end++ it 'persists the grid' do+ expect(Grids::Grid.count)+ .to be(1)+ end+ end+describe '#post api/v3/grids' dolet(:path) { api_v3_paths.grids }let(:permissions) { %i[manage_overview] }- let(:stored_grids) do- end-let(:params) do{"rowCount": 10,@@ -133,115 +142,32 @@ describe 'API v3 Grids resource', type: :request, content_type: :json doendbefore do- stored_grids-post path, params.to_jsonend- it 'responds with 201 CREATED' do- expect(subject.status).to eq(201)- end-- it 'returns the created grid block' do- expect(subject.body)- .to be_json_eql('Grid'.to_json)- .at_path('_type')- expect(subject.body)- .to be_json_eql(params['rowCount'].to_json)- .at_path('rowCount')- end-- it 'persists the grid' do- expect(Grids::Grid.count)- .to eql(1)- end+ it_behaves_like 'creates a grid resource'context 'if lacking the manage_overview permission' do+ # Since manage_overview is a public permission, being a member in the project will grant the permission+ # which is why this test case is the same as the one abovelet(:permissions) { %i[] }- let(:params) do- {- "_links": {- "scope": {- "href": project_overview_path(project)- }- }- }.with_indifferent_access- end-- it 'responds with 201 CREATED' do- expect(subject.status).to eq(201)- end-- it 'returns the created grid block' do- expect(subject.body)- .to be_json_eql('Grid'.to_json)- .at_path('_type')- expect(subject.body)- .to be_json_eql(Overviews::GridRegistration.defaults[:row_count].to_json)- .at_path('rowCount')- end-- it 'persists the grid' do- expect(Grids::Grid.count)- .to eql(1)- end-- context 'if deviating from the default page parameters' do- let(:params) do- {- "_links": {- "scope": {- "href": project_overview_path(project)- }- }- }.with_indifferent_access- end-- it 'responds with 201 CREATED' do- expect(subject.status).to eq(201)- end-- it 'returns the created grid block' do- expect(subject.body)- .to be_json_eql('Grid'.to_json)- .at_path('_type')- expect(subject.body)- .to be_json_eql(Overviews::GridRegistration.defaults[:row_count].to_json)- .at_path('rowCount')- end-- it 'persists the grid' do- expect(Grids::Grid.count)- .to eql(1)- end- end+ it_behaves_like 'creates a grid resource'end- context 'if lacking the manage_overview permission and deviating from the default page' do- let(:permissions) { %i[] }-- let(:stored_grids) do- end- let(:params) do- {- "rowCount": Overviews::GridRegistration.defaults[:row_count] + 1,- "_links": {- "scope": {- "href": project_overview_path(project)- }- }- }.with_indifferent_access+ context 'if not being a member in the project' do+ current_user do+ create(:user)endit 'responds with 422' doexpect(subject.status).to eq(422)end- it 'does not persists the grid' do+ it 'persists no grid' doexpect(Grids::Grid.count)- .to eql(0)+ .to be(0)endendenddiff --git a/modules/reporting/spec/features/me_value_spec.rb b/modules/reporting/spec/features/me_value_spec.rbindex a045550976a..753ac2b529a 100644--- a/modules/reporting/spec/features/me_value_spec.rb+++ b/modules/reporting/spec/features/me_value_spec.rb@@ -58,7 +58,7 @@ describe 'Cost report showing my own times', type: :feature, js: true doexpect(page).to have_no_selector('.report', text: '10.00')expect(page).to have_selector('.report', text: '15.00')- expect(find("##{filter_selector}").value).to eq 'me'+ expect(page).to have_field(filter_selector, text: 'me')endenddiff --git a/spec/features/global_roles/mock_global_permissions.rb b/spec/features/global_roles/mock_global_permissions.rbindex 04b3c4aa9b9..fe77a804563 100644--- a/spec/features/global_roles/mock_global_permissions.rb+++ b/spec/features/global_roles/mock_global_permissions.rb@@ -22,6 +22,9 @@ def mock_permissions(name, options = {})::OpenProject::AccessControl::Permission.new(name,{ does_not: :matter },- { project_module: 'Foo', public: false, global: false }.merge(options)+ project_module: 'Foo',+ public: false,+ global: false,+ **options)enddiff --git a/spec/lib/open_project/access_control/permission_spec.rb b/spec/lib/open_project/access_control/permission_spec.rbindex b73b9f3d633..576b98f7954 100644--- a/spec/lib/open_project/access_control/permission_spec.rb+++ b/spec/lib/open_project/access_control/permission_spec.rb@@ -51,25 +51,45 @@ describe OpenProject::AccessControl::Permission dodescribe '#global?' dodescribe 'setting global permission' do- before { @permission = OpenProject::AccessControl::Permission.new(:perm, { cont: [:action] }, { global: true }) }+ let(:permission) { described_class.new(:perm, { cont: [:action] }, global: true) }- it { expect(@permission.global?).to be_truthy }+ it { expect(permission).to be_global }enddescribe 'setting non global permission' do- before { @permission = OpenProject::AccessControl::Permission.new :perm, { cont: [:action] }, { global: false } }+ let(:permission) { described_class.new :perm, { cont: [:action] }, global: false }it 'is false' do- expect(@permission.global?).to be_falsey+ expect(permission).not_to be_globalendenddescribe 'not specifying -> default' do- before { @permission = OpenProject::AccessControl::Permission.new :perm, { cont: [:action] }, {} }+ let(:permission) { described_class.new :perm, { cont: [:action] } }it 'is false' do- expect(@permission.global?).to be_falsey+ expect(permission).not_to be_globalendendend++ describe '#grant_to_admin?' do+ context 'if explicitly specified' do+ let(:permission) { described_class.new(:perm, {}, grant_to_admin: true) }++ it { expect(permission).to be_grant_to_admin }+ end++ context 'as a default' do+ let(:permission) { described_class.new(:perm, {}) }++ it { expect(permission).to be_grant_to_admin }+ end++ context 'if explicitly specified not to' do+ let(:permission) { described_class.new(:perm, {}, grant_to_admin: false) }++ it { expect(permission).not_to be_grant_to_admin }+ end+ endenddiff --git a/spec/lib/open_project/access_control_spec.rb b/spec/lib/open_project/access_control_spec.rbindex 4c52c988e95..db84c62b6bc 100644--- a/spec/lib/open_project/access_control_spec.rb+++ b/spec/lib/open_project/access_control_spec.rb@@ -55,12 +55,12 @@ describe OpenProject::AccessControl doendmap.project_module :mixed_module do |mod|- mod.permission :proj3, { dont: :care }+ mod.permission :proj3, { dont: :care }, grant_to_admin: truemod.permission :global2, { dont: :care }, global: true, contract_actions: { baz: %i[destroy] }endmap.project_module :dependent_module, dependencies: :project_module do |mod|- mod.permission :proj4, { dont: :care }+ mod.permission :proj4, { dont: :care }, grant_to_admin: falseendendend@@ -243,9 +243,49 @@ describe OpenProject::AccessControl doit 'contains all contract actions grouped by the permission' doexpect(subject.contract_actions_map)- .to eql(global2: { actions: { baz: [:destroy] }, global: true, module: :mixed_module },- proj0: { actions: { foo: :create }, global: false, module: nil },- proj2: { actions: { bar: %i[create read] }, global: false, module: :project_module })+ .to eql(global2: { actions: { baz: [:destroy] }, global: true, module_name: :mixed_module, grant_to_admin: true },+ proj0: { actions: { foo: :create }, global: false, module_name: nil, grant_to_admin: true },+ proj2: { actions: { bar: %i[create read] }, global: false, module_name: :project_module, grant_to_admin: true })+ end+ end++ describe '.grant_to_admin?' do+ before do+ stash_access_control_permissions++ setup_global_permissions+ end++ after do+ restore_access_control_permissions+ end++ context 'for a granted permission (default)' do+ it 'is granted' do+ expect(described_class)+ .to be_grant_to_admin(:proj0)+ end+ end++ context 'for a non granted permission' do+ it 'is granted' do+ expect(described_class)+ .not_to be_grant_to_admin(:proj4)+ end+ end++ context 'for an explicitly granted permission' do+ it 'is granted' do+ expect(described_class)+ .to be_grant_to_admin(:proj3)+ end+ end++ context 'for a non existing permission' do+ it 'is granted' do+ expect(described_class)+ .to be_grant_to_admin(:not_existing)+ endendendenddiff --git a/spec/models/actions/scopes/default_spec.rb b/spec/models/actions/scopes/default_spec.rbindex 1025d81e14c..478b53e50f9 100644--- a/spec/models/actions/scopes/default_spec.rb+++ b/spec/models/actions/scopes/default_spec.rb@@ -44,7 +44,7 @@ describe Actions::Scopes::Default, type: :model doOpenProject::AccessControl.contract_actions_map.map do |permission, v|- v[:actions].map { |vk, vv| vv.map { |vvv| item.call(permission, vk, vvv, v[:global], v[:module]) } }+ v[:actions].map { |vk, vv| vv.map { |vvv| item.call(permission, vk, vvv, v[:global], v[:module_name]) } }end.flatten(2)enddiff --git a/spec/models/capabilities/scopes/default_spec.rb b/spec/models/capabilities/scopes/default_spec.rbindex 9710bc9f11f..a178ba5f94b 100644--- a/spec/models/capabilities/scopes/default_spec.rb+++ b/spec/models/capabilities/scopes/default_spec.rb@@ -236,7 +236,8 @@ describe Capabilities::Scopes::Default, type: :model doOpenProject::AccessControl.contract_actions_map- .map { |_, v| v[:actions].map { |vk, vv| vv.map { |vvv| item.call(vk, vvv, v[:global], v[:module]) } } }+ .select { |_, v| v[:grant_to_admin] }+ .map { |_, v| v[:actions].map { |vk, vv| vv.map { |vvv| item.call(vk, vvv, v[:global], v[:module_name]) } } }.flatten(2).compactend@@ -263,7 +264,8 @@ describe Capabilities::Scopes::Default, type: :model doOpenProject::AccessControl.contract_actions_map- .map { |_, v| v[:actions].map { |vk, vv| vv.map { |vvv| item.call(vk, vvv, v[:global], v[:module]) } } }+ .select { |_, v| v[:grant_to_admin] }+ .map { |_, v| v[:actions].map { |vk, vv| vv.map { |vvv| item.call(vk, vvv, v[:global], v[:module_name]) } } }.flatten(2).compactend@@ -306,6 +308,19 @@ describe Capabilities::Scopes::Default, type: :model doendend+ context 'with a member with an action permission that is not granted to admin' do+ let(:permissions) { %i[work_package_assigned] }+ let(:members) { [member] }++ it_behaves_like 'consists of contract actions' do+ let(:expected) do+ [+ ['work_packages/assigned', user.id, project.id]+ ]+ end+ end+ end+context 'with a member with an action permission and the project being archived' dolet(:permissions) { %i[manage_members] }let(:members) { [member] }diff --git a/spec/models/users/allowed_scope_spec.rb b/spec/models/users/allowed_scope_spec.rbindex 24a9ce072f0..f72f077828d 100644--- a/spec/models/users/allowed_scope_spec.rb+++ b/spec/models/users/allowed_scope_spec.rb@@ -62,8 +62,8 @@ describe User, 'allowed scope' domember.save!end- it 'should return the user' do- expect(User.allowed(action, project).where(id: user.id)).to match_array [user]+ it 'returns the user' do+ expect(described_class.allowed(action, project).where(id: user.id)).to match_array [user]endend@@ -75,8 +75,24 @@ describe User, 'allowed scope' douser.update_attribute(:admin, true)end- it 'should return the user' do- expect(User.allowed(action, project).where(id: user.id)).to match_array [user]+ it 'returns the user' do+ expect(described_class.allowed(action, project).where(id: user.id)).to match_array [user]+ end+ end++ context 'w/ the context being a project+ w/o the project being public+ w/o the user being member in the project+ w/ the user being admin+ w/ the action not granted to admins' do+ let(:action) { :work_package_assigned }++ before do+ user.update_attribute(:admin, true)+ end++ it 'is empty' do+ expect(described_class.allowed(action, project).where(id: user.id)).to be_emptyendend@@ -89,8 +105,8 @@ describe User, 'allowed scope' domember.save!end- it 'should be empty' do- expect(User.allowed(action, project).where(id: user.id)).to be_empty+ it 'is empty' do+ expect(described_class.allowed(action, project).where(id: user.id)).to be_emptyendend@@ -102,8 +118,8 @@ describe User, 'allowed scope' dorole.add_permission! actionend- it 'should return the user' do- expect(User.allowed(action, project).where(id: user.id)).to be_empty+ it 'returns the user' do+ expect(described_class.allowed(action, project).where(id: user.id)).to be_emptyendend@@ -119,8 +135,8 @@ describe User, 'allowed scope' domember.save!end- it 'should be empty' do- expect(User.allowed(action, project).where(id: user.id)).to be_empty+ it 'is empty' do+ expect(described_class.allowed(action, project).where(id: user.id)).to be_emptyendend@@ -138,8 +154,8 @@ describe User, 'allowed scope' domember.save!end- it 'should be empty' do- expect(User.allowed(action, project).where(id: user.id)).to be_empty+ it 'is empty' do+ expect(described_class.allowed(action, project).where(id: user.id)).to be_emptyendend@@ -156,8 +172,8 @@ describe User, 'allowed scope' doproject.save!end- it 'should return the user' do- expect(User.allowed(action, project).where(id: user.id)).to match_array [user]+ it 'returns the user' do+ expect(described_class.allowed(action, project).where(id: user.id)).to match_array [user]endend@@ -174,8 +190,8 @@ describe User, 'allowed scope' doproject.save!end- it 'should return the anonymous user' do- expect(User.allowed(action, project).where(id: [user.id, anonymous.id])).to match_array([anonymous])+ it 'returns the anonymous user' do+ expect(described_class.allowed(action, project).where(id: [user.id, anonymous.id])).to match_array([anonymous])endend@@ -192,8 +208,8 @@ describe User, 'allowed scope' doproject.save!end- it 'should be empty' do- expect(User.allowed(action, project).where(id: user.id)).to be_empty+ it 'is empty' do+ expect(described_class.allowed(action, project).where(id: user.id)).to be_emptyendend@@ -208,8 +224,8 @@ describe User, 'allowed scope' doproject.save!end- it 'should be empty' do- expect(User.allowed(action, project).where(id: user.id)).to be_empty+ it 'is empty' do+ expect(described_class.allowed(action, project).where(id: user.id)).to be_emptyendend@@ -229,8 +245,8 @@ describe User, 'allowed scope' donon_member.add_permission! actionend- it 'should be empty' do- expect(User.allowed(action, project).where(id: user.id)).to be_empty+ it 'is empty' do+ expect(described_class.allowed(action, project).where(id: user.id)).to be_emptyendend@@ -243,8 +259,8 @@ describe User, 'allowed scope' domember.save!end- it 'should return the user' do- expect(User.allowed(public_action, project).where(id: user.id)).to match_array [user]+ it 'returns the user' do+ expect(described_class.allowed(public_action, project).where(id: user.id)).to match_array [user]endend@@ -258,8 +274,8 @@ describe User, 'allowed scope' doproject.saveend- it 'should return the user and anonymous' do- expect(User.allowed(public_action, project).where(id: [user.id, anonymous.id])).to match_array [user, anonymous]+ it 'returns the user and anonymous' do+ expect(described_class.allowed(public_action, project).where(id: [user.id, anonymous.id])).to match_array [user, anonymous]endend@@ -279,8 +295,8 @@ describe User, 'allowed scope' domember.save!end- it 'should be empty' do- expect(User.allowed(permission.name, project).where(id: user.id)).to eq []+ it 'is empty' do+ expect(described_class.allowed(permission.name, project).where(id: user.id)).to eq []endend@@ -300,8 +316,8 @@ describe User, 'allowed scope' domember.save!end- it 'should return the user' do- expect(User.allowed(permission.name, project).where(id: user.id)).to eq [user]+ it 'returns the user' do+ expect(described_class.allowed(permission.name, project).where(id: user.id)).to eq [user]endend@@ -317,21 +333,8 @@ describe User, 'allowed scope' doproject.update(active: false)end- it 'should be empty' do- expect(User.allowed(action, project)).to eq []- end- end-- context 'w/ only asking for members- w/o the project being public- w/o the user being member in the project- w/ the user being admin' do- before do- user.update_attribute(:admin, true)- end-- it 'should return the user' do- expect(User.allowed_members(action, project).where(id: user.id)).to be_empty+ it 'is empty' do+ expect(described_class.allowed(action, project)).to eq []endend@@ -345,8 +348,8 @@ describe User, 'allowed scope' domember.save!end- it 'should return the user' do- expect(User.allowed_members(action, project).where(id: user.id)).to match_array [user]+ it 'returns the user' do+ expect(described_class.allowed_members(action, project).where(id: user.id)).to match_array [user]endend@@ -358,8 +361,8 @@ describe User, 'allowed scope' douser.update_attribute(:admin, true)end- it 'should return the user' do- expect(User.allowed_members(action, project).where(id: user.id)).to be_empty+ it 'returns the user' do+ expect(described_class.allowed_members(action, project).where(id: user.id)).to be_emptyendend@@ -375,8 +378,8 @@ describe User, 'allowed scope' domember.save!end- it 'should return the user' do- expect(User.allowed_members(action, project).where(id: user.id)).to match_array [user]+ it 'returns the user' do+ expect(described_class.allowed_members(action, project).where(id: user.id)).to match_array [user]endend@@ -390,8 +393,8 @@ describe User, 'allowed scope' dorole.add_permission! actionend- it 'should return the user' do- expect(User.allowed_members(action, project).where(id: user.id)).to be_empty+ it 'returns the user' do+ expect(described_class.allowed_members(action, project).where(id: user.id)).to be_emptyendendenddiff --git a/spec/models/users/allowed_to_spec.rb b/spec/models/users/allowed_to_spec.rbindex f61f7b48063..04a0e0390a7 100644--- a/spec/models/users/allowed_to_spec.rb+++ b/spec/models/users/allowed_to_spec.rb@@ -73,8 +73,8 @@ describe User, 'allowed_to?' dofinal_setup_stepend- it 'should be true' do- expect(user.allowed_to?(permission, project)).to be_truthy+ it 'is true' do+ expect(user).to be_allowed_to(permission, project)endend@@ -87,8 +87,8 @@ describe User, 'allowed_to?' dofinal_setup_stepend- it 'should be false' do- expect(user.allowed_to?(permission, project)).to be_falsey+ it 'is false' do+ expect(user).not_to be_allowed_to(permission, project)endend@@ -101,8 +101,25 @@ describe User, 'allowed_to?' dofinal_setup_stepend- it 'should be false' do- expect(user.allowed_to?(permission, project)).to be_falsey+ it 'is false' do+ expect(user).not_to be_allowed_to(permission, project)+ end+ end++ context 'w/ the user being admin+ w/ the permission not automatically granted to admins' do+ let(:permission) { :work_package_assigned }++ before do+ user.update(admin: true)++ project.save++ final_setup_step+ end++ it 'is false' do+ expect(user).not_to be_allowed_to(permission, project)endend@@ -114,8 +131,8 @@ describe User, 'allowed_to?' dofinal_setup_stepend- it 'should be false' do- expect(user.allowed_to?(permission, project)).to be_falsey+ it 'is false' do+ expect(user).not_to be_allowed_to(permission, project)endend@@ -129,8 +146,8 @@ describe User, 'allowed_to?' dofinal_setup_stepend- it 'should be true' do- expect(user.allowed_to?(permission, project)).to be_truthy+ it 'is true' do+ expect(user).to be_allowed_to(permission, project)endend@@ -148,8 +165,8 @@ describe User, 'allowed_to?' dofinal_setup_stepend- it 'should be false' do- expect(user.allowed_to?(permission, project)).to be_falsey+ it 'is false' do+ expect(user).not_to be_allowed_to(permission, project)endend@@ -168,8 +185,8 @@ describe User, 'allowed_to?' dofinal_setup_stepend- it 'should be false' do- expect(user.allowed_to?(permission, project)).to be_falsey+ it 'is false' do+ expect(user).not_to be_allowed_to(permission, project)endend@@ -187,8 +204,8 @@ describe User, 'allowed_to?' dofinal_setup_stepend- it 'should be false' do- expect(user.allowed_to?(permission, project)).to be_falsey+ it 'is false' do+ expect(user).not_to be_allowed_to(permission, project)endend@@ -205,8 +222,8 @@ describe User, 'allowed_to?' dofinal_setup_stepend- it 'should be true' do- expect(user.allowed_to?(permission, project)).to be_truthy+ it 'is true' do+ expect(user).to be_allowed_to(permission, project)endend@@ -224,8 +241,8 @@ describe User, 'allowed_to?' dofinal_setup_stepend- it 'should be false' do- expect(user.allowed_to?(permission, project)).to be_falsey+ it 'is false' do+ expect(user).not_to be_allowed_to(permission, project)endend@@ -243,8 +260,8 @@ describe User, 'allowed_to?' dofinal_setup_stepend- it 'should be true' do- expect(user.allowed_to?(permission, project)).to be_truthy+ it 'is true' do+ expect(user).to be_allowed_to(permission, project)endend@@ -264,8 +281,8 @@ describe User, 'allowed_to?' dofinal_setup_stepend- it 'should be false' do- expect(user.allowed_to?(permission, project)).to be_falsey+ it 'is false' do+ expect(user).not_to be_allowed_to(permission, project)endend@@ -281,8 +298,8 @@ describe User, 'allowed_to?' dofinal_setup_stepend- it 'should be true' do- expect(anonymous.allowed_to?(permission, project)).to be_truthy+ it 'is true' do+ expect(anonymous).to be_allowed_to(permission, project)endend@@ -300,8 +317,8 @@ describe User, 'allowed_to?' dofinal_setup_stepend- it 'should be true' do- expect(anonymous.allowed_to?(permission, project)).to be_truthy+ it 'is true' do+ expect(anonymous).to be_allowed_to(permission, project)endend@@ -320,9 +337,9 @@ describe User, 'allowed_to?' dofinal_setup_stepend- it 'should be true' do- expect(anonymous.allowed_to?(permission, project))- .to be_truthy+ it 'is true' do+ expect(anonymous)+ .to be_allowed_to(permission, project)endend@@ -336,8 +353,8 @@ describe User, 'allowed_to?' dofinal_setup_stepend- it 'should be false' do- expect(anonymous.allowed_to?(permission, project)).to be_falsey+ it 'is false' do+ expect(anonymous).not_to be_allowed_to(permission, project)endend@@ -353,8 +370,8 @@ describe User, 'allowed_to?' dofinal_setup_stepend- it 'should be true' do- expect(user.allowed_to?(permission, [project, project2])).to be_truthy+ it 'is true' do+ expect(user).to be_allowed_to(permission, [project, project2])endend@@ -369,8 +386,8 @@ describe User, 'allowed_to?' dofinal_setup_stepend- it 'should be false' do- expect(user.allowed_to?(permission, [project, project2])).to be_falsey+ it 'is false' do+ expect(user).not_to be_allowed_to(permission, [project, project2])endend@@ -387,8 +404,8 @@ describe User, 'allowed_to?' dofinal_setup_stepend- it 'should be true' do- expect(user.allowed_to?(permission, [project, project2])).to be_truthy+ it 'is true' do+ expect(user).to be_allowed_to(permission, [project, project2])endend@@ -405,8 +422,8 @@ describe User, 'allowed_to?' dofinal_setup_stepend- it 'should be false' do- expect(user.allowed_to?(permission, [project, project2])).to be_falsey+ it 'is false' do+ expect(user).not_to be_allowed_to(permission, [project, project2])endend@@ -422,7 +439,7 @@ describe User, 'allowed_to?' doendit 'is false' do- expect(user.allowed_to?(global_permission.name, project)).to be_falsey+ expect(user).not_to be_allowed_to(global_permission.name, project)endend@@ -438,9 +455,9 @@ describe User, 'allowed_to?' dofinal_setup_stepend- it 'should be true' do- expect(user.allowed_to?({ controller: 'wiki', action: 'show' }, project))- .to be_truthy+ it 'is true' do+ expect(user)+ .to be_allowed_to({ controller: 'wiki', action: 'show' }, project)endend@@ -456,9 +473,9 @@ describe User, 'allowed_to?' dofinal_setup_stepend- it 'should be true' do- expect(user.allowed_to?({ controller: 'projects', action: 'show' }, project))- .to be_truthy+ it 'is true' do+ expect(user)+ .to be_allowed_to({ controller: 'projects', action: 'show' }, project)endendend@@ -475,8 +492,8 @@ describe User, 'allowed_to?' dofinal_setup_stepend- it 'should be true' do- expect(user.allowed_to?(permission, nil, global: true)).to be_truthy+ it 'is true' do+ expect(user).to be_allowed_to(permission, nil, global: true)endend@@ -488,8 +505,8 @@ describe User, 'allowed_to?' dofinal_setup_stepend- it 'should be false' do- expect(user.allowed_to?(permission, nil, global: true)).to be_falsey+ it 'is false' do+ expect(user).not_to be_allowed_to(permission, nil, global: true)endend@@ -503,8 +520,8 @@ describe User, 'allowed_to?' dofinal_setup_stepend- it 'should be true' do- expect(user.allowed_to?(permission, nil, global: true)).to be_truthy+ it 'is true' do+ expect(user).to be_allowed_to(permission, nil, global: true)endend@@ -521,9 +538,9 @@ describe User, 'allowed_to?' dofinal_setup_stepend- it 'should be true' do- expect(user.allowed_to?({ controller: 'wiki', action: 'show' }, nil, global: true))- .to be_truthy+ it 'is true' do+ expect(user)+ .to be_allowed_to({ controller: 'wiki', action: 'show' }, nil, global: true)endend@@ -539,8 +556,8 @@ describe User, 'allowed_to?' dofinal_setup_stepend- it 'should be true' do- expect(user.allowed_to?(permission, nil, global: true)).to be_truthy+ it 'is true' do+ expect(user).to be_allowed_to(permission, nil, global: true)endend@@ -553,8 +570,8 @@ describe User, 'allowed_to?' dofinal_setup_stepend- it 'should be true' do- expect(user.allowed_to?(permission, nil, global: true)).to be_truthy+ it 'is true' do+ expect(user).to be_allowed_to(permission, nil, global: true)endend@@ -568,7 +585,7 @@ describe User, 'allowed_to?' doendit 'is true' do- expect(user.allowed_to?(global_permission.name, nil, global: true)).to be_truthy+ expect(user).to be_allowed_to(global_permission.name, nil, global: true)endend@@ -583,7 +600,7 @@ describe User, 'allowed_to?' doendit 'is false' do- expect(user.allowed_to?(global_permission.name, nil, global: true)).to be_falsey+ expect(user).not_to be_allowed_to(global_permission.name, nil, global: true)endend@@ -595,7 +612,7 @@ describe User, 'allowed_to?' doendit 'is false' do- expect(user.allowed_to?(global_permission.name, nil, global: true)).to be_falsey+ expect(user).not_to be_allowed_to(global_permission.name, nil, global: true)endend@@ -607,8 +624,8 @@ describe User, 'allowed_to?' dofinal_setup_stepend- it 'should be true' do- expect(anonymous.allowed_to?(permission, nil, global: true)).to be_truthy+ it 'is true' do+ expect(anonymous).to be_allowed_to(permission, nil, global: true)endend@@ -627,7 +644,7 @@ describe User, 'allowed_to?' dofinal_setup_stepend- it 'should be true' do+ it 'is true' doexpect(user).to be_allowed_to({ controller: '/projects/settings/categories', action: 'show' }, nil, global: true)end@@ -639,8 +656,8 @@ describe User, 'allowed_to?' dofinal_setup_stepend- it 'should be false' do- expect(anonymous.allowed_to?(permission, nil, global: true)).to be_falsey+ it 'is false' do+ expect(anonymous).not_to be_allowed_to(permission, nil, global: true)endendend