Improved agent limit detection (do not complain if role with ticket.agent permission is assigned and agent already owns a role with ticket.agent permission).

This commit is contained in:
Martin Edenhofer 2017-11-17 15:39:56 +01:00
parent eb5ad7d9fa
commit 17ce2dcbde
2 changed files with 69 additions and 2 deletions

View file

@ -1036,8 +1036,22 @@ raise 'Minimum one user need to have admin permissions'
return true if !role.with_permission?('ticket.agent') return true if !role.with_permission?('ticket.agent')
ticket_agent_role_ids = Role.joins(:permissions).where(permissions: { name: 'ticket.agent', active: true }, roles: { active: true }).pluck(:id) ticket_agent_role_ids = Role.joins(:permissions).where(permissions: { name: 'ticket.agent', active: true }, roles: { active: true }).pluck(:id)
count = User.joins(:roles).where(roles: { id: ticket_agent_role_ids }, users: { active: true }).count count = User.joins(:roles).where(roles: { id: ticket_agent_role_ids }, users: { active: true }).count
# if new added role is a ticket.agent role
if ticket_agent_role_ids.include?(role.id) if ticket_agent_role_ids.include?(role.id)
count += 1
# if user already has a ticket.agent role
hint = false
role_ids.each do |locale_role_id|
next if !ticket_agent_role_ids.include?(locale_role_id)
hint = true
break
end
# user has not already a ticket.agent role
if hint == false
count += 1
end
end end
raise Exceptions::UnprocessableEntity, 'Agent limit exceeded, please check your account settings.' if count > Setting.get('system_agent_limit') raise Exceptions::UnprocessableEntity, 'Agent limit exceeded, please check your account settings.' if count > Setting.get('system_agent_limit')
true true

View file

@ -2,7 +2,7 @@
require 'test_helper' require 'test_helper'
class RoleValidateAgentLimit < ActiveSupport::TestCase class RoleValidateAgentLimit < ActiveSupport::TestCase
test 'role_validate_agent_limit' do test 'role validate agent limit' do
agent_max = User.with_permissions('ticket.agent').count agent_max = User.with_permissions('ticket.agent').count
UserInfo.current_user_id = 1 UserInfo.current_user_id = 1
@ -70,4 +70,57 @@ class RoleValidateAgentLimit < ActiveSupport::TestCase
role_agent_limit_fail.destroy! role_agent_limit_fail.destroy!
Setting.set('system_agent_limit', nil) Setting.set('system_agent_limit', nil)
end end
test 'role validate agent limit - 1 user 2 ticket.agent roles' do
agent_max = User.with_permissions('ticket.agent').count
UserInfo.current_user_id = 1
Setting.set('system_agent_limit', agent_max + 1)
permission_ticket_agent = Permission.find_by(name: 'ticket.agent')
role_agent_limit1 = Role.create!(
name: 'agent-limit-test1',
note: 'agent-limit-test1 Role.',
permissions: [permission_ticket_agent],
active: true,
)
role_agent_limit2 = Role.create!(
name: 'agent-limit-test2',
note: 'agent-limit-test2 Role.',
permissions: [permission_ticket_agent],
active: true,
)
user1 = User.create!(
firstname: 'Firstname',
lastname: 'Lastname',
email: 'some-agentlimit-role@example.com',
login: 'some-agentlimit-role@example.com',
roles: [role_agent_limit1, role_agent_limit2],
active: true,
)
user1.roles = Role.where(name: %w(Admin Agent))
user1.role_ids = [Role.find_by(name: 'Agent').id]
user1.role_ids = [Role.find_by(name: 'Agent').id, role_agent_limit1.id]
assert_raises(Exceptions::UnprocessableEntity) do
user2 = User.create!(
firstname: 'Firstname2',
lastname: 'Lastname2',
email: 'some-agentlimit-role-2@example.com',
login: 'some-agentlimit-role-2@example.com',
roles: [role_agent_limit1],
active: true,
)
end
role_agent_limit1.destroy!
role_agent_limit2.destroy!
Setting.set('system_agent_limit', nil)
end
end end