forked from zammad/zammad
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Maintenance: Add assets level to have different data sets based on pe…
…rmissions
- Loading branch information
1 parent
acc93a2
commit 867b36b
Showing
11 changed files
with
284 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/ | ||
|
||
class Group | ||
module Assets | ||
extend ActiveSupport::Concern | ||
|
||
def filter_unauthorized_attributes(attributes) | ||
return super if UserInfo.assets.blank? || UserInfo.assets.agent? | ||
|
||
attributes = super | ||
attributes.slice('id', 'name', 'active') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/ | ||
|
||
class UserInfo::Assets | ||
LEVEL_CUSTOMER = 1 | ||
LEVEL_AGENT = 2 | ||
LEVEL_ADMIN = 3 | ||
|
||
attr_accessor :current_user_id, :level, :filter_attributes, :user | ||
|
||
def initialize(current_user_id) | ||
@current_user_id = current_user_id | ||
@user = User.find_by(id: current_user_id) if current_user_id.present? | ||
|
||
set_level | ||
end | ||
|
||
def admin? | ||
check_level?(UserInfo::Assets::LEVEL_ADMIN) | ||
end | ||
|
||
def agent? | ||
check_level?(UserInfo::Assets::LEVEL_AGENT) | ||
end | ||
|
||
def customer? | ||
check_level?(UserInfo::Assets::LEVEL_CUSTOMER) | ||
end | ||
|
||
def set_level | ||
if user.blank? | ||
self.level = nil | ||
return | ||
end | ||
|
||
self.level = UserInfo::Assets::LEVEL_CUSTOMER | ||
Permission.where(id: user.permissions_with_child_ids).each do |permission| | ||
case permission.name | ||
when %r{^admin\.} | ||
self.level = UserInfo::Assets::LEVEL_ADMIN | ||
break | ||
when 'ticket.agent' | ||
self.level = UserInfo::Assets::LEVEL_AGENT | ||
end | ||
end | ||
end | ||
|
||
def check_level?(check) | ||
return true if user.blank? | ||
|
||
level >= check | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/ | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe 'Assets', type: :system, db_strategy: :reset do | ||
let(:organization) { create(:organization, note: 'hello') } | ||
let(:customer) { create(:customer, organization: organization, note: 'hello', last_login: Time.zone.now, login_failed: 1) } | ||
let(:agent) { create(:agent, groups: [Group.find_by(name: 'Users')], note: 'hello', last_login: Time.zone.now, login_failed: 1) } | ||
let(:admin) { create(:admin, groups: [Group.find_by(name: 'Users')], note: 'hello', last_login: Time.zone.now, login_failed: 1) } | ||
let(:ticket) { create(:ticket, owner: agent, group: Group.find_by(name: 'Users'), customer: customer, created_by: admin) } | ||
|
||
context 'groups' do | ||
def group_note | ||
page.execute_script('return App.Group.first().note') | ||
end | ||
|
||
describe 'when customer', authenticated_as: :customer do | ||
it 'can not access group details' do | ||
expect(group_note).to be nil | ||
end | ||
end | ||
|
||
describe 'when agent', authenticated_as: :agent do | ||
it 'can access group details' do | ||
expect(group_note).not_to be nil | ||
end | ||
end | ||
|
||
describe 'when admin', authenticated_as: :admin do | ||
it 'can access group details' do | ||
expect(group_note).not_to be nil | ||
end | ||
end | ||
end | ||
|
||
context 'organizations' do | ||
def organization_note | ||
page.execute_script("return App.Organization.find(#{organization.id}).note") | ||
end | ||
|
||
before do | ||
visit "#ticket/zoom/#{ticket.id}" | ||
end | ||
|
||
describe 'when customer', authenticated_as: :customer do | ||
it 'can not access organization details' do | ||
expect(organization_note).to be nil | ||
end | ||
end | ||
|
||
describe 'when agent', authenticated_as: :agent do | ||
it 'can access organization details' do | ||
expect(organization_note).not_to be nil | ||
end | ||
end | ||
|
||
describe 'when admin', authenticated_as: :admin do | ||
it 'can access organization details' do | ||
expect(organization_note).not_to be nil | ||
end | ||
end | ||
end | ||
|
||
context 'roles' do | ||
def role_name | ||
page.execute_script('return App.Role.first().name') | ||
end | ||
|
||
before do | ||
visit "#ticket/zoom/#{ticket.id}" | ||
end | ||
|
||
describe 'when customer', authenticated_as: :customer do | ||
it 'can not access role details' do | ||
expect(role_name).to eq('Role_1') | ||
end | ||
end | ||
|
||
describe 'when agent', authenticated_as: :agent do | ||
it 'can access role details' do | ||
expect(role_name).not_to eq('Role_1') | ||
end | ||
end | ||
|
||
describe 'when admin', authenticated_as: :admin do | ||
it 'can access role details' do | ||
expect(role_name).not_to eq('Role_1') | ||
end | ||
end | ||
end | ||
|
||
context 'users' do | ||
def customer_email | ||
page.execute_script("return App.User.find(#{customer.id}).email") | ||
end | ||
|
||
def customer_note | ||
page.execute_script("return App.User.find(#{customer.id}).note") | ||
end | ||
|
||
def owner_firstname | ||
page.execute_script("return App.User.find(#{agent.id}).firstname") | ||
end | ||
|
||
def owner_details | ||
[ | ||
page.execute_script("return App.User.find(#{agent.id}).last_login"), | ||
page.execute_script("return App.User.find(#{agent.id}).login_failed"), | ||
page.execute_script("return App.User.find(#{agent.id}).email"), | ||
page.execute_script("return App.User.find(#{agent.id}).note"), | ||
].compact | ||
end | ||
|
||
before do | ||
visit "#ticket/zoom/#{ticket.id}" | ||
end | ||
|
||
describe 'when customer', authenticated_as: :customer do | ||
it 'can access customer email' do | ||
expect(customer_email).not_to be nil | ||
end | ||
|
||
it 'can not access customer note' do | ||
expect(customer_note).to be nil | ||
end | ||
|
||
it 'can not access owner details' do | ||
expect(owner_details).to be_empty | ||
end | ||
|
||
it 'can access owner firstname' do | ||
expect(owner_firstname).not_to be nil | ||
end | ||
end | ||
|
||
describe 'when agent', authenticated_as: :agent do | ||
it 'can access customer email' do | ||
expect(customer_email).not_to be nil | ||
end | ||
|
||
it 'can access customer note' do | ||
expect(customer_note).not_to be nil | ||
end | ||
|
||
it 'can access owner details' do | ||
expect(owner_details).not_to be_empty | ||
end | ||
|
||
it 'can access owner firstname' do | ||
expect(owner_firstname).not_to be nil | ||
end | ||
end | ||
|
||
describe 'when admin', authenticated_as: :admin do | ||
it 'can access customer email' do | ||
expect(customer_email).not_to be nil | ||
end | ||
|
||
it 'can access customer note' do | ||
expect(customer_note).not_to be nil | ||
end | ||
|
||
it 'can access owner details' do | ||
expect(owner_details).not_to be_empty | ||
end | ||
|
||
it 'can access owner firstname' do | ||
expect(owner_firstname).not_to be nil | ||
end | ||
end | ||
end | ||
end |