sync_attr_with_auth0 0.1.7 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cbd1d27afcb626e027d6b5d610d048e52611480e
4
- data.tar.gz: 1c0ba300b82dc2a2cb1a8da61ed0b16805967d96
3
+ metadata.gz: 3b60ecf7f9e253d574e3424fb106ba114d3bc905
4
+ data.tar.gz: 7d6ddfa0bee4af49a41efa160d43d9c3fedeb172
5
5
  SHA512:
6
- metadata.gz: 2f8a355e4b26d652563dc2812108c13019ea5a18f1def848efdc749149e39877c3d0722780f11ae8449b2233629e130bf0fe9810943fc7a6f3c26e576e161f9c
7
- data.tar.gz: 0250be3d225041f815f7960f5fb59f25834799750b405168297bdecaea174611d8115ee5352c65d68eb58432f8a5c228585a83d3638d00c3d955259f03828b81
6
+ metadata.gz: 9c5f7901b787108e020a90e21e698492ac8eca6d21367efb1503254068bf9c95db5825d0f6fb1e5fb997824801b04b5476f07dc615aff70585bdc4c9433d5ae7
7
+ data.tar.gz: 9e15675fdb73869ca0119aa5948107690de934af8e75538eed36796cfe7f49ecaaeaf8b73166dc27ca53a5e686cb676321fcae8d7d31db839112d68e7a9e143a
@@ -23,40 +23,50 @@ module SyncAttrWithAuth0
23
23
  end # sync_with_auth0_on_update?
24
24
 
25
25
 
26
- def save_to_auth0_on_create
26
+ def save_to_auth0_after_create
27
27
  return true unless sync_with_auth0_on_create?
28
28
 
29
29
  save_to_auth0
30
30
 
31
31
  true # don't abort the callback chain
32
- end # save_to_auth0_on_create
32
+ end # save_to_auth0_after_create
33
33
 
34
34
 
35
- def save_to_auth0_on_update
35
+ def save_to_auth0_after_update
36
36
  return true unless sync_with_auth0_on_update?
37
- return true unless auth0_dirty?
37
+ return true unless auth0_saved_change_dirty?
38
38
 
39
39
  save_to_auth0
40
40
 
41
41
  true # don't abort the callback chain
42
- end # save_to_auth0_on_update
42
+ end # save_to_auth0_after_update
43
43
 
44
44
 
45
- def auth0_dirty?
46
- is_dirty = !!(
47
- auth0_attributes_to_sync.inject(false) do |memo, attrib|
48
- memo || self.try("#{attrib}_changed?")
45
+ def auth0_saved_change_dirty?
46
+ is_dirty = auth0_attributes_to_sync.any? do |attrib|
47
+ if respond_to? :"saved_change_to_#{attrib}?"
48
+ # Prefer modern method
49
+ public_send :"saved_change_to_#{attrib}?"
50
+ elsif respond_to? :"#{attrib}_changed?"
51
+ # Legacy method. Drop when no longer supporting <= Rails 5.1
52
+ public_send :"#{attrib}_changed?"
53
+ else
54
+ # Specs currently verify attributes specified as needing synced
55
+ # that are not defined not cause an error. I'm not sure why we
56
+ # need this. Seems like a misconfiguration and we should blow
57
+ # up. But to limit scope of change keeping with defined behavior.
58
+ false
49
59
  end
50
- )
60
+ end
51
61
 
52
62
  # If the password was changed, force is_dirty to be true
53
- is_dirty = true if auth0_user_password_changed?
63
+ is_dirty = true if auth0_user_saved_change_to_password?
54
64
 
55
65
  # If the email was changed, force is_dirty to be true
56
- is_dirty = true if auth0_user_email_changed?
66
+ is_dirty = true if auth0_user_saved_change_to_email?
57
67
 
58
68
  return is_dirty
59
- end # auth0_dirty?
69
+ end # auth0_saved_change_dirty?
60
70
 
61
71
 
62
72
  def save_to_auth0
@@ -86,8 +96,9 @@ module SyncAttrWithAuth0
86
96
 
87
97
  response = SyncAttrWithAuth0::Auth0.create_user(auth0_user_name, params, config: auth0_sync_configuration)
88
98
 
89
- # Update the record with the uid after_commit
99
+ # Update the record with the uid and picture after_commit
90
100
  @auth0_uid = response['user_id']
101
+ @auth0_picture = response['picture']
91
102
  end # create_in_auth0
92
103
 
93
104
 
@@ -97,10 +108,11 @@ module SyncAttrWithAuth0
97
108
  params = auth0_update_params
98
109
 
99
110
  begin
