Class: Identity::AccountCreator
- Inherits:
-
Object
- Object
- Identity::AccountCreator
- Defined in:
- app/services/identity/account_creator.rb
Instance Attribute Summary collapse
-
#user ⇒ Object
Returns the value of attribute user.
Instance Method Summary collapse
- #call ⇒ Object
- #clean_up_deprecated_accounts(current_account_id, uuid, identifier) ⇒ Object private
- #create_account_attribute_hash(identity) ⇒ Object private
- #create_if_needed! ⇒ Object private
- #find_matching_account(accounts) ⇒ Object private
- #get_accounts_for_user ⇒ Object private
- #hash_diff(first_hash, second_hash) ⇒ Object private
-
#initialize(user) ⇒ AccountCreator
constructor
A new instance of AccountCreator.
- #match_account_for_identifier(accounts, identifier) ⇒ Object private
- #update_if_needed!(account) ⇒ Object private
Constructor Details
#initialize(user) ⇒ AccountCreator
Returns a new instance of AccountCreator.
5 6 7 |
# File 'app/services/identity/account_creator.rb', line 5 def initialize(user) @user = user end |
Instance Attribute Details
#user ⇒ Object
Returns the value of attribute user.
9 10 11 |
# File 'app/services/identity/account_creator.rb', line 9 def user @user end |
Instance Method Details
#call ⇒ Object
11 12 13 14 15 16 17 |
# File 'app/services/identity/account_creator.rb', line 11 def call return unless user.idme_uuid || user.logingov_uuid || user.sec_id account = create_if_needed! update_if_needed!(account) end |
#clean_up_deprecated_accounts(current_account_id, uuid, identifier) ⇒ Object (private)
81 82 83 84 85 86 87 |
# File 'app/services/identity/account_creator.rb', line 81 def clean_up_deprecated_accounts(current_account_id, uuid, identifier) return unless uuid account = Account.find_by(identifier => uuid) account.destroy if account && account.id != current_account_id end |
#create_account_attribute_hash(identity) ⇒ Object (private)
47 48 49 50 51 52 53 54 55 |
# File 'app/services/identity/account_creator.rb', line 47 def create_account_attribute_hash(identity) { idme_uuid: identity.idme_uuid, logingov_uuid: identity.logingov_uuid, sec_id: identity.sec_id, edipi: identity.edipi, icn: identity.icn }.compact end |
#create_if_needed! ⇒ Object (private)
21 22 23 24 25 |
# File 'app/services/identity/account_creator.rb', line 21 def create_if_needed! accounts = get_accounts_for_user account = accounts.length > 1 ? find_matching_account(accounts) : accounts.first account.presence || Account.create(**create_account_attribute_hash(user)) end |
#find_matching_account(accounts) ⇒ Object (private)
33 34 35 36 37 38 |
# File 'app/services/identity/account_creator.rb', line 33 def find_matching_account(accounts) match_account_for_identifier(accounts, :idme_uuid) || match_account_for_identifier(accounts, :logingov_uuid) || match_account_for_identifier(accounts, :sec_id) || accounts.first end |
#get_accounts_for_user ⇒ Object (private)
27 28 29 30 31 |
# File 'app/services/identity/account_creator.rb', line 27 def get_accounts_for_user Account.idme_uuid_match(user.idme_uuid) .or(Account.sec_id_match(user.sec_id)) .or(Account.logingov_uuid_match(user.logingov_uuid)) end |
#hash_diff(first_hash, second_hash) ⇒ Object (private)
77 78 79 |
# File 'app/services/identity/account_creator.rb', line 77 def hash_diff(first_hash, second_hash) (first_hash.to_a - second_hash.to_a).to_h end |
#match_account_for_identifier(accounts, identifier) ⇒ Object (private)
40 41 42 43 44 45 |
# File 'app/services/identity/account_creator.rb', line 40 def match_account_for_identifier(accounts, identifier) user_identifier_id = user.send(identifier) return unless user_identifier_id accounts.find { |account| account.send(identifier) == user_identifier_id } end |
#update_if_needed!(account) ⇒ Object (private)
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'app/services/identity/account_creator.rb', line 57 def update_if_needed!(account) # account has yet to be saved, no need to update return account unless account.persisted? # return account as is if all non-nil user attributes match up to be the same account_attributes = create_account_attribute_hash(account) user_attributes = create_account_attribute_hash(user) attribute_diff = hash_diff(user_attributes, account_attributes) return account if attribute_diff.blank? if attribute_diff[:logingov_uuid] clean_up_deprecated_accounts(account.id, attribute_diff[:logingov_uuid], :logingov_uuid) end Account.update(account.id, **user_attributes) end |