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

Validate a separate params contract

Oliver Günther7908c7eebebb
diff --git a/app/contracts/params_contract.rb b/app/contracts/params_contract.rb
new file mode 100644
index 00000000000..849b7d5d3ba
--- /dev/null
+++ b/app/contracts/params_contract.rb
@@ -0,0 +1,39 @@
+#-- 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 COPYRIGHT and LICENSE files for more details.
+#++
+
+class ParamsContract < BaseContract
+ attr_reader :params
+
+ def initialize(model, user, params:, options: {})
+ super(model, user, options: options)
+
+ @params = params
+ end
+end
diff --git a/app/contracts/user_preferences/params_contract.rb b/app/contracts/user_preferences/params_contract.rb
new file mode 100644
index 00000000000..61b60eef21b
--- /dev/null
+++ b/app/contracts/user_preferences/params_contract.rb
@@ -0,0 +1,79 @@
+#-- 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 COPYRIGHT and LICENSE files for more details.
+#++
+
+module UserPreferences
+ class ParamsContract < ::ParamsContract
+ validate :only_one_global_setting,
+ if: -> { notifications.present? }
+
+ validate :global_email_alerts,
+ if: -> { notifications.present? }
+
+ protected
+
+ def only_one_global_setting
+ if has_more_than_one_global_channel?
+ errors.add :notification_settings, :only_one_global_setting
+ end
+ end
+
+ def global_email_alerts
+ if project_notifications.any?(method(:email_alerts_set?))
+ errors.add :notification_settings, :email_alerts_global
+ end
+ end
+
+ ##
+ # Check if the given notification hash has email-only settings set
+ def email_alerts_set?(notification_setting)
+ NotificationSetting.email_settings.any? do |setting|
+ notification_setting[setting] == true
+ end
+ end
+
+ def has_more_than_one_global_channel?
+ global_notifications
+ .group_by { |notification| notification[:channel] }
+ .any? { |_, items| items.count > 1 }
+ end
+
+ def global_notifications
+ notifications.select { |notification| notification[:project_id].nil? }
+ end
+
+ def project_notifications
+ notifications.select { |notification| notification[:project_id].present? }
+ end
+
+ def notifications
+ params[:notification_settings]
+ end
+ end
+end
diff --git a/app/models/notification_setting.rb b/app/models/notification_setting.rb
index 392df053b2f..37e36dfe36b 100644
--- a/app/models/notification_setting.rb
+++ b/app/models/notification_setting.rb
@@ -27,6 +27,13 @@ class NotificationSetting < ApplicationRecord
WORK_PACKAGE_PROCESSED,
WORK_PACKAGE_PRIORITIZED,
WORK_PACKAGE_SCHEDULED,
+ ALL,
+ *email_settings
+ ]
+ end
+
+ def self.email_settings
+ [
NEWS_ADDED,
NEWS_COMMENTED,
DOCUMENT_ADDED,
@@ -34,8 +41,7 @@ class NotificationSetting < ApplicationRecord
WIKI_PAGE_ADDED,
WIKI_PAGE_UPDATED,
MEMBERSHIP_ADDED,
- MEMBERSHIP_UPDATED,
- ALL
+ MEMBERSHIP_UPDATED
]
end
diff --git a/app/services/base_services/base_contracted.rb b/app/services/base_services/base_contracted.rb
index 6a9ac0403ff..d2a9b0a729e 100644
--- a/app/services/base_services/base_contracted.rb
+++ b/app/services/base_services/base_contracted.rb
@@ -58,8 +58,8 @@ module BaseServices
def perform(params = nil)
service_context do
- service_call = before_perform(params)
-
+ service_call = validate_params(params)
+ service_call = before_perform(params, service_call) if service_call.success?
service_call = validate_contract(service_call) if service_call.success?
service_call = after_validate(params, service_call) if service_call.success?
service_call = persist(service_call) if service_call.success?
@@ -69,7 +69,11 @@ module BaseServices
end
end
- def before_perform(_params)
+ def validate_params(_params)
+ ServiceResult.new(success: true, result: model)
+ end
+
+ def before_perform(*)
ServiceResult.new(success: true, result: model)
end
diff --git a/app/services/base_services/write.rb b/app/services/base_services/write.rb
index bc2b11be36f..505b430dd6b 100644
--- a/app/services/base_services/write.rb
+++ b/app/services/base_services/write.rb
@@ -49,7 +49,7 @@ module BaseServices
service_result
end
- def before_perform(params)
+ def before_perform(params, _service_result)
set_attributes(params)
end
diff --git a/app/services/projects/enabled_modules_service.rb b/app/services/projects/enabled_modules_service.rb
index fd0e6fc786f..f5b9d834f62 100644
--- a/app/services/projects/enabled_modules_service.rb
+++ b/app/services/projects/enabled_modules_service.rb
@@ -40,7 +40,7 @@ module Projects
private
- def before_perform(params)
+ def before_perform(params, _service_result)
model.enabled_module_names = params[:enabled_modules]
super
end
diff --git a/app/services/projects/schedule_deletion_service.rb b/app/services/projects/schedule_deletion_service.rb
index 72a83358123..d211daaf4a5 100644
--- a/app/services/projects/schedule_deletion_service.rb
+++ b/app/services/projects/schedule_deletion_service.rb
@@ -41,7 +41,7 @@ module Projects
private
- def before_perform(_params)
+ def before_perform(_params, _service_result)
Projects::ArchiveService
.new(user: user, model: model)
.call
diff --git a/app/services/user_preferences/update_service.rb b/app/services/user_preferences/update_service.rb
index 392ac1f93de..6f1f8102021 100644
--- a/app/services/user_preferences/update_service.rb
+++ b/app/services/user_preferences/update_service.rb
@@ -34,7 +34,15 @@ module UserPreferences
attr_accessor :notifications
- def before_perform(params)
+ def validate_params(params)
+ contract = ParamsContract.new(model, user, params: params)
+
+ ServiceResult.new success: contract.valid?,
+ errors: contract.errors,
+ result: model
+ end
+
+ def before_perform(params, _service_result)
self.notifications = params&.delete(:notification_settings)
super
diff --git a/app/services/users/update_service.rb b/app/services/users/update_service.rb
index eb768c7c87b..237821c7b04 100644
--- a/app/services/users/update_service.rb
+++ b/app/services/users/update_service.rb
@@ -34,12 +34,12 @@ module Users
protected
- def before_perform(params)
+ def before_perform(params, _service_result)
call_hook :service_update_user_before_save,
params: params,
user: model
- super(params)
+ super
end
def persist(service_result)
diff --git a/config/locales/en.yml b/config/locales/en.yml
index cdd44f13607..4a0627a9ae0 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -696,6 +696,10 @@ en:
no_notification_reason: 'cannot be blank as mail is chosen as a channel.'
reason_mail_digest:
no_notification_reason: 'cannot be blank as mail digest is chosen as a channel.'
+ notification_settings:
+ only_one_global_setting: 'There must only be one global notification setting.'
+ email_alerts_global: 'The email notification settings can only be set globally.'
+ format: "%{message}"
parse_schema_filter_params_service:
attributes:
base:
diff --git a/modules/bim/app/services/bim/bcf/issues/create_service.rb b/modules/bim/app/services/bim/bcf/issues/create_service.rb
index 0ddcbd47e47..2138d30c6bd 100644
--- a/modules/bim/app/services/bim/bcf/issues/create_service.rb
+++ b/modules/bim/app/services/bim/bcf/issues/create_service.rb
@@ -33,11 +33,11 @@ module Bim::Bcf
class CreateService < ::BaseServices::Create
private
- def before_perform(params)
+ def before_perform(params, service_result)
wp_call = get_work_package params
return wp_call if wp_call.failure?
- super issue_params(work_package: wp_call.result, params: params)
+ super issue_params(work_package: wp_call.result, params: params), service_result
end
def issue_params(work_package:, params:)
diff --git a/modules/bim/app/services/bim/bcf/issues/update_service.rb b/modules/bim/app/services/bim/bcf/issues/update_service.rb
index 66a98d37e9c..f17bde5e35e 100644
--- a/modules/bim/app/services/bim/bcf/issues/update_service.rb
+++ b/modules/bim/app/services/bim/bcf/issues/update_service.rb
@@ -33,7 +33,7 @@ module Bim::Bcf
class UpdateService < ::BaseServices::Update
private
- def before_perform(params)
+ def before_perform(params, service_result)
wp_call = ::WorkPackages::UpdateService
.new(model: model.work_package,
user: user,
@@ -43,7 +43,7 @@ module Bim::Bcf
if wp_call.success?
issue_params = params.slice(*Bim::Bcf::Issue::SETTABLE_ATTRIBUTES)
- super(issue_params)
+ super(issue_params, service_result)
else
wp_call
end
diff --git a/modules/bim/app/services/bim/ifc_models/update_service.rb b/modules/bim/app/services/bim/ifc_models/update_service.rb
index cfdd9835f5c..dc4167d591d 100644
--- a/modules/bim/app/services/bim/ifc_models/update_service.rb
+++ b/modules/bim/app/services/bim/ifc_models/update_service.rb
@@ -31,7 +31,7 @@ module Bim
class UpdateService < ::BaseServices::Update
protected
- def before_perform(params)
+ def before_perform(params, _service_result)
@ifc_attachment_updated = params[:ifc_attachment].present?
super
diff --git a/modules/documents/app/controllers/documents_controller.rb b/modules/documents/app/controllers/documents_controller.rb
index 60bccb37fb9..cc892c15083 100644
--- a/modules/documents/app/controllers/documents_controller.rb
+++ b/modules/documents/app/controllers/documents_controller.rb
@@ -108,14 +108,6 @@ class DocumentsController < ApplicationController
model: @document,
args: document_params
- if call.success?
- added = call.result
- .attachments
- .reject { |a| current_attachments.include?(a.id) }
-
- notify_attachments added
- end
-
redirect_to action: 'show', id: @document
end
@@ -124,14 +116,4 @@ class DocumentsController < ApplicationController
def document_params
params.fetch(:document, {}).permit('category_id', 'title', 'description')
end
-
- def notify_attachments(added)
- return if added.empty?
-
- users = added.first.container.recipients
- users.each do |user|
- UserMailer.attachments_added(user, added).deliver_later
- end
- end
-
end
diff --git a/spec/contracts/user_preferences/params_contract_spec.rb b/spec/contracts/user_preferences/params_contract_spec.rb
new file mode 100644
index 00000000000..0b1bec63c14
--- /dev/null
+++ b/spec/contracts/user_preferences/params_contract_spec.rb
@@ -0,0 +1,109 @@
+#-- 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 COPYRIGHT and LICENSE files for more details.
+#++
+
+require 'spec_helper'
+require 'contracts/shared/model_contract_shared_context'
+
+describe UserPreferences::ParamsContract do
+ include_context 'ModelContract shared context'
+
+ let(:current_user) { FactoryBot.build_stubbed(:user) }
+ let(:preference_user) { current_user }
+ let(:user_preference) do
+ FactoryBot.build_stubbed(:user_preference,
+ user: preference_user)
+ end
+ let(:params) do
+ {
+ hide_mail: true,
+ auto_hide_popups: true,
+ comments_sorting: 'desc',
+ daily_reminders: {
+ enabled: true,
+ times: %w[08:00:00+00:00 12:00:00+00:00]
+ },
+ time_zone: 'Brasilia',
+ warn_on_leaving_unsaved: true,
+ notification_settings: notification_settings
+ }
+ end
+ let(:contract) { described_class.new(user_preference, current_user, params: params) }
+
+ describe 'notification settings' do
+ context 'when multiple global settings' do
+ let(:notification_settings) do
+ [
+ { project_id: nil, mentioned: true },
+ { project_id: nil, mentioned: true }
+ ]
+ end
+
+ it_behaves_like 'contract is invalid', notification_settings: :only_one_global_setting
+ end
+
+ context 'when project settings with an email alert set' do
+ let(:notification_settings) do
+ [
+ { project_id: 1234, news_added: true }
+ ]
+ end
+
+ it_behaves_like 'contract is invalid', notification_settings: :email_alerts_global
+ end
+
+ context 'when global settings with an email alert set' do
+ let(:notification_settings) do
+ [
+ { project_id: nil, news_added: true }
+ ]
+ end
+
+ it_behaves_like 'contract is valid'
+ end
+
+ context 'when notification_settings empty' do
+ let(:params) do
+ {
+ hide_mail: true,
+ auto_hide_popups: true,
+ comments_sorting: 'desc',
+ daily_reminders: {
+ enabled: true,
+ times: %w[08:00:00+00:00 12:00:00+00:00]
+ },
+ time_zone: 'Brasilia',
+ warn_on_leaving_unsaved: true
+ }
+ end
+
+ it_behaves_like 'contract is valid'
+ end
+ end
+end
diff --git a/spec/models/setting_spec.rb b/spec/models/setting_spec.rb
index 16ebb1e9a14..5b093ba11a3 100644
--- a/spec/models/setting_spec.rb
+++ b/spec/models/setting_spec.rb
@@ -46,7 +46,7 @@ describe Setting, type: :model do
end
it 'allows anonymous users to access public information' do
- expect(described_class).to be_login_required
+ expect(described_class).not_to be_login_required
end
end
diff --git a/spec/services/user_preferences/update_service_spec.rb b/spec/services/user_preferences/update_service_spec.rb
index 861323afb30..cf100680e65 100644
--- a/spec/services/user_preferences/update_service_spec.rb
+++ b/spec/services/user_preferences/update_service_spec.rb
@@ -32,5 +32,24 @@ require 'spec_helper'
require 'services/base_services/behaves_like_update_service'
describe UserPreferences::UpdateService, type: :model do
- it_behaves_like 'BaseServices update service'
+ it_behaves_like 'BaseServices update service' do
+ let(:params_success) { true }
+ let(:params_errors) { ActiveModel::Errors.new({}) }
+ let(:params_contract) do
+ instance_double(UserPreferences::ParamsContract, valid?: params_success, errors: params_errors)
+ end
+
+ before do
+ allow(UserPreferences::ParamsContract).to receive(:new).and_return(params_contract)
+ end
+
+ context 'when the params contract is invalid' do
+ let(:params_success) { false }
+
+ it 'returns that error' do
+ expect(subject).to be_failure
+ expect(subject.errors).to eq(params_errors)
+ end
+ end
+ end
end