diff --git a/README.md b/README.md index ab1e162..53d4a9c 100644 --- a/README.md +++ b/README.md @@ -43,10 +43,6 @@ before running them. Version History --------------- -0.2.11 (06/01/2020) - -Added method to get a user by an access token - bjoernHeneka - 0.2.10 (21/10/2019) * Added tests - [franjid](https://github.com/franjid) diff --git a/src/CognitoClient.php b/src/CognitoClient.php index aa59cbd..8823257 100644 --- a/src/CognitoClient.php +++ b/src/CognitoClient.php @@ -235,7 +235,7 @@ public function getUser($username) 'UserPoolId' => $this->userPoolId, ]); return $response; - } catch (Exception $e) { + } catch (CognitoIdentityProviderException $e) { throw CognitoResponseException::createFromCognitoException($e); } } @@ -259,6 +259,90 @@ public function deleteUser($accessToken) } } + + /** + * Disable a user + * @param string $username + */ + public function adminEnableUser($username) + { + try { + return $this->client->adminEnableUser([ + 'UserPoolId' => $this->userPoolId, + 'Username' => $username + ]); + } catch (CognitoIdentityProviderException $e) { + throw CognitoResponseException::createFromCognitoException($e); + } + } + + /** + * Disable a user + * @param string $username + */ + public function adminDisableUser($username) + { + try { + return $this->client->adminDisableUser([ + 'UserPoolId' => $this->userPoolId, + 'Username' => $username + ]); + } catch (CognitoIdentityProviderException $e) { + throw CognitoResponseException::createFromCognitoException($e); + } + } + + + /** + * @param string $username + * @param string $password + * @param boolean $permanent, decide whether to set this password as parmanent + * + * @return empty body + * @throws Exception + */ + public function adminSetUserPassword($username, $password, $permanent = 0) + { + $permanent = filter_var($permanent, FILTER_VALIDATE_BOOLEAN); + try { + return $this->client->AdminSetUserPassword([ + "Password" => $password, + "Permanent" => $permanent, + "Username" => $username, + "UserPoolId" => $this->userPoolId + ]); + } catch (CognitoIdentityProviderException $e) { + throw CognitoResponseException::createFromCognitoException($e); + } + } + + /** + * @param string $username + * @param string $password + * @param array $userAttributes + * @param boolean $confirmSignup + * @return object + * @throws Exception + */ + public function adminCreateUser($username, $password, $attributes = []) + { + $userAttributes = $this->buildAttributesArray($attributes); + try { + $registeredUser = $this->client->adminCreateUser([ + 'UserPoolId' => $this->userPoolId, + 'Username' => $username, + 'TemporaryPassword' => $password, + 'UserAttributes' => $userAttributes, + 'MessageAction' => "SUPPRESS", + 'DesiredDeliveryMediums' => ["EMAIL"] + ]); + + return $registeredUser; + } catch (CognitoIdentityProviderException $e) { + throw CognitoResponseException::createFromCognitoException($e); + } + } + /** * @param string $username * @throws Exception @@ -276,6 +360,24 @@ public function adminDeleteUser($username) } } + /** + * @param string $username + * @param string $groupName + * @throws Exception + */ + public function removeUserFromGroup($username, $groupName) + { + try { + $this->client->adminRemoveUserFromGroup([ + 'UserPoolId' => $this->userPoolId, + 'Username' => $username, + "GroupName" => $groupName + ]); + } catch (CognitoIdentityProviderException $e) { + throw CognitoResponseException::createFromCognitoException($e); + } + } + /** * @param string $username * @param string $groupName @@ -294,6 +396,24 @@ public function addUserToGroup($username, $groupName) } } + /** + * Create a group for users + * @param string $username + * @param string $groupName + * @throws Exception + */ + public function createGroup($groupName) + { + try { + return $this->client->createGroup([ + "GroupName" => $groupName, + "UserPoolId" => $this->userPoolId + ]); + } catch (CognitoIdentityProviderException $e) { + throw CognitoResponseException::createFromCognitoException($e); + } + } + /** * @param $username * @param array $attributes