CAuthManager

优质
小牛编辑
124浏览
2023-12-01
所有包 | 属性 | 方法
system.web.auth
继承abstract class CAuthManager » CApplicationComponent » CComponent
实现IApplicationComponent, IAuthManager
子类CDbAuthManager, CPhpAuthManager
源自1.0
版本$Id: CAuthManager.php 3515 2011-12-28 12:29:24Z mdomba $
源码framework/web/auth/CAuthManager.php
CAuthManager是授权管理类的基类。

CAuthManager继承于CApplicationComponent,实现了一些 在授权管理类中的通用方法。

CAuthManager和它的具体子类实现了 基于用户角色访问控制(RBAC)。

主要思想是,层级权限是由 授权项目来管理的。高层的项目 继承了低层的项目权限。角色是最高级的授权项目, 会赋予给每个用户。一个用户所拥有的权限, 是由他的角色所继承的授权项目决定的。

使用授权管理器包含两个方面。第一,授权等级和任务 必须要既定的。CAuthManager和它的子类 提供API来完成这个任务。开发人员需要开发一些GUI, 使这些操作更加直观的展示给最终用户。第二,开发人员在程序里面适当的位置 调用IAuthManager::checkAccess来检测当前用户 是否含有该操作的权限。

公共属性

隐藏继承属性

属性类型描述定义在
authItemsarray返回指定类型和用户的授权项。IAuthManager
behaviorsarray这个应用组件附加的行为。 这此行为将在应用组件调用init时附加在应用组件上。 请参照CModel::behaviors如何指定此属性值。CApplicationComponent
defaultRolesarray隐式赋予给所有用户的角色名字列表。 这些角色不需要显式赋予给所有用户。 当调用checkAccess,会首先检查这些角色。 为了能够提高程序效率,这样的角色越少越好。 一个典型的用法是,定义一个“authenticated”角色,然后 把它关联到一个业务逻辑规则,这个规则是用来验证当前用户的。 然后在这个属性声明“authenticated”以便应用到 所有的验证用户。CAuthManager
isInitializedboolean检查应用组件是否已经初始化。CApplicationComponent
operationsarray返回操作。CAuthManager
rolesarray返回角色。CAuthManager
showErrorsboolean允许业务规则错误报告。CAuthManager
tasksarray返回任务。CAuthManager

公共方法

隐藏继承方法

