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
Handle finer permissions for journals/diff endpoint
Christophe Bliard78b3c94ecb5d
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rbindex 69d090348b9..bfb79a9c77d 100644--- a/app/controllers/application_controller.rb+++ b/app/controllers/application_controller.rb@@ -230,10 +230,28 @@ class ApplicationController < ActionController::Baseendend- # Authorize the user for the requested action- def authorize(ctrl = params[:controller], action = params[:action], global = false)+ # Authorize the user for the requested controller action.+ # To be used in before_action hooks+ def authorize+ action = { controller: params[:controller], action: params[:action] }+ do_authorize(action, global: false)+ end++ # Authorize the user for the requested controller action outside a project+ # To be used in before_action hooks+ def authorize_global+ action = { controller: params[:controller], action: params[:action] }+ do_authorize(action, global: true)+ end++ # Deny access if user is not allowed to do the specified action.+ #+ # Action can be:+ # * a parameter-like Hash (eg. { controller: '/projects', action: 'edit' })+ # * a permission Symbol (eg. :edit_project)+ def do_authorize(action, global: false)context = @project || @projects- is_authorized = User.current.allowed_to?({ controller: ctrl, action: }, context, global:)+ is_authorized = User.current.allowed_to?(action, context, global:)unless is_authorizedif @project&.archived?@@ -242,12 +260,6 @@ class ApplicationController < ActionController::Basedeny_accessendend- is_authorized- end-- # Authorize the user for the requested action outside a project- def authorize_global(ctrl = params[:controller], action = params[:action], global = true)- authorize(ctrl, action, global)end# Find project of id params[:id]diff --git a/app/controllers/journals_controller.rb b/app/controllers/journals_controller.rbindex 1bd89dac253..4d7d0418053 100644--- a/app/controllers/journals_controller.rb+++ b/app/controllers/journals_controller.rb@@ -1,3 +1,5 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.# Copyright (C) 2012-2023 the OpenProject GmbH@@ -27,9 +29,9 @@#++class JournalsController < ApplicationController- before_action :find_journal, except: [:index]before_action :find_optional_project, only: [:index]- before_action :authorize, only: [:diff]+ before_action :find_journal, only: [:diff]+ before_action :ensure_permitted, only: [:diff]accept_key_auth :indexmenu_item :issues@@ -91,6 +93,15 @@ class JournalsController < ApplicationControllerrender_404end+ def ensure_permitted+ permission =+ case @journal.journable_type+ when 'WorkPackage' then :view_work_packages+ when 'Project' then :view_project+ end+ do_authorize(permission)+ end+# Is this a valid field for diff'ing?def valid_field?(field)field.to_s.strip == 'description'diff --git a/app/services/authorization/user_allowed_service.rb b/app/services/authorization/user_allowed_service.rbindex fa140bdb09f..ecddf6a6d64 100644--- a/app/services/authorization/user_allowed_service.rb+++ b/app/services/authorization/user_allowed_service.rb@@ -36,7 +36,7 @@ class Authorization::UserAllowedService# Return true if the user is allowed to do the specified action on a specific context# Action can be:- # * a parameter-like Hash (eg. controller: '/projects', action: 'edit')+ # * a parameter-like Hash (eg. { controller: '/projects', action: 'edit' })# * a permission Symbol (eg. :edit_project)# Context can be:# * a project : returns true if user is allowed to do the specified action on this projectdiff --git a/config/initializers/permissions.rb b/config/initializers/permissions.rbindex d6ceaef8e0e..63df2ffec57 100644--- a/config/initializers/permissions.rb+++ b/config/initializers/permissions.rb@@ -140,7 +140,7 @@ Rails.application.reloader.to_prepare dowpt.permission :view_work_packages,{versions: %i[index show status_by],- journals: %i[index diff],+ journals: %i[index],work_packages: %i[show index],work_packages_api: [:get],'work_packages/reports': %i[report report_details]diff --git a/spec/controllers/journals_controller_spec.rb b/spec/controllers/journals_controller_spec.rbindex 3b55c19fa89..02ec1e59785 100644--- a/spec/controllers/journals_controller_spec.rb+++ b/spec/controllers/journals_controller_spec.rb@@ -29,58 +29,120 @@require 'spec_helper'describe JournalsController do- let(:user) { create(:user, member_in_project: project, member_through_role: role) }- let(:project) { create(:project_with_types) }- let(:role) { create(:role, permissions:) }- let(:member) do- build(:member, project:,- roles: [role],- principal: user)- end- let(:work_package) do- build(:work_package, type: project.types.first,- author: user,- project:,- description: '')- end- let(:journal) do- create(:work_package_journal,- journable: work_package,- user:)- end- let(:permissions) { [:view_work_packages] }+ shared_let(:project) { create(:project_with_types) }+ shared_let(:user) { create(:user, member_in_project: project, member_with_permissions: [:view_work_packages]) }- before do- allow(User).to receive(:current).and_return user+ current_user { user }+ subject(:response) do+ get :diff,+ xhr: true,+ params:enddescribe 'GET diff' dorender_views- let(:params) { { id: work_package.journals.last.id.to_s, field: :description, format: 'js' } }+ context 'for work package description' do+ shared_let(:work_package) do+ create(:work_package, type: project.types.first,+ author: user,+ project:,+ description: '')+ end+ let(:params) { { id: work_package.last_journal.id.to_s, field: :description, format: 'js' } }- before do- work_package.update_attribute :description, 'description'+ before do+ work_package.update_attribute :description, 'description'+ end++ describe 'with a user having :view_work_package permission' do+ it { expect(response).to have_http_status(:ok) }- get :diff,- xhr: true,- params:+ it 'presents the diff correctly' do+ expect(response.body.strip).to eq(+ "<div class=\"text-diff\">" \+ "\n " \+ "<label class=\"hidden-for-sighted\">Begin of the insertion</label><ins class=\"diffmod\">description</ins>" \+ "<label class=\"hidden-for-sighted\">End of the insertion</label>" \+ "\n" \+ "</div>"+ )+ end+ end++ describe 'with a user not having the :view_work_package permission' do+ before do+ RolePermission.delete_all+ end++ it { expect(response).to have_http_status(:forbidden) }+ endend- describe 'w/ authorization' do- it 'is successful' do- expect(response).to be_successful+ context 'for project description' do+ let(:params) { { id: project.last_journal.id.to_s, field: :description, format: 'js' } }++ before do+ project.update_attribute :description, 'description'+ end++ describe 'with a user being member of the project' do+ it { expect(response).to have_http_status(:ok) }++ it 'presents the diff correctly' do+ expect(response.body.strip).to eq(+ "<div class=\"text-diff\">" \+ "\n " \+ "<label class=\"hidden-for-sighted\">Begin of the insertion</label><ins class=\"diffmod\">description</ins>" \+ "<label class=\"hidden-for-sighted\">End of the insertion</label>" \+ "\n" \+ "</div>"+ )+ endend- it 'presents the diff correctly' do- expect(response.body.strip).to eq("<div class=\"text-diff\">\n <label class=\"hidden-for-sighted\">Begin of the insertion</label><ins class=\"diffmod\">description</ins><label class=\"hidden-for-sighted\">End of the insertion</label>\n</div>")+ describe 'with a user not being member of the project' do+ before do+ Member.delete_all+ end++ it { expect(response).to have_http_status(:forbidden) }+ end++ describe 'when "Work Package Tracking" module is disabled' do+ before do+ project.enabled_module_names -= ['work_package_tracking']+ end++ it { expect(response).to have_http_status(:ok) }+ end++ describe 'when project is archived' do+ before do+ project.update(active: false)+ end++ it { expect(response).to have_http_status(:forbidden) }endend- describe 'w/o authorization' do- let(:permissions) { [] }+ context 'for other types, like forum message' do+ shared_let(:forum) { create(:forum, project:) }+ shared_let(:message) { create(:message, forum:, content: 'initial content') }++ let(:params) { { id: message.last_journal.id.to_s, field: :description, format: 'js' } }++ before do+ message.update_attribute :content, 'initial content updated'+ end++ describe 'even with a user having all permissions' do+ before do+ user.update(admin: true)+ end- it { expect(response).not_to be_successful }+ it { expect(response).to have_http_status(:forbidden) }+ endendendend