Skip to content

Commit 33da197

Browse files
authored
AuthKit multiple roles support (#397)
* AuthKit multiple roles support
1 parent 014332f commit 33da197

File tree

8 files changed

+222
-15
lines changed

8 files changed

+222
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,4 @@
4949
# .rubocop-https?--*
5050

5151
.vscode
52+
.idea/

lib/workos/organization_membership.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module WorkOS
77
class OrganizationMembership
88
include HashProvider
99

10-
attr_accessor :id, :user_id, :organization_id, :status, :role, :created_at, :updated_at
10+
attr_accessor :id, :user_id, :organization_id, :status, :role, :roles, :created_at, :updated_at
1111

1212
def initialize(json)
1313
hash = JSON.parse(json, symbolize_names: true)
@@ -17,6 +17,7 @@ def initialize(json)
1717
@organization_id = hash[:organization_id]
1818
@status = hash[:status]
1919
@role = hash[:role]
20+
@roles = hash[:roles]
2021
@created_at = hash[:created_at]
2122
@updated_at = hash[:updated_at]
2223
end
@@ -28,6 +29,7 @@ def to_json(*)
2829
organization_id: organization_id,
2930
status: status,
3031
role: role,
32+
roles: roles,
3133
created_at: created_at,
3234
updated_at: updated_at,
3335
}

lib/workos/session.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def initialize(user_management:, client_id:, session_data:, cookie_password:)
3030

3131
# Authenticates the user based on the session data
3232
# @return [Hash] A hash containing the authentication response and a reason if the authentication failed
33+
# rubocop:disable Metrics/AbcSize
3334
def authenticate
3435
return { authenticated: false, reason: 'NO_SESSION_COOKIE_PROVIDED' } if @session_data.nil?
3536

@@ -49,6 +50,7 @@ def authenticate
4950
session_id: decoded['sid'],
5051
organization_id: decoded['org_id'],
5152
role: decoded['role'],
53+
roles: decoded['roles'],
5254
permissions: decoded['permissions'],
5355
entitlements: decoded['entitlements'],
5456
feature_flags: decoded['feature_flags'],
@@ -64,7 +66,6 @@ def authenticate
6466
# @option options [String] :organization_id The organization ID to use for refreshing the session
6567
# @return [Hash] A hash containing a new sealed session, the authentication response,
6668
# and a reason if the refresh failed
67-
# rubocop:disable Metrics/AbcSize
6869
# rubocop:disable Metrics/PerceivedComplexity
6970
def refresh(options = nil)
7071
cookie_password = options.nil? || options[:cookie_password].nil? ? @cookie_password : options[:cookie_password]

lib/workos/user_management.rb

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -926,16 +926,23 @@ def list_organization_memberships(options = {})
926926
# @param [String] user_id The ID of the User.
927927
# @param [String] organization_id The ID of the Organization to which the user belongs to.
928928
# @param [String] role_slug The slug of the role to grant to this membership. (Optional)
929+
# @param [Array<String>] role_slugs Array of role slugs to assign to this membership. (Optional)
929930
#
930931
# @return [WorkOS::OrganizationMembership]
931-
def create_organization_membership(user_id:, organization_id:, role_slug: nil)
932+
def create_organization_membership(user_id:, organization_id:, role_slug: nil, role_slugs: nil)
933+
raise ArgumentError, 'Cannot specify both role_slug and role_slugs' if role_slug && role_slugs
934+
935+
body = {
936+
user_id: user_id,
937+
organization_id: organization_id,
938+
}
939+
940+
body[:role_slugs] = role_slugs if role_slugs
941+
body[:role_slug] = role_slug if role_slug
942+
932943
request = post_request(
933944
path: '/user_management/organization_memberships',
934-
body: {
935-
user_id: user_id,
936-
organization_id: organization_id,
937-
role_slug: role_slug,
938-
}.compact,
945+
body: body.compact,
939946
auth: true,
940947
)
941948

@@ -946,17 +953,22 @@ def create_organization_membership(user_id:, organization_id:, role_slug: nil)
946953

947954
# Update an Organization Membership
948955
#
949-
# @param [String] organization_membership_id The ID of the Organization Membership.
950-
# @param [String] role_slug The slug of the role to grant to this membership.
956+
# @param [String] id The ID of the Organization Membership.
957+
# @param [String] role_slug The slug of the role to grant to this membership. (Optional)
958+
# @param [Array<String>] role_slugs Array of role slugs to assign to this membership. (Optional)
951959
#
952960
# @return [WorkOS::OrganizationMembership]
953-
def update_organization_membership(id:, role_slug:)
961+
def update_organization_membership(id:, role_slug: nil, role_slugs: nil)
962+
raise ArgumentError, 'Cannot specify both role_slug and role_slugs' if role_slug && role_slugs
963+
964+
body = { id: id }
965+
966+
body[:role_slugs] = role_slugs if role_slugs
967+
body[:role_slug] = role_slug if role_slug
968+
954969
request = put_request(
955970
path: "/user_management/organization_memberships/#{id}",
956-
body: {
957-
id: id,
958-
role_slug: role_slug,
959-
},
971+
body: body.compact,
960972
auth: true,
961973
)
962974

