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

refactor: expand options argument of allowed_to calls

Christophe Bliard8da459140e85
diff --git a/.rubocop.yml b/.rubocop.yml
index 6abcb51bc24..5ddc14858de 100644
--- a/.rubocop.yml
+++ b/.rubocop.yml
@@ -3,7 +3,7 @@ require:
- rubocop-rspec
AllCops:
- TargetRubyVersion: 3.0
+ TargetRubyVersion: 3.1
# Enable any new cops in new versions by default
NewCops: enable
Exclude:
diff --git a/app/contracts/work_packages/create_contract.rb b/app/contracts/work_packages/create_contract.rb
index ce675a64760..528fac1ab51 100644
--- a/app/contracts/work_packages/create_contract.rb
+++ b/app/contracts/work_packages/create_contract.rb
@@ -43,7 +43,7 @@ module WorkPackages
def user_allowed_to_add
if (model.project && [email protected]_to?(:add_work_packages, model.project)) ||
- [email protected]_to?(:add_work_packages, nil, global: true)
+ [email protected]_to_globally?(:add_work_packages)
errors.add :base, :error_unauthorized
end
diff --git a/app/helpers/projects_helper.rb b/app/helpers/projects_helper.rb
index f1cbb1fb833..4a81334888c 100644
--- a/app/helpers/projects_helper.rb
+++ b/app/helpers/projects_helper.rb
@@ -57,7 +57,7 @@ module ProjectsHelper
end
def no_projects_result_box_params
- if User.current.allowed_to?(:add_project, nil, global: true)
+ if User.current.allowed_to_globally?(:add_project)
{ action_url: new_project_path, display_action: true }
else
{}
diff --git a/app/models/queries/work_packages/filter/relatable_filter.rb b/app/models/queries/work_packages/filter/relatable_filter.rb
index 0e1ceed63b4..3c9a87a6b43 100644
--- a/app/models/queries/work_packages/filter/relatable_filter.rb
+++ b/app/models/queries/work_packages/filter/relatable_filter.rb
@@ -30,7 +30,7 @@ class Queries::WorkPackages::Filter::RelatableFilter < Queries::WorkPackages::Fi
include Queries::WorkPackages::Filter::FilterForWpMixin
def available?
- User.current.allowed_to?(:manage_work_package_relations, nil, global: true)
+ User.current.allowed_to_globally?(:manage_work_package_relations)
end
def type
diff --git a/app/models/user.rb b/app/models/user.rb
index d12c97a3440..0f58cb40fc2 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -518,16 +518,16 @@ class User < Principal
Authorization.users(action, project).where.not(members: { id: nil })
end
- def allowed_to?(action, context, options = {})
- authorization_service.call(action, context, options).result
+ def allowed_to?(action, context, global: false)
+ authorization_service.call(action, context, global:).result
end
- def allowed_to_in_project?(action, project, options = {})
- authorization_service.call(action, project, options).result
+ def allowed_to_in_project?(action, project)
+ allowed_to?(action, project)
end
- def allowed_to_globally?(action, options = {})
- authorization_service.call(action, nil, options.merge(global: true)).result
+ def allowed_to_globally?(action)
+ allowed_to?(action, nil, global: true)
end
delegate :preload_projects_allowed_to, to: :authorization_service
diff --git a/app/services/authorization/user_allowed_service.rb b/app/services/authorization/user_allowed_service.rb
index a390aa10ef9..7480028051b 100644
--- a/app/services/authorization/user_allowed_service.rb
+++ b/app/services/authorization/user_allowed_service.rb
@@ -41,12 +41,12 @@ class Authorization::UserAllowedService
# Context can be:
# * a project : returns true if user is allowed to do the specified action on this project
# * a group of projects : returns true if user is allowed on every project
- # * nil with options[:global] set : check if user has at least one role allowed for this action,
+ # * nil with +global+ set to +true+ : check if user has at least one role allowed for this action,
# or falls back to Non Member / Anonymous permissions depending if the user is logged
- def call(action, context, options = {})
- if supported_context?(context, options)
+ def call(action, context, global: false)
+ if supported_context?(context, global:)
ServiceResult.new(success: true,
- result: allowed_to?(action, context, options))
+ result: allowed_to?(action, context, global:))
else
ServiceResult.new(success: false,
result: false)
@@ -61,25 +61,21 @@ class Authorization::UserAllowedService
attr_accessor :project_role_cache
- def allowed_to?(action, context, options = {})
+ def allowed_to?(action, context, global: false)
action = normalize_action(action)
- if context.nil? && options[:global]
- allowed_to_globally?(action, options)
+ if context.nil? && global
+ allowed_to_globally?(action)
elsif context.is_a? Project
- allowed_to_in_project?(action, context, options)
+ allowed_to_in_project?(action, context)
elsif context.respond_to?(:to_a)
- context = context.to_a
- # Authorize if user is authorized on every element of the array
- context.present? && context.all? do |project|
- allowed_to?(action, project, options)
- end
+ allowed_to_in_all_projects?(action, context)
else
false
end
end
- def allowed_to_in_project?(action, project, _options = {})
+ def allowed_to_in_project?(action, project)
if project_authorization_cache.cached?(action)
return project_authorization_cache.allowed?(action, project)
end
@@ -97,9 +93,17 @@ class Authorization::UserAllowedService
has_authorized_role?(action, project)
end
+ def allowed_to_in_all_projects?(action, projects)
+ projects = projects.to_a
+ # Authorize if user is authorized on every element of the array
+ projects.present? && projects.all? do |project|
+ allowed_to?(action, project)
+ end
+ end
+
# Is the user allowed to do the specified action on any project?
- # See allowed_to? for the actions and valid options.
- def allowed_to_globally?(action, _options = {})
+ # See allowed_to? for the action parameter description.
+ def allowed_to_globally?(action)
# Inactive users are never authorized
return false unless authorizable_user?
# Admin users are always authorized
@@ -142,8 +146,8 @@ class Authorization::UserAllowedService
action
end
- def supported_context?(context, options)
- (context.nil? && options[:global]) ||
+ def supported_context?(context, global:)
+ (context.nil? && global) ||
context.is_a?(Project) ||
(!context.nil? && context.respond_to?(:to_a))
end
diff --git a/app/views/homescreen/blocks/_projects.html.erb b/app/views/homescreen/blocks/_projects.html.erb
index 2b1a252a127..66d1e2cf4c0 100644
--- a/app/views/homescreen/blocks/_projects.html.erb
+++ b/app/views/homescreen/blocks/_projects.html.erb
@@ -17,7 +17,7 @@
<% end %>
<div class="widget-box--blocks--buttons">
- <% if User.current.allowed_to?(:add_project, nil, global: true) %>
+ <% if User.current.allowed_to_globally?(:add_project) %>
<%= link_to new_project_path,
{ class: 'button -alt-highlight',
aria: {label: t(:label_project_new)},
diff --git a/app/views/projects/index.html.erb b/app/views/projects/index.html.erb
index 77a0eabbe0a..a472ccec781 100644
--- a/app/views/projects/index.html.erb
+++ b/app/views/projects/index.html.erb
@@ -33,7 +33,7 @@ See COPYRIGHT and LICENSE files for more details.
<% html_title(t(:label_project_plural)) -%>
<%= toolbar title: t(:label_project_plural), html: { class: '-with-dropdown' } do %>
- <% if User.current.allowed_to?(:add_project, nil, global: true) %>
+ <% if User.current.allowed_to_globally?(:add_project) %>
<li class="toolbar-item">
<%= link_to new_project_path,
{ class: 'button -alt-highlight',
diff --git a/config/initializers/menus.rb b/config/initializers/menus.rb
index bd6d14d989c..627fa034ccf 100644
--- a/config/initializers/menus.rb
+++ b/config/initializers/menus.rb
@@ -44,7 +44,7 @@ Redmine::MenuManager.map :top_menu do |menu|
caption: I18n.t('label_work_package_plural'),
if: Proc.new {
(User.current.logged? || !Setting.login_required?) &&
- User.current.allowed_to?(:view_work_packages, nil, global: true)
+ User.current.allowed_to_globally?(:view_work_packages)
}
menu.push :news,
{ controller: '/news', project_id: nil, action: 'index' },
@@ -52,7 +52,7 @@ Redmine::MenuManager.map :top_menu do |menu|
caption: I18n.t('label_news_plural'),
if: Proc.new {
(User.current.logged? || !Setting.login_required?) &&
- User.current.allowed_to?(:view_news, nil, global: true)
+ User.current.allowed_to_globally?(:view_news)
}
menu.push :help,
OpenProject::Static::Links.help_link,
diff --git a/lib/api/v3/root_representer.rb b/lib/api/v3/root_representer.rb
index b0c6a8a89f5..4341170e057 100644
--- a/lib/api/v3/root_representer.rb
+++ b/lib/api/v3/root_representer.rb
@@ -44,8 +44,8 @@ module API
end
link :memberships do
- next unless current_user.allowed_to?(:view_members, nil, global: true) ||
- current_user.allowed_to?(:manage_members, nil, global: true)
+ next unless current_user.allowed_to_globally?(:view_members) ||
+ current_user.allowed_to_globally?(:manage_members)
{
href: api_v3_paths.memberships
diff --git a/lib/plugins/acts_as_journalized/lib/acts/journalized/permissions.rb b/lib/plugins/acts_as_journalized/lib/acts/journalized/permissions.rb
index 5318b6b31a6..95132227e6f 100644
--- a/lib/plugins/acts_as_journalized/lib/acts/journalized/permissions.rb
+++ b/lib/plugins/acts_as_journalized/lib/acts/journalized/permissions.rb
@@ -56,8 +56,7 @@ module Acts::Journalized
editable_by?(user)
else
p = @project || (project if respond_to? :project)
- options = { global: p.present? }
- user.allowed_to? journable_edit_permission, p, options
+ user.allowed_to? journable_edit_permission, p, global: p.present?
end
editable && journal.user_id == user.id
diff --git a/modules/costs/app/controllers/hourly_rates_controller.rb b/modules/costs/app/controllers/hourly_rates_controller.rb
index 3e0040ffc1e..3ab10b2c242 100644
--- a/modules/costs/app/controllers/hourly_rates_controller.rb
+++ b/modules/costs/app/controllers/hourly_rates_controller.rb
@@ -44,7 +44,7 @@ class HourlyRatesController < ApplicationController
# TODO: this should be an index
def show
if @project
- return deny_access unless User.current.allowed_to?(:view_hourly_rates, @project, for: @user)
+ return deny_access unless User.current.allowed_to?(:view_hourly_rates, @project)
@rates = HourlyRate.where(user_id: @user, project_id: @project)
.order("#{HourlyRate.table_name}.valid_from desc")
diff --git a/modules/reporting/app/controllers/cost_reports_controller.rb b/modules/reporting/app/controllers/cost_reports_controller.rb
index b8f97776300..d5ae5d38b29 100644
--- a/modules/reporting/app/controllers/cost_reports_controller.rb
+++ b/modules/reporting/app/controllers/cost_reports_controller.rb
@@ -359,31 +359,22 @@ class CostReportsController < ApplicationController
# If report does not belong to a project, it is ok to look for the
# permission in any project. Otherwise, the user should have the permission
# in this project.
- options = if report.project.present?
- {}
- else
- { global: true }
- end
-
- case action
- when :create
- user.allowed_to?(:save_cost_reports, @project, options) or
- user.allowed_to?(:save_private_cost_reports, @project, options)
-
- when :save, :destroy, :rename
- if report.is_public?
- user.allowed_to?(:save_cost_reports, @project, options)
- else
- user.allowed_to?(:save_cost_reports, @project, options) or
- user.allowed_to?(:save_private_cost_reports, @project, options)
+ global = report.project.nil?
+
+ permissions =
+ case action
+ when :create
+ %i[save_cost_reports save_private_cost_reports]
+ when :save, :destroy, :rename
+ if report.is_public?
+ %i[save_cost_reports]
+ else
+ %i[save_cost_reports save_private_cost_reports]
+ end
+ when :save_as_public
+ %i[save_cost_reports]
end
-
- when :save_as_public
- user.allowed_to?(:save_cost_reports, @project, options)
-
- else
- false
- end
+ Array(permissions).any? { |permission| user.allowed_to?(permission, @project, global:) }
end
def display_report_list
diff --git a/modules/reporting/lib/open_project/reporting/engine.rb b/modules/reporting/lib/open_project/reporting/engine.rb
index 78e7bd14aff..572524dc8c6 100644
--- a/modules/reporting/lib/open_project/reporting/engine.rb
+++ b/modules/reporting/lib/open_project/reporting/engine.rb
@@ -62,10 +62,10 @@ module OpenProject::Reporting
if: Proc.new {
(User.current.logged? || !Setting.login_required?) &&
(
- User.current.allowed_to?(:view_time_entries, nil, global: true) ||
- User.current.allowed_to?(:view_own_time_entries, nil, global: true) ||
- User.current.allowed_to?(:view_cost_entries, nil, global: true) ||
- User.current.allowed_to?(:view_own_cost_entries, nil, global: true)
+ User.current.allowed_to_globally?(:view_time_entries) ||
+ User.current.allowed_to_globally?(:view_own_time_entries) ||
+ User.current.allowed_to_globally?(:view_cost_entries) ||
+ User.current.allowed_to_globally?(:view_own_cost_entries)
)
}
diff --git a/modules/reporting/spec/features/top_menu_item_spec.rb b/modules/reporting/spec/features/top_menu_item_spec.rb
index 5ce55365f74..d0715793286 100644
--- a/modules/reporting/spec/features/top_menu_item_spec.rb
+++ b/modules/reporting/spec/features/top_menu_item_spec.rb
@@ -65,7 +65,7 @@ feature 'Top menu items', js: true do
create(:non_member)
if ex.metadata.key?(:allowed_to)
- allow(user).to receive(:allowed_to?).and_return(ex.metadata[:allowed_to])
+ allow(user).to receive(:allowed_to_globally?).and_return(ex.metadata[:allowed_to])
end
visit root_path
diff --git a/modules/xls_export/app/views/hooks/xls_report/_view_cost_report_toolbar.html.erb b/modules/xls_export/app/views/hooks/xls_report/_view_cost_report_toolbar.html.erb
index 25419b6ba6c..518faed2ebd 100644
--- a/modules/xls_export/app/views/hooks/xls_report/_view_cost_report_toolbar.html.erb
+++ b/modules/xls_export/app/views/hooks/xls_report/_view_cost_report_toolbar.html.erb
@@ -1,10 +1,10 @@
-<% if User.current.allowed_to? :export_work_packages, @project, :global => @project.nil? %>
+<% if User.current.allowed_to? :export_work_packages, @project, global: @project.nil? %>
<li class="toolbar-item">
<%= link_to(t(:export_to_excel),
{ controller: "cost_reports" ,
action: :index,
format: 'xls',
project_id: @project },
- :class => "button icon-export-xls-descr") %>
+ class: "button icon-export-xls-descr") %>
</li>
<% end %>
diff --git a/spec/lib/api/v3/root_representer_spec.rb b/spec/lib/api/v3/root_representer_spec.rb
index 666fde0976e..95053f9ee5e 100644
--- a/spec/lib/api/v3/root_representer_spec.rb
+++ b/spec/lib/api/v3/root_representer_spec.rb
@@ -39,9 +39,9 @@ describe ::API::V3::RootRepresenter do
before do
allow(user)
- .to receive(:allowed_to?) do |action, _project, options|
- permissions.include?(action) && options[:global] = true
- end
+ .to receive(:allowed_to_globally?) do |action|
+ permissions.include?(action)
+ end
end
context 'generation' do