acl.php

来自「Cake Framwork , Excellent」· PHP 代码 · 共 628 行 · 第 1/2 页

PHP
628
字号
				$perms = Set::extract($perms, '{n}.' . $this->Aro->Permission->alias);				foreach ($perms as $perm) {					if ($action == '*') {						foreach ($permKeys as $key) {							if (!empty($perm)) {								if ($perm[$key] == -1) {									return false;								} elseif ($perm[$key] == 1) {									$inherited[$key] = 1;								}							}						}						if (count($inherited) === count($permKeys)) {							return true;						}					} else {						switch($perm['_' . $action]) {							case -1:								return false;							case 0:								continue;							break;							case 1:								return true;							break;						}					}				}			}		}		return false;	}/** * Allow $aro to have access to action $actions in $aco * * @param string $aro ARO * @param string $aco ACO * @param string $actions Action (defaults to *) * @param integer $value Value to indicate access type (1 to give access, -1 to deny, 0 to inherit) * @return boolean Success * @access public */	function allow($aro, $aco, $actions = "*", $value = 1) {		$perms = $this->getAclLink($aro, $aco);		$permKeys = $this->_getAcoKeys($this->Aro->Permission->schema());		$save = array();		if ($perms == false) {			trigger_error(__('DbAcl::allow() - Invalid node', true), E_USER_WARNING);			return false;		}		if (isset($perms[0])) {			$save = $perms[0][$this->Aro->Permission->alias];		}		if ($actions == "*") {			$permKeys = $this->_getAcoKeys($this->Aro->Permission->schema());			$save = array_combine($permKeys, array_pad(array(), count($permKeys), $value));		} else {			if (!is_array($actions)) {				$actions = array('_' . $actions);			}			if (is_array($actions)) {				foreach ($actions as $action) {					if ($action{0} != '_') {						$action = '_' . $action;					}					if (in_array($action, $permKeys)) {						$save[$action] = $value;					}				}			}		}		list($save['aro_id'], $save['aco_id']) = array($perms['aro'], $perms['aco']);		if ($perms['link'] != null && count($perms['link']) > 0) {			$save['id'] = $perms['link'][0][$this->Aro->Permission->alias]['id'];		} else {			unset($save['id']);			$this->Aro->Permission->id = null;		}		return ($this->Aro->Permission->save($save) !== false);	}/** * Deny access for $aro to action $action in $aco * * @param string $aro ARO * @param string $aco ACO * @param string $actions Action (defaults to *) * @return boolean Success * @access public */	function deny($aro, $aco, $action = "*") {		return $this->allow($aro, $aco, $action, -1);	}/** * Let access for $aro to action $action in $aco be inherited * * @param string $aro ARO * @param string $aco ACO * @param string $actions Action (defaults to *) * @return boolean Success * @access public */	function inherit($aro, $aco, $action = "*") {		return $this->allow($aro, $aco, $action, 0);	}/** * Allow $aro to have access to action $actions in $aco * * @param string $aro ARO * @param string $aco ACO * @param string $actions Action (defaults to *) * @return boolean Success * @see allow() * @access public */	function grant($aro, $aco, $action = "*") {		return $this->allow($aro, $aco, $action);	}/** * Deny access for $aro to action $action in $aco * * @param string $aro ARO * @param string $aco ACO * @param string $actions Action (defaults to *) * @return boolean Success * @see deny() * @access public */	function revoke($aro, $aco, $action = "*") {		return $this->deny($aro, $aco, $action);	}/** * Get an array of access-control links between the given Aro and Aco * * @param string $aro ARO * @param string $aco ACO * @return array Indexed array with: 'aro', 'aco' and 'link' * @access public */	function getAclLink($aro, $aco) {		$obj = array();		$obj['Aro'] = $this->Aro->node($aro);		$obj['Aco'] = $this->Aco->node($aco);		if (empty($obj['Aro']) || empty($obj['Aco'])) {			return false;		}		return array(			'aro' => Set::extract($obj, 'Aro.0.'.$this->Aro->alias.'.id'),			'aco'  => Set::extract($obj, 'Aco.0.'.$this->Aco->alias.'.id'),			'link' => $this->Aro->Permission->find('all', array('conditions' => array(				$this->Aro->Permission->alias . '.aro_id' => Set::extract($obj, 'Aro.0.'.$this->Aro->alias.'.id'),				$this->Aro->Permission->alias . '.aco_id' => Set::extract($obj, 'Aco.0.'.$this->Aco->alias.'.id')			)))		);	}/** * Get the keys used in an ACO * * @param array $keys Permission model info * @return array ACO keys * @access protected */	function _getAcoKeys($keys) {		$newKeys = array();		$keys = array_keys($keys);		foreach ($keys as $key) {			if (!in_array($key, array('id', 'aro_id', 'aco_id'))) {				$newKeys[] = $key;			}		}		return $newKeys;	}}/** * In this file you can extend the AclBase. * * @package		cake * @subpackage	cake.cake.libs.model.iniacl */class IniAcl extends AclBase {/** * Array with configuration, parsed from ini file * * @var array * @access public */	var $config = null;/** * The constructor must be overridden, as AclBase is abstract. * */	function __construct() {	}/** * Main ACL check function. Checks to see if the ARO (access request object) has access to the ACO (access control object). * Looks at the acl.ini.php file for permissions (see instructions in /config/acl.ini.php). * * @param string $aro ARO * @param string $aco ACO * @param string $aco_action Action * @return boolean Success * @access public */	function check($aro, $aco, $aco_action = null) {		if ($this->config == null) {			$this->config = $this->readConfigFile(CONFIGS . 'acl.ini.php');		}		$aclConfig = $this->config;		if (isset($aclConfig[$aro]['deny'])) {			$userDenies = $this->arrayTrim(explode(",", $aclConfig[$aro]['deny']));			if (array_search($aco, $userDenies)) {				return false;			}		}		if (isset($aclConfig[$aro]['allow'])) {			$userAllows = $this->arrayTrim(explode(",", $aclConfig[$aro]['allow']));			if (array_search($aco, $userAllows)) {				return true;			}		}		if (isset($aclConfig[$aro]['groups'])) {			$userGroups = $this->arrayTrim(explode(",", $aclConfig[$aro]['groups']));			foreach ($userGroups as $group) {				if (array_key_exists($group, $aclConfig)) {					if (isset($aclConfig[$group]['deny'])) {						$groupDenies=$this->arrayTrim(explode(",", $aclConfig[$group]['deny']));						if (array_search($aco, $groupDenies)) {							return false;						}					}					if (isset($aclConfig[$group]['allow'])) {						$groupAllows = $this->arrayTrim(explode(",", $aclConfig[$group]['allow']));						if (array_search($aco, $groupAllows)) {							return true;						}					}				}			}		}		return false;	}/** * Parses an INI file and returns an array that reflects the INI file's section structure. Double-quote friendly. * * @param string $fileName File * @return array INI section structure * @access public */	function readConfigFile($fileName) {		$fileLineArray = file($fileName);		foreach ($fileLineArray as $fileLine) {			$dataLine = trim($fileLine);			$firstChar = substr($dataLine, 0, 1);			if ($firstChar != ';' && $dataLine != '') {				if ($firstChar == '[' && substr($dataLine, -1, 1) == ']') {					$sectionName = preg_replace('/[\[\]]/', '', $dataLine);				} else {					$delimiter = strpos($dataLine, '=');					if ($delimiter > 0) {						$key = strtolower(trim(substr($dataLine, 0, $delimiter)));						$value = trim(substr($dataLine, $delimiter + 1));						if (substr($value, 0, 1) == '"' && substr($value, -1) == '"') {							$value = substr($value, 1, -1);						}						$iniSetting[$sectionName][$key]=stripcslashes($value);					} else {						if (!isset($sectionName)) {							$sectionName = '';						}						$iniSetting[$sectionName][strtolower(trim($dataLine))]='';					}				}			}		}		return $iniSetting;	}/** * Removes trailing spaces on all array elements (to prepare for searching) * * @param array $array Array to trim * @return array Trimmed array * @access public */	function arrayTrim($array) {		foreach ($array as $key => $value) {			$array[$key] = trim($value);		}		array_unshift($array, "");		return $array;	}}?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?