20.11.2014 Views

O Guia Definitivo do Yii 1.1

O Guia Definitivo do Yii 1.1

O Guia Definitivo do Yii 1.1

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Using Business Rules<br />

When we are defining the authorization hierarchy, we can associate a role, a task or an<br />

operation with a so-called business rule. We may also associate a business rule when we<br />

assign a role to a user. A business rule is a piece of PHP code that is executed when we<br />

perform access checking. The returning value of the code is used to determine if the role<br />

or assignment applies to the current user. In the example above, we associated a<br />

business rule with the updateOwnPost task. In the business rule we simply check if the<br />

current user ID is the same as the specified post's author ID. The post information in the<br />

$params array is supplied by developers when performing access checking.<br />

Access Checking<br />

To perform access checking, we first need to know the name of the authorization item. For<br />

example, to check if the current user can create a post, we would check if he has the<br />

permission represented by the createPost operation. We then call<br />

CWebUser::checkAccess to perform the access checking:<br />

if(<strong>Yii</strong>::app()->user->checkAccess('createPost'))<br />

{<br />

// create post<br />

}<br />

If the authorization rule is associated with a business rule which requires additional<br />

parameters, we can pass them as well. For example, to check if a user can update a post,<br />

we would pass in the post data in the $params:<br />

$params=array('post'=>$post);<br />

if(<strong>Yii</strong>::app()->user->checkAccess('updateOwnPost',$params))<br />

{<br />

// update post<br />

}<br />

Using Default Roles<br />

Note: The default role feature has been available since version 1.0.3<br />

Many Web applications need some very special roles that would be assigned to every or<br />

most of the system users. For example, we may want to assign some privileges to all<br />

authenticated users. It poses a lot of maintenance trouble if we explicitly specify and store<br />

these role assignments. We can exploit default roles to solve this problem.<br />

A default role is a role that is implicitly assigned to every user, including both authenticated<br />

and guest. We <strong>do</strong> not need to explicitly assign it to a user. When CWebUser::checkAccess<br />

is invoked, default roles will be checked first as if they are assigned to the user.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!