From 9791c6b72cd011c5abbac72d4644aea43e0b0786 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Fri, 13 Apr 2018 09:11:48 +0200 Subject: [PATCH] Fixed issue #1933 - Can't find organization anymore after index rebuild. --- app/models/user.rb | 20 +++- ...nup_user_preferences_notification_sound.rb | 36 ------ ...up_user_preferences_notification_sound2.rb | 57 +++++++++ test/unit/user_test.rb | 108 ++++++++++++++++++ 4 files changed, 183 insertions(+), 38 deletions(-) delete mode 100644 db/migrate/20180223000001_cleanup_user_preferences_notification_sound.rb create mode 100644 db/migrate/20180410000001_cleanup_user_preferences_notification_sound2.rb diff --git a/app/models/user.rb b/app/models/user.rb index 868920364..b30329d13 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -46,8 +46,8 @@ class User < ApplicationModel belongs_to :organization, inverse_of: :members before_validation :check_name, :check_email, :check_login, :check_mail_delivery_failed, :ensure_uniq_email, :ensure_password, :ensure_roles, :ensure_identifier - before_create :check_preferences_default, :validate_ooo, :domain_based_assignment, :set_locale - before_update :check_preferences_default, :validate_ooo, :reset_login_failed, :validate_agent_limit_by_attributes, :last_admin_check_by_attribute + before_create :check_preferences_default, :validate_preferences, :validate_ooo, :domain_based_assignment, :set_locale + before_update :check_preferences_default, :validate_preferences, :validate_ooo, :reset_login_failed, :validate_agent_limit_by_attributes, :last_admin_check_by_attribute after_create :avatar_for_email_check after_update :avatar_for_email_check before_destroy :avatar_destroy, :user_device_destroy, :cit_caller_id_destroy, :task_destroy @@ -1001,6 +1001,22 @@ returns true end + def validate_preferences + return true if !changes + return true if !changes['preferences'] + return true if preferences.blank? + return true if !preferences[:notification_sound] + return true if !preferences[:notification_sound][:enabled] + if preferences[:notification_sound][:enabled] == 'true' + preferences[:notification_sound][:enabled] = true + elsif preferences[:notification_sound][:enabled] == 'false' + preferences[:notification_sound][:enabled] = false + end + class_name = preferences[:notification_sound][:enabled].class.to_s + raise Exceptions::UnprocessableEntity, "preferences.notification_sound.enabled need to be an boolean, but it was a #{class_name}" if class_name != 'TrueClass' && class_name != 'FalseClass' + true + end + =begin checks if the current user is the last one with admin permissions. diff --git a/db/migrate/20180223000001_cleanup_user_preferences_notification_sound.rb b/db/migrate/20180223000001_cleanup_user_preferences_notification_sound.rb deleted file mode 100644 index b7b4b71a4..000000000 --- a/db/migrate/20180223000001_cleanup_user_preferences_notification_sound.rb +++ /dev/null @@ -1,36 +0,0 @@ -class CleanupUserPreferencesNotificationSound < ActiveRecord::Migration[5.1] - def up - User.with_permissions('ticket.agent').each do |user| - local_to_h!(user.preferences) - user.save! - end - - User.with_permissions('ticket.agent').each do |user| - next if !user.preferences - next if !user.preferences[:notification_sound] - next if !user.preferences[:notification_sound][:enabled] - if user.preferences[:notification_sound][:enabled] == 'true' - user.preferences[:notification_sound][:enabled] = true - user.save! - next - end - next if user.preferences[:notification_sound][:enabled] != 'false' - user.preferences[:notification_sound][:enabled] = false - user.save! - next - end - end - - def local_to_h!(value) - if value.class == ActionController::Parameters - value = value.permit!.to_h - end - if value.class == Hash || value.class == ActiveSupport::HashWithIndifferentAccess - value.each_key do |local_key| - value[local_key] = local_to_h!(value[local_key]) - end - end - value - end - -end diff --git a/db/migrate/20180410000001_cleanup_user_preferences_notification_sound2.rb b/db/migrate/20180410000001_cleanup_user_preferences_notification_sound2.rb new file mode 100644 index 000000000..c1f3ff976 --- /dev/null +++ b/db/migrate/20180410000001_cleanup_user_preferences_notification_sound2.rb @@ -0,0 +1,57 @@ +class CleanupUserPreferencesNotificationSound2 < ActiveRecord::Migration[5.1] + + def local_to_h!(value) + if value.class == ActionController::Parameters + value = value.permit!.to_h + end + if value.class == Hash || value.class == ActiveSupport::HashWithIndifferentAccess + value.each_key do |local_key| + value[local_key] = local_to_h!(value[local_key]) + end + end + value + end + + def local_clear_preferences(user) + return false if !user + return false if !user.preferences + return false if !user.preferences[:notification_sound] + return false if !user.preferences[:notification_sound][:enabled] + if user.preferences[:notification_sound][:enabled] == 'true' + user.preferences[:notification_sound][:enabled] = true + user.save! + return true + end + return false if user.preferences[:notification_sound][:enabled] != 'false' + user.preferences[:notification_sound][:enabled] = false + user.save! + true + end + + def up + User.with_permissions('ticket.agent').each do |user| + local_to_h!(user.preferences) + user.save! + end + + items = SearchIndexBackend.search('preferences.notification_sound.enabled:*', 3000, 'User') || [] + items.each do |item| + next if !item[:id] + user = User.find_by(id: item[:id]) + local_to_h!(user.preferences) + local_clear_preferences(user) + end + + Organization.all.each do |organization| + organization.members.each do |user| + local_to_h!(user.preferences) + local_clear_preferences(user) + end + end + + Delayed::Job.all.each do |job| + Delayed::Worker.new.run(job) + end + end + +end diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index 6f9299a18..5e9b9b383 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -1063,4 +1063,112 @@ class UserTest < ActiveSupport::TestCase assert_equal(1, User.group_access(group.id, 'full').count) end + test 'preferences[:notification_sound][:enabled] value check' do + roles = Role.where(name: 'Agent') + agent1 = User.create!( + login: "agent-default-preferences-1#{name}@example.com", + firstname: 'vaild_agent_group_permission-1', + lastname: "Agent#{name}", + email: "agent-default-preferences-1#{name}@example.com", + password: 'agentpw', + active: true, + roles: roles, + preferences: { + notification_sound: { + enabled: true, + } + }, + updated_by_id: 1, + created_by_id: 1, + ) + assert_equal(true, agent1.preferences[:notification_sound][:enabled]) + + agent2 = User.create!( + login: "agent-default-preferences-2#{name}@example.com", + firstname: 'vaild_agent_group_permission-2', + lastname: "Agent#{name}", + email: "agent-default-preferences-2#{name}@example.com", + password: 'agentpw', + active: true, + roles: roles, + preferences: { + notification_sound: { + enabled: false, + } + }, + updated_by_id: 1, + created_by_id: 1, + ) + assert_equal(false, agent2.preferences[:notification_sound][:enabled]) + + agent3 = User.create!( + login: "agent-default-preferences-3#{name}@example.com", + firstname: 'vaild_agent_group_permission-3', + lastname: "Agent#{name}", + email: "agent-default-preferences-3#{name}@example.com", + password: 'agentpw', + active: true, + roles: roles, + preferences: { + notification_sound: { + enabled: true, + } + }, + updated_by_id: 1, + created_by_id: 1, + ) + assert_equal(true, agent3.preferences[:notification_sound][:enabled]) + agent3.preferences[:notification_sound][:enabled] = 'false' + agent3.save! + agent3.reload + assert_equal(false, agent3.preferences[:notification_sound][:enabled]) + + agent4 = User.create!( + login: "agent-default-preferences-4#{name}@example.com", + firstname: 'vaild_agent_group_permission-4', + lastname: "Agent#{name}", + email: "agent-default-preferences-4#{name}@example.com", + password: 'agentpw', + active: true, + roles: roles, + preferences: { + notification_sound: { + enabled: false, + } + }, + updated_by_id: 1, + created_by_id: 1, + ) + assert_equal(false, agent4.preferences[:notification_sound][:enabled]) + agent4.preferences[:notification_sound][:enabled] = 'true' + agent4.save! + agent4.reload + assert_equal(true, agent4.preferences[:notification_sound][:enabled]) + + agent4.preferences[:notification_sound][:enabled] = 'invalid' + assert_raises(Exceptions::UnprocessableEntity) do + agent4.save! + end + + assert_raises(Exceptions::UnprocessableEntity) do + User.create!( + login: "agent-default-preferences-5#{name}@example.com", + firstname: 'vaild_agent_group_permission-5', + lastname: "Agent#{name}", + email: "agent-default-preferences-5#{name}@example.com", + password: 'agentpw', + active: true, + roles: roles, + preferences: { + notification_sound: { + enabled: 'invalid string', + } + }, + updated_by_id: 1, + created_by_id: 1, + ) + end + + end + end