Yii2-admin is Yii2 RBAC a set of management tools, to achieve a beautiful interface and complete rights management functions, do not have to write permission code, before use, please update YII2 source to the latest version.
Git Source Address: https://github.com/mdmsoft/yii2-admin
Install Yii2-admin:
1, first switch to the project directory
2. Execute the statement: Composer.phar require mdmsoft/yii2-admin
Note: If prompted could not open input file Composer.phar
Please execute the following two statements first
Composer Self-update
Composer Install--prefer-dist
3, composer after the completion of the configuration file under the project to add the following configuration items
' aliases ' and [' @mdm/admin ' = ' $PATH \yii2-admin-1.0.3 ',], ' modules ' = [' Admi n ' = = [' Class ' = ' mdm\admin\module ', ' layout ' = ' left-menu ',//It can be ' @path/to/your/layout ' . /**/' controllermap ' + = [' Assignment ' = [' class ' and ' = ' mdm\admin\controllers\as Signmentcontroller ', ' userclassname ' = ' app\models\user ', ' idfield ' = ' id ' ]], ' menus ' = [' assignment ' and ' = ' label ' = ' Grand Access '/change Label],//' route ' = NULL,//disable menu route]], ' debug ' = = [' Clas s ' = ' yii\debug\module ',],],
The components array is joined with the AuthManager component, there are Phpmanager and Dbmanager two ways, phpmanager the permission relationship is saved in the file, here is used Dbmanager way, Save the permission relationship in the database. ' AuthManager ' = [ ' class ' = ' Yii\rbac\dbmanager ',//or use ' Yii\rbac\dbmanager '], ' i18n ' = [ ' translations ' = [' * ' = ' = ' class ' = ' Yii\i18n\phpmessagesource ', ' basepath ' + ' @app/ Messages ',//If advanced application, set @frontend/messages ' sourcelanguage ' = ' en ', ' filemap ' = [ //' main ' = ' main.php ', ], ], ], ,
4. Command line switch to YII2 directory, execute the following command, create the required table for RBAC (you need to create your own database, library name default yii2basic)
Yii Migrate [Email protected]/rbac/migrations
Yii migrate [e-mail protected]/admin/migrations (Create menu Navigator table), if an error occurs, you can move the SQL file in migrations to @yii/rbac/ Generating tables in the Migrations directory
The above command uses the YII2 framework of the CLI mode, so you need to put the above configuration, in console.php also write a copy, this people look at the error prompt to know.
Anyway, I did not succeed in this step, the goal of this step is to generate the following 5 tables in the database:
Menu
Auth_rule//Rules, rule class name
Auth_item_child//role-corresponding permissions, parent role, child permission name
Auth_item//Roles | Permissions table, type=1 role, type=2 permissions
Auth_assignment//Role vs. user Relationship table
I generate these 5 tables directly from the SQL statement below
SET foreign_key_checks=0;--------------------------------Table structure for Dh_sm_menu--------------------------- ---DROP TABLE IF EXISTS ' Yc_menu '; CREATE TABLE ' Yc_menu ' (' id ' int (one) not null auto_increment, ' name ' varchar ($) NOT NULL, ' parent ' int (one) ' DEFAULT N ULL, ' route ' varchar default NULL, ' order ' int (one) default null, ' data ' text, PRIMARY key (' ID '), key ' parent ' ( ' Parent '), key ' name ' (' name '), key ' Route ' (' Route ' (255)), key ' order ' (' Order '), CONSTRAINT ' Dh_menu_ibfk_1 ' FOREIGN KEY (' parent ') REFERENCES ' Dh_menu ' (' id ') on DELETE SET NULL on UPDATE CASCADE) engine=innodb auto_increment=1 DEFAULT C Harset=utf8 comment= ' System Administrator menu permissions table \ r \ n ';D rop table IF EXISTS ' Yc_auth_rule '; CREATE TABLE ' yc_auth_rule ' (' name ' varchar () not NULL, ' data ' text, ' created_at ' int (one-by-one) DEFAULT NULL, ' Updated_at ' Int (one) DEFAULT NULL, PRIMARY key (' name '), key ' name ' (' name '), key ' Created_at ' (' created_at '), key ' Updated_at ' ( ' Updated_at ') Engine=innodb DEFAULT CHARSET=utf8 comment= ' Administrator rights rule table ';D ROP table IF EXISTS ' Yc_auth_item_child '; CREATE TABLE ' Yc_auth_item_child ' (' Parent ' varchar (') ' is not null, ' child ' varchar ($) NOT NULL, PRIMARY KEY (' Parent ', "Child"), key ' child ' (' child '), key ' parent ' (' parent ')) Engine=innodb DEFAULT Charset=utf8 comment= ' Administrator rights relationship table ';D ROP TAB LE IF EXISTS ' Yc_auth_item '; CREATE TABLE ' Yc_auth_item ' (' name ' varchar (') ' is not null, ' type ' int (one) ' NOT NULL, ' description ' text, ' Rule_name ' VA Rchar (+) default NULL, ' Data ' text, ' created_at ' int (one) default null, ' Updated_at ' int (one) default NULL, PRIMARY KEY (' name '), key ' Rule_name ' (' rule_name '), key ' type ' (' type '), key ' name ' (' name '), key ' Created_at ' (' Created_at '), CONSTRAINT ' yc_auth_item_ibfk_2 ' FOREIGN KEY (' rule_name ') REFERENCES ' yc_auth_rule ' (' name ') on DELETE SET NULL on UPDATE CASCADE) Engine=innodb DEFAULT Charset=utf8 comment= ' Administrative rights entry ';D ROP TABLE IF EXISTS ' yc_auth_assignment '; CREATE TABLE ' yc_auth_assignment ' (' item_name ' varchar) not NULL, ' user_id ' varchar (+) NOT null, ' created_at ' int (one) DEFAULT NULL, PRIMARY key (' Item_name ', ' user_id '), key ' User_i d ' (' user_id '), key ' Created_at ' (' created_at '), key ' Item_name ' (' Item_name '), CONSTRAINT ' yc_auth_assignment_ibfk_2 ' FOREIGN KEY (' item_name ') REFERENCES ' Yc_auth_item ' (' name ') on DELETE CASCADE on UPDATE CASCADE) Engine=innodb DEFAULT CH Arset=utf8 comment= ' Administrator Authorization form ';
5. Access to the management interface, Http://localhost/admin, at this time because there is no data, will be error, we need to add Users table user
My field is like this, only need to exist user_id, other fields according to the need to increase or decrease their own
CREATE TABLE ' user ' ( ' user_id ' int () unsigned not NULL auto_increment, ' username ' varchar (+) DEFAULT Null,
' password ' varchar (+) default null, ' realname ' varchar (+) default NULL, ' email ' varchar (+) Default null,< C5/>primary KEY (' user_id ')) Engine=innodb DEFAULT Charset=utf8;
This is a visit that may also be error-free because we do not implement the validation class, and the components in the config file web.php add the user entry
' User ' = [ ' identityclass ' = ' app\models\user ', ' enableautologin ' = True, ],
The app\models\user here is the validation class that we implement, you can name it casually.
Class User extends \yii\db\activerecord implements \yii\web\identityinterface, which implements 5 functions in the Identityinterface interface, There are 2 methods that must be implemented findidentity ($id) //query user information by ID getId () //Get User ID
Here, the basic is completed, access to the http://localhost/admin, you can see the management interface
6. Use
1) Modify the Navigation menu Template main view file views/layouts/main.php
Use Mdm\admin\components\menuhelper; Use Yii\bootstrap\nav; Echo nav::widget ([ ' options '] = [' class ' = ' Navbar-nav navbar-right nav-pills '] , #修改使用yii2-admin Menu control ' Items ' = Menuhelper::getassignedmenu (Yii:: $app->user->id), ]);
2) Enter the Yii2-admin directory, vi components/menuhelper.php
public static function Getassignedmenu ($userId, $root = null, $callback = NULL, $refresh = true) Change $refresh to True
3) Add a navigation dish, create a rule, create a role
Https://github.com/mdmsoft/yii2-admin/blob/master/docs/guide/basic-usage.md
When you create a Rule, you must write a class and inherit Yii\rbac\rule, and implement its abstract methods.
Implementation of Yii2-admin RBAC Rights Management