Yii2 Authentication.
Yii 2 User Authentication & Role Based Access Control (RBAC) Module, (*1)
Installation
The preferred way to install this extension is through composer., (*2)
Either run, (*3)
php composer.phar require --prefer-dist prawee/yii2-auth-module "*"
or add, (*4)
"prawee/yii2-auth-module": "*"
above file, (*5)
...
"repositories": [
{
"type": "git",
"url": "https://bitbucket.org/prawee/yii2-auth-module.git"
},
],
....
to the require section of your composer.json
file., (*6)
Usage
Once the extension is installed, simply use it in your code by :, (*7)
main.php
<?php
....
'modules'=>[
'auth'=>[
'class'=>'auth\Module',
'tableMap'=>[
'User'=>'user'
],
'modal'=>true
],
],
'components'=>[
'user'=>[
//'identityClass' => 'auth\models\User',
//'enableAutoLogin' => true,
//'loginUrl'=>'auth/default/login',
'class' => 'auth\components\User',
],
'authManager' => [
'class' => 'yii\rbac\DbManager',
],
],
....
?>
params.php
return [
...
'icon-framework'=>'fa',
...
];
Login: localhost/projectname/admin/auth/default/login
Logout: localhost/projectname/admin/auth/default/logout
Signup: localhost/projectname/admin/auth/default/signup
Reset Password: localhost/projectname/admin/auth/resetpassword
Set Permission: localhost/projectname/admin/auth/user
Manage Permission: localhost/projectname/admin/auth/permission
Profile: localhost/projectname/admin/auth/user/profile
Rbac
in console config file main-local.php, (*8)
.....
'components'=>[
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=127.0.0.1;dbname=teachdev',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
'authManager' => [
'class' => 'yii\rbac\DbManager',
],
]
....
Migrate Database
Using command. ( Linux ), (*9)
./yii migrate --migrationPath=@vendor/yiisoft/yii2/rbac/migrations
./yii migrate --migrationPath=@auth/migrations
./yii migrate --migrationPath=@auth/migrations/profile
./yii migrate --migrationPath=@auth/migrations/api
Using command. (Windows), (*10)
yii migrate --migrationPath=@vendor/yiisoft/yii2/rbac/migrations
yii migrate --migrationPath=@auth/migrations
yii migrate --migrationPath=@auth/migrations/profile
yii migrate --migrationPath=@auth/migrations/api
Initializing
1.Create all models, (*11)
localhost/your-project/gii/model
Table Name
*
Click Preview Button
Click Generate Button
2.Create admin\app\models\User model extends by common\models\User, (*12)
localhost/your-project/admin/gii/model
namespace admin\app\models;
use auth\models\User as CUser;
class User extends CUser{}
3.Create admin\app\models\UserSearch by localhost/your-project/admin/gii/crud, (*13)
Model Class
admin\app\models\User
Search Model Class
admin\app\models\UserSearch
Controller Class
admin\app\controllers\UserController
Click Preview Button
Action Unselect all
Check UserSearch only
Click Generate Button
History
branch 1.0.tpt, (*14)