Yii quick learning tutorial (5) ------ user authorization
1. Configure/config/Main. php
'Components' => array (
'Authmanager' => array (
'Class' => 'cdbauthmanager ',
'Connectionid' => 'db'
),
:
2. Create the required data table. authitem (Operation definition table, user and operation definition). authitemchild (permission definition table role --- operation). authasignment (authorization table, user -- Role)
Create Table 'authitem '(
'Name' varchar (64) not null,
'Type' int (11) not null,
'Description' text,
'Bizrule' text,
'Data' text,
Primary Key ('name ')
) Engine = MyISAM default charset = utf8
Create Table 'authitemchild '(
'Parent' varchar (64) not null,
'Child 'varchar (64) not null,
Primary Key ('parent', 'Child '),
Key 'child '('child ')
) Engine = MyISAM default charset = utf8
Create Table 'authassignment '(
'Itemname' varchar (64) not null,
'Userid' varchar (64) not null,
'Bizrule' text,
'Data' text,
Primary Key ('itemname', 'userid ')
) Engine = MyISAM default charset = utf8
3. Add a method to a project controller, such as site/config. Add the following instance code in the actionconfig () method.
$ Auth = yii: APP ()-> authmanager;
$ Auth-> createoperation ('createpost', 'create a Post ');
$ Auth-> createoperation ('readpost', 'read A Post ');
$ Auth-> createoperation ('updatepost', 'Update A Post ');
$ Auth-> createoperation ('deletepost', 'delete A Post ');
$ Bizrule = 'Return yii: APP ()-> User-> id = $ Params ["Post"]-> authid ;';
$ Task = $ auth-> createtask ('updateownpost', 'Update a post by author himself ', $ bizrule );
$ Task-> addchild ('updatepost ');
$ Role = $ auth-> createrole ('reader ');
$ Role-> addchild ('readpost ');
$ Role = $ auth-> createrole ('author ');
$ Role-> addchild ('reader ');
$ Role-> addchild ('createpost ');
$ Role-> addchild ('updateownpost ');
$ Role = $ auth-> createrole ('editor ');
$ Role-> addchild ('reader ');
$ Role-> addchild ('updatepost ');
$ Role = $ auth-> createrole ('admin ');
$ Role-> addchild ('editor ');
$ Role-> addchild ('author ');
$ Role-> addchild ('deletepost ');
$ Auth-> assign ('reader', 'readera ');
$ Auth-> assign ('author', 'authorb ');
$ Auth-> assign ('edit', 'editorc ');
$ Auth-> assign ('admin', 'admind ');
Then run test.cn? R = site/config. You can add the above permission definitions to the database.
Then, use rules and filter as needed.
Reprinted: http://hi.baidu.com/itrunner/item/3034f01744ba9dd5bf904213