Fixed issue #1933 - Can't find organization anymore after index rebuild.

This commit is contained in:
Martin Edenhofer 2018-04-13 09:11:48 +02:00
parent fbeccbc85c
commit 9791c6b72c
4 changed files with 183 additions and 38 deletions

View file

@ -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.

View file

@ -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

View file

@ -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

View file

@ -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