100
- SyncAttrWithAuth0::Auth0.patch_user(user_uid, params, config: auth0_sync_configuration)
111
+ response = SyncAttrWithAuth0::Auth0.patch_user(user_uid, params, config: auth0_sync_configuration)
101
112
 
102
113
  # Update the record with the uid after_commit (in case it doesn't match what's on file).
103
114
  @auth0_uid = user_uid
115
+ @auth0_picture = response['picture']
104
116
  rescue ::Auth0::NotFound => e
105
117
  # For whatever reason, the passed in uid was invalid,
106
118
  # determine how to proceed.
@@ -113,10 +125,11 @@ module SyncAttrWithAuth0
113
125
  else
114
126
  # The uid was incorrect, so re-attempt with the new uid
115
127
  # and update the one on file.
116
- SyncAttrWithAuth0::Auth0.patch_user(found_user['user_id'], params, config: auth0_sync_configuration)
128
+ response = SyncAttrWithAuth0::Auth0.patch_user(found_user['user_id'], params, config: auth0_sync_configuration)
117
129
 
118
130
  # Update the record with the uid after_commit
119
131
  @auth0_uid = found_user['user_id']
132
+ @auth0_picture = response['picture']
120
133
  end
121
134
 
122
135
  rescue Exception => e
@@ -163,13 +176,13 @@ module SyncAttrWithAuth0
163
176
  'user_metadata' => user_metadata
164
177
  }
165
178
 
166
- if auth0_user_password_changed?
179
+ if auth0_user_saved_change_to_password?
167
180
  # The password needs to be updated.
168
181
  params['password'] = auth0_user_password
169
182
  params['verify_password'] = auth0_verify_password?
170
183
  end
171
184
 
172
- if auth0_user_email_changed?
185
+ if auth0_user_saved_change_to_email?
173
186
  # The email needs to be updated.
174
187
  params['email'] = auth0_user_email
175
188
  params['verify_email'] = auth0_email_verified?
@@ -179,20 +192,26 @@ module SyncAttrWithAuth0
179
192
  end # auth0_update_params
180
193
 
181
194
 
182
- def update_uid_from_auth0
183
- if @auth0_uid
184
- self.sync_with_auth0_on_update = false if self.respond_to?(:sync_with_auth0_on_update=)
185
- self.send("#{auth0_sync_configuration.auth0_uid_attribute}=", @auth0_uid)
195
+ def update_uid_and_picture_from_auth0
196
+ data = {}
186
197
 
187
- # Nil the instance variable to prevent an infinite loop
188
- @auth0_uid = nil
198
+ if @auth0_uid
199
+ attr = auth0_sync_configuration.auth0_uid_attribute
200
+ data[attr] = @auth0_uid if respond_to?(attr) && @auth0_uid != public_send(attr)
201
+ end
189
202
 
190
- # Save!
191
- self.save
203
+ if @auth0_picture
204
+ attr = auth0_sync_configuration.picture_attribute
205
+ data[attr] = @auth0_picture if respond_to?(attr) && @auth0_picture != public_send(attr)
192
206
  end
193
207
 
208
+ update_columns data unless data.empty?
209
+
210
+ remove_instance_variable :@auth0_uid if defined? @auth0_uid
211
+ remove_instance_variable :@auth0_picture if defined? @auth0_picture
212
+
194
213
  true # don't abort the callback chain
195
- end # update_uid_from_auth0
214
+ end # update_uid_and_picture_from_auth0
196
215
 
197
216
  end
198
217
  end
@@ -33,9 +33,9 @@ module SyncAttrWithAuth0
33
33
 
34
34
  # Setup callbacks
35
35
  after_validation :validate_email_with_auth0
36
- after_create :save_to_auth0_on_create
37
- after_update :save_to_auth0_on_update
38
- after_commit :update_uid_from_auth0
36
+ after_create :save_to_auth0_after_create
37
+ after_update :save_to_auth0_after_update
38
+ after_commit :update_uid_and_picture_from_auth0
39
39
  end # sync_attr_with_auth0
40
40
 
41
41
  end # ClassMethods
@@ -52,18 +52,27 @@ module SyncAttrWithAuth0
52
52
  end # auth0_user_email
53
53
 
54
54
 
55
- def auth0_user_email_changed?
55
+ def auth0_user_saved_change_to_email?
56
56
  return false unless self.respond_to?(auth0_sync_configuration.email_attribute)
57
57
  # return false unless sync_email_with_auth0? # We don't care if it changed if we aren't syncing it.
58
58
 
