https://github.com/piko-framework/user
https://github.com/piko-framework/user
Last synced: over 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/piko-framework/user
- Owner: piko-framework
- License: lgpl-3.0
- Created: 2021-09-25T10:54:45.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2022-11-04T14:35:04.000Z (over 3 years ago)
- Last Synced: 2025-03-05T18:24:24.622Z (over 1 year ago)
- Language: PHP
- Size: 18.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Piko User
[](https://github.com/piko-framework/user/actions/workflows/php.yml)
[](https://coveralls.io/github/piko-framework/user?branch=main)
A lightweight user session manager to login/logout, check permissions and retrieve user identity between sessions.
# Installation
It's recommended that you use Composer to install Piko User.
```bash
composer require piko/user
```
# Usage
Basic exemple:
```php
require 'vendor/autoload.php';
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';
}
```
Advanced example: [See UserTest.php](tests/UserTest.php)