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)



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

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 = begin
      JSON.parse(e.response.body)['code']
    rescue StandardError
      nil
    end
    raise e unless error_code == 40_002 # 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)



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/models/kaui/allowed_user.rb', line 56

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 => _e
    # 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)



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/kaui/allowed_user.rb', line 37

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