59
- return self.send("#{auth0_sync_configuration.email_attribute}_changed?")
60
- end # auth0_email_changed?
59
+ if respond_to? :"saved_change_to_#{auth0_sync_configuration.email_attribute}?"
60
+ # Modern method
61
+ public_send :"saved_change_to_#{auth0_sync_configuration.email_attribute}?"
62
+ else
63
+ # Legacy method. Drop when no longer supporting <= Rails 5.1
64
+ public_send :"#{auth0_sync_configuration.email_attribute}_changed?"
65
+ end
66
+ end # auth0_user_saved_change_to_email?
61
67
 
62
68
 
63
69
  def auth0_user_uid
64
70
  self.send(auth0_sync_configuration.auth0_uid_attribute) if self.respond_to?(auth0_sync_configuration.auth0_uid_attribute)
65
71
  end # auth0_user_uid
66
72
 
73
+ def auth0_picture
74
+ public_send auth0_sync_configuration.picture_attribute if respond_to? auth0_sync_configuration.picture_attribute
75
+ end
67
76
 
68
77
  def auth0_user_name
69
78
  self.send(auth0_sync_configuration.name_attribute) if self.respond_to?(auth0_sync_configuration.name_attribute)
@@ -85,18 +94,21 @@ module SyncAttrWithAuth0
85
94
  end # auth0_user_password
86
95
 
87
96
 
88
- def auth0_user_password_changed?
97
+ def auth0_user_saved_change_to_password?
89
98
  return false unless self.respond_to?(auth0_sync_configuration.password_attribute)
90
- # return false unless sync_password_with_auth0? # We don't care if it changed if we aren't syncing it.
91
99
 
92
- if self.respond_to?(:"#{auth0_sync_configuration.password_attribute.to_s}_changed?")
93
- # We have a changed method, use it
94
- return self.send(:"#{auth0_sync_configuration.password_attribute.to_s}_changed?")
95
- else
96
- # We don't have a changed method, check if the attribute was set.
97
- return !self.send(auth0_sync_configuration.password_attribute).nil?
100
+ case
101
+ when respond_to?(:"saved_change_to_#{auth0_sync_configuration.password_attribute}?")
102
+ # Prefer modern method
103
+ public_send :"saved_change_to_#{auth0_sync_configuration.password_attribute}?"
104
+ when respond_to?(:"#{auth0_sync_configuration.password_attribute}_changed?")
105
+ # Legacy method. Drop when no longer supporting <= Rails 5.1
106
+ public_send :"#{auth0_sync_configuration.password_attribute}_changed?"
107
+ else
108
+ # Neither exists so must be in-memory accessor. Just check if set.
109
+ public_send(auth0_sync_configuration.password_attribute).present?
98
110
  end
99
- end # auth0_user_password_changed?
111
+ end # auth0_user_saved_change_to_password?
100
112
 
101
113
 
102
114
  def auth0_default_password
@@ -31,7 +31,7 @@ module SyncAttrWithAuth0
31
31
  :auth0_client_id, :auth0_client_secret, :auth0_namespace,
32
32
  :auth0_uid_attribute, :name_attribute, :given_name_attribute,
33
33
  :family_name_attribute, :email_attribute, :password_attribute,
34
- :email_verified_attribute, :verify_password_attribute,
34
+ :email_verified_attribute, :verify_password_attribute, :picture_attribute,
35
35
  :connection_name
36
36
 
37
37
 
@@ -50,6 +50,7 @@ module SyncAttrWithAuth0
50
50
  @password_attribute = :password
51
51
  @email_verified_attribute = :email_verified
52
52
  @verify_password_attribute = :verify_password
53
+ @picture_attribute = :picture
53
54
  @connection_name = 'Username-Password-Authentication'
54
55
  end
55
56
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sync_attr_with_auth0
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrick McGraw
@@ -29,16 +29,16 @@ dependencies:
29
29
  name: json
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
- - - "~>"
32
+ - - ">="
33
33
  - !ruby/object:Gem::Version
34
- version: 1.8.1
34
+ version: '0'
35
35
  type: :runtime
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
- - - "~>"
39
+ - - ">="
40
40
  - !ruby/object:Gem::Version
41
- version: 1.8.1
41
+ version: '0'
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: activerecord
44
44
  requirement: !ruby/object:Gem::Requirement
@@ -99,14 +99,14 @@ dependencies:
99
99
  name: jwt
100
100
  requirement: !ruby/object:Gem::Requirement
101
101
  requirements:
102
- - - '='
102
+ - - "~>"
103
103
  - !ruby/object:Gem::Version
104
104
  version: 1.5.0
105
105
  type: :runtime
106
106
  prerelease: false
107
107
  version_requirements: !ruby/object:Gem::Requirement
108
108
  requirements:
109
- - - '='
109
+ - - "~>"
110
110
  - !ruby/object:Gem::Version
111
111
  version: 1.5.0
112
112
  - !ruby/object:Gem::Dependency