Module: SyncAttrWithAuth0::Adapters::ActiveRecord::Auth0Sync
- Included in:
- SyncAttrWithAuth0::Adapters::ActiveRecord
- Defined in:
- lib/sync_attr_with_auth0/adapters/active_record/auth0_sync.rb
Instance Method Summary collapse
- #auth0_create_params ⇒ Object
- #auth0_dirty? ⇒ Boolean
- #auth0_update_params ⇒ Object
- #create_in_auth0 ⇒ Object
- #save_to_auth0 ⇒ Object
- #save_to_auth0_on_create ⇒ Object
- #save_to_auth0_on_update ⇒ Object
- #sync_email_with_auth0? ⇒ Boolean
- #sync_password_with_auth0? ⇒ Boolean
- #sync_with_auth0_on_create? ⇒ Boolean
- #sync_with_auth0_on_update? ⇒ Boolean
- #update_in_auth0(user_uid) ⇒ Object
- #update_uid_from_auth0 ⇒ Object
Instance Method Details
#auth0_create_params ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/sync_attr_with_auth0/adapters/active_record/auth0_sync.rb', line 131 def auth0_create_params = = password = auth0_user_password if password.nil? or password.empty? # We MUST include a password on create. password = auth0_default_password end email_verified = auth0_email_verified? params = { 'email' => auth0_user_email, 'password' => password, 'connection' => auth0_sync_configuration.connection_name, 'email_verified' => email_verified, 'user_metadata' => , 'app_metadata' => } return params end |
#auth0_dirty? ⇒ Boolean
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/sync_attr_with_auth0/adapters/active_record/auth0_sync.rb', line 45 def auth0_dirty? is_dirty = !!( auth0_attributes_to_sync.inject(false) do |memo, attrib| memo || self.try("#{attrib}_changed?") end ) # If the password was changed, force is_dirty to be true is_dirty = true if auth0_user_password_changed? # If the email was changed, force is_dirty to be true is_dirty = true if auth0_user_email_changed? return is_dirty end |
#auth0_update_params ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/sync_attr_with_auth0/adapters/active_record/auth0_sync.rb', line 157 def auth0_update_params = = params = { 'app_metadata' => , 'user_metadata' => } if auth0_user_password_changed? # The password needs to be updated. params['password'] = auth0_user_password params['verify_password'] = auth0_verify_password? end if auth0_user_email_changed? # The email needs to be updated. params['email'] = auth0_user_email params['verify_email'] = auth0_email_verified? end return params end |
#create_in_auth0 ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'lib/sync_attr_with_auth0/adapters/active_record/auth0_sync.rb', line 84 def create_in_auth0 params = auth0_create_params response = SyncAttrWithAuth0::Auth0.create_user(auth0_user_name, params, config: auth0_sync_configuration) # Update the record with the uid after_commit @auth0_uid = response['user_id'] end |
#save_to_auth0 ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/sync_attr_with_auth0/adapters/active_record/auth0_sync.rb', line 62 def save_to_auth0 # Determine if the user needs to be created or updated user_uid = auth0_user_uid if user_uid.nil? or user_uid.empty? found_user = users_in_auth0_with_matching_email.first user_uid = found_user['user_id'] if found_user end if user_uid.nil? or user_uid.empty? # The user has no auth0 uid assigned and we can't find a user # with a matching email address, so create. create_in_auth0 else # The user already has an auth0 UID assigned or we have a user # with a matching email address, so update. update_in_auth0(user_uid) end end |
#save_to_auth0_on_create ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/sync_attr_with_auth0/adapters/active_record/auth0_sync.rb', line 26 def save_to_auth0_on_create return true unless sync_with_auth0_on_create? save_to_auth0 true # don't abort the callback chain end |
#save_to_auth0_on_update ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/sync_attr_with_auth0/adapters/active_record/auth0_sync.rb', line 35 def save_to_auth0_on_update return true unless sync_with_auth0_on_update? return true unless auth0_dirty? save_to_auth0 true # don't abort the callback chain end |
#sync_email_with_auth0? ⇒ Boolean
11 12 13 |
# File 'lib/sync_attr_with_auth0/adapters/active_record/auth0_sync.rb', line 11 def sync_email_with_auth0? !!(auth0_attributes_to_sync.index(auth0_sync_configuration.email_attribute)) end |
#sync_password_with_auth0? ⇒ Boolean
6 7 8 |
# File 'lib/sync_attr_with_auth0/adapters/active_record/auth0_sync.rb', line 6 def sync_password_with_auth0? !!(auth0_attributes_to_sync.index(auth0_sync_configuration.password_attribute)) end |
#sync_with_auth0_on_create? ⇒ Boolean
16 17 18 |
# File 'lib/sync_attr_with_auth0/adapters/active_record/auth0_sync.rb', line 16 def sync_with_auth0_on_create? !!((self.respond_to?(:sync_with_auth0_on_create) and !self.sync_with_auth0_on_create.nil?) ? self.sync_with_auth0_on_create : true) end |
#sync_with_auth0_on_update? ⇒ Boolean
21 22 23 |
# File 'lib/sync_attr_with_auth0/adapters/active_record/auth0_sync.rb', line 21 def sync_with_auth0_on_update? !!((self.respond_to?(:sync_with_auth0_on_update) and !self.sync_with_auth0_on_update.nil?) ? self.sync_with_auth0_on_update : true) end |
#update_in_auth0(user_uid) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/sync_attr_with_auth0/adapters/active_record/auth0_sync.rb', line 94 def update_in_auth0(user_uid) return unless user_uid params = auth0_update_params begin SyncAttrWithAuth0::Auth0.patch_user(user_uid, params, config: auth0_sync_configuration) # Update the record with the uid after_commit (in case it doesn't match what's on file). @auth0_uid = user_uid rescue ::Auth0::NotFound => e # For whatever reason, the passed in uid was invalid, # determine how to proceed. found_user = users_in_auth0_with_matching_email.first if found_user.nil? # We could not find a user with that email address, so create # instead. create_in_auth0 else # The uid was incorrect, so re-attempt with the new uid # and update the one on file. SyncAttrWithAuth0::Auth0.patch_user(found_user['user_id'], params, config: auth0_sync_configuration) # Update the record with the uid after_commit @auth0_uid = found_user['user_id'] end rescue Exception => e ::Rails.logger.error e. ::Rails.logger.error e.backtrace.join("\n") raise e end end |
#update_uid_from_auth0 ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/sync_attr_with_auth0/adapters/active_record/auth0_sync.rb', line 182 def update_uid_from_auth0 if @auth0_uid self.sync_with_auth0_on_update = false if self.respond_to?(:sync_with_auth0_on_update=) self.send("#{auth0_sync_configuration.auth0_uid_attribute}=", @auth0_uid) # Nil the instance variable to prevent an infinite loop @auth0_uid = nil # Save! self.save end true # don't abort the callback chain end |