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
Add user job to provide system user with privileges (#9191)
Oliver Günther06bcfbbb06ca
diff --git a/app/services/projects/schedule_deletion_service.rb b/app/services/projects/schedule_deletion_service.rbindex b3d247ba8e3..c2cef94c75e 100644--- a/app/services/projects/schedule_deletion_service.rb+++ b/app/services/projects/schedule_deletion_service.rb@@ -48,7 +48,7 @@ module Projectsenddef persist(call)- DeleteProjectJob.perform_later(user_id: user.id, project_id: model.id)+ DeleteProjectJob.perform_later(user: user, project: model)callendenddiff --git a/app/workers/projects/delete_project_job.rb b/app/workers/projects/delete_project_job.rbindex c6b990fd37a..4b548eeb4ea 100644--- a/app/workers/projects/delete_project_job.rb+++ b/app/workers/projects/delete_project_job.rb@@ -29,16 +29,14 @@#++module Projects- class DeleteProjectJob < ApplicationJob+ class DeleteProjectJob < UserJobqueue_with_priority :lowinclude OpenProject::LocaleHelper- attr_reader :user_id,- :project_id+ attr_reader :project- def perform(user_id:, project_id:)- @user_id = user_id- @project_id = project_id+ def execute(project:)+ @project = projectservice_call = delete_project@@ -59,7 +57,7 @@ module Projectsdef log_standard_error(e)logger.error('Encountered an error when trying to delete project '\- "'#{project_id}' : #{e.message} #{e.backtrace.join("\n")}")+ "'#{project}' : #{e.message} #{e.backtrace.join("\n")}")enddef log_service_failure(service_call)@@ -67,14 +65,6 @@ module Projects"#{service_call.message}"end- def user- @user ||= User.find user_id- end-- def project- @project ||= Project.find project_id- end-def loggerDelayed::Worker.loggerenddiff --git a/app/workers/user_job.rb b/app/workers/user_job.rbnew file mode 100644index 00000000000..e76d0974dce--- /dev/null+++ b/app/workers/user_job.rb@@ -0,0 +1,56 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2021 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++class UserJob < ApplicationJob+ attr_reader :user++ def perform(user:, **args)+ @user = user++ as_user do+ execute(**args)+ end+ rescue StandardError => e+ Rails.logger.error "Failed to execute job #{self.class.name} for #{user}: #{e}"+ raise e+ end++ private++ ##+ # Returns the user for using in delayed job+ # If a system user was passed, assume that it receives+ # admin privileges.+ def as_user(&block)+ if user.is_a?(SystemUser)+ user.run_given(&block)+ else+ User.execute_as(user, &block)+ end+ end+enddiff --git a/spec/services/projects/schedule_deletion_service_spec.rb b/spec/services/projects/schedule_deletion_service_spec.rbindex 59e86dfd506..d8de0f5bd87 100644--- a/spec/services/projects/schedule_deletion_service_spec.rb+++ b/spec/services/projects/schedule_deletion_service_spec.rb@@ -91,7 +91,7 @@ describe ::Projects::ScheduleDeletionService, type: :model doexpect(::Projects::DeleteProjectJob).to receive(:perform_later)- .with(user_id: user.id, project_id: project.id)+ .with(user: user, project: project)expect(subject).to be_successenddiff --git a/spec/workers/user_job_spec.rb b/spec/workers/user_job_spec.rbnew file mode 100644index 00000000000..fc7c91803bc--- /dev/null+++ b/spec/workers/user_job_spec.rb@@ -0,0 +1,63 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2021 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++require 'spec_helper'++describe UserJob do+ let(:test_job) do+ Class.new(::UserJob) do+ def execute(foo:)+ user.admin?+ end+ end+ end++ subject do+ test_job.new(user: user, foo: 'foo').perform_now+ end++ describe 'with system user' do+ let(:user) { User.system }+ it 'sets admin privileges' do+ expect(subject).to eq true++ # But does not retain admin privileges+ expect(user).not_to be_admin+ end+ end++ describe 'with a regular user' do+ let(:user) { FactoryBot.build_stubbed :user }+ it 'just uses that' do+ expect(subject).to eq false+ expect(user).not_to be_admin+ end+ end+end