php-auth
Simply authentication library, (*1)
Key points:
- Supports drivers ('file' and 'database' adapters already included in the package).
- Easy to use
- Minimum configuration, (*2)
Installation
This package is available via Composer:, (*3)
{
"require": {
"dmitrymomot/php-auth": "1.*"
}
}
Example of usage
$auth = new \Auth\Adapter\File;
$auth->setUsers(array('test_user' => array('password' => 'hashed_password', 'role' => 'user')));
Log in, (*4)
$auth->login('test_user', 'password'); // returns boolean value
Get user, (*5)
$auth->getUser('guest') // returns current username or 'guest', if user isn't logged in
Get user role, (*6)
$auth->getRole() // returns string
Check log in, (*7)
$auth->loggedIn() // returned true
Check log in as, (*8)
$auth->loggedIn('admin') // returned false
$auth->loggedIn('user') // returned true
Log out, (*9)
$auth->logout(); // returns boolean value
Log in as another user, (*10)
$auth->loginAs('username'); // returns boolean value
Come back to initial user, (*11)
$auth->comeBack(); // returns boolean value
(supports all the same that 'file' adapter), (*12)
In composer.json add package php-activerecord/php-activerecord, (*13)
"require": {
"dmitrymomot/php-session": "1.*",
"php-activerecord/php-activerecord":"dev-master"
},
and update composer., (*14)
Set database config (read more in php-activerecord docs), (*15)
$cfg = \ActiveRecord\Config::instance();
$cfg->set_connections(array(
'development' => 'mysql://username_for_dev:password@localhost/username_for_dev',
'production' => 'mysql://username:password@localhost/database_name'
));
Initialization, (*16)
$auth = new \Auth\Adapter\Database();
Initialization with custom model User, (*17)
class CustomUser implements \Auth\Model\UserInterface {
//....realisation of interface
}
$model = '\Custom\Model\CustomUser'; // full path to class
$auth = new \Auth\Adapter\Database($model);
Get user, (*18)
$auth->getUser('guest') // returns instance of class \Auth\Model\User or 'guest', if user isn't logged in
Create new user, (*19)
$auth->createUser(array('username' => 'test_user', 'password' => 'some_password', 'email' => 'user@mail.com', ...)); // returns boolean value or array error messages
Update current user, (*20)
$auth->updateUser(array('username' => 'test_user', 'password' => 'some_password', ....)); // returns boolean value or array error messages
Helpers
echo \Auth\Auth::hash('admin'); // returns hashed string 'admin'
Also you can set hash key, (*21)
\Auth\Auth::hashKey = 'vv34r3v4c34r';
License
The MIT License (MIT). Please see License File for more information., (*22)