方法描述定义在
__call()如果类中没有调的方法名,则调用这个方法。CComponent
__get()返回一个属性值、一个事件处理程序列表或一个行为名称。CComponent
__isset()检查一个属性是否为null。CComponent
__set()设置一个组件的属性值。CComponent
__unset()设置一个组件的属性为null。CComponent
addItemChild()添加一个授权项作为另一个授权项的子授权项。IAuthManager
asa()返回这个名字的行为对象。CComponent
assign()为用户分配一个授权项。IAuthManager
attachBehavior()附加一个行为到组件。CComponent
attachBehaviors()附加一个行为列表到组件。CComponent
attachEventHandler()为事件附加一个事件处理程序。CComponent
canGetProperty()确定属性是否可读。CComponent
canSetProperty()确定属性是否可写。CComponent
checkAccess()检查指定用户的执行权限。IAuthManager
clearAll()移除所有授权数据。IAuthManager
clearAuthAssignments()移除所有授权分配信息。IAuthManager
createAuthItem()创建一个授权项。IAuthManager
createOperation()创建一个操作。CAuthManager
createRole()创建一个角色。CAuthManager
createTask()创建一个任务。CAuthManager
detachBehavior()从组件中分离一个行为。CComponent
detachBehaviors()从组件中分离所有行为。CComponent
detachEventHandler()分离一个存在的事件处理程序。CComponent
disableBehavior()禁用一个附加行为。CComponent
disableBehaviors()禁用组件附加的所有行为。CComponent
enableBehavior()启用一个附加行为。CComponent
enableBehaviors()启用组件附加的所有行为。CComponent
evaluateExpression()计算一个PHP表达式,或根据组件上下文执行回调。CComponent
executeBizRule()执行指定的业务规则。CAuthManager
getAuthAssignment()返回授权项分配信息。IAuthManager
getAuthAssignments()返回指定用户的授权项分配信息。IAuthManager
getAuthItem()返回指定名字的授权项IAuthManager
getAuthItems()返回指定类型和用户的授权项。IAuthManager
getEventHandlers()返回一个事件的附加处理程序列表。CComponent
getIsInitialized()检查应用组件是否已经初始化。CApplicationComponent
getItemChildren()返回指定授权项的子授权项。IAuthManager
getOperations()返回操作。CAuthManager
getRoles()返回角色。CAuthManager
getTasks()返回任务。CAuthManager
hasEvent()确定一个事件是否定义。CComponent
hasEventHandler()检查事件是否有附加的处理程序。CComponent
hasItemChild()返回一个值表示父授权项是否存在子授权项。IAuthManager
hasProperty()确定属性是否被定义。CComponent
init()初始化应用组件。CApplicationComponent
isAssigned()返回一个值表示判断授权项是否分配给指定用户。IAuthManager
raiseEvent()发起一个事件。CComponent
removeAuthItem()移除指定授权项。IAuthManager
removeItemChild()从父授权项中移除子授权项。IAuthManager
revoke()撤销一个用户分配的授权项。IAuthManager
save()保存授权数据到持久存储。IAuthManager
saveAuthAssignment()保存修改的授权信息。IAuthManager
saveAuthItem()保存授权项到持久存储。IAuthManager

受保护方法

隐藏继承方法

方法描述定义在
checkItemChildType()检查项目类型以确定一个子项目已经赋予给一个父项目。CAuthManager

属性详细

defaultRoles 属性 public array $defaultRoles;

隐式赋予给所有用户的角色名字列表。 这些角色不需要显式赋予给所有用户。 当调用checkAccess,会首先检查这些角色。 为了能够提高程序效率,这样的角色越少越好。 一个典型的用法是,定义一个“authenticated”角色,然后 把它关联到一个业务逻辑规则,这个规则是用来验证当前用户的。 然后在这个属性声明“authenticated”以便应用到 所有的验证用户。

operations 属性 只读 public array getOperations(mixed $userId=NULL)

返回操作。 这个方法是IAuthManager::getAuthItems的快捷方式

roles 属性 只读 public array getRoles(mixed $userId=NULL)

返回角色。 这个方法是IAuthManager::getAuthItems的快捷方式。

showErrors 属性 (可用自 v1.1.3) public boolean $showErrors;

允许业务规则错误报告。

tasks 属性 只读 public array getTasks(mixed $userId=NULL)

返回任务。 这个方法是IAuthManager::getAuthItems的快捷方式

方法详细

checkItemChildType() 方法
protected void checkItemChildType(integer $parentType, integer $childType)
$parentTypeinteger父项目的类型
$childTypeinteger子项目类型
源码: framework/web/auth/CAuthManager.php#159 (显示) protectedfunctioncheckItemChildType($parentType,$childType)
{
static$types=array('operation','task','role');
if($parentType<$childType)
thrownewCException(Yii::t('yii','Cannotaddanitemoftype"{child}"toanitemoftype"{parent}".',
array('{child}'=>$types[$childType],'{parent}'=>$types[$parentType])));
}

检查项目类型以确定一个子项目已经赋予给一个父项目。

createOperation() 方法
public CAuthItem createOperation(string $name, string $description='', string $bizRule=NULL, mixed $data=NULL)
$namestring项目名字
$descriptionstring项目描述。
$bizRulestring关联到这个项目的业务逻辑
$datamixed当执行业务规则的时候所传递的额外的数据。
{return}CAuthItem授权项目。
源码: framework/web/auth/CAuthManager.php#99 (显示) publicfunctioncreateOperation($name,$description='',$bizRule=null,$data=null)
{
return$this->createAuthItem($name,CAuthItem::TYPE_OPERATION,$description,$bizRule,$data);
}