spec/lib/workos/session_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
sid: 'session_id',
109109
org_id: 'org_id',
110110
role: 'role',
111+
roles: ['role'],
111112
permissions: ['read'],
112113
exp: Time.now.to_i + 3600,
113114
}
@@ -173,6 +174,7 @@
173174
session_id: 'session_id',
174175
organization_id: 'org_id',
175176
role: 'role',
177+
roles: ['role'],
176178
permissions: ['read'],
177179
feature_flags: nil,
178180
entitlements: nil,
@@ -188,6 +190,7 @@
188190
sid: 'session_id',
189191
org_id: 'org_id',
190192
role: 'role',
193+
roles: ['role'],
191194
permissions: ['read'],
192195
entitlements: ['billing'],
193196
exp: Time.now.to_i + 3600,
@@ -208,6 +211,7 @@
208211
session_id: 'session_id',
209212
organization_id: 'org_id',
210213
role: 'role',
214+
roles: ['role'],
211215
permissions: ['read'],
212216
entitlements: ['billing'],
213217
feature_flags: nil,
@@ -224,6 +228,7 @@
224228
sid: 'session_id',
225229
org_id: 'org_id',
226230
role: 'role',
231+
roles: ['role'],
227232
permissions: ['read'],
228233
feature_flags: ['new_feature_enabled'],
229234
exp: Time.now.to_i + 3600,
@@ -244,6 +249,7 @@
244249
session_id: 'session_id',
245250
organization_id: 'org_id',
246251
role: 'role',
252+
roles: ['role'],
247253
permissions: ['read'],
248254
entitlements: nil,
249255
feature_flags: ['new_feature_enabled'],

spec/lib/workos/user_management_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,6 +1302,23 @@
13021302
end
13031303
end
13041304
end
1305+
1306+
context 'with role slugs' do
1307+
it 'creates an organization membership with multiple roles' do
1308+
VCR.use_cassette 'user_management/create_organization_membership/valid_multiple_roles' do
1309+
organization_membership = described_class.create_organization_membership(
1310+
user_id: 'user_01H5JQDV7R7ATEYZDEG0W5PRYS',
1311+
organization_id: 'org_01H5JQDV7R7ATEYZDEG0W5PRYS',
1312+
role_slugs: %w[admin member],
1313+
)
1314+
1315+
expect(organization_membership.organization_id).to eq('organization_01H5JQDV7R7ATEYZDEG0W5PRYS')
1316+
expect(organization_membership.user_id).to eq('user_01H5JQDV7R7ATEYZDEG0W5PRYS')
1317+
expect(organization_membership.roles).to be_an(Array)
1318+
expect(organization_membership.roles.length).to eq(2)
1319+
end
1320+
end
1321+
end
13051322
end
13061323

13071324
describe '.update_organization_membership' do
@@ -1329,6 +1346,22 @@
13291346
end
13301347
end
13311348
end
1349+
1350+
context 'with role slugs' do
1351+
it 'updates an organization membership with multiple roles' do
1352+
VCR.use_cassette('user_management/update_organization_membership/valid_multiple_roles') do
1353+
organization_membership = WorkOS::UserManagement.update_organization_membership(
1354+
id: 'om_01H5JQDV7R7ATEYZDEG0W5PRYS',
1355+
role_slugs: %w[admin editor],
1356+
)
1357+
1358+
expect(organization_membership.organization_id).to eq('organization_01H5JQDV7R7ATEYZDEG0W5PRYS')
1359+
expect(organization_membership.user_id).to eq('user_01H5JQDV7R7ATEYZDEG0W5PRYS')
1360+
expect(organization_membership.roles).to be_an(Array)
1361+
expect(organization_membership.roles.length).to eq(2)
1362+
end
1363+
end
1364+
end
13321365
end
13331366

13341367
describe '.delete_organization_membership' do

spec/support/fixtures/vcr_cassettes/user_management/create_organization_membership/valid_multiple_roles.yml

Lines changed: 76 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/support/fixtures/vcr_cassettes/user_management/update_organization_membership/valid_multiple_roles.yml

Lines changed: 76 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)