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

[Fix] Create rake task to fix missing notification settings (#11362)

Dombi Attila1691077d44b8
diff --git a/app/contracts/users/create_contract.rb b/app/contracts/users/create_contract.rb
index e7eeff62087..11e80033860 100644
--- a/app/contracts/users/create_contract.rb
+++ b/app/contracts/users/create_contract.rb
@@ -41,6 +41,7 @@ module Users
validate :authentication_defined
validate :type_is_user
validate :user_limit_not_exceeded
+ validate :notification_settings_present
private
@@ -50,6 +51,12 @@ module Users
end
end
+ def notification_settings_present
+ if model.notification_settings.empty?
+ errors.add :notification_settings, :blank
+ end
+ end
+
def authentication_defined
errors.add :password, :blank if model.active? && no_auth?
end
diff --git a/lib/tasks/fix_missing_notification_settings.rake b/lib/tasks/fix_missing_notification_settings.rake
new file mode 100644
index 00000000000..8fdd0366b02
--- /dev/null
+++ b/lib/tasks/fix_missing_notification_settings.rake
@@ -0,0 +1,44 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 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.
+#++
+
+namespace :migrations do
+ desc "Fix the missing notification settings"
+ task fix_missing_notification_settings: :environment do
+ ActiveRecord::Base.connection.execute <<~SQL.squish
+ INSERT INTO
+ notification_settings
+ (user_id)
+ SELECT
+ u.id
+ FROM
+ users u
+ WHERE type = 'User'
+ AND NOT EXISTS (SELECT * FROM notification_settings ns WHERE ns.user_id = u.id)
+ SQL
+ end
+end
diff --git a/spec/contracts/users/create_contract_spec.rb b/spec/contracts/users/create_contract_spec.rb
index 63f8a87c4b0..c2d83b7390b 100644
--- a/spec/contracts/users/create_contract_spec.rb
+++ b/spec/contracts/users/create_contract_spec.rb
@@ -43,7 +43,8 @@ describe Users::CreateContract do
login: user_login,
mail: user_mail,
password: user_password,
- password_confirmation: user_password_confirmation
+ password_confirmation: user_password_confirmation,
+ notification_settings: [NotificationSetting.new]
}
end