Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ before running them.
Version History
---------------

0.2.11 (22/02/2020)

* Added method to create a user by admin and set the status to CONFIRMED - [abhi36](https://github.com/abhi36)

0.2.11 (06/01/2020)

Added method to get a user by an access token - bjoernHeneka
Expand Down
55 changes: 54 additions & 1 deletion src/CognitoClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,48 @@ public function deleteUser($accessToken)
}
}

/**
* @param string $username
* @param string $password
* @param array $userAttributes
* @param boolean $confirmSignup
* @return object
* @throws Exception
*/
public function adminCreateUser($username, $password, $attributes = [], $confirmSignup = true)
{
try {
$registeredUser = $this->client->adminCreateUser([
'UserPoolId' => $this->userPoolId,
'Username' => $username,
'TemporaryPassword' => $password,
'UserAttributes' => $attributes,
'MessageAction' => "SUPPRESS",
'DesiredDeliveryMediums' => ["EMAIL"]
]);

/**
* Auto confirm added user if confirm signup is set to true
*/
if($confirmSignup){
$respAuthenticate = [];
try {
$respAuthenticate = $this->authenticate($username, $password);
} catch (ChallengeException $e) {
if ($e->getChallengeName() === self::CHALLENGE_NEW_PASSWORD_REQUIRED) {
$respAuthenticate = $this->respondToNewPasswordRequiredChallenge($username, $password, $e->getSession());
}
}
return $respAuthenticate;
}

return $registeredUser;
} catch (CognitoIdentityProviderException $e) {
throw CognitoResponseException::createFromCognitoException($e);
}
}


/**
* @param string $username
* @throws Exception
Expand Down Expand Up @@ -290,7 +332,18 @@ public function addUserToGroup($username, $groupName)
"GroupName" => $groupName
]);
} catch (CognitoIdentityProviderException $e) {
throw CognitoResponseException::createFromCognitoException($e);
if($e->getAwsErrorCode() == "ResourceNotFoundException"){
try{
$this->client->createGroup([
"GroupName" => $groupName,
"UserPoolId" => $this->userPoolId
]);
$this->addUserToGroup($username, $groupName);
} catch( AwsException $ae ){}

}else{
throw CognitoResponseException::createFromCognitoException($e);
}
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/Exception/InvalidParameterException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace pmill\AwsCognito\Exception;

class InvalidParameterException extends CognitoResponseException
{

}
7 changes: 7 additions & 0 deletions src/Exception/NotAuthorizedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace pmill\AwsCognito\Exception;

class NotAuthorizedException extends \Exception
{

}