CAccessRule

CDbHttpSession

Yii Framework v1.1.10 类参考

CAccessRule

system.web.auth
继承 class CAccessRule » CComponent
源自 1.0
版本 $Id: CAccessControlFilter.php 3515 2011-12-28 12:29:24Z mdomba $
源码 framework/web/auth/CAccessControlFilter.php
CAccessRule代表由CAccessControlFilter管理的访问规则。

公共属性

隐藏继承属性

属性类型描述定义在
actions array 该规则可应用的动作ID列表。这个比较过程是不分大小写的。 如果没有指定动作,那么就会应用到所有的动作。 CAccessRule
allow boolean 该规则是‘allow’还是’deny‘。 CAccessRule
controllers array 该规则可应用的控制器ID列表。这个比较过程是不分大小写的。 CAccessRule
expression string 用PHP表达式来指出什么情况下该规则才会生效。 在表达式里面,你可以使用$user来代替使用Yii::app()->user。 这个表达式也可以使用PHP回调函数, 包括类的方法名(array(ClassName/Object, MethodName)), 或者是匿名函数(PHP 5. CAccessRule
ips IP规则。 CAccessRule
message string 当该规则验证当前请求没有权限时所显示的错误信息。 如果没有设置,那么就会显示默认的信息。 CAccessRule
roles array 该规则可应用的角色列表。对于每一个角色,都会调用 当前用户的CWebUser::checkAccess。只要有一个调用 返回true,就通过这个规则了。 注意的是,你应该在’allow‘规则里面使用角色,因为从定义上来说, 一个角色反映的是权限的集合。 CAccessRule
users array 该规则可应用的用户名列表。这个比较过程是不分大小写的。 如果没有指定用户名,那么这个规则就会应用于所有用户。 CAccessRule
verbs array 该规则可应用的请求类型列表(比如:GET,POST)。 CAccessRule

公共方法

隐藏继承方法

方法描述定义在
__call() 如果类中没有调的方法名,则调用这个方法。 CComponent
__get() 返回一个属性值、一个事件处理程序列表或一个行为名称。 CComponent
__isset() 检查一个属性是否为null。 CComponent
__set() 设置一个组件的属性值。 CComponent
__unset() 设置一个组件的属性为null。 CComponent
asa() 返回这个名字的行为对象。 CComponent
attachBehavior() 附加一个行为到组件。 CComponent
attachBehaviors() 附加一个行为列表到组件。 CComponent
attachEventHandler() 为事件附加一个事件处理程序。 CComponent
canGetProperty() 确定属性是否可读。 CComponent
canSetProperty() 确定属性是否可写。 CComponent
detachBehavior() 从组件中分离一个行为。 CComponent
detachBehaviors() 从组件中分离所有行为。 CComponent
detachEventHandler() 分离一个存在的事件处理程序。 CComponent
disableBehavior() 禁用一个附加行为。 CComponent
disableBehaviors() 禁用组件附加的所有行为。 CComponent
enableBehavior() 启用一个附加行为。 CComponent
enableBehaviors() 启用组件附加的所有行为。 CComponent
evaluateExpression() 计算一个PHP表达式,或根据组件上下文执行回调。 CComponent
getEventHandlers() 返回一个事件的附加处理程序列表。 CComponent
hasEvent() 确定一个事件是否定义。 CComponent
hasEventHandler() 检查事件是否有附加的处理程序。 CComponent
hasProperty() 确定属性是否被定义。 CComponent
isUserAllowed() 检查当前用户是否允许访问指定的动作。 CAccessRule
raiseEvent() 发起一个事件。 CComponent

受保护方法

隐藏继承方法

方法描述定义在
isActionMatched() CAccessRule
isControllerMatched() CAccessRule
isExpressionMatched() CAccessRule
isIpMatched() CAccessRule
isRoleMatched() CAccessRule
isUserMatched() CAccessRule
isVerbMatched() CAccessRule

属性详细

actions 属性
public array $actions;

该规则可应用的动作ID列表。这个比较过程是不分大小写的。 如果没有指定动作,那么就会应用到所有的动作。

allow 属性
public boolean $allow;

该规则是‘allow’还是’deny‘。

controllers 属性
public array $controllers;

该规则可应用的控制器ID列表。这个比较过程是不分大小写的。

expression 属性
public string $expression;

用PHP表达式来指出什么情况下该规则才会生效。 在表达式里面,你可以使用$user来代替使用Yii::app()->user。 这个表达式也可以使用PHP回调函数, 包括类的方法名(array(ClassName/Object, MethodName)), 或者是匿名函数(PHP 5.3.0+)。这个函数/方法名应具体下面的格式:

function foo($user, $rule) { ... }
$user是指当前应用的访问用户对象,$rule是这个访问规则。

ips 属性
public IP规则。 $ips;

message 属性 (可用自 v1.1.1)
public string $message;

当该规则验证当前请求没有权限时所显示的错误信息。 如果没有设置,那么就会显示默认的信息。

roles 属性
public array $roles;

该规则可应用的角色列表。对于每一个角色,都会调用 当前用户的CWebUser::checkAccess。只要有一个调用 返回true,就通过这个规则了。 注意的是,你应该在’allow‘规则里面使用角色,因为从定义上来说, 一个角色反映的是权限的集合。

参见

users 属性
public array $users;

该规则可应用的用户名列表。这个比较过程是不分大小写的。 如果没有指定用户名,那么这个规则就会应用于所有用户。

verbs 属性
public array $verbs;

该规则可应用的请求类型列表(比如:GET,POST)。

方法详细

isActionMatched() 方法
protected boolean isActionMatched(CAction $action)
$action CAction 动作对象
{return} boolean 是否该规则可应用于该动作。
源码: framework/web/auth/CAccessControlFilter.php#254 (显示)
protected function isActionMatched($action)
{
    return empty(
$this->actions) || in_array(strtolower($action->getId()),$this->actions);
}

isControllerMatched() 方法
protected boolean isControllerMatched(CAction $controller)
$controller CAction 控制器对象
{return} boolean 是否该规则可应用于该控制器。
源码: framework/web/auth/CAccessControlFilter.php#263 (显示)
protected function isControllerMatched($controller)
{
    return empty(
$this->controllers) || in_array(strtolower($controller->getId()),$this->controllers);
}

isExpressionMatched() 方法
protected boolean isExpressionMatched(IWebUser $user)
$user IWebUser 用户对象
{return} boolean 表达式的内容。如果该表达式没有指定内容,则返回true。
源码: framework/web/auth/CAccessControlFilter.php#335 (显示)
protected function isExpressionMatched($user)
{
    if(
$this->expression===null)
        return 
true;
    else
        return 
$this->evaluateExpression($this->expression, array('user'=>$user));
}

isIpMatched() 方法
protected boolean isIpMatched(string $ip)
$ip string IP地址
{return} boolean 是否该规则可应用于该IP地址。
源码: framework/web/auth/CAccessControlFilter.php#310 (显示)
protected function isIpMatched($ip)
{
    if(empty(
$this->ips))
        return 
true;
    foreach(
$this->ips as $rule)
    {
        if(
$rule==='*' || $rule===$ip || (($pos=strpos($rule,'*'))!==false && !strncmp($ip,$rule,$pos)))
            return 
true;
    }
    return 
false;
}

isRoleMatched() 方法
protected boolean isRoleMatched(IWebUser $user)
$user IWebUser 用户对象
{return} boolean 是否该规则可应用于该角色。
源码: framework/web/auth/CAccessControlFilter.php#294 (显示)
protected function isRoleMatched($user)
{
    if(empty(
$this->roles))
        return 
true;
    foreach(
$this->roles as $role)
    {
        if(
$user->checkAccess($role))
            return 
true;
    }
    return 
false;
}

isUserAllowed() 方法
public integer isUserAllowed(CWebUser $user, CController $controller, CAction $action, string $ip, string $verb)
$user CWebUser 用户对象
$controller CController 当前调用的控制器对象
$action CAction 准备调用的动作对象
$ip string 请求的IP地址
$verb string 请求类型(如:GET,POST……)
{return} integer 返回值:1表示允许该用户,-1表示不禁止该用户,0表示没有应用到该用户。
源码: framework/web/auth/CAccessControlFilter.php#236 (显示)
public function isUserAllowed($user,$controller,$action,$ip,$verb)
{
    if(
$this->isActionMatched($action)
        && 
$this->isUserMatched($user)
        && 
$this->isRoleMatched($user)
        && 
$this->isIpMatched($ip)
        && 
$this->isVerbMatched($verb)
        && 
$this->isControllerMatched($controller)
        && 
$this->isExpressionMatched($user))
        return 
$this->allow : -1;
    else
        return 
0;
}

检查当前用户是否允许访问指定的动作。

isUserMatched() 方法
protected boolean isUserMatched(IWebUser $user)
$user IWebUser 用户对象
{return} boolean 是否该规则可应用于该用户。
源码: framework/web/auth/CAccessControlFilter.php#272 (显示)
protected function isUserMatched($user)
{
    if(empty(
$this->users))
        return 
true;
    foreach(
$this->users as $u)
    {
        if(
$u==='*')
            return 
true;
        else if(
$u==='?' && $user->getIsGuest())
            return 
true;
        else if(
$u==='@' && !$user->getIsGuest())
            return 
true;
        else if(!
strcasecmp($u,$user->getName()))
            return 
true;
    }
    return 
false;
}

isVerbMatched() 方法
protected boolean isVerbMatched(string $verb)
$verb string 请求方法
{return} boolean 是否该规则可应用于该请求。
源码: framework/web/auth/CAccessControlFilter.php#326 (显示)
protected function isVerbMatched($verb)
{
    return empty(
$this->verbs) || in_array(strtolower($verb),$this->verbs);
}

Copyright © 2008-2011 by Yii Software LLC
All Rights Reserved.