prawee/yii2-auth-module

Yii 2 用户认证及基于角色的访问控制(RBAC)模块

安装: 28

依赖: 0

建议: 0

安全: 0

类型:yii2-extension

1.2.0 2015-02-20 05:44 UTC

This package is auto-updated.

Last update: 2024-09-24 03:54:31 UTC


README

Yii 2 用户认证及基于角色的访问控制(RBAC)模块

安装

安装此扩展的首选方式是通过composer

运行以下命令:

php composer.phar require --prefer-dist prawee/yii2-auth-module "*"

或者在您的composer.json文件的require部分添加以下文件:

"prawee/yii2-auth-module": "*"

以上文件

...
"repositories": [
        {
            "type": "git",
            "url": "https://bitbucket.org/prawee/yii2-auth-module.git"
        },
    ],
....

使用

安装扩展后,只需在代码中使用即可

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

在控制台配置文件main-local.php中

.....
'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',
        ],
    ]
....

迁移数据库

使用命令。(Linux)

./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

使用命令。(Windows)

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

初始化

1.创建所有模型

localhost/your-project/gii/model

Table Name
*

Click Preview Button
Click Generate Button

2.创建admin\app\models\User模型,继承自common\models\User

localhost/your-project/admin/gii/model

namespace admin\app\models;
use auth\models\User as CUser;
class User extends CUser{}

3.通过localhost/your-project/admin/gii/crud创建admin\app\models\UserSearch

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

历史

分支1.0.tpt