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_saved_change_dirty? ⇒ Boolean
- #auth0_update_params ⇒ Object
- #create_in_auth0 ⇒ Object
- #save_to_auth0 ⇒ Object
- #save_to_auth0_after_create ⇒ Object
- #save_to_auth0_after_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_and_picture_from_auth0 ⇒ Object
Instance Method Details
#auth0_create_params ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/sync_attr_with_auth0/adapters/active_record/auth0_sync.rb', line 144 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, 'name' => auth0_user_name, 'nickname' => auth0_user_name, 'given_name' => auth0_user_given_name, 'family_name' => auth0_user_family_name, 'user_metadata' => , 'app_metadata' => } return params end |
#auth0_saved_change_dirty? ⇒ Boolean
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/sync_attr_with_auth0/adapters/active_record/auth0_sync.rb', line 45 def auth0_saved_change_dirty? is_dirty = auth0_attributes_to_sync.any? do |attrib| if respond_to? :"saved_change_to_#{attrib}?" # Prefer modern method public_send :"saved_change_to_#{attrib}?" elsif respond_to? :"#{attrib}_changed?" # Legacy method. Drop when no longer supporting <= Rails 5.1 public_send :"#{attrib}_changed?" else # Specs currently verify attributes specified as needing synced # that are not defined not cause an error. I'm not sure why we # need this. Seems like a misconfiguration and we should blow # up. But to limit scope of change keeping with defined behavior. false end end # If the password was changed, force is_dirty to be true is_dirty = true if auth0_user_saved_change_to_password? # If the email was changed, force is_dirty to be true is_dirty = true if auth0_user_saved_change_to_email? return is_dirty end |
#auth0_update_params ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/sync_attr_with_auth0/adapters/active_record/auth0_sync.rb', line 174 def auth0_update_params = = params = { 'name' => auth0_user_name, 'nickname' => auth0_user_name, 'given_name' => auth0_user_given_name, 'family_name' => auth0_user_family_name, 'app_metadata' => , 'user_metadata' => } if auth0_user_saved_change_to_password? # The password needs to be updated. params['password'] = auth0_user_password params['verify_password'] = auth0_verify_password? end if auth0_user_saved_change_to_email? # 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
94 95 96 97 98 99 100 101 102 |
# File 'lib/sync_attr_with_auth0/adapters/active_record/auth0_sync.rb', line 94 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 and picture after_commit @auth0_uid = response['user_id'] @auth0_picture = response['picture'] end |
#save_to_auth0 ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/sync_attr_with_auth0/adapters/active_record/auth0_sync.rb', line 72 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_after_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_after_create return true unless sync_with_auth0_on_create? save_to_auth0 true # don't abort the callback chain end |
#save_to_auth0_after_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_after_update return true unless sync_with_auth0_on_update? return true unless auth0_saved_change_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
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/sync_attr_with_auth0/adapters/active_record/auth0_sync.rb', line 105 def update_in_auth0(user_uid) return unless user_uid params = auth0_update_params begin response = 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 @auth0_picture = response['picture'] 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. response = 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'] @auth0_picture = response['picture'] end rescue Exception => e ::Rails.logger.error e. ::Rails.logger.error e.backtrace.join("\n") raise e end end |
#update_uid_and_picture_from_auth0 ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/sync_attr_with_auth0/adapters/active_record/auth0_sync.rb', line 203 def update_uid_and_picture_from_auth0 data = {} if @auth0_uid attr = auth0_sync_configuration.auth0_uid_attribute data[attr] = @auth0_uid if respond_to?(attr) && @auth0_uid != public_send(attr) end if @auth0_picture attr = auth0_sync_configuration.picture_attribute data[attr] = @auth0_picture if respond_to?(attr) && @auth0_picture != public_send(attr) end update_columns data unless data.empty? remove_instance_variable :@auth0_uid if defined? @auth0_uid remove_instance_variable :@auth0_picture if defined? @auth0_picture true # don't abort the callback chain end |