Class: Moura::Model::Remote
- Inherits:
-
Object
- Object
- Moura::Model::Remote
- Defined in:
- lib/moura/model/remote.rb
Instance Method Summary collapse
- #add_role_users(name, emails) ⇒ Object
- #app(id) ⇒ Object
- #create_role(name) ⇒ Object
- #delete_role(name) ⇒ Object
- #dump ⇒ Object
- #find_app_id_by_name(name) ⇒ Object
- #find_role_id(name) ⇒ Object
- #find_role_id_by_name(name) ⇒ Object
- #find_user_id_by_email(email) ⇒ Object
-
#initialize ⇒ Remote
constructor
A new instance of Remote.
- #remove_role_users(name, emails) ⇒ Object
- #role(name) ⇒ Object
- #set_role_apps(name, app_ids) ⇒ Object
- #validate_apps(apps) ⇒ Object
- #validate_emails(emails) ⇒ Object
Constructor Details
#initialize ⇒ Remote
Returns a new instance of Remote.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/moura/model/remote.rb', line 10 def initialize @client = OneLogin.client # データを圧縮するために取得フィールドを絞る @users = @client.list_users(fields: "id,email,firstname,lastname", limit: 1000).to_h do |user| [user.id, user] end @apps = @client.list_apps(limit: 1000).to_h { |app| [app.id, app] } update_roles end |
Instance Method Details
#add_role_users(name, emails) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/moura/model/remote.rb', line 67 def add_role_users(name, emails) role_id = find_role_id_by_name(name) user_ids = emails&.map { |email| find_user_id_by_email(email) } @client.add_role_users(role_id, user_ids) if user_ids&.present? end |
#app(id) ⇒ Object
25 26 27 |
# File 'lib/moura/model/remote.rb', line 25 def app(id) @apps[id] end |
#create_role(name) ⇒ Object
56 57 58 59 60 |
# File 'lib/moura/model/remote.rb', line 56 def create_role(name) body = { name: } @client.create_roles(debug_body: body) update_roles end |
#delete_role(name) ⇒ Object
74 75 76 77 |
# File 'lib/moura/model/remote.rb', line 74 def delete_role(name) role_id = find_role_id_by_name(name) @client.delete_role(role_id) if role_id end |
#dump ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/moura/model/remote.rb', line 36 def dump @roles.map do |(name, role)| apps = role.apps.map { |id| @apps[id].name }.sort users = role.users.map { |id| @users[id].email }.sort [name, { "apps" => apps, "users" => users }] end.sort_by(&:first).to_h end |
#find_app_id_by_name(name) ⇒ Object
92 93 94 95 96 |
# File 'lib/moura/model/remote.rb', line 92 def find_app_id_by_name(name) return unless (app = @apps.find { |_, a| a.name == name }) app.last.id end |
#find_role_id(name) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/moura/model/remote.rb', line 29 def find_role_id(name) role = @roles[name] raise RoleNotFound unless role role.id end |
#find_role_id_by_name(name) ⇒ Object
86 87 88 89 90 |
# File 'lib/moura/model/remote.rb', line 86 def find_role_id_by_name(name) return unless (role = @roles.find { |_, r| r.name == name }) role.last.id end |
#find_user_id_by_email(email) ⇒ Object
98 99 100 101 102 |
# File 'lib/moura/model/remote.rb', line 98 def find_user_id_by_email(email) return unless (user = @users.find { |_, v| v.email == email }) user.last.id end |
#remove_role_users(name, emails) ⇒ Object
79 80 81 82 83 84 |
# File 'lib/moura/model/remote.rb', line 79 def remove_role_users(name, emails) role_id = find_role_id_by_name(name) user_ids = emails&.map { |email| find_user_id_by_email(email) } @client.remove_role_users(role_id, user_ids) if user_ids&.present? end |
#role(name) ⇒ Object
21 22 23 |
# File 'lib/moura/model/remote.rb', line 21 def role(name) @roles[name] end |
#set_role_apps(name, app_ids) ⇒ Object
62 63 64 65 |
# File 'lib/moura/model/remote.rb', line 62 def set_role_apps(name, app_ids) role_id = find_role_id_by_name(name) @client.set_role_apps(role_id, app_ids) end |
#validate_apps(apps) ⇒ Object
44 45 46 47 48 |
# File 'lib/moura/model/remote.rb', line 44 def validate_apps(apps) apps.each do |app| raise ApplicationNotFound, app unless find_app_id_by_name(app) end end |
#validate_emails(emails) ⇒ Object
50 51 52 53 54 |
# File 'lib/moura/model/remote.rb', line 50 def validate_emails(emails) emails.each do |email| raise UserNotFound, email unless find_user_id_by_email(email) end end |