Import also organisations and group permissions of agents.
This commit is contained in:
parent
9fd93852c2
commit
2eb413dfd0
1 changed files with 142 additions and 15 deletions
|
@ -240,12 +240,22 @@ module Import::OTRS2
|
||||||
priority(records)
|
priority(records)
|
||||||
|
|
||||||
# create groups
|
# create groups
|
||||||
records = load('Queue')
|
queues = load('Queue')
|
||||||
ticket_group(records)
|
ticket_group(queues)
|
||||||
|
|
||||||
|
# get agents groups
|
||||||
|
groups = load('Group')
|
||||||
|
|
||||||
|
# get agents roles
|
||||||
|
roles = load('Role')
|
||||||
|
|
||||||
# create agents
|
# create agents
|
||||||
records = load('User')
|
users = load('User')
|
||||||
user(records)
|
user(users, groups, roles, queues)
|
||||||
|
|
||||||
|
# create organizations
|
||||||
|
organizations = load('Customer')
|
||||||
|
organization(organizations)
|
||||||
|
|
||||||
# create customers
|
# create customers
|
||||||
count = 0
|
count = 0
|
||||||
|
@ -259,7 +269,7 @@ module Import::OTRS2
|
||||||
run = false
|
run = false
|
||||||
next
|
next
|
||||||
end
|
end
|
||||||
customer(records)
|
customer(records, organizations)
|
||||||
end
|
end
|
||||||
|
|
||||||
Thread.abort_on_exception = true
|
Thread.abort_on_exception = true
|
||||||
|
@ -275,7 +285,6 @@ module Import::OTRS2
|
||||||
steps = 20
|
steps = 20
|
||||||
while run
|
while run
|
||||||
count += steps
|
count += steps
|
||||||
sleep 1
|
|
||||||
puts "loading... thread# #{thread} ..."
|
puts "loading... thread# #{thread} ..."
|
||||||
offset = count-steps
|
offset = count-steps
|
||||||
if offset != 0
|
if offset != 0
|
||||||
|
@ -288,6 +297,7 @@ module Import::OTRS2
|
||||||
next
|
next
|
||||||
end
|
end
|
||||||
_ticket_result(records, locks)
|
_ticket_result(records, locks)
|
||||||
|
sleep 1
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -819,8 +829,7 @@ module Import::OTRS2
|
||||||
end
|
end
|
||||||
|
|
||||||
# sync agents
|
# sync agents
|
||||||
|
def self.user(records, groups, roles, queues)
|
||||||
def self.user(records)
|
|
||||||
|
|
||||||
map = {
|
map = {
|
||||||
:ChangeTime => :updated_at,
|
:ChangeTime => :updated_at,
|
||||||
|
@ -838,16 +847,23 @@ module Import::OTRS2
|
||||||
:UserPw => :password,
|
:UserPw => :password,
|
||||||
};
|
};
|
||||||
|
|
||||||
role = Role.lookup( :name => 'Agent' )
|
|
||||||
records.each { |user|
|
records.each { |user|
|
||||||
_set_valid(user)
|
_set_valid(user)
|
||||||
|
|
||||||
|
# get roles
|
||||||
|
role_ids = get_roles_ids(user, groups, roles, queues)
|
||||||
|
|
||||||
|
# get groups
|
||||||
|
group_ids = get_queue_ids(user, groups, roles, queues)
|
||||||
|
|
||||||
# get new attributes
|
# get new attributes
|
||||||
user_new = {
|
user_new = {
|
||||||
:created_by_id => 1,
|
:created_by_id => 1,
|
||||||
:updated_by_id => 1,
|
:updated_by_id => 1,
|
||||||
:source => 'OTRS Import',
|
:source => 'OTRS Import',
|
||||||
:role_ids => [ role.id ],
|
:role_ids => role_ids,
|
||||||
|
:group_ids => group_ids,
|
||||||
}
|
}
|
||||||
map.each { |key,value|
|
map.each { |key,value|
|
||||||
if user[key.to_s]
|
if user[key.to_s]
|
||||||
|
@ -855,9 +871,11 @@ module Import::OTRS2
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# set pw
|
||||||
if user_new[:password]
|
if user_new[:password]
|
||||||
user_new[:password] = "{sha2}#{user_new[:password]}"
|
user_new[:password] = "{sha2}#{user_new[:password]}"
|
||||||
end
|
end
|
||||||
|
|
||||||
# check if agent already exists
|
# check if agent already exists
|
||||||
user_old = User.where( :id => user_new[:id] ).first
|
user_old = User.where( :id => user_new[:id] ).first
|
||||||
|
|
||||||
|
@ -880,9 +898,56 @@ module Import::OTRS2
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.get_queue_ids(user, groups, roles, queues)
|
||||||
|
queue_ids = []
|
||||||
|
|
||||||
|
# lookup by groups
|
||||||
|
user['GroupIDs'].each {|group_id, permissions|
|
||||||
|
queues.each {|queue_lookup|
|
||||||
|
if queue_lookup['GroupID'] == group_id
|
||||||
|
if permissions && permissions.include?('rw')
|
||||||
|
queue_ids.push queue_lookup['QueueID']
|
||||||
|
end
|
||||||
|
end
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# lookup by roles
|
||||||
|
|
||||||
|
# roles of user
|
||||||
|
# groups of roles
|
||||||
|
# queues of group
|
||||||
|
|
||||||
|
queue_ids
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.get_roles_ids(user, groups, roles, queues)
|
||||||
|
roles = ['Agent']
|
||||||
|
role_ids = []
|
||||||
|
user['GroupIDs'].each {|group_id, permissions|
|
||||||
|
groups.each {|group_lookup|
|
||||||
|
if group_id == group_lookup['ID']
|
||||||
|
if group_lookup['Name'] == 'admin' && permissions && permissions.include?('rw')
|
||||||
|
roles.push 'Admin'
|
||||||
|
end
|
||||||
|
if group_lookup['Name'] =~ /^(stats|report)/ && permissions && ( permissions.include?('ro') || permissions.include?('rw') )
|
||||||
|
roles.push 'Report'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
}
|
||||||
|
}
|
||||||
|
roles.each {|role|
|
||||||
|
role_lookup = Role.lookup( :name => role )
|
||||||
|
if role_lookup
|
||||||
|
role_ids.push role_lookup.id
|
||||||
|
end
|
||||||
|
}
|
||||||
|
role_ids
|
||||||
|
end
|
||||||
|
|
||||||
# sync customers
|
# sync customers
|
||||||
|
|
||||||
def self.customer(records)
|
def self.customer(records, organizations)
|
||||||
map = {
|
map = {
|
||||||
:ChangeTime => :updated_at,
|
:ChangeTime => :updated_at,
|
||||||
:CreateTime => :created_at,
|
:CreateTime => :created_at,
|
||||||
|
@ -913,10 +978,11 @@ module Import::OTRS2
|
||||||
|
|
||||||
# get new attributes
|
# get new attributes
|
||||||
user_new = {
|
user_new = {
|
||||||
:created_by_id => 1,
|
:created_by_id => 1,
|
||||||
:updated_by_id => 1,
|
:updated_by_id => 1,
|
||||||
:source => 'OTRS Import',
|
:source => 'OTRS Import',
|
||||||
:role_ids => [ role_customer.id ],
|
:organization_id => get_organization_id(user, organizations),
|
||||||
|
:role_ids => [ role_customer.id ],
|
||||||
}
|
}
|
||||||
map.each { |key,value|
|
map.each { |key,value|
|
||||||
if user[key.to_s]
|
if user[key.to_s]
|
||||||
|
@ -948,9 +1014,65 @@ module Import::OTRS2
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.get_organization_id(user, organizations)
|
||||||
|
organization_id = nil
|
||||||
|
if user['UserCustomerID']
|
||||||
|
organizations.each {|organization|
|
||||||
|
if user['UserCustomerID'] == organization['CustomerID']
|
||||||
|
organization = Organization.where(:name => organization['CustomerCompanyName'] ).first
|
||||||
|
organization_id = organization.id
|
||||||
|
end
|
||||||
|
}
|
||||||
|
end
|
||||||
|
organization_id
|
||||||
|
end
|
||||||
|
|
||||||
|
# sync organizations
|
||||||
|
|
||||||
|
def self.organization(records)
|
||||||
|
map = {
|
||||||
|
:ChangeTime => :updated_at,
|
||||||
|
:CreateTime => :created_at,
|
||||||
|
:CreateBy => :created_by_id,
|
||||||
|
:ChangeBy => :updated_by_id,
|
||||||
|
:CustomerCompanyName => :name,
|
||||||
|
:ValidID => :active,
|
||||||
|
:CustomerCompanyComment => :note,
|
||||||
|
};
|
||||||
|
|
||||||
|
records.each { |organization|
|
||||||
|
_set_valid(organization)
|
||||||
|
|
||||||
|
# get new attributes
|
||||||
|
organization_new = {
|
||||||
|
:created_by_id => 1,
|
||||||
|
:updated_by_id => 1,
|
||||||
|
}
|
||||||
|
map.each { |key,value|
|
||||||
|
if organization[key.to_s]
|
||||||
|
organization_new[value] = organization[key.to_s]
|
||||||
|
end
|
||||||
|
}
|
||||||
|
|
||||||
|
# check if state already exists
|
||||||
|
organization_old = Organization.where( :name => organization_new[:name] ).first
|
||||||
|
|
||||||
|
# set state types
|
||||||
|
if organization_old
|
||||||
|
organization_old.update_attributes(organization_new)
|
||||||
|
else
|
||||||
|
organization = Organization.new(organization_new)
|
||||||
|
organization.id = organization_new[:id]
|
||||||
|
organization.save
|
||||||
|
end
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
# set translate valid ids to active = true|false
|
# set translate valid ids to active = true|false
|
||||||
|
|
||||||
def self._set_valid(record)
|
def self._set_valid(record)
|
||||||
|
|
||||||
# map
|
# map
|
||||||
if record['ValidID'].to_s == '3'
|
if record['ValidID'].to_s == '3'
|
||||||
record['ValidID'] = '2'
|
record['ValidID'] = '2'
|
||||||
|
@ -964,5 +1086,10 @@ module Import::OTRS2
|
||||||
if record['ValidID'].to_s == '0'
|
if record['ValidID'].to_s == '0'
|
||||||
record['ValidID'] = false
|
record['ValidID'] = false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# fallback
|
||||||
|
if !record['ValidID']
|
||||||
|
record['ValidID'] = true
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
Loading…
Reference in a new issue