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..feb36aa 100644 --- a/tests/WorkOS/UserManagementTest.php +++ b/tests/WorkOS/UserManagementTest.php @@ -2,14 +2,16 @@ namespace WorkOS; -use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; +use WorkOS\Resource\UserIdentityProviderType; +use PHPUnit\Framework\Attributes\DataProvider; class UserManagementTest extends TestCase { use TestHelper { setUp as traitSetUp; } + private $userManagement; protected function setUp(): void { @@ -1265,6 +1267,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()