Skip to content

Commit

Permalink
Add "role" to organization membership and directory sync user (#309)
Browse files Browse the repository at this point in the history
  • Loading branch information
ameesha authored Jun 24, 2024
1 parent 15c2449 commit 14285a2
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 32 deletions.
4 changes: 3 additions & 1 deletion lib/workos/directory_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class DirectoryUser < DeprecatedHashWrapper
include HashProvider

attr_accessor :id, :idp_id, :emails, :first_name, :last_name, :job_title, :username, :state,
:groups, :custom_attributes, :raw_attributes, :directory_id, :organization_id,
:groups, :role, :custom_attributes, :raw_attributes, :directory_id, :organization_id,
:created_at, :updated_at

# rubocop:disable Metrics/AbcSize
Expand All @@ -26,6 +26,7 @@ def initialize(json)
@username = hash[:username]
@state = hash[:state]
@groups = hash[:groups]
@role = hash[:role]
@custom_attributes = hash[:custom_attributes]
@raw_attributes = hash[:raw_attributes]
@created_at = hash[:created_at]
Expand All @@ -48,6 +49,7 @@ def to_json(*)
username: username,
state: state,
groups: groups,
role: role,
custom_attributes: custom_attributes,
raw_attributes: raw_attributes,
created_at: created_at,
Expand Down
4 changes: 3 additions & 1 deletion lib/workos/organization_membership.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module WorkOS
class OrganizationMembership
include HashProvider

attr_accessor :id, :user_id, :organization_id, :status, :created_at, :updated_at
attr_accessor :id, :user_id, :organization_id, :status, :role, :created_at, :updated_at

def initialize(json)
hash = JSON.parse(json, symbolize_names: true)
Expand All @@ -16,6 +16,7 @@ def initialize(json)
@user_id = hash[:user_id]
@organization_id = hash[:organization_id]
@status = hash[:status]
@role = hash[:role]
@created_at = hash[:created_at]
@updated_at = hash[:updated_at]
end
Expand All @@ -26,6 +27,7 @@ def to_json(*)
user_id: user_id,
organization_id: organization_id,
status: status,
role: role,
created_at: created_at,
updated_at: updated_at,
}
Expand Down
25 changes: 24 additions & 1 deletion lib/workos/user_management.rb
Original file line number Diff line number Diff line change
Expand Up @@ -841,14 +841,37 @@ def list_organization_memberships(options = {})
#
# @param [String] user_id The ID of the User.
# @param [String] organization_id The ID of the Organization to which the user belongs to.
# @param [String] role_slug The slug of the role to grant to this membership. (Optional)
#
# @return [WorkOS::OrganizationMembership]
def create_organization_membership(user_id:, organization_id:)
def create_organization_membership(user_id:, organization_id:, role_slug: nil)
request = post_request(
path: '/user_management/organization_memberships',
body: {
user_id: user_id,
organization_id: organization_id,
role_slug: role_slug,
},
auth: true,
)

response = execute_request(request: request)

WorkOS::OrganizationMembership.new(response.body)
end

# Update an Organization Membership
#
# @param [String] organization_membership_id The ID of the Organization Membership.
# @param [String] role_slug The slug of the role to grant to this membership.
#
# @return [WorkOS::OrganizationMembership]
def update_organization_membership(organization_membership_id:, role_slug:)
request = put_request(
path: "/user_management/organization_memberships/#{id}",
body: {
organization_membership_id: organization_membership_id,
role_slug: role_slug,
},
auth: true,
)
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/workos/directory_sync_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -462,10 +462,10 @@
'directory_user_01FAZYNPC8M0HRYTKFP2GNX852',
)

