From 857321d68154efd4f1bd2328e3c7815830c73768 Mon Sep 17 00:00:00 2001 From: Braden Keith Date: Sun, 30 Mar 2025 16:31:35 -0400 Subject: [PATCH 1/2] Add UserIdentityProvider and UserIdentityProviderType classes, and implement getUserIdentityProviders method in UserManagement class - Introduced UserIdentityProvider and UserIdentityProviderType classes to represent user identity providers and their types. - Added getUserIdentityProviders method in UserManagement to retrieve a user's identity providers. - Implemented unit tests for the new functionality in UserManagementTest. Resolves #271 --- lib/Resource/UserIdentityProvider.php | 32 +++++++++++++++ lib/Resource/UserIdentityProviderType.php | 39 ++++++++++++++++++ lib/UserManagement.php | 23 +++++++++++ tests/WorkOS/UserManagementTest.php | 49 ++++++++++++++++++++++- 4 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 lib/Resource/UserIdentityProvider.php create mode 100644 lib/Resource/UserIdentityProviderType.php diff --git a/lib/Resource/UserIdentityProvider.php b/lib/Resource/UserIdentityProvider.php new file mode 100644 index 0000000..56e3324 --- /dev/null +++ b/lib/Resource/UserIdentityProvider.php @@ -0,0 +1,32 @@ + "idpId", + "type" => "type", + "provider" => "provider", + ]; + + public static function constructFromResponse($response) + { + $instance = parent::constructFromResponse($response); + + $instance->values["type"] = (string) new UserIdentityProviderType($response["type"]); + + return $instance; + } +} diff --git a/lib/Resource/UserIdentityProviderType.php b/lib/Resource/UserIdentityProviderType.php new file mode 100644 index 0000000..a0f333e --- /dev/null +++ b/lib/Resource/UserIdentityProviderType.php @@ -0,0 +1,39 @@ + self::OAuth, + // Add future types here in the format: 'lowercase_value' => self::ConstantName + ]; + + $lowercaseType = strtolower($type); + + // Use our mapped constant if available, otherwise keep the original value + $this->type = isset($typeMap[$lowercaseType]) ? $typeMap[$lowercaseType] : $type; + } + + public function __toString() + { + return $this->type; + } +} diff --git a/lib/UserManagement.php b/lib/UserManagement.php index f40bd88..a636b66 100644 --- a/lib/UserManagement.php +++ b/lib/UserManagement.php @@ -81,6 +81,29 @@ public function getUser($userId) return Resource\User::constructFromResponse($response); } + /** + * Get User's identity providers + * + * @param string $userId The unique ID of the user. + * + * @throws Exception\WorkOSException + * + * @return Resource\UserIdentityProvider[] + */ + public function getUserIdentityProviders($userId) + { + $path = "user_management/users/{$userId}/identities"; + + $response = Client::request(Client::METHOD_GET, $path, null, null, true); + + $userIdentityProviders = []; + foreach ($response as $responseData) { + \array_push($userIdentityProviders, Resource\UserIdentityProvider::constructFromResponse($responseData)); + } + + return $userIdentityProviders; + } + /** * Get a User by external ID. * diff --git a/tests/WorkOS/UserManagementTest.php b/tests/WorkOS/UserManagementTest.php index 1b6ff72..1032f0e 100644 --- a/tests/WorkOS/UserManagementTest.php +++ b/tests/WorkOS/UserManagementTest.php @@ -2,11 +2,14 @@ namespace WorkOS; -use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; +use WorkOS\Resource\UserIdentityProviderType; +use PHPUnit\Framework\Attributes\DataProvider; class UserManagementTest extends TestCase { + private $userManagement; + use TestHelper { setUp as traitSetUp; } @@ -1265,6 +1268,50 @@ public function testGetLogoutUrlException() } } + public function testGetUserIdentityProviders() + { + $userId = "user_01E4ZCR3C56J083X43JQXF3JK5"; + $path = "user_management/users/{$userId}/identities"; + + $result = $this->userIdentityProvidersResponseFixture(); + + $this->mockRequest( + Client::METHOD_GET, + $path, + null, + null, + true, + $result + ); + + $userIdentityProviderFixture = $this->userIdentityProviderFixture(); + + $response = $this->userManagement->getUserIdentityProviders($userId); + + $this->assertCount(1, $response); + $this->assertSame($userIdentityProviderFixture, $response[0]->toArray()); + } + + private function userIdentityProvidersResponseFixture() + { + return json_encode([ + [ + "idp_id" => "4F42ABDE-1E44-4B66-824A-5F733C037A6D", + "type" => "OAuth", + "provider" => "MicrosoftOAuth" + ] + ]); + } + + private function userIdentityProviderFixture() + { + return [ + "idpId" => "4F42ABDE-1E44-4B66-824A-5F733C037A6D", + "type" => "OAuth", + "provider" => "MicrosoftOAuth" + ]; + } + //Fixtures private function invitationResponseFixture() From bc2f9feb3c5b73f71979abe02dc48ff06fcd195f Mon Sep 17 00:00:00 2001 From: Braden Keith Date: Wed, 2 Apr 2025 21:05:52 -0400 Subject: [PATCH 2/2] Improve code style. --- tests/WorkOS/UserManagementTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/WorkOS/UserManagementTest.php b/tests/WorkOS/UserManagementTest.php index 1032f0e..feb36aa 100644 --- a/tests/WorkOS/UserManagementTest.php +++ b/tests/WorkOS/UserManagementTest.php @@ -8,11 +8,10 @@ class UserManagementTest extends TestCase { - private $userManagement; - use TestHelper { setUp as traitSetUp; } + private $userManagement; protected function setUp(): void {