9951 explained code solutions for 126 technologies


php-awsHow do I use Amazon's PHP SDK to ban a user?


Using Amazon's PHP SDK to ban a user requires the following steps:

  1. Include the Amazon PHP SDK library in your project:
require 'vendor/autoload.php';
  1. Create an Amazon Cognito Identity client. This is used to access the Amazon Cognito user pool:
$cognitoClient = new CognitoIdentityClient([
    'region' => 'us-east-1',
    'version' => '2016-04-18',
    'credentials' => [
        'key'    => 'YOUR_AWS_ACCESS_KEY_ID',
        'secret' => 'YOUR_AWS_SECRET_ACCESS_KEY',
    ],
]);
  1. Get the user pool ID and the user pool client ID from the AWS console:
$userPoolId = 'YOUR_USER_POOL_ID';
$clientId = 'YOUR_CLIENT_ID';
  1. Create an Amazon Cognito user pool client:
$userPoolClient = new CognitoUserPool($cognitoClient, $userPoolId, $clientId);
  1. Get the user to ban:
$userToBan = $userPoolClient->getUser('USERNAME');
  1. Ban the user:
$userToBan->adminDisableUser();
  1. Check if the user has been successfully banned:
if ($userToBan->isEnabled()) {
    echo 'User is not banned.';
} else {
    echo 'User is banned.';
}

Output example

User is banned.

Helpful links

Edit this code on GitHub