Class: Kaui::AllowedUser

Inherits:
ApplicationRecord show all
Defined in:
app/models/kaui/allowed_user.rb

Instance Method Summary collapse

Instance Method Details

#create_in_kb!(password, roles = [], user = nil, reason = nil, comment = nil, options = {}) ⇒ Object

Create the user locally and in Kill Bill (if needed)



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/models/kaui/allowed_user.rb', line 13

def create_in_kb!(password, roles = [], user = nil, reason = nil, comment = nil, options = {})
  # Create in Kill Bill
  kb_user = KillBillClient::Model::UserRoles.new
  kb_user.username = kb_username
  kb_user.password = password
  kb_user.roles = roles

  begin
    kb_user.create(user, reason, comment, options)
  rescue KillBillClient::API::BadRequest => e
    error_code = JSON.parse(e.response.body)['code'] rescue nil
    raise e unless error_code == 40002 # SECURITY_USER_ALREADY_EXISTS
  end

  # Save locally
  save!
end

#destroy_in_kb!(user = nil, reason = nil, comment = nil, options = {}) ⇒ Object

Delete the user locally and in Kill Bill (if needed)



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/kaui/allowed_user.rb', line 52

def destroy_in_kb!(user = nil, reason = nil, comment = nil, options = {})
  user_role = KillBillClient::Model::UserRoles.new
  user_role.username = kb_username

  begin
    user_role.destroy(user, reason, comment, options)
  rescue KillBillClient::API::BadRequest => _
    # User already deactivated in Kill Bill
  end

  # Destroy locally
  destroy!
end

#update_in_kb!(password, roles, user = nil, reason = nil, comment = nil, options = {}) ⇒ Object

Update the user locally and in Kill Bill (if needed)



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/kaui/allowed_user.rb', line 32

def update_in_kb!(password, roles, user = nil, reason = nil, comment = nil, options = {})

  user_role = KillBillClient::Model::UserRoles.new
  user_role.username = kb_username

  # We have two different APIs to update password and roles
  unless password.nil?
    user_role.password = password
    user_role.update(user, reason, comment, options)
    user_role.password = nil
  end
  unless roles.nil?
    user_role.roles = roles
    user_role.update(user, reason, comment, options)
  end

  save!
end