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)
|
|
|
|
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)
|
|
|
|
if @local_user.role_ids == user[:role_ids]
|
|
|
|
user.delete(:role_ids)
|
|
|
|
end
|
|
|
|
|
|
|
|
log "update User.find_by(id: #{user[:id]})"
|
|
|
|
@local_user.update_attributes(user)
|
|
|
|
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?
|
|
|
|
"#{login}_#{user[:id]}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def map(user)
|
|
|
|
{
|
|
|
|
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']
|
|
|
|
"{sha2}#{user['UserPw']}"
|
2016-11-25 16:10:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def group_ids(user)
|
|
|
|
result = []
|
|
|
|
queues = Import::OTRS::Requester.load('Queue')
|
|
|
|
queues.each { |queue|
|
|
|
|
|
|
|
|
permissions = user['GroupIDs'][ queue['GroupID'] ]
|
|
|
|
|
|
|
|
next if !permissions
|
|
|
|
next if !permissions.include?('rw')
|
|
|
|
|
|
|
|
result.push queue['QueueID']
|
|
|
|
}
|
|
|
|
|
|
|
|
# lookup by roles
|
|
|
|
|
|
|
|
# roles of user
|
|
|
|
# groups of roles
|
|
|
|
# queues of group
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def role_ids(user)
|
|
|
|
local_role_ids = []
|
|
|
|
roles(user).each { |role|
|
|
|
|
role_lookup = Role.lookup(name: role)
|
|
|
|
next if !role_lookup
|
|
|
|
local_role_ids.push role_lookup.id
|
|
|
|
}
|
|
|
|
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 = []
|
|
|
|
groups.each { |group|
|
2016-12-06 12:19:56 +00:00
|
|
|
result += groups_from_otrs_group(role_object, group)
|
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 = []
|
2016-12-06 12:19:56 +00:00
|
|
|
return result if role_object['GroupIDs'].empty?
|
|
|
|
permissions = role_object['GroupIDs'][ group['ID'] ]
|
2016-11-25 16:10:37 +00:00
|
|
|
|
|
|
|
return result if !permissions
|
|
|
|
|
|
|
|
if group['Name'] == 'admin' && permissions.include?('rw')
|
|
|
|
result.push 'Admin'
|
|
|
|
end
|
|
|
|
|
|
|
|
return result if group['Name'] !~ /^(stats|report)/
|
|
|
|
return result if !( permissions.include?('ro') || permissions.include?('rw') )
|
|
|
|
|
|
|
|
result.push 'Report'
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def groups_from_otrs_roles(user)
|
|
|
|
result = []
|
|
|
|
roles = Import::OTRS::Requester.load('Role')
|
|
|
|
roles.each { |role|
|
|
|
|
next if !user['RoleIDs'].include?(role['ID'])
|
2016-12-06 12:19:56 +00:00
|
|
|
result += groups_from_otrs_groups(role['GroupIDs'])
|
2016-11-25 16:10:37 +00:00
|
|
|
}
|
|
|
|
result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|