Module: SyncAttrWithAuth0::Model
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/sync_attr_with_auth0/model.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #auth0_create ⇒ Object
- #auth0_default_password ⇒ Object
- #auth0_email_verified? ⇒ Boolean
- #auth0_new_uuid ⇒ Object
- #auth0_update ⇒ Object
- #auth0_user_metadata ⇒ Object
- #auth0_user_password ⇒ Object
- #auth0_verify_password? ⇒ Boolean
- #create_user_in_auth0 ⇒ Object
- #find_user_in_auth0 ⇒ Object
- #update_user_in_auth0(uid) ⇒ Object
- #validate_email_with_auth0 ⇒ Object
Instance Method Details
#auth0_create ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/sync_attr_with_auth0/model.rb', line 71 def auth0_create # When creating a new user, create the user in auth0. ok_to_sync = (self.respond_to?(:sync_with_auth0_on_create) and !self.sync_with_auth0_on_create.nil? ? self.sync_with_auth0_on_create : true) # Do not create a user in auth0 if the user already has a uid from auth0 if ok_to_sync unless self.send([:uid_att]).nil? or self.send([:uid_att]).empty? ok_to_sync = false end end if ok_to_sync create_user_in_auth0 end true # don't abort the callback chain end |
#auth0_default_password ⇒ Object
214 215 216 217 |
# File 'lib/sync_attr_with_auth0/model.rb', line 214 def auth0_default_password # Need a9 or something similar to guarantee one letter and one number in the password "#{auth0_new_uuid[0..19]}aA9" end |
#auth0_email_verified? ⇒ Boolean
206 207 208 |
# File 'lib/sync_attr_with_auth0/model.rb', line 206 def auth0_email_verified? !!(self.respond_to?([:email_verified_att]) ? self.send([:email_verified_att]) : false) end |
#auth0_new_uuid ⇒ Object
219 220 221 |
# File 'lib/sync_attr_with_auth0/model.rb', line 219 def auth0_new_uuid ::UUIDTools::UUID.random_create().to_s end |
#auth0_update ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/sync_attr_with_auth0/model.rb', line 90 def auth0_update ok_to_sync = (self.respond_to?(:sync_with_auth0_on_update) and !self.sync_with_auth0_on_update.nil? ? self.sync_with_auth0_on_update : true) if ok_to_sync # Get the auth0 uid uid = self.send([:uid_att]) # TODO: create a user if the uid is nil unless uid.nil? # Update the user in auth0 update_user_in_auth0(uid) end end true # don't abort the callback chain end |
#auth0_user_metadata ⇒ Object
223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/sync_attr_with_auth0/model.rb', line 223 def = {} = [[:family_name_att], [:given_name_att], [:email_att], [:password_att], [:email_verified_att], [:name_att]] [:sync_atts].each do |key| [key] = self.send(key) if self.respond_to?(key) and .index(key).nil? end return end |
#auth0_user_password ⇒ Object
202 203 204 |
# File 'lib/sync_attr_with_auth0/model.rb', line 202 def auth0_user_password self.respond_to?([:password_att]) ? self.send([:password_att]) : auth0_default_password end |
#auth0_verify_password? ⇒ Boolean
210 211 212 |
# File 'lib/sync_attr_with_auth0/model.rb', line 210 def auth0_verify_password? !!(self.respond_to?([:verify_password_att]) ? self.send([:verify_password_att]) : true) end |
#create_user_in_auth0 ⇒ Object
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 |
# File 'lib/sync_attr_with_auth0/model.rb', line 109 def create_user_in_auth0() = password = auth0_user_password if password.nil? password = auth0_default_password end email_verified = auth0_email_verified? args = { 'email' => self.send([:email_att]), 'password' => password, 'connection' => [:connection_name], 'email_verified' => email_verified, 'user_metadata' => } auth0 = SyncAttrWithAuth0::Auth0.create_auth0_client response = auth0.create_user(self.send([:name_att]), args) # Update the record with the uid self.send("#{[:uid_att]}=", response['user_id']) self.save end |
#find_user_in_auth0 ⇒ Object
194 195 196 197 198 199 200 |
# File 'lib/sync_attr_with_auth0/model.rb', line 194 def find_user_in_auth0 auth0 = SyncAttrWithAuth0::Auth0.create_auth0_client(api_version: 1) response = auth0.users("email:#{self.send([:email_att])}") return response end |
#update_user_in_auth0(uid) ⇒ Object
136 137 138 139 140 141 142 143 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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/sync_attr_with_auth0/model.rb', line 136 def update_user_in_auth0(uid) = auth0 = SyncAttrWithAuth0::Auth0.create_auth0_client args = { 'app_metadata' => { 'name' => self.send([:name_att]), 'nickname' => self.send([:name_att]), 'given_name' => self.send([:given_name_att]), 'family_name' => self.send([:family_name_att]) } } if ( [:sync_atts].index([:password_att]) and # Because the password being passed to auth0 probably is not a real # field (and if it is it needs to be the unencrypted value), we # can't rely on checking if the password attribute changed (chances # are, that method does not exist). So assume the password attribute # is only set if it's being changed. !self.send([:password_att]).nil? ) # The password should be sync'd and was changed args['password'] = self.send([:password_att]) args['verify_password'] = auth0_verify_password? end args['user_metadata'] = begin auth0.patch_user(uid, args) rescue ::Auth0::NotFound => e # TODO: We need to attempt to find the correct UID by email or nil the UID on the user. response = find_user_in_auth0 found_user = response.first if found_user.nil? # Could not find the user, create it in auth0 create_user_in_auth0 else # Update with the new uid and correct the one on file auth0 = SyncAttrWithAuth0::Auth0.create_auth0_client auth0.patch_user(found_user['user_id'], args) self.send("#{[:uid_att]}=", found_user['user_id']) self.save end rescue Exception => e ::Rails.logger.error e. ::Rails.logger.error e.backtrace.join("\n") raise e end end |
#validate_email_with_auth0 ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/sync_attr_with_auth0/model.rb', line 56 def validate_email_with_auth0 # If the email is being modified, verify the new email does not already # exist in auth0. ok_to_validate = (self.respond_to?(:validate_with_auth0) and !self.validate_with_auth0.nil? ? self.validate_with_auth0 : true) if ok_to_validate and self.email_changed? response = find_user_in_auth0 return response.empty? end return true end |