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
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.rbindex 702053e3951..0398273b66c 100644--- a/lib/api/decorators/single.rb+++ b/lib/api/decorators/single.rb@@ -68,10 +68,6 @@ module APIclass_attribute :to_eager_loadclass_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 representerdef _type; enddiff --git a/lib/api/v3/memberships/membership_representer.rb b/lib/api/v3/memberships/membership_representer.rbindex e0e82b62e2e..5fe2b4e5e1f 100644--- a/lib/api/v3/memberships/membership_representer.rb+++ b/lib/api/v3/memberships/membership_representer.rb@@ -42,7 +42,7 @@ module APIendlink :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 APIendlink :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.rbindex 91603c57e55..a242a077dae 100644--- a/lib/api/v3/projects/project_representer.rb+++ b/lib/api/v3/projects/project_representer.rb@@ -44,7 +44,7 @@ module APIself_linklink :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 APIendlink :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 APIlink :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) }endlinks :storages,cache_if: -> {- current_user_allowed_to(:view_file_links, context: represented)+ current_user.allowed_in_project?(:view_file_links, represented)} dorepresented.storages.map do |storage|{@@ -84,15 +84,15 @@ module APIlink :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) }endlink :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 APIlink :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) }endlink :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 APIlink :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.rbindex aa8d89a97cc..e476cdbdcdf 100644--- a/lib/api/v3/relations/relation_representer.rb+++ b/lib/api/v3/relations/relation_representer.rb@@ -105,7 +105,7 @@ module APIenddef 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)endself.to_eager_load = [:to,diff --git a/lib/api/v3/versions/version_representer.rb b/lib/api/v3/versions/version_representer.rbindex ba7d35cb634..23d44e39aea 100644--- a/lib/api/v3/versions/version_representer.rb+++ b/lib/api/v3/versions/version_representer.rb@@ -50,7 +50,7 @@ module APIendlink :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 APIendlink :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 APIendlink :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: :deletediff --git a/lib/api/v3/work_packages/create_form_representer.rb b/lib/api/v3/work_packages/create_form_representer.rbindex 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 APIendlink :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.rbindex 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 APIendlink :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.rbindex 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 APItype: '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_groupsdiff --git a/lib/api/v3/work_packages/work_package_collection_representer.rb b/lib/api/v3/work_packages/work_package_collection_representer.rbindex 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 APIlink :customFields doif 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 APIrender_nil: falsedef 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)+ endenddef 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)+ endenddef schemasdiff --git a/lib/api/v3/work_packages/work_package_representer.rb b/lib/api/v3/work_packages/work_package_representer.rbindex 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 APIendlink :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 APIendlink :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) } donext if represented.new_record?{@@ -153,7 +153,7 @@ module APIendlink :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) } donext if represented.project.nil?{@@ -181,7 +181,7 @@ module APIendlink :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 APIendlink :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)}endlink :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 APIendlink :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 APIendlink :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 APIendlink :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 APIendlink :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 APIdef 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)enddef 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)enddef 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)enddef 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)enddef 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)enddef 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)enddef relationsdiff --git a/modules/costs/lib/api/v3/costs_api_user_permission_check.rb b/modules/costs/lib/api/v3/costs_api_user_permission_check.rbindex 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::CostsApiUserPermissionCheckprivatedef 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)enddef 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)enddef 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)endenddiff --git a/modules/costs/lib/api/v3/time_entries/time_entry_representer.rb b/modules/costs/lib/api/v3/time_entries/time_entry_representer.rbindex 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 APIendend- 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.rbindex 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 doclass CostsApiUserPermissionCheckTestClass# mimic representerdef 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)endinclude API::V3::CostsApiUserPermissionCheck@@ -52,32 +52,23 @@ RSpec.describe API::V3::CostsApiUserPermissionCheck dolet(: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+ endendsubject { 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?' dodescribe :overall_costs_visible? doshared_examples_for 'not visible' do