oauth_im 0.10.2 → 0.10.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3157351b7b2f1b010967a23541e3661835a7dc600a4e37d75d26f512e71169d6
4
- data.tar.gz: b54781e3b5e79d8f167a09e9d1c36e09a4f975a3bf47d2a8279d9fb0adb8be74
3
+ metadata.gz: 22d12f50a39c1a642f23bcbaa44e9714ca0c6524cfa364e6872a14b41a7ce582
4
+ data.tar.gz: 1aca1d5f3683936829fa3886c58570ba89095542c6ceb5447c77afdc86e3edd1
5
5
  SHA512:
6
- metadata.gz: 14694090ee6a56ad6498adeb3980ac65405437953a603507a699eb40d987d9938baadf1e293c58f7c5b0869f2c0bea8fa674eed97eefa9c229ce76c32925d066
7
- data.tar.gz: 3a03bee203ffc14fc59cf50319fae33d4dbdc4603d93d68a93fe12ca78dd89d2e6b8e1415fa1fca441e39f8108a6f171f7d056cff6618df9b7cefad05f143922
6
+ metadata.gz: 567b8a9267a062b6b06d18930d85b94e14eeb8cffc8f4bd25fdbfbbbf4b30de3475c3ace6d2c7defe24a57bd3da4262142e0ab68a622407b9a55b6df04203c09
7
+ data.tar.gz: fa168d58bf0786d43f04d209e5b6b7bf7b4ac201702f8f0730984bd63e50fecbb748ee2a9e9b17c5b085a9eff85f11c654a0bf26664561216ede2d1e707dcd69
@@ -15,10 +15,14 @@ module OauthIm
15
15
  end
16
16
 
17
17
  def proxy_user_for(user_id:)
18
+ ProxyUser.new proxy_attrs_for(user_id: user_id)
19
+ end
20
+
21
+ def proxy_attrs_for(user_id:)
18
22
  response = client.retrieve_user(user_id).success_response
19
23
  raise "No user for id #{user_id}" if response.blank?
20
24
 
21
- ProxyUser.new response.user
25
+ response.user
22
26
  end
23
27
 
24
28
  private
@@ -2,6 +2,13 @@
2
2
 
3
3
  require 'fusionauth/fusionauth_client'
4
4
 
5
+ ############################################################
6
+ # # EDC: memoizing state for these methods is problematic. #
7
+ # # without care, an object's local state can diverge from #
8
+ # # the remote state returned by the API. so for now #
9
+ # # it seems safest to leave things unmemoized #
10
+ ############################################################
11
+
5
12
  module OauthIm
6
13
  module HasRegistrationData
7
14
  delegate :email, to: :attrs
@@ -18,8 +25,20 @@ module OauthIm
18
25
  delegate :name, :id,
19
26
  to: :state, prefix: true
20
27
 
28
+ def registrations
29
+ attrs[:registrations] || []
30
+ end
31
+
32
+ def registration
33
+ registrations.first
34
+ end
35
+
21
36
  def registration_data
22
- @registration_data ||= attrs[:registrations]&.first&.data || {}
37
+ registration&.data || {}
38
+ end
39
+
40
+ def application_id
41
+ registration&.applicationId
23
42
  end
24
43
 
25
44
  def active?
@@ -47,7 +66,7 @@ module OauthIm
47
66
  end
48
67
 
49
68
  def full_name
50
- @full_name ||= "#{first_name} #{last_name}"
69
+ "#{first_name} #{last_name}"
51
70
  end
52
71
  end
53
72
  end
@@ -1,36 +1,107 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ ############################################################
4
+ # # EDC: memoizing state for these methods is problematic. #
5
+ # # without care, an object's local state can diverge from #
6
+ # # the remote state returned by the API. so for now #
7
+ # # it seems safest to leave things unmemoized #
8
+ ############################################################
9
+
3
10
  module OauthIm
4
11
  class ProxyUser < IdpClient
5
12
  include HasRegistrationData
6
13
 
7
- attr_reader :attrs
8
-
9
- delegate :to_h, to: :attrs
10
-
11
14
  def self.for(user_id:)
12
15
  AdminClient.new.proxy_user_for user_id: user_id
13
16
  end
14
17
 
18
+ attr_reader :attrs
19
+
15
20
  def initialize(attrs)
16
21
  @attrs = attrs
17
22
  super()
18
23
  end
19
24
 
25
+ def grant_sponsorship
26
+ update_registration { |data| data[:sponsor] = 'true' }
27
+ end
28
+
29
+ def revoke_sponsorship
30
+ update_registration { |data| data[:sponsor] = 'false' }
31
+ end
32
+
33
+ def to_h
34
+ os_to_h attrs
35
+ end
36
+
20
37
  def user_id
21
- @user_id ||= attrs[:id]
38
+ attrs[:id]
22
39
  end
23
40
 
24
41
  def send_reset_password_email
25
42
  client.forgot_password loginId: login_id
43
+ reset_attrs
26
44
  end
27
45
 
28
46
  def deactivate
29
47
  client.deactivate_user user_id
48
+ reset_attrs
30
49
  end
31
50
 
32
51
  def reactivate
33
52
  client.reactivate_user user_id
53
+ reset_attrs
54
+ end
55
+
56
+ private
57
+
58
+ def update_registration
59
+ os_to_h(registration_data).then do |data|
60
+ yield data
61
+ client.patch_registration user_id, params_for(data)
62
+ end
63
+ reset_attrs
64
+ end
65
+
66
+ def params_for(data)
67
+ { registration:
68
+ { applicationId: application_id,
69
+ data: data } }
70
+ end
71
+
72
+ def reset_attrs
73
+ @attrs = AdminClient.new.proxy_attrs_for(user_id: user_id)
74
+ self
75
+ end
76
+
77
+ ########################################################
78
+ # # EDC: even though the FusionAuth client returns #
79
+ # # an OpenStruct with nested OpenStruct values, the #
80
+ # # patch_registration method does not seem to handle #
81
+ # # nested OpenStruct objects. #
82
+ # # #
83
+ # # this method recurses over these structures and #
84
+ # # turns them into ordinary nested hashes that seem #
85
+ # # to be correctly handled. #
86
+ # # #
87
+ # # see this SO thread for inspiration: #
88
+ # # https://stackoverflow.com/a/62804063/778174 #
89
+ ########################################################
90
+ def os_to_h(obj) # rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity
91
+ case obj
92
+ when Hash, OpenStruct # rubocop:disable Style/OpenStructUse
93
+ obj.to_h.transform_values do |val|
94
+ case val
95
+ when OpenStruct then os_to_h(val) # rubocop:disable Style/OpenStructUse
96
+ when Array
97
+ val.map { |entry| os_to_h(entry) }
98
+ else val
99
+ end
100
+ end
101
+ when Array
102
+ obj.map { |entry| os_to_h(entry) }
103
+ else obj
104
+ end
34
105
  end
35
106
  end
36
107
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OauthIm
4
- VERSION = '0.10.2'
4
+ VERSION = '0.10.3'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oauth_im
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.2
4
+ version: 0.10.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Connally
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-09-08 00:00:00.000000000 Z
11
+ date: 2022-09-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fusionauth_client