CPhpAuthManager
鍖 | system.web.auth |
---|---|
缁ф壙 | class CPhpAuthManager » CAuthManager » CApplicationComponent » CComponent |
瀹炵幇 | IAuthManager, IApplicationComponent |
婧愯嚜 | 1.0 |
鐗堟湰 | $Id: CPhpAuthManager.php 3515 2011-12-28 12:29:24Z mdomba $ |
婧愮爜 | framework/web/auth/CPhpAuthManager.php |
鎺堟潈鏁版嵁鍦ㄦ寚瀹氱殑鏂囦欢authFile 閲岄潰淇濆瓨锛忓姞杞斤紝 榛樿鏄檖rotected/data/auth.php鈥樸
CPhpAuthManager涓昏鏄簲鐢ㄥ湪鎺堟潈鏁版嵁涓嶅鐨勬儏鍐典笅 锛堝锛屼釜浜哄崥瀹㈢郴缁熺殑鎺堟潈淇℃伅锛夈 濡傛灉鏄瘮杈冨鏉傜殑鎺堟潈鏁版嵁锛屽簲璇ヤ娇鐢CDbAuthManager銆
鍏叡灞炴
灞炴 | 绫诲瀷 | 鎻忚堪 | 瀹氫箟鍦 |
---|---|---|---|
authFile | string | 鍖呭惈鎺堟潈鏁版嵁鐨凱HP鏂囦欢璺緞銆 濡傛灉娌℃湁璁剧疆锛屽畠浼氫娇鐢ㄢ榩rotected/data/auth. | CPhpAuthManager |
authItems | array | 杩斿洖鎸囧畾绫诲瀷鍜岀敤鎴风殑鎺堟潈椤圭洰銆 | CPhpAuthManager |
behaviors | array | 杩欎釜搴旂敤缁勪欢闄勫姞鐨勮涓恒 杩欐琛屼负灏嗗湪搴旂敤缁勪欢璋冪敤init鏃堕檮鍔犲湪搴旂敤缁勪欢涓娿 璇峰弬鐓CModel::behaviors濡備綍鎸囧畾姝ゅ睘鎬у笺 | CApplicationComponent |
defaultRoles | array | 闅愬紡璧嬩簣缁欐墍鏈夌敤鎴风殑瑙掕壊鍚嶅瓧鍒楄〃銆 杩欎簺瑙掕壊涓嶉渶瑕佹樉寮忚祴浜堢粰鎵鏈夌敤鎴枫 褰撹皟鐢checkAccess锛屼細棣栧厛妫鏌ヨ繖浜涜鑹层 涓轰簡鑳藉鎻愰珮绋嬪簭鏁堢巼锛岃繖鏍风殑瑙掕壊瓒婂皯瓒婂ソ銆 涓涓吀鍨嬬殑鐢ㄦ硶鏄紝瀹氫箟涓涓渁uthenticated鈥濊鑹诧紝鐒跺悗 鎶婂畠鍏宠仈鍒颁竴涓笟鍔¢昏緫瑙勫垯锛岃繖涓鍒欐槸鐢ㄦ潵楠岃瘉褰撳墠鐢ㄦ埛鐨勩 鐒跺悗鍦ㄨ繖涓睘鎬у0鏄庘渁uthenticated鈥濅互渚垮簲鐢ㄥ埌 鎵鏈夌殑楠岃瘉鐢ㄦ埛銆 | CAuthManager |
isInitialized | boolean | 妫鏌ュ簲鐢ㄧ粍浠舵槸鍚﹀凡缁忓垵濮嬪寲銆 | CApplicationComponent |
operations | array | 杩斿洖鎿嶄綔銆 | CAuthManager |
roles | array | 杩斿洖瑙掕壊銆 | CAuthManager |
showErrors | boolean | 鍏佽涓氬姟瑙勫垯閿欒鎶ュ憡銆 | CAuthManager |
tasks | array | 杩斿洖浠诲姟銆 | CAuthManager |
鍏叡鏂规硶
鏂规硶 | 鎻忚堪 | 瀹氫箟鍦 |
---|---|---|
__call() | 濡傛灉绫讳腑娌℃湁璋冪殑鏂规硶鍚嶏紝鍒欒皟鐢ㄨ繖涓柟娉曘 | CComponent |
__get() | 杩斿洖涓涓睘鎬у笺佷竴涓簨浠跺鐞嗙▼搴忓垪琛ㄦ垨涓涓涓哄悕绉般 | CComponent |
__isset() | 妫鏌ヤ竴涓睘鎬ф槸鍚︿负null銆 | CComponent |
__set() | 璁剧疆涓涓粍浠剁殑灞炴у笺 | CComponent |
__unset() | 璁剧疆涓涓粍浠剁殑灞炴т负null銆 | CComponent |
addItemChild() | 娣诲姞涓涓巿鏉冮」鐩綔涓哄彟涓涓巿鏉冮」鐨勫瓙鎺堟潈椤圭洰銆 | CPhpAuthManager |
asa() | 杩斿洖杩欎釜鍚嶅瓧鐨勮涓哄璞° | CComponent |
assign() | 涓虹敤鎴峰垎閰嶄竴涓巿鏉冮」鐩 | CPhpAuthManager |
attachBehavior() | 闄勫姞涓涓涓哄埌缁勪欢銆 | CComponent |
attachBehaviors() | 闄勫姞涓涓涓哄垪琛ㄥ埌缁勪欢銆 | CComponent |
attachEventHandler() | 涓轰簨浠堕檮鍔犱竴涓簨浠跺鐞嗙▼搴忋 | CComponent |
canGetProperty() | 纭畾灞炴ф槸鍚﹀彲璇汇 | CComponent |
canSetProperty() | 纭畾灞炴ф槸鍚﹀彲鍐欍 | CComponent |
checkAccess() | 妫鏌ユ寚瀹氱敤鎴风殑鎵ц鏉冮檺銆 | CPhpAuthManager |
clearAll() | 绉婚櫎鎵鏈夋巿鏉冩暟鎹 | CPhpAuthManager |
clearAuthAssignments() | 绉婚櫎鎵鏈夋巿鏉冨垎閰嶄俊鎭 | CPhpAuthManager |
createAuthItem() | 鍒涘缓涓涓巿鏉冮」鐩 | CPhpAuthManager |
createOperation() | 鍒涘缓涓涓搷浣溿 | CAuthManager |
createRole() | 鍒涘缓涓涓鑹层 | CAuthManager |
createTask() | 鍒涘缓涓涓换鍔° | CAuthManager |
detachBehavior() | 浠庣粍浠朵腑鍒嗙涓涓涓恒 | CComponent |
detachBehaviors() | 浠庣粍浠朵腑鍒嗙鎵鏈夎涓恒 | CComponent |
detachEventHandler() | 鍒嗙涓涓瓨鍦ㄧ殑浜嬩欢澶勭悊绋嬪簭銆 | CComponent |
disableBehavior() | 绂佺敤涓涓檮鍔犺涓恒 | CComponent |
disableBehaviors() | 绂佺敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 | CComponent |
enableBehavior() | 鍚敤涓涓檮鍔犺涓恒 | CComponent |
enableBehaviors() | 鍚敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 | CComponent |
evaluateExpression() | 璁$畻涓涓狿HP琛ㄨ揪寮忥紝鎴栨牴鎹粍浠朵笂涓嬫枃鎵ц鍥炶皟銆 | CComponent |
executeBizRule() | 鎵ц鎸囧畾鐨勪笟鍔¤鍒欍 | CAuthManager |
getAuthAssignment() | 杩斿洖椤圭洰浠诲姟淇℃伅銆 | CPhpAuthManager |
getAuthAssignments() | 杩斿洖鎸囧畾鐢ㄦ埛鐨勯」鐩换鍔° | CPhpAuthManager |
getAuthItem() | 杩斿洖鎸囧畾鍚嶅瓧鐨勬巿鏉冮」鐩 | CPhpAuthManager |
getAuthItems() | 杩斿洖鎸囧畾绫诲瀷鍜岀敤鎴风殑鎺堟潈椤圭洰銆 | CPhpAuthManager |
getEventHandlers() | 杩斿洖涓涓簨浠剁殑闄勫姞澶勭悊绋嬪簭鍒楄〃銆 | CComponent |
getIsInitialized() | 妫鏌ュ簲鐢ㄧ粍浠舵槸鍚﹀凡缁忓垵濮嬪寲銆 | CApplicationComponent |
getItemChildren() | 杩斿洖鎸囧畾椤圭洰鐨勫瓙椤圭洰 | CPhpAuthManager |
getOperations() | 杩斿洖鎿嶄綔銆 | CAuthManager |
getRoles() | 杩斿洖瑙掕壊銆 | CAuthManager |
getTasks() | 杩斿洖浠诲姟銆 | CAuthManager |
hasEvent() | 纭畾涓涓簨浠舵槸鍚﹀畾涔夈 | CComponent |
hasEventHandler() | 妫鏌ヤ簨浠舵槸鍚︽湁闄勫姞鐨勫鐞嗙▼搴忋 | CComponent |
hasItemChild() | 杩斿洖鍊艰鏄庣埗鎺堟潈椤圭洰鏄惁鍖呭惈瀛愭巿鏉冮」鐩 | CPhpAuthManager |
hasProperty() | 纭畾灞炴ф槸鍚﹁瀹氫箟銆 | CComponent |
init() | 鍒濆鍖栧簲鐢ㄧ粍浠躲 | CPhpAuthManager |
isAssigned() | 杩斿洖鍊艰鏄庤繖涓」鐩槸鍚﹀凡缁忚祴浜堢粰鐢ㄦ埛銆 | CPhpAuthManager |
load() | 鍔犺浇鎺堟潈鏁版嵁銆 | CPhpAuthManager |
raiseEvent() | 鍙戣捣涓涓簨浠躲 | CComponent |
removeAuthItem() | 绉婚櫎鎸囧畾鐨勬巿鏉冮」鐩 | CPhpAuthManager |
removeItemChild() | 绉婚櫎瀛愰」鐩 | CPhpAuthManager |
revoke() | 鎾ゆ秷鐢ㄦ埛鐨勬巿鏉冧换鍔° | CPhpAuthManager |
save() | 灏嗘巿鏉冩暟鎹繚瀛樺埌鎸佷箙鍖栧瓨鍌ㄥ櫒銆 | CPhpAuthManager |
saveAuthAssignment() | 淇濆瓨淇敼鐨勬巿鏉冧俊鎭 | CPhpAuthManager |
saveAuthItem() | 灏嗘巿鏉冮」鐩繚瀛樺埌鎸佷箙鍖栧瓨鍌ㄥ櫒銆 | CPhpAuthManager |
鍙椾繚鎶ゆ柟娉
鏂规硶 | 鎻忚堪 | 瀹氫箟鍦 |
---|---|---|
checkItemChildType() | 妫鏌ラ」鐩被鍨嬩互纭畾涓涓瓙椤圭洰宸茬粡璧嬩簣缁欎竴涓埗椤圭洰銆 | CAuthManager |
detectLoop() | 妫鏌ユ巿鏉冮」鐩眰绾ф槸鍚﹀瓨鍦ㄥ惊鐜 | CPhpAuthManager |
loadFromFile() | 浠嶱HP鏂囦欢鍔犺浇鎺堟潈鏁版嵁銆 | CPhpAuthManager |
saveToFile() | 淇濆瓨鎺堟潈鏁版嵁鍒癙HP鏂囦欢銆 | CPhpAuthManager |
灞炴ц缁
鍖呭惈鎺堟潈鏁版嵁鐨凱HP鏂囦欢璺緞銆 濡傛灉娌℃湁璁剧疆锛屽畠浼氫娇鐢ㄢ榩rotected/data/auth.php鈥欎綔涓烘暟鎹枃浠躲 濡傛灉闇瑕佷慨鏀规巿鏉冩暟鎹紝瑕佺‘淇濈綉缁滄湇鍔¤繘绋嬪璇ユ枃浠 鏈夊啓鐨勬潈闄愩
杩斿洖鎸囧畾绫诲瀷鍜岀敤鎴风殑鎺堟潈椤圭洰銆
鏂规硶璇︾粏
public boolean addItemChild(string $itemName, string $childName)
| ||
$itemName | string | 鐖堕」鐩悕瀛 |
$childName | string | 瀛愰」鐩悕瀛 |
{return} | boolean | 鏄惁娣诲姞鎴愬姛 |
public function addItemChild($itemName,$childName)
{
if(!isset($this->_items[$childName],$this->_items[$itemName]))
throw new CException(Yii::t('yii','Either "{parent}" or "{child}" does not exist.',array('{child}'=>$childName,'{name}'=>$itemName)));
$child=$this->_items[$childName];
$item=$this->_items[$itemName];
$this->checkItemChildType($item->getType(),$child->getType());
if($this->detectLoop($itemName,$childName))
throw new CException(Yii::t('yii','Cannot add "{child}" as a child of "{parent}". A loop has been detected.',
array('{child}'=>$childName,'{parent}'=>$itemName)));
if(isset($this->_children[$itemName][$childName]))
throw new CException(Yii::t('yii','The item "{parent}" already has a child "{child}".',
array('{child}'=>$childName,'{parent}'=>$itemName)));
$this->_children[$itemName][$childName]=$this->_items[$childName];
return true;
}
娣诲姞涓涓巿鏉冮」鐩綔涓哄彟涓涓巿鏉冮」鐨勫瓙鎺堟潈椤圭洰銆
public CAuthAssignment assign(string $itemName, mixed $userId, string $bizRule=NULL, mixed $data=NULL)
| ||
$itemName | string | 椤圭洰鍚嶅瓧 |
$userId | mixed | 鐢ㄦ埛ID锛堣鎯呰鍙傝IWebUser::getId锛 |
$bizRule | string | 褰撹皟鐢checkAccess鏃讹紝 鍏蜂綋鐨勬巿鏉冮」鐩殑涓氬姟瑙勫垯銆 |
$data | mixed | 杩欎釜浠诲姟棰濆鐨勬暟鎹 |
{return} | CAuthAssignment | 鎺堟潈浠诲姟鐨勪俊鎭 |
public function assign($itemName,$userId,$bizRule=null,$data=null)
{
if(!isset($this->_items[$itemName]))
throw new CException(Yii::t('yii','Unknown authorization item "{name}".',array('{name}'=>$itemName)));
else if(isset($this->_assignments[$userId][$itemName]))
throw new CException(Yii::t('yii','Authorization item "{item}" has already been assigned to user "{user}".',
array('{item}'=>$itemName,'{user}'=>$userId)));
else
return $this->_assignments[$userId][$itemName]=new CAuthAssignment($this,$itemName,$userId,$bizRule,$data);
}
涓虹敤鎴峰垎閰嶄竴涓巿鏉冮」鐩
public boolean checkAccess(string $itemName, mixed $userId, array $params=array (
))
| ||
$itemName | string | 闇瑕佹潈闄愭鏌ョ殑鎺堟潈椤瑰悕绉 |
$userId | mixed | 鐢ㄦ埛ID銆傚畠搴旇鏄竴涓暣鏁版垨涓涓瓧绗︿覆,浠h〃鐢ㄦ埛鐨勫敮涓鏍囪瘑 璇︽儏璇峰弬鑰IWebUser::getId銆 |
$params | array | 鍒嗛厤缁欑敤鎴风殑浠诲姟鎴栬鑹茬殑 锛堥敭锛嶅煎锛夊舰寮忕殑涓氬姟瑙勫垯銆 |
{return} | boolean | 鐢ㄦ埛鏄惁鏈夋潈鎵ц鎿嶄綔銆 |
public function checkAccess($itemName,$userId,$params=array())
{
if(!isset($this->_items[$itemName]))
return false;
$item=$this->_items[$itemName];
Yii::trace('Checking permission "'.$item->getName().'"','system.web.auth.CPhpAuthManager');
if($this->executeBizRule($item->getBizRule(),$params,$item->getData()))
{
if(in_array($itemName,$this->defaultRoles))
return true;
if(isset($this->_assignments[$userId][$itemName]))
{
$assignment=$this->_assignments[$userId][$itemName];
if($this->executeBizRule($assignment->getBizRule(),$params,$assignment->getData()))
return true;
}
foreach($this->_children as $parentName=>$children)
{
if(isset($children[$itemName]) && $this->checkAccess($parentName,$userId,$params))
return true;
}
}
return false;
}
妫鏌ユ寚瀹氱敤鎴风殑鎵ц鏉冮檺銆
public void clearAll()
|
public function clearAll()
{
$this->clearAuthAssignments();
$this->_children=array();
$this->_items=array();
}
绉婚櫎鎵鏈夋巿鏉冩暟鎹
public void clearAuthAssignments()
|
public function clearAuthAssignments()
{
$this->_assignments=array();
}
绉婚櫎鎵鏈夋巿鏉冨垎閰嶄俊鎭
public CAuthItem createAuthItem(string $name, integer $type, string $description='', string $bizRule=NULL, mixed $data=NULL)
| ||
$name | string | 椤圭洰鍚嶅瓧銆傝繖涓竴涓槸鍞竴鏍囪瘑銆 |
$type | integer | 椤圭洰绫诲瀷锛0锛氭搷浣滐紝1锛氫换鍔★紝2锛氳鑹诧級銆 |
$description | string | 椤圭洰鎻忚堪 |
$bizRule | string | 鍏宠仈鍒伴」鐩殑涓氬姟瑙勫垯銆傝繖閲屾槸PHP浠g爜娈碉紝褰撹皟鐢checkAccess鏃 浼氭墽琛屻 |
$data | mixed | 椤圭洰鍏宠仈鐨勯澶栨暟鎹 |
{return} | CAuthItem | 鎺堟潈椤圭洰 |
public function createAuthItem($name,$type,$description='',$bizRule=null,$data=null)
{
if(isset($this->_items[$name]))
throw new CException(Yii::t('yii','Unable to add an item whose name is the same as an existing item.'));
return $this->_items[$name]=new CAuthItem($this,$name,$type,$description,$bizRule,$data);
}
鍒涘缓涓涓巿鏉冮」鐩 浠h〃鍔ㄤ綔鏉冮檺鐨勬巿鏉冮」鐩紙濡傦紝鍒涘缓涓涓唴瀹癸級銆 瀹冩湁涓夌绫诲瀷锛氭搷浣滐紝浠诲姟锛岃鑹层 灞傜骇鎺堟潈椤圭洰銆傞珮灞傞」鐩户鎵 浣庡眰椤圭洰鐨勬潈闄愩
protected boolean detectLoop(string $itemName, string $childName)
| ||
$itemName | string | 鐖堕」鐩悕瀛 |
$childName | string | 瑕佹坊鍔犲埌灞傜骇鐨勫瓙椤圭洰鍚嶅瓧銆 |
{return} | boolean | 杩斿洖杩欎釜寰幆鏄惁瀛樺湪銆 |
protected function detectLoop($itemName,$childName)
{
if($childName===$itemName)
return true;
if(!isset($this->_children[$childName], $this->_items[$itemName]))
return false;
foreach($this->_children[$childName] as $child)
{
if($this->detectLoop($itemName,$child->getName()))
return true;
}
return false;
}
妫鏌ユ巿鏉冮」鐩眰绾ф槸鍚﹀瓨鍦ㄥ惊鐜
public CAuthAssignment getAuthAssignment(string $itemName, mixed $userId)
| ||
$itemName | string | 椤圭洰鍚嶅瓧 |
$userId | mixed | 鐢ㄦ埛ID锛堣鎯呰鍙傝IWebUser::getId锛夈 |
{return} | CAuthAssignment | 椤圭洰浠诲姟淇℃伅銆傚鏋滄病鏈夎祴浜堢粰鐢ㄦ埛锛 鍒欒繑鍥瀗ull銆 |
public function getAuthAssignment($itemName,$userId)
{
return isset($this->_assignments[$userId][$itemName])?$this->_assignments[$userId][$itemName]:null;
}
杩斿洖椤圭洰浠诲姟淇℃伅銆
public array getAuthAssignments(mixed $userId)
| ||
$userId | mixed | 鐢ㄦ埛ID锛堣鎯呰鍙傝IWebUser::getId锛夈 |
{return} | array | 鐢ㄦ埛鐨勯」鐩换鍔′俊鎭傚鏋滄病鏈夐」鐩祴浜堢粰鐢ㄦ埛锛 鍒欒繑鍥瀗ull銆 |
public function getAuthAssignments($userId)
{
return isset($this->_assignments[$userId])?$this->_assignments[$userId]:array();
}
杩斿洖鎸囧畾鐢ㄦ埛鐨勯」鐩换鍔°
public CAuthItem getAuthItem(string $name)
| ||
$name | string | 椤圭洰鍚嶅瓧 |
{return} | CAuthItem | 鎺堟潈椤圭洰銆傚鏋滄壘涓嶅埌璇ラ」鐩紝鍒欒繑鍥瀗ull銆 |
public function getAuthItem($name)
{
return isset($this->_items[$name])?$this->_items[$name]:null;
}
杩斿洖鎸囧畾鍚嶅瓧鐨勬巿鏉冮」鐩
public array getAuthItems(integer $type=NULL, mixed $userId=NULL)
| ||
$type | integer | 椤圭洰绫诲瀷锛0锛氭搷浣滐紝1锛氫换鍔★紝2锛氳鑹诧級銆傞粯璁や负null锛 鎰忓懗鐫杩斿洖鎵鏈夌被鍨嬨 |
$userId | mixed | 鐢ㄦ埛ID銆傞粯璁や负null锛屾剰鍛崇潃鍗充娇娌℃湁璧嬩簣缁欑敤鎴凤紝 涔熻繑鍥炴墍鏈夌殑椤圭洰銆 |
{return} | array | 鎸囧畾绫诲瀷鐨勬巿鏉冮」鐩 |
public function getAuthItems($type=null,$userId=null)
{
if($type===null && $userId===null)
return $this->_items;
$items=array();
if($userId===null)
{
foreach($this->_items as $name=>$item)
{
if($item->getType()==$type)
$items[$name]=$item;
}
}
else if(isset($this->_assignments[$userId]))
{
foreach($this->_assignments[$userId] as $assignment)
{
$name=$assignment->getItemName();
if(isset($this->_items[$name]) && ($type===null || $this->_items[$name]->getType()==$type))
$items[$name]=$this->_items[$name];
}
}
return $items;
}
杩斿洖鎸囧畾绫诲瀷鍜岀敤鎴风殑鎺堟潈椤圭洰銆
public array getItemChildren(mixed $names)
| ||
$names | mixed | 鐖堕」鐩悕瀛椼傚彲浠ユ槸瀛楃涓叉垨鑰呮暟缁勩 濡傛灉鏄悗鑰咃紝閭d箞瀹冩槸椤圭洰鍚嶅瓧鍒楄〃銆 |
{return} | array | 鐖堕」鐩殑鎵鏈夊瓙椤圭洰 |
public function getItemChildren($names)
{
if(is_string($names))
return isset($this->_children[$names]) ? $this->_children[$names] : array();
$children=array();
foreach($names as $name)
{
if(isset($this->_children[$name]))
$children=array_merge($children,$this->_children[$name]);
}
return $children;
}
杩斿洖鎸囧畾椤圭洰鐨勫瓙椤圭洰
public boolean hasItemChild(string $itemName, string $childName)
| ||
$itemName | string | 鐖堕」鐩悕瀛 |
$childName | string | 瀛愰」鐩悕瀛 |
{return} | boolean | 瀛愰」鐩槸鍚﹀瓨鍦 |
public function hasItemChild($itemName,$childName)
{
return isset($this->_children[$itemName][$childName]);
}
杩斿洖鍊艰鏄庣埗鎺堟潈椤圭洰鏄惁鍖呭惈瀛愭巿鏉冮」鐩
public void init()
|
public function init()
{
parent::init();
if($this->authFile===null)
$this->authFile=Yii::getPathOfAlias('application.data.auth').'.php';
$this->load();
}
鍒濆鍖栧簲鐢ㄧ粍浠躲 杩欎釜鏂规硶鏄鐩栫埗绫荤殑鏂规硶锛屼粠PHP鏂囦欢 鍔犺浇鎺堟潈鏁版嵁銆
public boolean isAssigned(string $itemName, mixed $userId)
| ||
$itemName | string | 椤圭洰鍚嶅瓧 |
$userId | mixed | 鐢ㄦ埛ID锛堣鎯呰鍙傝IWebUser::getId锛夈 |
{return} | boolean | 杩欎釜椤圭洰鏄惁宸茬粡璧嬩簣缁欑敤鎴枫 |
public function isAssigned($itemName,$userId)
{
return isset($this->_assignments[$userId][$itemName]);
}
杩斿洖鍊艰鏄庤繖涓」鐩槸鍚﹀凡缁忚祴浜堢粰鐢ㄦ埛銆
public void load()
|
public function load()
{
$this->clearAll();
$items=$this->loadFromFile($this->authFile);
foreach($items as $name=>$item)
$this->_items[$name]=new CAuthItem($this,$name,$item['type'],$item['description'],$item['bizRule'],$item['data']);
foreach($items as $name=>$item)
{
if(isset($item['children']))
{
foreach($item['children'] as $childName)
{
if(isset($this->_items[$childName]))
$this->_children[$name][$childName]=$this->_items[$childName];
}
}
if(isset($item['assignments']))
{
foreach($item['assignments'] as $userId=>$assignment)
{
$this->_assignments[$userId][$name]=new CAuthAssignment($this,$name,$userId,$assignment['bizRule'],$assignment['data']);
}
}
}
}
鍔犺浇鎺堟潈鏁版嵁銆
protected array loadFromFile(string $file)
| ||
$file | string | 鏂囦欢璺緞銆 |
{return} | array | 鎺堟潈鏁版嵁銆 |
protected function loadFromFile($file)
{
if(is_file($file))
return require($file);
else
return array();
}
浠嶱HP鏂囦欢鍔犺浇鎺堟潈鏁版嵁銆
鍙傝
public boolean removeAuthItem(string $name)
| ||
$name | string | 瑕佺Щ闄ょ殑椤圭洰鍚嶅瓧 |
{return} | boolean | 瀛樺偍鍣ㄥ瓨鍦ㄧ殑椤圭洰鏄惁宸茬粡绉婚櫎鎴愬姛銆 |
public function removeAuthItem($name)
{
if(isset($this->_items[$name]))
{
foreach($this->_children as &$children)
unset($children[$name]);
foreach($this->_assignments as &$assignments)
unset($assignments[$name]);
unset($this->_items[$name]);
return true;
}
else
return false;
}
绉婚櫎鎸囧畾鐨勬巿鏉冮」鐩
public boolean removeItemChild(string $itemName, string $childName)
| ||
$itemName | string | 鐖堕」鐩悕瀛 |
$childName | string | 瀛愰」鐩悕瀛 |
{return} | boolean | 鏄惁鍒犻櫎鎴愬姛 |
public function removeItemChild($itemName,$childName)
{
if(isset($this->_children[$itemName][$childName]))
{
unset($this->_children[$itemName][$childName]);
return true;
}
else
return false;
}
绉婚櫎瀛愰」鐩 瑕佹敞鎰忕殑鏄紝瀛愰」鐩槸娌℃湁琚垹闄ょ殑銆傚彧鏄畠璺熺埗椤圭洰鐨勫叧绯昏В銆
public boolean revoke(string $itemName, mixed $userId)
| ||
$itemName | string | 椤圭洰鍚嶅瓧 |
$userId | mixed | 鐢ㄦ埛ID锛堣鎯呰鍙傝IWebUser::getId锛夈 |
{return} | boolean | 鎾ゆ秷鏄惁鎴愬姛 |
public function revoke($itemName,$userId)
{
if(isset($this->_assignments[$userId][$itemName]))
{
unset($this->_assignments[$userId][$itemName]);
return true;
}
else
return false;
}
鎾ゆ秷鐢ㄦ埛鐨勬巿鏉冧换鍔°
public void save()
|
public function save()
{
$items=array();
foreach($this->_items as $name=>$item)
{
$items[$name]=array(
'type'=>$item->getType(),
'description'=>$item->getDescription(),
'bizRule'=>$item->getBizRule(),
'data'=>$item->getData(),
);
if(isset($this->_children[$name]))
{
foreach($this->_children[$name] as $child)
$items[$name]['children'][]=$child->getName();
}
}
foreach($this->_assignments as $userId=>$assignments)
{
foreach($assignments as $name=>$assignment)
{
if(isset($items[$name]))
{
$items[$name]['assignments'][$userId]=array(
'bizRule'=>$assignment->getBizRule(),
'data'=>$assignment->getData(),
);
}
}
}
$this->saveToFile($items,$this->authFile);
}
灏嗘巿鏉冩暟鎹繚瀛樺埌鎸佷箙鍖栧瓨鍌ㄥ櫒銆 濡傛灉鎺堟潈鏁版嵁鏈変换浣曟敼鍙橈紝 璇风‘淇濅綘璋冪敤鐨勮繖涓柟娉曡兘灏嗘敼鍙樻暟鎹繚瀛樺埌鎸佷箙瀛樺偍銆
public void saveAuthAssignment(CAuthAssignment $assignment)
| ||
$assignment | CAuthAssignment | 宸茬粡淇敼鐨勬巿鏉冧俊鎭 |
public function saveAuthAssignment($assignment)
{
}
淇濆瓨淇敼鐨勬巿鏉冧俊鎭
public void saveAuthItem(CAuthItem $item, string $oldName=NULL)
| ||
$item | CAuthItem | 瑕佷繚瀛樼殑椤圭洰銆 |
$oldName | string | 鏃х殑椤圭洰鍚嶅瓧銆傚鏋滀负null锛屾剰鍛崇潃椤圭洰鍚嶅瓧娌℃湁鏀瑰彉銆 |
public function saveAuthItem($item,$oldName=null)
{
if($oldName!==null && ($newName=$item->getName())!==$oldName) // name changed
{
if(isset($this->_items[$newName]))
throw new CException(Yii::t('yii','Unable to change the item name. The name "{name}" is already used by another item.',array('{name}'=>$newName)));
if(isset($this->_items[$oldName]) && $this->_items[$oldName]===$item)
{
unset($this->_items[$oldName]);
$this->_items[$newName]=$item;
if(isset($this->_children[$oldName]))
{
$this->_children[$newName]=$this->_children[$oldName];
unset($this->_children[$oldName]);
}
foreach($this->_children as &$children)
{
if(isset($children[$oldName]))
{
$children[$newName]=$children[$oldName];
unset($children[$oldName]);
}
}
foreach($this->_assignments as &$assignments)
{
if(isset($assignments[$oldName]))
{
$assignments[$newName]=$assignments[$oldName];
unset($assignments[$oldName]);
}
}
}
}
}
灏嗘巿鏉冮」鐩繚瀛樺埌鎸佷箙鍖栧瓨鍌ㄥ櫒銆
protected void saveToFile(array $data, string $file)
| ||
$data | array | 鎺堟潈鏁版嵁 |
$file | string | 鏂囦欢璺緞銆 |
protected function saveToFile($data,$file)
{
file_put_contents($file,"<?php\nreturn ".var_export($data,true).";\n");
}
淇濆瓨鎺堟潈鏁版嵁鍒癙HP鏂囦欢銆