PHP code example of pauldevelop / library-auth
1. Go to this page and download the library: Download pauldevelop/library-auth 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/ */
pauldevelop / library-auth example snippets
$variables = new VariableCollection();
$variables->add(new Variable('baseHost', 'example.com'));
$authorisator = new Authorisator(
new MyRoleChecker(), // implements IRoleChecker
$variables
);
class MyRoleChecker implements IRoleChecker
{
/**
* @var model\ImpersonationPeer
*/
private $impersonationPeer;
/**
* @var model\RolePeer
*/
private $rolePeer;
public function __construct(model\ImpersonationPeer $impersonationPeer = null, model\RolePeer $rolePeer = null)
{
$this->impersonationPeer = $impersonationPeer;
$this->rolePeer = $rolePeer;
}
public function check($id = 0, $roleName = '')
{
// init
$result = false;
// action
$roles = $this->rolePeer->queryPath('role[@name='.$roleName.']#');
if (count($roles) == 1) {
/** @var model\Role $role */
$role = $roles[0];
$impersonations = $this->impersonationPeer->queryPath(
'impersonation[@user='.$id.',@role='.$role->Id.']#'
);
if (count($impersonations) == 1) {
$result = true;
}
}
// return
return $result;
}
}
$authorisator->addAccessRestriction(
new AccessRestriction(
'backend.%baseHost%/*',
new CredentialAuthenticator(), // implements IAuthenticator
'Administrator',
'http://backend.%baseHost%/login/',
null,
array(
'backend.%baseHost%/login/',
'backend.%baseHost%/login/process/'
)
)
);
class CredentialAuthenticator implements IAuthenticator
{
/**
* @var IPathQueryable
*/
private $storage;
public function __construct(IPathQueryable $storage = null)
{
$this->storage = $storage;
}
/**
* @param Entity $credentialEntity
*
* @return bool
*/
public function check(Entity $credentialEntity)
{
// init
$result = false;
// action
if (($nameProperty = $credentialEntity->Properties['name']) !== null) {
/** @var IPathQueryable $storage */
$storage = $this->storage;
$userList = $storage->queryPath('user[@name='.$nameProperty->Value.']#');
if ( sizeof($userList) > 0 ) {
/** @var Entity $userEntity */
$userEntity = $userList[0];
if ($userEntity != null
&& password_verify(
$credentialEntity->Properties['password']->Value,
$storageEntity->Properties['password']->Value
)
) {
$result = $storageEntity->Properties['id']->Value;
}
}
}
// return
return $result;
}
}
// setup credentials
$credentials = new Entity();
$credentials->Properties = new IPropertyCollection();
$result->Properties->add(new Property('name', $name), 'name');
$result->Properties->add(new Property('password', $password), 'password');
if ($authorisator->check($url, $credentials)) {
// load protected content
}