创建一个操作。 这个方法是IAuthManager::createAuthItem的快捷方式。

createRole() 方法
public CAuthItem createRole(string $name, string $description='', string $bizRule=NULL, mixed $data=NULL)
$namestring项目名字
$descriptionstring项目描述。
$bizRulestring关联到这个项目的业务规则
$datamixed执行业务规则的时候所传递的额外的数据。
{return}CAuthItem授权项目
源码: framework/web/auth/CAuthManager.php#71 (显示) publicfunctioncreateRole($name,$description='',$bizRule=null,$data=null)
{
return$this->createAuthItem($name,CAuthItem::TYPE_ROLE,$description,$bizRule,$data);
}

创建一个角色。 这个方法是IAuthManager::createAuthItem的快捷方式。

createTask() 方法
public CAuthItem createTask(string $name, string $description='', string $bizRule=NULL, mixed $data=NULL)
$namestring项目名字
$descriptionstring项目描述。
$bizRulestring关联到这个项目的业务规则。
$datamixed执行这个业务规则时候所传递的额外的数据。
{return}CAuthItem验证项目。
源码: framework/web/auth/CAuthManager.php#85 (显示) publicfunctioncreateTask($name,$description='',$bizRule=null,$data=null)
{
return$this->createAuthItem($name,CAuthItem::TYPE_TASK,$description,$bizRule,$data);
}

创建一个任务。 这个方法是IAuthManager::createAuthItem的快捷方式。

executeBizRule() 方法
public boolean executeBizRule(string $bizRule, array $params, mixed $data)
$bizRulestring要执行的业务规则。
$paramsarray传递给IAuthManager::checkAccess的参数
$datamixed关联到授权项目或者任务的额外的数据。
{return}boolean这个业务规则是否返回true。 如果业务规则为空,则返回true。
源码: framework/web/auth/CAuthManager.php#148 (显示) publicfunctionexecuteBizRule($bizRule,$params,$data)
{
return$bizRule===''||$bizRule===null||($this->showErrors?eval($bizRule)!=0:@eval($bizRule)!=0);
}

执行指定的业务规则。

getOperations() 方法
public array getOperations(mixed $userId=NULL)
$userIdmixed用户ID。如果不为null,那么只会返回赋予给这个 用户的操作。否则返回所有的操作。
{return}arrayoperations (name=>CAuthItem)
源码: framework/web/auth/CAuthManager.php#135 (显示) publicfunctiongetOperations($userId=null)
{
return$this->getAuthItems(CAuthItem::TYPE_OPERATION,$userId);
}

返回操作。 这个方法是IAuthManager::getAuthItems的快捷方式

getRoles() 方法
public array getRoles(mixed $userId=NULL)
$userIdmixed用户ID。如果不为null,那么只返回赋予给这个用户 的角色。否则返回所有的角色。
{return}array角色(name=>CAuthItem)
源码: framework/web/auth/CAuthManager.php#111 (显示) publicfunctiongetRoles($userId=null)
{
return$this->getAuthItems(CAuthItem::TYPE_ROLE,$userId);
}

返回角色。 这个方法是IAuthManager::getAuthItems的快捷方式。

getTasks() 方法
public array getTasks(mixed $userId=NULL)
$userIdmixed用户ID。如果不为null,只返回赋予给这个用户 任务。否则返回所有任务。
{return}arraytasks (name=>CAuthItem)
源码: framework/web/auth/CAuthManager.php#123 (显示) publicfunctiongetTasks($userId=null)
{
return$this->getAuthItems(CAuthItem::TYPE_TASK,$userId);
}

返回任务。 这个方法是IAuthManager::getAuthItems的快捷方式