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

Get rid of `current_user_allowed_to` method in the API

Klaus Zandersd67c29253e6f
diff --git a/lib/api/decorators/single.rb b/lib/api/decorators/single.rb
index 702053e3951..0398273b66c 100644
--- a/lib/api/decorators/single.rb
+++ b/lib/api/decorators/single.rb
@@ -68,10 +68,6 @@ module API
class_attribute :to_eager_load
class_attribute :checked_permissions
- def current_user_allowed_to(permission, context: represented.respond_to?(:project) ? represented.project : nil)
- current_user.allowed_to?(permission, context)
- end
-
# Override in subclasses to specify the JSON indicated "_type" of this representer
def _type; end
diff --git a/lib/api/v3/memberships/membership_representer.rb b/lib/api/v3/memberships/membership_representer.rb
index e0e82b62e2e..5fe2b4e5e1f 100644
--- a/lib/api/v3/memberships/membership_representer.rb
+++ b/lib/api/v3/memberships/membership_representer.rb
@@ -42,7 +42,7 @@ module API
end
link :update do
- next unless current_user_allowed_to(:manage_members, context: represented.project)
+ next unless current_user.allowed_in_project?(:manage_members, represented.project)
{
href: api_v3_paths.membership_form(represented.id),
@@ -51,7 +51,7 @@ module API
end
link :updateImmediately do
- next unless current_user_allowed_to(:manage_members, context: represented.project)
+ next unless current_user.allowed_in_project?(:manage_members, represented.project)
{
href: api_v3_paths.membership(represented.id),
diff --git a/lib/api/v3/projects/project_representer.rb b/lib/api/v3/projects/project_representer.rb
index 91603c57e55..a242a077dae 100644
--- a/lib/api/v3/projects/project_representer.rb
+++ b/lib/api/v3/projects/project_representer.rb
@@ -44,7 +44,7 @@ module API
self_link
link :createWorkPackage,
- cache_if: -> { current_user_allowed_to(:add_work_packages, context: represented) } do
+ cache_if: -> { current_user.allowed_in_project?(:add_work_packages, represented) } do
{
href: api_v3_paths.create_project_work_package_form(represented.id),
method: :post
@@ -52,7 +52,7 @@ module API
end
link :createWorkPackageImmediately,
- cache_if: -> { current_user_allowed_to(:add_work_packages, context: represented) } do
+ cache_if: -> { current_user.allowed_in_project?(:add_work_packages, represented) } do
{
href: api_v3_paths.work_packages_by_project(represented.id),
method: :post
@@ -61,14 +61,14 @@ module API
link :workPackages,
cache_if: -> {
- current_user_allowed_to(:view_work_packages, context: represented)
+ current_user.allowed_in_project?(:view_work_packages, represented)
} do
{ href: api_v3_paths.work_packages_by_project(represented.id) }
end
links :storages,
cache_if: -> {
- current_user_allowed_to(:view_file_links, context: represented)
+ current_user.allowed_in_project?(:view_file_links, represented)
} do
represented.storages.map do |storage|
{
@@ -84,15 +84,15 @@ module API
link :versions,
cache_if: -> {
- current_user_allowed_to(:view_work_packages, context: represented) ||
- current_user_allowed_to(:manage_versions, context: represented)
+ current_user.allowed_in_project?(:view_work_packages, represented) ||
+ current_user.allowed_in_project?(:manage_versions, represented)
} do
{ href: api_v3_paths.versions_by_project(represented.id) }
end
link :memberships,
cache_if: -> {
- current_user_allowed_to(:view_members, context: represented)
+ current_user.allowed_in_project?(:view_members, represented)
} do
{
href: api_v3_paths.path_for(:memberships, filters: [{ project: { operator: "=", values: [represented.id.to_s] } }])
@@ -101,15 +101,15 @@ module API
link :types,
cache_if: -> {
- current_user_allowed_to(:view_work_packages, context: represented) ||
- current_user_allowed_to(:manage_types, context: represented)
+ current_user.allowed_in_project?(:view_work_packages, represented) ||
+ current_user.allowed_in_project?(:manage_types, represented)
} do
{ href: api_v3_paths.types_by_project(represented.id) }
end
link :update,
cache_if: -> {
- current_user_allowed_to(:edit_project, context: represented)
+ current_user.allowed_in_project?(:edit_project, represented)
} do
{
href: api_v3_paths.project_form(represented.id),
@@ -119,7 +119,7 @@ module API
link :updateImmediately,
cache_if: -> {
- current_user_allowed_to(:edit_project, context: represented)
+ current_user.allowed_in_project?(:edit_project, represented)
} do
{
href: api_v3_paths.project(represented.id),
diff --git a/lib/api/v3/relations/relation_representer.rb b/lib/api/v3/relations/relation_representer.rb
index aa8d89a97cc..e476cdbdcdf 100644
--- a/lib/api/v3/relations/relation_representer.rb
+++ b/lib/api/v3/relations/relation_representer.rb
@@ -105,7 +105,7 @@ module API
end
def manage_relations?
- current_user_allowed_to :manage_work_package_relations, context: represented.from.project
+ current_user.allowed_in_project?(:manage_work_package_relations, represented.from.project)
end
self.to_eager_load = [:to,
diff --git a/lib/api/v3/versions/version_representer.rb b/lib/api/v3/versions/version_representer.rb
index ba7d35cb634..23d44e39aea 100644
--- a/lib/api/v3/versions/version_representer.rb
+++ b/lib/api/v3/versions/version_representer.rb
@@ -50,7 +50,7 @@ module API
end
link :update,
- cache_if: -> { current_user_allowed_to(:manage_versions, context: represented.project) } do
+ cache_if: -> { current_user.allowed_in_project?(:manage_versions, represented.project) } do
{
href: api_v3_paths.version_form(represented.id),
method: :post
@@ -58,7 +58,7 @@ module API
end
link :updateImmediately,
- cache_if: -> { current_user_allowed_to(:manage_versions, context: represented.project) } do
+ cache_if: -> { current_user.allowed_in_project?(:manage_versions, represented.project) } do
{
href: api_v3_paths.version(represented.id),
method: :patch
@@ -66,7 +66,7 @@ module API
end
link :delete,
- cache_if: -> { current_user_allowed_to(:manage_versions, context: represented.project) } do
+ cache_if: -> { current_user.allowed_in_project?(:manage_versions, represented.project) } do
{
href: api_v3_paths.version(represented.id),
method: :delete
diff --git a/lib/api/v3/work_packages/create_form_representer.rb b/lib/api/v3/work_packages/create_form_representer.rb
index c936ebc9175..aaa58a6c20c 100644
--- a/lib/api/v3/work_packages/create_form_representer.rb
+++ b/lib/api/v3/work_packages/create_form_representer.rb
@@ -65,9 +65,7 @@ module API
end
link :customFields do
- if represented.project &&
- current_user_allowed_to(:select_custom_fields,
- context: represented.project)
+ if represented.project && current_user.allowed_in_project?(:select_custom_fields, represented.project)
{
href: project_settings_custom_fields_path(represented.project.identifier),
type: 'text/html',
diff --git a/lib/api/v3/work_packages/create_project_form_representer.rb b/lib/api/v3/work_packages/create_project_form_representer.rb
index 48b91f017b6..fa15c7b6b92 100644
--- a/lib/api/v3/work_packages/create_project_form_representer.rb
+++ b/lib/api/v3/work_packages/create_project_form_representer.rb
@@ -63,8 +63,7 @@ module API
end
link :customFields do
- if current_user_allowed_to(:select_custom_fields,
- context: represented.project)
+ if current_user.allowed_in_project?(:select_custom_fields, represented.project)
{
href: project_settings_custom_fields_path(represented.project.identifier),
type: 'text/html',
diff --git a/lib/api/v3/work_packages/schema/work_package_schema_representer.rb b/lib/api/v3/work_packages/schema/work_package_schema_representer.rb
index 0f2102925fe..4d60ef31cbe 100644
--- a/lib/api/v3/work_packages/schema/work_package_schema_representer.rb
+++ b/lib/api/v3/work_packages/schema/work_package_schema_representer.rb
@@ -169,8 +169,8 @@ module API
type: 'Duration',
required: false,
show_if: ->(*) {
- current_user_allowed_to(:view_time_entries, context: represented.project) ||
- current_user_allowed_to(:view_own_time_entries, context: represented.project)
+ current_user.allowed_in_project?(:view_time_entries, represented.project) ||
+ current_user.allowed_in_project?(:view_own_time_entries, represented.project)
}
schema :percentage_done,
@@ -299,7 +299,7 @@ module API
}
},
show_if: ->(*) {
- current_user_allowed_to(:view_budgets, context: represented.project)
+ current_user.allowed_in_project?(:view_budgets, represented.project)
}
def attribute_groups
diff --git a/lib/api/v3/work_packages/work_package_collection_representer.rb b/lib/api/v3/work_packages/work_package_collection_representer.rb
index 47307208cab..7ff908b84f8 100644
--- a/lib/api/v3/work_packages/work_package_collection_representer.rb
+++ b/lib/api/v3/work_packages/work_package_collection_representer.rb
@@ -125,7 +125,7 @@ module API
link :customFields do
if project.present? &&
- current_user_allowed_to(:select_custom_fields, context: project)
+ current_user.allowed_in_project?(:select_custom_fields, project)
{
href: project_settings_custom_fields_path(project.identifier),
type: 'text/html',
@@ -175,12 +175,19 @@ module API
render_nil: false
def current_user_allowed_to_add_work_packages?
- @current_user_allowed_to_add_work_packages ||=
- current_user.allowed_to?(:add_work_packages, project, global: project.nil?)
+ if project
+ current_user.allowed_in_project?(:add_work_packages, project)
+ else
+ current_user.allowed_in_any_project?(:add_work_packages)
+ end
end
def current_user_allowed_to_edit_work_packages?
- current_user.allowed_to?(:edit_work_packages, project, global: project.nil?)
+ if project
+ current_user.allowed_in_project?(:edit_work_packages, project)
+ else
+ current_user.allowed_in_any_project?(:edit_work_packages)
+ end
end
def schemas
diff --git a/lib/api/v3/work_packages/work_package_representer.rb b/lib/api/v3/work_packages/work_package_representer.rb
index 961fc1a49c7..492d2103f20 100644
--- a/lib/api/v3/work_packages/work_package_representer.rb
+++ b/lib/api/v3/work_packages/work_package_representer.rb
@@ -83,7 +83,7 @@ module API
end
link :delete,
- cache_if: -> { current_user_allowed_to(:delete_work_packages, context: represented.project) } do
+ cache_if: -> { current_user.allowed_in_project?(:delete_work_packages, represented.project) } do
{
href: api_v3_paths.work_package(represented.id),
method: :delete
@@ -101,7 +101,7 @@ module API
end
link :move,
- cache_if: -> { current_user_allowed_to(:move_work_packages, context: represented.project) } do
+ cache_if: -> { current_user.allowed_in_project?(:move_work_packages, represented.project) } do
next if represented.new_record?
{
@@ -153,7 +153,7 @@ module API
end
link :customFields,
- cache_if: -> { current_user_allowed_to(:select_custom_fields, context: represented.project) } do
+ cache_if: -> { current_user.allowed_in_project?(:select_custom_fields, represented.project) } do
next if represented.project.nil?
{
@@ -181,7 +181,7 @@ module API
end
link :availableWatchers,
- cache_if: -> { current_user_allowed_to(:add_work_package_watchers, context: represented.project) } do
+ cache_if: -> { current_user.allowed_in_project?(:add_work_package_watchers, represented.project) } do
{
href: api_v3_paths.available_watchers(represented.id)
}
@@ -221,14 +221,14 @@ module API
end
link :watchers,
- cache_if: -> { current_user_allowed_to(:view_work_package_watchers, context: represented.project) } do
+ cache_if: -> { current_user.allowed_in_project?(:view_work_package_watchers, represented.project) } do
{
href: api_v3_paths.work_package_watchers(represented.id)
}
end
link :addWatcher,
- cache_if: -> { current_user_allowed_to(:add_work_package_watchers, context: represented.project) } do
+ cache_if: -> { current_user.allowed_in_project?(:add_work_package_watchers, represented.project) } do
{
href: api_v3_paths.work_package_watchers(represented.id),
method: :post,
@@ -238,7 +238,7 @@ module API
end
link :removeWatcher,
- cache_if: -> { current_user_allowed_to(:delete_work_package_watchers, context: represented.project) } do
+ cache_if: -> { current_user.allowed_in_project?(:delete_work_package_watchers, represented.project) } do
{
href: api_v3_paths.watcher('{user_id}', represented.id),
method: :delete,
@@ -247,7 +247,7 @@ module API
end
link :addRelation,
- cache_if: -> { current_user_allowed_to(:manage_work_package_relations, context: represented.project) } do
+ cache_if: -> { current_user.allowed_in_project?(:manage_work_package_relations, represented.project) } do
{
href: api_v3_paths.work_package_relations(represented.id),
method: :post,
@@ -267,7 +267,7 @@ module API
end
link :changeParent,
- cache_if: -> { current_user_allowed_to(:manage_subtasks, context: represented.project) } do
+ cache_if: -> { current_user.allowed_in_project?(:manage_subtasks, represented.project) } do
{
href: api_v3_paths.work_package(represented.id),
method: :patch,
@@ -276,7 +276,7 @@ module API
end
link :addComment,
- cache_if: -> { current_user_allowed_to(:add_work_package_notes, context: represented.project) } do
+ cache_if: -> { current_user.allowed_in_project?(:add_work_package_notes, represented.project) } do
{
href: api_v3_paths.work_package_activities(represented.id),
method: :post,
@@ -565,34 +565,34 @@ module API
def current_user_update_allowed?
@current_user_update_allowed ||=
- current_user_allowed_to(:edit_work_packages, context: represented.project) ||
- current_user_allowed_to(:assign_versions, context: represented.project)
+ current_user.allowed_in_project?(:edit_work_packages, represented.project) ||
+ current_user.allowed_in_project?(:assign_versions, represented.project)
end
def view_time_entries_allowed?
@view_time_entries_allowed ||=
- current_user_allowed_to(:view_time_entries, context: represented.project) ||
- current_user_allowed_to(:view_own_time_entries, context: represented.project)
+ current_user.allowed_in_project?(:view_time_entries, represented.project) ||
+ current_user.allowed_in_project?(:view_own_time_entries, represented.project)
end
def log_time_allowed?
@log_time_allowed ||=
- current_user_allowed_to(:log_time, context: represented.project) ||
- current_user_allowed_to(:log_own_time, context: represented.project)
+ current_user.allowed_in_project?(:log_time, represented.project) ||
+ current_user.allowed_in_project?(:log_own_time, represented.project)
end
def view_budgets_allowed?
- @view_budgets_allowed ||= current_user_allowed_to(:view_budgets, context: represented.project)
+ @view_budgets_allowed ||= current_user.allowed_in_project?(:view_budgets, represented.project)
end
def export_work_packages_allowed?
@export_work_packages_allowed ||=
- current_user_allowed_to(:export_work_packages, context: represented.project)
+ current_user.allowed_in_project?(:export_work_packages, represented.project)
end
def add_work_packages_allowed?
@add_work_packages_allowed ||=
- current_user_allowed_to(:add_work_packages, context: represented.project)
+ current_user.allowed_in_project?(:add_work_packages, represented.project)
end
def relations
diff --git a/modules/costs/lib/api/v3/costs_api_user_permission_check.rb b/modules/costs/lib/api/v3/costs_api_user_permission_check.rb
index a39e42816cc..4a70cbc22f3 100644
--- a/modules/costs/lib/api/v3/costs_api_user_permission_check.rb
+++ b/modules/costs/lib/api/v3/costs_api_user_permission_check.rb
@@ -23,16 +23,16 @@ module API::V3::CostsApiUserPermissionCheck
private
def user_has_hourly_rate_permissions?
- current_user_allowed_to(:view_hourly_rates, context: represented.project) ||
- current_user_allowed_to(:view_own_hourly_rate, context: represented.project)
+ current_user.allowed_in_project?(:view_hourly_rates, represented.project) ||
+ current_user.allowed_in_project?(:view_own_hourly_rate, represented.project)
end
def user_has_cost_rates_permission?
- current_user_allowed_to(:view_cost_rates, context: represented.project)
+ current_user.allowed_in_project?(:view_cost_rates, represented.project)
end
def user_has_cost_entry_permissions?
- current_user_allowed_to(:view_own_cost_entries, context: represented.project) ||
- current_user_allowed_to(:view_cost_entries, context: represented.project)
+ current_user.allowed_in_project?(:view_own_cost_entries, represented.project) ||
+ current_user.allowed_in_project?(:view_cost_entries, represented.project)
end
end
diff --git a/modules/costs/lib/api/v3/time_entries/time_entry_representer.rb b/modules/costs/lib/api/v3/time_entries/time_entry_representer.rb
index cc3e64369b5..2eb14ffa3e4 100644
--- a/modules/costs/lib/api/v3/time_entries/time_entry_representer.rb
+++ b/modules/costs/lib/api/v3/time_entries/time_entry_representer.rb
@@ -123,10 +123,6 @@ module API
end
end
- def current_user_allowed_to(permission, context:)
- current_user.allowed_to?(permission, context)
- end
-
def hours=(value)
represented.hours = datetime_formatter.parse_duration_to_hours(value,
'hours',
diff --git a/modules/costs/spec/lib/api/v3/costs_api_user_permission_check_spec.rb b/modules/costs/spec/lib/api/v3/costs_api_user_permission_check_spec.rb
index 6564f0b7b05..4b1c630bd48 100644
--- a/modules/costs/spec/lib/api/v3/costs_api_user_permission_check_spec.rb
+++ b/modules/costs/spec/lib/api/v3/costs_api_user_permission_check_spec.rb
@@ -32,8 +32,8 @@ RSpec.describe API::V3::CostsApiUserPermissionCheck do
class CostsApiUserPermissionCheckTestClass
# mimic representer
def view_time_entries_allowed?
- current_user_allowed_to(:view_time_entries, context: represented.project) ||
- current_user_allowed_to(:view_own_time_entries, context: represented.project)
+ current_user.allowed_in_project?(:view_time_entries, represented.project) ||
+ current_user.allowed_in_project?(:view_own_time_entries, represented.project)
end
include API::V3::CostsApiUserPermissionCheck
@@ -52,32 +52,23 @@ RSpec.describe API::V3::CostsApiUserPermissionCheck do
let(:work_package) { build_stubbed(:work_package, project:) }
before do
- allow(subject)
- .to receive(:current_user)
- .and_return(user)
- allow(subject)
- .to receive(:represented)
- .and_return(work_package)
+ allow(subject).to receive(:current_user).and_return(user)
+ allow(subject).to receive(:represented).and_return(work_package)
+
+ mock_permissions_for(user) do |mock|
+ mock.allow_in_project :view_time_entries, project: work_package.project if view_time_entries
+ mock.allow_in_project :view_own_time_entries, project: work_package.project if view_own_time_entries
+ mock.allow_in_project :view_hourly_rates, project: work_package.project if view_hourly_rates
+ mock.allow_in_project :view_own_hourly_rate, project: work_package.project if view_own_hourly_rate
+ mock.allow_in_project :view_cost_rates, project: work_package.project if view_cost_rates
+ mock.allow_in_project :view_own_cost_entries, project: work_package.project if view_own_cost_entries
+ mock.allow_in_project :view_cost_entries, project: work_package.project if view_cost_entries
+ mock.allow_in_project :view_budgets, project: work_package.project if view_budgets
+ end
end
subject { CostsApiUserPermissionCheckTestClass.new }
- before do
- %i[view_time_entries
- view_own_time_entries
- view_hourly_rates
- view_own_hourly_rate
- view_cost_rates
- view_own_cost_entries
- view_cost_entries
- view_budgets].each do |permission|
- allow(subject)
- .to receive(:current_user_allowed_to)
- .with(permission, context: work_package.project)
- .and_return send(permission)
- end
- end
-
describe '#overall_costs_visible?' do
describe :overall_costs_visible? do
shared_examples_for 'not visible' do