Yii quick learning tutorial (5)-user authorization

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.