This repository has been archived by the owner on Feb 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
users.rb
75 lines (64 loc) · 2.06 KB
/
users.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
require_relative 'settings'
require 'mysql2'
require 'ontologies_linked_data'
require 'progressbar'
client = Mysql2::Client.new(host: USERS_DB_HOST, username: USERS_DB_USERNAME, password: USERS_DB_PASSWORD, database: "bioportal")
user_query = <<-EOS
SELECT * from ncbo_user
EOS
role_query = <<-EOS
SELECT r.`name` FROM ncbo_user_role ur
JOIN `ncbo_l_role` r ON ur.`role_id` = r.id
WHERE user_id = %user_id%
EOS
users = client.query(user_query)
puts "Number of users to migrate: #{users.count}"
pbar = ProgressBar.new("Migrating", users.count*2)
users.each_with_index(:symbolize_keys => true) do |user, index|
# Build user object
new_user_attrs = {
username: user[:username].strip,
email: user[:email],
firstName: user[:firstname],
lastName: user[:lastname],
created: DateTime.parse(user[:date_created].to_s),
apikey: user[:api_key],
}
new_user = LinkedData::Models::User.new(new_user_attrs)
password = user[:password].strip
new_user.passwordHash = password
pbar.inc
# Assign roles
roles = client.query(role_query.gsub("%user_id%", user[:id].to_s))
new_roles = []
roles.each(:symbolize_keys => true).each do |role|
role = LinkedData::Models::Users::Role.find(role[:name].gsub("ROLE_", "")).first
new_roles << role
end
new_roles = [LinkedData::Models::Users::Role.default] if new_roles.length < 1
new_user.role = new_roles
if new_user.valid?
new_user.save
else
puts "User #{user[:username]} not valid: #{new_user.errors}"
end
# Some simple checks
retrieved_user = LinkedData::Models::User.find(user[:username].strip).include(LinkedData::Models::User.attributes(:all)).first
errors = []
if retrieved_user.nil?
errors << "ERRORS: #{user[:username]}"
errors << "retrieval"
else
errors << "ERRORS: #{user[:username]}"
unless retrieved_user.passwordHash.eql?(password)
binding.pry
errors << "hash"
end
unless retrieved_user.apikey.eql?(user[:api_key])
errors << "apikey"
end
end
puts errors.join("\n") + "\n\n" unless errors.length <= 1
pbar.inc
end
pbar.finish