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

Instance Method Details

#auth0_default_passwordObject



230
231
232
233
# File 'lib/sync_attr_with_auth0/model.rb', line 230

def auth0_default_password
  # Need a9 or something similar to guarantee one letter and one number in the password
  "#{auth0_new_uuid[0..19]}a9"
end

#auth0_email_verified?Boolean



226
227
228
# File 'lib/sync_attr_with_auth0/model.rb', line 226

def auth0_email_verified?
  !!(self.respond_to?(auth0_sync_options[:email_verified_att]) ? self.send(auth0_sync_options[:email_verified_att]) : false)
end

#auth0_new_uuidObject



235
236
237
# File 'lib/sync_attr_with_auth0/model.rb', line 235

def auth0_new_uuid
  ::UUIDTools::UUID.random_create().to_s
end

#auth0_user_metadataObject



239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/sync_attr_with_auth0/model.rb', line 239

def 
   = {}
   = [auth0_sync_options[:family_name_att],
    auth0_sync_options[:given_name_att], auth0_sync_options[:email_att],
    auth0_sync_options[:password_att],
    auth0_sync_options[:email_verified_att], auth0_sync_options[:name_att]]

  auth0_sync_options[:sync_atts].each do |key|
    [key] = self.send(key) if self.respond_to?(key) and .index(key).nil?
  end

  return 
end

#auth0_user_passwordObject



222
223
224
# File 'lib/sync_attr_with_auth0/model.rb', line 222

def auth0_user_password
  self.respond_to?(auth0_sync_options[:password_att]) ? self.send(auth0_sync_options[:password_att]) : auth0_default_password
end

#create_user_in_auth0Object



83
84
85
86
87
88
89
90
91
92
93
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
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/sync_attr_with_auth0/model.rb', line 83

def create_user_in_auth0
  # 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(auth0_sync_options[:uid_att]).nil? or self.send(auth0_sync_options[:uid_att]).empty?
      ok_to_sync = false
    end
  end

  if ok_to_sync
    # # Look for matches between what's changing
    # # and what needs to be transmitted to Auth0
    # matches = ( (self.class.auth0_sync_options[:sync_atts] || []) & (self.changes.keys || []) )
    #
    # # Figure out what needs to be sent to Auth0
    # changes = {}
    # matches.each do |m|
    #   changes[m] = self.send(m) if self.respond_to?(m)
    # end

     = 

    # unless user_metadata['email'].nil?
    #   # Email is already being sent
    #   user_metadata.delete('email')
    # end
    #
    # unless user_metadata['password'].nil?
    #   # Password is already being sent
    #   user_metadata.delete('password')
    # end

    password = auth0_user_password
    email_verified = auth0_email_verified?
    args = {
      'email' => self.send(auth0_sync_options[:email_att]),
      'password' => password,
      'connection' => auth0_sync_options[:connection_name],
      'email_verified' => email_verified,
      'user_metadata' => 
    }

    auth0 = SyncAttrWithAuth0::Auth0.create_auth0_client

    response = auth0.create_user(self.send(auth0_sync_options[:name_att]), args)

    response = JSON.parse(response)

    # Update the record with the uid
    self.send("#{auth0_sync_options[:uid_att]}=", response['user_id'])
    self.save
  end

  true # don't abort the callback chain
end

#sync_attr_with_auth0Object



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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/sync_attr_with_auth0/model.rb', line 142

def sync_attr_with_auth0
  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
    # # Look for matches between what's changing
    # # and what needs to be transmitted to Auth0
    # matches = ( (self.class.auth0_sync_options[:sync_atts] || []) & (self.changes.keys || []) )

    # Get the auth0 uid
    uid = self.send(auth0_sync_options[:uid_att])

    # Don't try to update auth0 if the user doesn't have a uid
    unless uid.nil?
       = 

      auth0 = SyncAttrWithAuth0::Auth0.create_auth0_client

      args = {
        # 'email' => self.send(auth0_sync_options[:email_att]),
        # 'password' => auth0_user_password, Don't want to reset the user's password every edit.

        'app_metadata' => {
          'name' => self.send(auth0_sync_options[:name_att]),
          'nickname' => self.send(auth0_sync_options[:name_att]),
          'given_name' => self.send(auth0_sync_options[:given_name_att]),
          'family_name' => self.send(auth0_sync_options[:family_name_att])
        }
      }

      args['user_metadata'] = 

      response = auth0.patch_user(uid, args)
    end

    # # If we find matches
    # unless matches.empty?
    #   # Figure out what needs to be sent to Auth0
    #   changes = {}
    #   matches.each do |m|
    #     changes[m] = self.send(m)
    #   end
    #
    #   # If we actually have changes
    #   unless changes.empty?
    #     # Get the auth0 uid
    #     uid = self.send(auth0_sync_options[:uid_att])
    #
    #     # Don't try to update auth0 if the user doesn't have a uid
    #     unless uid.nil?
    #       auth0 = SyncAttrWithAuth0::Auth0.create_auth0_client
    #
    #       args = {
    #         'app_metadata' => {
    #           'name' => self.send(auth0_sync_options[:name_att]),
    #           'nickname' => self.send(auth0_sync_options[:name_att]),
    #           'given_name' => self.send(auth0_sync_options[:given_name_att]),
    #           'family_name' => self.send(auth0_sync_options[:family_name_att])
    #         }
    #       }
    #       unless changes['email'].nil?
    #         args['app_metadata']['username'] = changes.delete('email')
    #       end
    #
    #       unless changes['password'].nil?
    #         args['app_metadata']['password'] = changes.delete('password')
    #       end
    #
    #       args['user_metadata'] = changes
    #
    #       response = auth0.patch_user(uid, args)
    #     end
    #
    #   end
    # end

  end

  true # don't abort the callback chain
end

#validate_email_with_auth0Object



55
56
57
58
59
60
61
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/model.rb', line 55

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?
    auth0 = SyncAttrWithAuth0::Auth0.create_auth0_client(api_version: 1)

    response = auth0.users("email:#{self.send(auth0_sync_options[:email_att])}")

    # response = auth0.users(
    #   1,
    #   0,
    #   nil,
    #   nil,
    #   auth0_sync_options[:connection_name],
    #   nil,
    #   nil,
    #   "email:#{self.send(auth0_sync_options[:email_att])}"
    # )

    return JSON.parse(response).empty?
  end

  return true
end