PHP code example of piko / user
1. Go to this page and download the library: Download piko/user 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/ */
piko / user example snippets
use Piko\User;
use Piko\User\IdentityInterface;
// Define first your user identity class
class Identity implements IdentityInterface
{
private static $users = [
1 => 'paul',
2 => 'pierre',
];
public $id;
public $username;
public static function findIdentity($id)
{
if (isset(static::$users[$id])) {
$user = new static();
$user->id = $id;
$user->username = static::$users[$id];
return $user;
}
return null;
}
public function getId()
{
return $this->id;
}
}
$user = new User([
'identityClass' => Identity::class,
'checkAccess' => function($id, $permission) {
return $id == 1 && $permission == 'test';
}
]);
// Login
$user->login(Identity::findIdentity(1));
if (!$user->isGuest()) {
echo $user->getIdentity()->username; // paul
}
if ($user->can('test')) {
echo 'I can test';
}
$user->logout();
if ($user->isGuest()) {
var_dump($user->getIdentity()); // null
echo 'Not Authenticated';
}
if (!$user->can('test')) {
echo 'I cannot test';
}