expect(user['first_name']).to eq('Logan')
expect(user['first_name']).to eq('Bob')
expect(user.directory_id).to eq('directory_01FAZYMST676QMTFN1DDJZZX87')
expect(user.organization_id).to eq('org_01FAZWCWR03DVWA83NCJYKKD54')
expect(user.first_name).to eq('Logan')
expect(user.first_name).to eq('Bob')
end
end
end
Expand Down
28 changes: 22 additions & 6 deletions spec/lib/workos/directory_user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,47 @@
describe '.get_primary_email' do
context 'with one primary email' do
it 'returns the primary email' do
user = WorkOS::DirectoryUser.new('{"object":"directory_user","id":"directory_user_01FAZYNPC8M0HRYTKFP2GNX852","directory_id":"directory_01FAZYMST676QMTFN1DDJZZX87","idp_id":"6092c280a3f1e19ef6d8cef8","username":"logan@workos.com","emails":[{"primary":true,"value":"logan@workos.com"}, {"primary":false,"value":"logan@gmail.com"}],"first_name":"Logan","last_name":"Gingerich","job_title":"Developer Success Engineer","state":"active","raw_attributes":{},"custom_attributes":{},"groups":[],"created_at":"2022-05-13T17:45:31.732Z", "updated_at":"2022-07-13T17:45:42.618Z"}')
expect(user.primary_email).to eq('logan@workos.com')
user = WorkOS::DirectoryUser.new('{"object":"directory_user","id":"directory_user_01FAZYNPC8M0HRYTKFP2GNX852","directory_id":"directory_01FAZYMST676QMTFN1DDJZZX87","idp_id":"6092c280a3f1e19ef6d8cef8","username":"bob.fakename@workos.com","emails":[{"primary":true,"value":"bob.fakename@workos.com"}, {"primary":false,"value":"Bob@gmail.com"}],"first_name":"Bob","last_name":"Fakename","job_title":"Developer Success Engineer","state":"active","raw_attributes":{},"custom_attributes":{},"groups":[],"created_at":"2022-05-13T17:45:31.732Z", "updated_at":"2022-07-13T17:45:42.618Z"}')
expect(user.primary_email).to eq('bob.fakename@workos.com')
end
end

context 'with multiple primary emails' do
it 'returns the first email marked as primary' do
user = WorkOS::DirectoryUser.new('{"object":"directory_user","id":"directory_user_01FAZYNPC8M0HRYTKFP2GNX852","directory_id":"directory_01FAZYMST676QMTFN1DDJZZX87","idp_id":"6092c280a3f1e19ef6d8cef8","username":"logan@workos.com","emails":[{"primary":true,"value":"logan@workos.com"}, {"primary":true,"value":"logan@gmail.com"}],"first_name":"Logan","last_name":"Gingerich","job_title":"Developer Success Engineer","state":"active","raw_attributes":{},"custom_attributes":{},"groups":[],"created_at":"2022-05-13T17:45:31.732Z", "updated_at":"2022-07-13T17:45:42.618Z"}')
expect(user.primary_email).to eq('logan@workos.com')
user = WorkOS::DirectoryUser.new('{"object":"directory_user","id":"directory_user_01FAZYNPC8M0HRYTKFP2GNX852","directory_id":"directory_01FAZYMST676QMTFN1DDJZZX87","idp_id":"6092c280a3f1e19ef6d8cef8","username":"bob.fakename@workos.com","emails":[{"primary":true,"value":"bob.fakename@workos.com"}, {"primary":true,"value":"Bob@gmail.com"}],"first_name":"Bob","last_name":"Fakename","job_title":"Developer Success Engineer","state":"active","raw_attributes":{},"custom_attributes":{},"groups":[],"created_at":"2022-05-13T17:45:31.732Z", "updated_at":"2022-07-13T17:45:42.618Z"}')
expect(user.primary_email).to eq('bob.fakename@workos.com')
end
end

context 'with no primary emails' do
it 'returns nil' do
user = WorkOS::DirectoryUser.new('{"object":"directory_user","id":"directory_user_01FAZYNPC8M0HRYTKFP2GNX852","directory_id":"directory_01FAZYMST676QMTFN1DDJZZX87","idp_id":"6092c280a3f1e19ef6d8cef8","username":"logan@workos.com","emails":[{"primary":false,"value":"logan@gmail.com"}],"first_name":"Logan","last_name":"Gingerich","job_title":"Developer Success Engineer","state":"active","raw_attributes":{},"custom_attributes":{},"groups":[],"created_at":"2022-05-13T17:45:31.732Z","updated_at":"2022-07-13T17:45:42.618Z"}')
user = WorkOS::DirectoryUser.new('{"object":"directory_user","id":"directory_user_01FAZYNPC8M0HRYTKFP2GNX852","directory_id":"directory_01FAZYMST676QMTFN1DDJZZX87","idp_id":"6092c280a3f1e19ef6d8cef8","username":"bob.fakename@workos.com","emails":[{"primary":false,"value":"Bob@gmail.com"}],"first_name":"Bob","last_name":"Fakename","job_title":"Developer Success Engineer","state":"active","raw_attributes":{},"custom_attributes":{},"groups":[],"created_at":"2022-05-13T17:45:31.732Z","updated_at":"2022-07-13T17:45:42.618Z"}')
expect(user.primary_email).to eq(nil)
end
end

