2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2016-11-25 16:10:37 +00:00
|
|
|
module Import
|
|
|
|
module OTRS
|
|
|
|
class User
|
|
|
|
include Import::Helper
|
|
|
|
include Import::OTRS::Helper
|
|
|
|
|
|
|
|
MAPPING = {
|
|
|
|
ChangeTime: :updated_at,
|
|
|
|
CreateTime: :created_at,
|
|
|
|
CreateBy: :created_by_id,
|
|
|
|
ChangeBy: :updated_by_id,
|
|
|
|
UserID: :id,
|
|
|
|
Comment: :note,
|
|
|
|
UserEmail: :email,
|
|
|
|
UserFirstname: :firstname,
|
|
|
|
UserLastname: :lastname,
|
|
|
|
UserLogin: :login,
|
|
|
|
}.freeze
|
|
|
|
|
|
|
|
def initialize(user)
|
|
|
|
import(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def import(user)
|
|
|
|
create_or_update(map(user))
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_or_update(user)
|
|
|
|
ensure_unique_login(user)
|
|
|
|
return if updated?(user)
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-11-25 16:10:37 +00:00
|
|
|
create(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def updated?(user)
|
|
|
|
@local_user = ::User.find_by(id: user[:id])
|
|
|
|
return false if !@local_user
|
|
|
|
|
|
|
|
# only update roles if different (reduce sql statements)
|
2017-11-23 08:09:44 +00:00
|
|
|
if user[:role_ids]&.sort == @local_user.role_ids.sort
|
2016-11-25 16:10:37 +00:00
|
|
|
user.delete(:role_ids)
|
|
|
|
end
|
|
|
|
|
|
|
|
log "update User.find_by(id: #{user[:id]})"
|
2017-09-11 11:16:08 +00:00
|
|
|
@local_user.update!(user)
|
2016-11-25 16:10:37 +00:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def create(user)
|
|
|
|
log "add User.find_by(id: #{user[:id]})"
|
|
|
|
@local_user = ::User.new(user)
|
|
|
|
@local_user.id = user[:id]
|
|
|
|
@local_user.save
|
|
|
|
reset_primary_key_sequence('users')
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_unique_login(user)
|
|
|
|
user[:login] = unique_login(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def unique_login(user)
|
|
|
|
login = user[:login]
|
|
|
|
return login if ::User.where('login = ? AND id != ?', login.downcase, user[:id]).count.zero?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-11-25 16:10:37 +00:00
|
|
|
"#{login}_#{user[:id]}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def map(user)
|
2017-05-14 16:33:30 +00:00
|
|
|
mapped = map_default(user)
|
|
|
|
mapped[:email].downcase!
|
2017-11-24 16:49:40 +00:00
|
|
|
mapped[:login].downcase!
|
2017-05-14 16:33:30 +00:00
|
|
|
mapped
|
|
|
|
end
|
|
|
|
|
|
|
|
def map_default(user)
|
2016-11-25 16:10:37 +00:00
|
|
|
{
|
|
|
|
created_by_id: 1,
|
|
|
|
updated_by_id: 1,
|
|
|
|
active: active?(user),
|
|
|
|
source: 'OTRS Import',
|
|
|
|
role_ids: role_ids(user),
|
|
|
|
group_ids: group_ids(user),
|
|
|
|
password: password(user),
|
|
|
|
}
|
|
|
|
.merge(from_mapping(user))
|
|
|
|
end
|
|
|
|
|
|
|
|
def password(user)
|
2016-11-28 15:30:33 +00:00
|
|
|
return if !user['UserPw']
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-11-28 15:30:33 +00:00
|
|
|
"{sha2}#{user['UserPw']}"
|
2016-11-25 16:10:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def group_ids(user)
|
|
|
|
result = []
|
|
|
|
queues = Import::OTRS::Requester.load('Queue')
|
2017-10-01 12:25:52 +00:00
|
|
|
queues.each do |queue|
|
2016-11-25 16:10:37 +00:00
|
|
|
|
|
|
|
permissions = user['GroupIDs'][ queue['GroupID'] ]
|
2018-01-23 09:09:20 +00:00
|
|
|
permissions ||= user['GroupIDs'][ queue['GroupID'].to_s ]
|
2016-11-25 16:10:37 +00:00
|
|
|
|
|
|
|
next if !permissions
|
2020-09-30 09:07:01 +00:00
|
|
|
next if permissions.exclude?('rw')
|
2016-11-25 16:10:37 +00:00
|
|
|
|
|
|
|
result.push queue['QueueID']
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-11-25 16:10:37 +00:00
|
|
|
|
|
|
|
# lookup by roles
|
|
|
|
|
|
|
|
# roles of user
|
|
|
|
# groups of roles
|
|
|
|
# queues of group
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def role_ids(user)
|
|
|
|
local_role_ids = []
|
2017-10-01 12:25:52 +00:00
|
|
|
roles(user).each do |role|
|
2016-11-25 16:10:37 +00:00
|
|
|
role_lookup = Role.lookup(name: role)
|
|
|
|
next if !role_lookup
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-11-25 16:10:37 +00:00
|
|
|
local_role_ids.push role_lookup.id
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-11-25 16:10:37 +00:00
|
|
|
local_role_ids
|
|
|
|
end
|
|
|
|
|
|
|
|
def roles(user)
|
|
|
|
local_roles = ['Agent']
|
|
|
|
local_roles += groups_from_otrs_groups(user)
|
|
|
|
local_roles += groups_from_otrs_roles(user)
|
|
|
|
local_roles.uniq
|
|
|
|
end
|
|
|
|
|
2016-12-06 12:19:56 +00:00
|
|
|
def groups_from_otrs_groups(role_object)
|
2016-11-25 16:10:37 +00:00
|
|
|
groups = Import::OTRS::Requester.load('Group')
|
2016-12-06 12:19:56 +00:00
|
|
|
groups_from_groups(role_object, groups)
|
2016-11-25 16:10:37 +00:00
|
|
|
end
|
|
|
|
|
2016-12-06 12:19:56 +00:00
|
|
|
def groups_from_groups(role_object, groups)
|
2016-11-25 16:10:37 +00:00
|
|
|
result = []
|
2017-10-01 12:25:52 +00:00
|
|
|
groups.each do |group|
|
2016-12-06 12:19:56 +00:00
|
|
|
result += groups_from_otrs_group(role_object, group)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-11-25 16:10:37 +00:00
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2016-12-06 12:19:56 +00:00
|
|
|
def groups_from_otrs_group(role_object, group)
|
2016-11-25 16:10:37 +00:00
|
|
|
result = []
|
2018-01-23 09:08:07 +00:00
|
|
|
return result if role_object.blank?
|
2017-11-23 08:09:44 +00:00
|
|
|
return result if role_object['GroupIDs'].blank?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-12-06 12:19:56 +00:00
|
|
|
permissions = role_object['GroupIDs'][ group['ID'] ]
|
2019-11-18 15:49:22 +00:00
|
|
|
permissions ||= role_object['GroupIDs'][ group['ID'].to_s ]
|
2016-11-25 16:10:37 +00:00
|
|
|
|
|
|
|
return result if !permissions
|
|
|
|
|
|
|
|
if group['Name'] == 'admin' && permissions.include?('rw')
|
|
|
|
result.push 'Admin'
|
|
|
|
end
|
|
|
|
|
2021-05-12 11:37:44 +00:00
|
|
|
return result if !group['Name'].match?(%r{^(stats|report)})
|
2021-07-16 13:38:01 +00:00
|
|
|
return result if !(permissions.include?('ro') || permissions.include?('rw'))
|
2016-11-25 16:10:37 +00:00
|
|
|
|
|
|
|
result.push 'Report'
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def groups_from_otrs_roles(user)
|
|
|
|
result = []
|
|
|
|
roles = Import::OTRS::Requester.load('Role')
|
2017-10-01 12:25:52 +00:00
|
|
|
roles.each do |role|
|
2020-09-30 09:07:01 +00:00
|
|
|
next if user['RoleIDs'].exclude?(role['ID'])
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2018-01-23 09:08:07 +00:00
|
|
|
result += groups_from_otrs_groups(role)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-11-25 16:10:37 +00:00
|
|
|
result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|