2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2012-04-16 11:57:33 +00:00
|
|
|
class Role < ApplicationModel
|
2019-01-28 06:04:05 +00:00
|
|
|
include CanBeImported
|
2017-05-02 15:21:13 +00:00
|
|
|
include HasActivityStreamLog
|
|
|
|
include ChecksClientNotification
|
2021-04-12 09:49:26 +00:00
|
|
|
include ChecksHtmlSanitized
|
2017-05-02 15:21:13 +00:00
|
|
|
include ChecksLatestChangeObserved
|
2017-06-16 20:43:09 +00:00
|
|
|
include HasGroups
|
2020-02-20 13:34:03 +00:00
|
|
|
include HasCollectionUpdate
|
|
|
|
include HasTicketCreateScreenImpact
|
2017-06-16 20:43:09 +00:00
|
|
|
|
|
|
|
include Role::Assets
|
2017-01-31 17:13:45 +00:00
|
|
|
|
2016-08-12 16:39:09 +00:00
|
|
|
has_and_belongs_to_many :users, after_add: :cache_update, after_remove: :cache_update
|
2019-01-15 12:55:59 +00:00
|
|
|
has_and_belongs_to_many :permissions,
|
|
|
|
before_add: %i[validate_agent_limit_by_permission validate_permissions],
|
|
|
|
after_add: :cache_update,
|
|
|
|
before_remove: :last_admin_check_by_permission,
|
|
|
|
after_remove: :cache_update
|
2020-06-22 09:57:45 +00:00
|
|
|
validates :name, presence: true
|
2016-08-12 16:39:09 +00:00
|
|
|
store :preferences
|
2013-12-26 21:51:25 +00:00
|
|
|
|
2019-01-15 12:55:59 +00:00
|
|
|
before_create :check_default_at_signup_permissions
|
|
|
|
before_update :last_admin_check_by_attribute, :validate_agent_limit_by_attributes, :check_default_at_signup_permissions
|
2016-08-12 16:39:09 +00:00
|
|
|
|
2018-06-22 12:06:14 +00:00
|
|
|
# ignore Users because this will lead to huge
|
|
|
|
# results for e.g. the Customer role
|
2017-06-16 20:43:09 +00:00
|
|
|
association_attributes_ignored :users
|
2017-01-31 17:13:45 +00:00
|
|
|
|
|
|
|
activity_stream_permission 'admin.role'
|
2016-08-12 16:39:09 +00:00
|
|
|
|
2021-04-12 09:49:26 +00:00
|
|
|
sanitized_html :note
|
|
|
|
|
2016-08-12 16:39:09 +00:00
|
|
|
=begin
|
|
|
|
|
2017-02-24 13:47:55 +00:00
|
|
|
grant permission to role
|
2016-08-12 16:39:09 +00:00
|
|
|
|
2017-02-24 13:47:55 +00:00
|
|
|
role.permission_grant('permission.key')
|
2016-08-12 16:39:09 +00:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
2017-02-24 13:47:55 +00:00
|
|
|
def permission_grant(key)
|
2016-08-12 16:39:09 +00:00
|
|
|
permission = Permission.lookup(name: key)
|
|
|
|
raise "Invalid permission #{key}" if !permission
|
|
|
|
return true if permission_ids.include?(permission.id)
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2020-09-30 09:07:01 +00:00
|
|
|
self.permission_ids = permission_ids.push permission.id # rubocop:disable Style/RedundantSelfAssignment
|
2016-08-12 16:39:09 +00:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
revoke permission of role
|
|
|
|
|
|
|
|
role.permission_revoke('permission.key')
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def permission_revoke(key)
|
|
|
|
permission = Permission.lookup(name: key)
|
|
|
|
raise "Invalid permission #{key}" if !permission
|
2020-09-30 09:07:01 +00:00
|
|
|
return true if permission_ids.exclude?(permission.id)
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-08-12 16:39:09 +00:00
|
|
|
self.permission_ids = self.permission_ids -= [permission.id]
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
get signup roles
|
|
|
|
|
|
|
|
Role.signup_roles
|
|
|
|
|
2016-12-08 14:06:54 +00:00
|
|
|
returns
|
2016-08-12 16:39:09 +00:00
|
|
|
|
|
|
|
[role1, role2, ...]
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.signup_roles
|
|
|
|
Role.where(active: true, default_at_signup: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
get signup role ids
|
|
|
|
|
|
|
|
Role.signup_role_ids
|
|
|
|
|
2016-12-08 14:06:54 +00:00
|
|
|
returns
|
2016-08-12 16:39:09 +00:00
|
|
|
|
|
|
|
[role1, role2, ...]
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.signup_role_ids
|
2018-03-15 09:11:39 +00:00
|
|
|
signup_roles.map(&:id)
|
2016-08-12 16:39:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
get all roles with permission
|
|
|
|
|
|
|
|
roles = Role.with_permissions('admin.session')
|
|
|
|
|
|
|
|
get all roles with permission "admin.session" or "ticket.agent"
|
|
|
|
|
|
|
|
roles = Role.with_permissions(['admin.session', 'ticket.agent'])
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
2017-02-15 12:29:25 +00:00
|
|
|
[role1, role2, ...]
|
2016-08-12 16:39:09 +00:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.with_permissions(keys)
|
2017-10-20 11:32:52 +00:00
|
|
|
permission_ids = Role.permission_ids_by_name(keys)
|
|
|
|
Role.joins(:roles_permissions).joins(:permissions).where(
|
|
|
|
'permissions_roles.permission_id IN (?) AND roles.active = ? AND permissions.active = ?', permission_ids, true, true
|
|
|
|
).distinct()
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
check if roles is with permission
|
|
|
|
|
|
|
|
role = Role.find(123)
|
|
|
|
role.with_permission?('admin.session')
|
|
|
|
|
|
|
|
get if role has permission of "admin.session" or "ticket.agent"
|
|
|
|
|
|
|
|
role.with_permission?(['admin.session', 'ticket.agent'])
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
true | false
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def with_permission?(keys)
|
|
|
|
permission_ids = Role.permission_ids_by_name(keys)
|
|
|
|
return true if Role.joins(:roles_permissions).joins(:permissions).where(
|
|
|
|
'roles.id = ? AND permissions_roles.permission_id IN (?) AND permissions.active = ?', id, permission_ids, true
|
|
|
|
).distinct().count.nonzero?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-10-20 11:32:52 +00:00
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.permission_ids_by_name(keys)
|
2018-03-28 05:33:41 +00:00
|
|
|
Array(keys).each_with_object([]) do |key, result|
|
2017-10-20 11:32:52 +00:00
|
|
|
::Permission.with_parents(key).each do |local_key|
|
|
|
|
permission = ::Permission.lookup(name: local_key)
|
2016-08-12 16:39:09 +00:00
|
|
|
next if !permission
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2018-03-28 05:33:41 +00:00
|
|
|
result.push permission.id
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
|
|
|
end
|
2016-08-12 16:39:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-01-15 12:55:59 +00:00
|
|
|
def validate_permissions(permission)
|
|
|
|
Rails.logger.debug { "self permission: #{permission.id}" }
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2019-01-15 12:55:59 +00:00
|
|
|
raise "Permission #{permission.name} is disabled" if permission.preferences[:disabled]
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2019-01-15 12:55:59 +00:00
|
|
|
permission.preferences[:not]
|
|
|
|
&.find { |name| name.in?(permissions.map(&:name)) }
|
|
|
|
&.tap { |conflict| raise "Permission #{permission} conflicts with #{conflict}" }
|
|
|
|
|
|
|
|
permissions.find { |p| p.preferences[:not]&.include?(permission.name) }
|
|
|
|
&.tap { |conflict| raise "Permission #{permission} conflicts with #{conflict}" }
|
2016-08-12 16:39:09 +00:00
|
|
|
end
|
|
|
|
|
2017-10-20 11:32:52 +00:00
|
|
|
def last_admin_check_by_attribute
|
|
|
|
return true if !will_save_change_to_attribute?('active')
|
|
|
|
return true if active != false
|
|
|
|
return true if !with_permission?(['admin', 'admin.user'])
|
|
|
|
raise Exceptions::UnprocessableEntity, 'Minimum one user needs to have admin permissions.' if last_admin_check_admin_count < 1
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-10-20 11:32:52 +00:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def last_admin_check_by_permission(permission)
|
|
|
|
return true if Setting.get('import_mode')
|
|
|
|
return true if permission.name != 'admin' && permission.name != 'admin.user'
|
|
|
|
raise Exceptions::UnprocessableEntity, 'Minimum one user needs to have admin permissions.' if last_admin_check_admin_count < 1
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-10-20 11:32:52 +00:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def last_admin_check_admin_count
|
|
|
|
admin_role_ids = Role.joins(:permissions).where(permissions: { name: ['admin', 'admin.user'], active: true }, roles: { active: true }).where.not(id: id).pluck(:id)
|
2017-12-13 11:17:57 +00:00
|
|
|
User.joins(:roles).where(roles: { id: admin_role_ids }, users: { active: true }).distinct().count
|
2017-10-20 11:32:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def validate_agent_limit_by_attributes
|
2018-09-12 04:23:48 +00:00
|
|
|
return true if Setting.get('system_agent_limit').blank?
|
2017-10-20 11:32:52 +00:00
|
|
|
return true if !will_save_change_to_attribute?('active')
|
|
|
|
return true if active != true
|
|
|
|
return true if !with_permission?('ticket.agent')
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-10-20 11:32:52 +00:00
|
|
|
ticket_agent_role_ids = Role.joins(:permissions).where(permissions: { name: 'ticket.agent', active: true }, roles: { active: true }).pluck(:id)
|
2017-12-13 11:17:57 +00:00
|
|
|
currents = User.joins(:roles).where(roles: { id: ticket_agent_role_ids }, users: { active: true }).distinct().pluck(:id)
|
|
|
|
news = User.joins(:roles).where(roles: { id: id }, users: { active: true }).distinct().pluck(:id)
|
2017-10-20 11:32:52 +00:00
|
|
|
count = currents.concat(news).uniq.count
|
2018-09-12 04:23:48 +00:00
|
|
|
raise Exceptions::UnprocessableEntity, 'Agent limit exceeded, please check your account settings.' if count > Setting.get('system_agent_limit').to_i
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-10-20 11:32:52 +00:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_agent_limit_by_permission(permission)
|
2018-09-12 04:23:48 +00:00
|
|
|
return true if Setting.get('system_agent_limit').blank?
|
2017-10-20 11:32:52 +00:00
|
|
|
return true if active != true
|
|
|
|
return true if permission.active != true
|
2017-06-16 22:53:20 +00:00
|
|
|
return true if permission.name != 'ticket.agent'
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-10-20 11:32:52 +00:00
|
|
|
ticket_agent_role_ids = Role.joins(:permissions).where(permissions: { name: 'ticket.agent' }, roles: { active: true }).pluck(:id)
|
2017-04-18 07:38:53 +00:00
|
|
|
ticket_agent_role_ids.push(id)
|
2017-12-13 11:17:57 +00:00
|
|
|
count = User.joins(:roles).where(roles: { id: ticket_agent_role_ids }, users: { active: true }).distinct().count
|
2018-09-12 04:23:48 +00:00
|
|
|
raise Exceptions::UnprocessableEntity, 'Agent limit exceeded, please check your account settings.' if count > Setting.get('system_agent_limit').to_i
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-06-16 22:53:20 +00:00
|
|
|
true
|
2017-04-18 07:38:53 +00:00
|
|
|
end
|
|
|
|
|
2018-01-26 18:57:27 +00:00
|
|
|
def check_default_at_signup_permissions
|
2020-10-07 12:05:07 +00:00
|
|
|
return true if !default_at_signup
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2020-10-07 12:05:07 +00:00
|
|
|
forbidden_permissions = permissions.reject(&:allow_signup)
|
|
|
|
return true if forbidden_permissions.blank?
|
|
|
|
|
|
|
|
raise Exceptions::UnprocessableEntity, "Cannot set default at signup when role has #{forbidden_permissions.join(', ')} permissions."
|
2018-01-26 18:57:27 +00:00
|
|
|
end
|
|
|
|
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|