context 'with an empty email array' do
it 'returns nil' do
user = WorkOS::DirectoryUser.new('{"object":"directory_user","id":"directory_user_01FAZYNPC8M0HRYTKFP2GNX852","directory_id":"directory_01FAZYMST676QMTFN1DDJZZX87","idp_id":"6092c280a3f1e19ef6d8cef8","username":"logan@workos.com","emails":[],"first_name":"Logan","last_name":"Gingerich","job_title":"Developer Success Engineer","state":"active","raw_attributes":{},"custom_attributes":{},"groups":[],"created_at":"2022-05-13T17:45:31.732Z","updated_at":"2022-07-13T17:45:42.618Z"}')
user = WorkOS::DirectoryUser.new('{"object":"directory_user","id":"directory_user_01FAZYNPC8M0HRYTKFP2GNX852","directory_id":"directory_01FAZYMST676QMTFN1DDJZZX87","idp_id":"6092c280a3f1e19ef6d8cef8","username":"bob.fakename@workos.com","emails":[],"first_name":"Bob","last_name":"Fakename","job_title":"Developer Success Engineer","state":"active","raw_attributes":{},"custom_attributes":{},"groups":[],"created_at":"2022-05-13T17:45:31.732Z","updated_at":"2022-07-13T17:45:42.618Z"}')
expect(user.primary_email).to eq(nil)
end
end
end

describe '.get_role' do
context 'with no role' do
it 'returns no role' do
user = WorkOS::DirectoryUser.new('{"object":"directory_user","id":"directory_user_01FAZYNPC8M0HRYTKFP2GNX852","directory_id":"directory_01FAZYMST676QMTFN1DDJZZX87","idp_id":"6092c280a3f1e19ef6d8cef8","username":"[email protected]","emails":[{"primary":true,"value":"[email protected]"}, {"primary":false,"value":"[email protected]"}],"first_name":"Bob","last_name":"Gingerich","job_title":"Developer Success Engineer","state":"active","raw_attributes":{},"custom_attributes":{},"groups":[],"created_at":"2022-05-13T17:45:31.732Z", "updated_at":"2022-07-13T17:45:42.618Z"}')
expect(user.role).to eq(nil)
end
end

context 'with a role' do
it 'returns the role slug' do
user = WorkOS::DirectoryUser.new('{"object":"directory_user","id":"directory_user_01FAZYNPC8M0HRYTKFP2GNX852","directory_id":"directory_01FAZYMST676QMTFN1DDJZZX87","idp_id":"6092c280a3f1e19ef6d8cef8","username":"[email protected]","emails":[{"primary":true,"value":"[email protected]"}, {"primary":false,"value":"[email protected]"}],"first_name":"Bob","last_name":"Gingerich","job_title":"Developer Success Engineer","state":"active","raw_attributes":{},"custom_attributes":{},"groups":[],"role":{"slug":"member"},"created_at":"2022-05-13T17:45:31.732Z", "updated_at":"2022-07-13T17:45:42.618Z"}')
expect(user.role).to eq({ slug: 'member' })
end
end
end
# rubocop:enable Layout/LineLength
end
37 changes: 27 additions & 10 deletions spec/lib/workos/user_management_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1039,19 +1039,36 @@

expect(organization_membership.organization_id).to eq('organization_01H5JQDV7R7ATEYZDEG0W5PRYS')
expect(organization_membership.user_id).to eq('user_01H5JQDV7R7ATEYZDEG0W5PRYS')
expect(organization_membership.role).to eq({ slug: 'member' })
end
end
end

context 'with an invalid payload' do
it 'returns an error' do
VCR.use_cassette 'user_management/create_organization_membership/invalid' do
expect do
described_class.create_organization_membership(user_id: '', organization_id: '')
end.to raise_error(
WorkOS::UnprocessableEntityError,
/user_id_string_required/,
)
end
context 'with an invalid payload' do
it 'returns an error' do
VCR.use_cassette 'user_management/create_organization_membership/invalid' do
expect do
described_class.create_organization_membership(user_id: '', organization_id: '')
end.to raise_error(
WorkOS::UnprocessableEntityError,
/user_id_string_required/,
)
end
end
end

context 'with a role slug' do
it 'creates an organization with the given role slug ' do
VCR.use_cassette 'user_management/create_organization_membership/valid' do
organization_membership = described_class.create_organization_membership(
user_id: 'user_01H5JQDV7R7ATEYZDEG0W5PRYS',
organization_id: 'org_01H5JQDV7R7ATEYZDEG0W5PRYS',
role_slug: 'member',
)

expect(organization_membership.organization_id).to eq('organization_01H5JQDV7R7ATEYZDEG0W5PRYS')
expect(organization_membership.user_id).to eq('user_01H5JQDV7R7ATEYZDEG0W5PRYS')
expect(organization_membership.role).to eq({ slug: 'member' })
end
end
end
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 14285a2

Please sign in to comment.