diff --git a/app/models/user.rb b/app/models/user.rb index 96e2c16d3..0360cee8c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -654,9 +654,9 @@ returns =begin -update/sync default preferences of users in a dedecated role +update/sync default preferences of users in a dedecated permissions - result = User.update_default_preferences('Agent', force) + result = User.update_default_preferences_by_permission('ticket.agent', force) returns @@ -664,24 +664,47 @@ returns =end - def self.update_default_preferences(role_name, force = false) + def self.update_default_preferences_by_permission(permission_name, force = false) + permission = Object.const_get('Permission').lookup(name: permission_name) + return if !permission + default = Rails.configuration.preferences_default_by_permission + return false if !default + default.deep_stringify_keys! + User.with_permissions(permission.name).each { |user| + next if !default[permission.name] + has_changed = false + default[permission.name].each { |key, value| + next if !force && user.preferences[key] + has_changed = true + user.preferences[key] = value + } + if has_changed + user.save! + end + } + true + end + +=begin + +update/sync default preferences of users in a dedecated role + + result = User.update_default_preferences_by_role('Agent', force) + +returns + + result = true # false + +=end + + def self.update_default_preferences_by_role(role_name, force = false) role = Role.lookup(name: role_name) + return if !role default = Rails.configuration.preferences_default_by_permission return false if !default default.deep_stringify_keys! role.permissions.each { |permission| - User.with_permissions(permission.name).each { |user| - next if !default[permission.name] - has_changed = false - default[permission.name].each { |key, value| - next if !force && user.preferences[key] - has_changed = true - user.preferences[key] = value - } - if has_changed - user.save! - end - } + User.update_default_preferences_by_permission(permission.name, force) } true end diff --git a/db/migrate/20160208000001_update_default_preferences.rb b/db/migrate/20160208000001_update_default_preferences.rb deleted file mode 100644 index d7f3760db..000000000 --- a/db/migrate/20160208000001_update_default_preferences.rb +++ /dev/null @@ -1,5 +0,0 @@ -class UpdateDefaultPreferences < ActiveRecord::Migration - def up - User.update_default_preferences('Agent') - end -end diff --git a/db/migrate/20160920000001_user_preferences_update.rb b/db/migrate/20160920000001_user_preferences_update.rb index 7dbf636d5..87a161d9e 100644 --- a/db/migrate/20160920000001_user_preferences_update.rb +++ b/db/migrate/20160920000001_user_preferences_update.rb @@ -4,6 +4,6 @@ class UserPreferencesUpdate < ActiveRecord::Migration # return if it's a new setup return if !Setting.find_by(name: 'system_init_done') - User.update_default_preferences('Agent') + User.update_default_preferences_by_permission('ticket.agent') end end