PHP code example of ccasanovas / cake-cognito

1. Go to this page and download the library: Download ccasanovas/cake-cognito library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ccasanovas / cake-cognito example snippets


'CognitoSDK' => [
    'AccessKeys' => [
        'id' => 'NSWPXE30F49XAOF',
        'secret' => 'QIQNxRO2425bb040e4adc8cc02fae05063c3c'
    ],
    'UserPool' => [
        'id' => 'us-east-2_rjaob1HaR',
    ],
    'IdentityProviderClient' => [
        'settings' => [], //https://docs.aws.amazon.com/sdkforruby/api/Aws/CognitoIdentityProvider/Client.html#initialize-instance_method
    ],
],
'ApiUsers' => [
    /* available user roles */
    'roles' => [
        'user' => __d('Ccasanovas/CognitoSDK', 'API User'),
    ],
    /* the max amount of errors alloweds before the validation process of the imported data is halted */
    'import_max_errors' => 10,

    /* the limit of accepted rows in the importing CSV data */
    'import_max_rows' => 500,
]

    $routes->connect('/api-users/*', [
        'plugin' => 'Ccasanovas/CognitoSDK', 'controller' => 'ApiUsers'
    ]);
    $routes->connect('/api-users/:action', [
        'plugin' => 'Ccasanovas/CognitoSDK', 'controller' => 'ApiUsers', 'action' => ':action'
    ]);
    $routes->connect('/api-users/:action/:id', [
        'plugin' => 'Ccasanovas/CognitoSDK', 'controller' => 'ApiUsers', 'action' => ':action'
    ],  ['id' => '\d+', 'pass' => ['id']]);

    

    Router::prefix('api', function ($routes) {
        $routes->extensions(['json']);

         /* Api Users */
         $routes->connect('/me', [
            'plugin'     => 'Ccasanovas/CognitoSDK',
            'prefix'     => 'Api',
            'controller' => 'ApiUsers',
            'action'     => 'profile',
            '_method'    => 'GET'
        ]);

        $routes->connect('/me', [
            'plugin'     => 'Ccasanovas/CognitoSDK',
            'prefix'     => 'Api',
            'controller' => 'ApiUsers',
            'action'     => 'editProfile',
            '_method'    => 'PATCH'
        ]);

        $routes->connect('/me/avatar', [
            'plugin'     => 'Ccasanovas/CognitoSDK',
            'prefix'     => 'Api',
            'controller' => 'ApiUsers',
            'action'     => 'uploadAvatar',
            '_method'    => 'PUT'
        ]);

        $routes->connect('/me/email', [
            'plugin'     => 'Ccasanovas/CognitoSDK',
            'prefix'     => 'Api',
            'controller' => 'ApiUsers',
            'action'     => 'changeEmail',
            '_method'    => 'PUT'
        ]);

    });
    

//archivo: src/Model/Table/ApiUsersTable.php
namespace App\Model\Table;

use Ccasanovas\CognitoSDK\Model\Entity\ApiUser;
use Ccasanovas\CognitoSDK\Model\Table\ApiUsersTable as AwsApiUsersTable;

class ApiUsersTable extends AwsApiUsersTable
{
    //esto solo es necesario si no se va a extender la Entidad
    protected $_entityClass = 'Ccasanovas\CognitoSDK\Model\Entity\ApiUser';

    public function initialize(array $config)
    {
        parent::initialize($config);

        //cambiando la tabla de usuarioa administradores
        $this->association('Creators')->className('AppUsers');
        $this->association('Modifiers')->className('AppUsers');

        //agregar nuevas asociaciones aca
         $this->belongsToMany('FunctionalUnits', [
            'through' => 'ApiUsersFunctionalUnits',
            'saveStrategy' =>'append',
        ]);

        //y podemos facilmente reconfigurar el search cambiando estos valores
        $this->searchQueryFields = [
            'ApiUsers.aws_cognito_username',
            'ApiUsers.email',
            'ApiUsers.phone',
            'ApiUsers.first_name',
            'ApiUsers.last_name',
        ];
    }

    //extendiendo la validación por defecto
    public function validationDefault(Validator $validator)
    {
        $validator = parent::validationDefault($validator);

        //se puede remover la validacion de un campo si no se usa
        $validator->remove('role');

        //y agregar campos nuevos
        $validator
            ->scalar('phone')
            ->allowEmpty('phone');

        return $validator;
    }


}

//archivo: src/Controller/ApiUsersController.php
namespace App\Controller;

use Ccasanovas\CognitoSDK\Controller\ApiUsersController as AwsApiUsersController;
use App\Model\Table\ApiUsersTable;

class ApiUsersController extends AwsApiUsersController
{
    //aca se pueden agregar nuevas acciones o reemplazar las existentes

    //por ejemplo el index:
    public function index()
    {
        $this->paginate['contain'] = ['PointsOfSale'];

        $this->set('api_users', $this->paginate('ApiUsers'));
        $this->set('_serialize', ['api_users']);
    }
}

//archivo: src/Model/Entity/ApiUser.php
namespace App\Model\Entity;

use Ccasanovas\CognitoSDK\Model\Entity\ApiUser as AwsApiUser;

class ApiUser extends AwsApiUser
{
    protected $_accessible = [
        '*' => true,
        'id' => false,
        'role' => false,

        //cognito fields:
        'aws_cognito_username' => false,
        'aws_cognito_id' => false,
        'email' => false,

        //dejando lo demás como viene, podemos agregar nuevos campos
        'phone' => false
    ];

    //podemos agregar nuevas virtual properties aca
}

//el index lleva al nuevo controller
$routes->connect('/api-users', ['controller' => 'ApiUsers', 'action' => 'index']);
$routes->connect('/api-users/index', ['controller' => 'ApiUsers', 'action' => 'index']);

//las demas rutas llevan al controller del plugin
$routes->connect('/api-users/*', [
    'plugin' => 'Ccasanovas/CognitoSDK', 'controller' => 'ApiUsers'
]);
$routes->connect('/api-users/:action', [
    'plugin' => 'Ccasanovas/CognitoSDK', 'controller' => 'ApiUsers', 'action' => ':action'
]);
$routes->connect('/api-users/:action/:id', [
    'plugin' => 'Ccasanovas/CognitoSDK', 'controller' => 'ApiUsers', 'action' => ':action'
],  ['id' => '\d+', 'pass' => ['id']]);