00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class AclComponent extends Object {
00036
00037
00038
00039
00040
00041
00042 var $_Instance = null;
00043
00044
00045
00046
00047 function __construct() {
00048 $name = Inflector::camelize(strtolower(Configure::read('Acl.classname')));
00049 if (!class_exists($name)) {
00050 if (App::import('Component', $name)) {
00051 if (strpos($name, '.') !== false) {
00052 list($plugin, $name) = explode('.', $name);
00053 }
00054 $name .= 'Component';
00055 } else {
00056 trigger_error(sprintf(__('Could not find %s.', true), $name), E_USER_WARNING);
00057 }
00058 }
00059 $this->_Instance =& new $name();
00060 $this->_Instance->initialize($this);
00061 }
00062
00063
00064
00065
00066
00067
00068
00069 function startup(&$controller) {
00070 return true;
00071 }
00072
00073
00074
00075
00076
00077 function _initACL() {
00078 }
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088 function check($aro, $aco, $action = "*") {
00089 return $this->_Instance->check($aro, $aco, $action);
00090 }
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100 function allow($aro, $aco, $action = "*") {
00101 return $this->_Instance->allow($aro, $aco, $action);
00102 }
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112 function deny($aro, $aco, $action = "*") {
00113 return $this->_Instance->deny($aro, $aco, $action);
00114 }
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124 function inherit($aro, $aco, $action = "*") {
00125 return $this->_Instance->inherit($aro, $aco, $action);
00126 }
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136 function grant($aro, $aco, $action = "*") {
00137 return $this->_Instance->grant($aro, $aco, $action);
00138 }
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148 function revoke($aro, $aco, $action = "*") {
00149 return $this->_Instance->revoke($aro, $aco, $action);
00150 }
00151 }
00152
00153
00154
00155
00156
00157
00158
00159
00160 class AclBase extends Object {
00161
00162
00163
00164
00165 function __construct() {
00166 if (strcasecmp(get_class($this), "AclBase") == 0 || !is_subclass_of($this, "AclBase")) {
00167 trigger_error(__("[acl_base] The AclBase class constructor has been called, or the class was instantiated. This class must remain abstract. Please refer to the Cake docs for ACL configuration.", true), E_USER_ERROR);
00168 return NULL;
00169 }
00170 }
00171
00172
00173
00174
00175
00176
00177
00178
00179 function check($aro, $aco, $action = "*") {
00180 }
00181
00182
00183
00184
00185
00186
00187 function initialize(&$component) {
00188 }
00189 }
00190
00191
00192
00193
00194
00195
00196 class DbAcl extends AclBase {
00197
00198
00199
00200
00201 function __construct() {
00202 parent::__construct();
00203 if (!class_exists('AclNode')) {
00204 uses('model' . DS . 'db_acl');
00205 }
00206 $this->Aro =& ClassRegistry::init(array('class' => 'Aro', 'alias' => 'Aro'));
00207 $this->Aco =& ClassRegistry::init(array('class' => 'Aco', 'alias' => 'Aco'));
00208 }
00209
00210
00211
00212
00213
00214
00215
00216 function initialize(&$component) {
00217 $component->Aro = $this->Aro;
00218 $component->Aco = $this->Aco;
00219 }
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229 function check($aro, $aco, $action = "*") {
00230 if ($aro == null || $aco == null) {
00231 return false;
00232 }
00233
00234 $permKeys = $this->_getAcoKeys($this->Aro->Permission->schema());
00235 $aroPath = $this->Aro->node($aro);
00236 $acoPath = $this->Aco->node($aco);
00237
00238 if (empty($aroPath) || empty($acoPath)) {
00239 trigger_error("DbAcl::check() - Failed ARO/ACO node lookup in permissions check. Node references:\nAro: " . print_r($aro, true) . "\nAco: " . print_r($aco, true), E_USER_WARNING);
00240 return false;
00241 }
00242
00243 if ($acoPath == null || $acoPath == array()) {
00244 trigger_error("DbAcl::check() - Failed ACO node lookup in permissions check. Node references:\nAro: " . print_r($aro, true) . "\nAco: " . print_r($aco, true), E_USER_WARNING);
00245 return false;
00246 }
00247
00248 $aroNode = $aroPath[0];
00249 $acoNode = $acoPath[0];
00250
00251 if ($action != '*' && !in_array('_' . $action, $permKeys)) {
00252 trigger_error(sprintf(__("ACO permissions key %s does not exist in DbAcl::check()", true), $action), E_USER_NOTICE);
00253 return false;
00254 }
00255
00256 $inherited = array();
00257 $acoIDs = Set::extract($acoPath, '{n}.' . $this->Aco->alias . '.id');
00258
00259 $count = count($aroPath);
00260 for ($i = 0 ; $i < $count; $i++) {
00261 $permAlias = $this->Aro->Permission->alias;
00262
00263 $perms = $this->Aro->Permission->find('all', array(
00264 'conditions' => array(
00265 "{$permAlias}.aro_id" => $aroPath[$i][$this->Aro->alias]['id'],
00266 "{$permAlias}.aco_id" => $acoIDs
00267 ),
00268 'order' => array($this->Aco->alias . '.lft' => 'desc'),
00269 'recursive' => 0
00270 ));
00271
00272 if (empty($perms)) {
00273 continue;
00274 } else {
00275 $perms = Set::extract($perms, '{n}.' . $this->Aro->Permission->alias);
00276 foreach ($perms as $perm) {
00277 if ($action == '*') {
00278
00279 foreach ($permKeys as $key) {
00280 if (!empty($perm)) {
00281 if ($perm[$key] == -1) {
00282 return false;
00283 } elseif ($perm[$key] == 1) {
00284 $inherited[$key] = 1;
00285 }
00286 }
00287 }
00288
00289 if (count($inherited) === count($permKeys)) {
00290 return true;
00291 }
00292 } else {
00293 switch ($perm['_' . $action]) {
00294 case -1:
00295 return false;
00296 case 0:
00297 continue;
00298 break;
00299 case 1:
00300 return true;
00301 break;
00302 }
00303 }
00304 }
00305 }
00306 }
00307 return false;
00308 }
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319 function allow($aro, $aco, $actions = "*", $value = 1) {
00320 $perms = $this->getAclLink($aro, $aco);
00321 $permKeys = $this->_getAcoKeys($this->Aro->Permission->schema());
00322 $save = array();
00323
00324 if ($perms == false) {
00325 trigger_error(__('DbAcl::allow() - Invalid node', true), E_USER_WARNING);
00326 return false;
00327 }
00328 if (isset($perms[0])) {
00329 $save = $perms[0][$this->Aro->Permission->alias];
00330 }
00331
00332 if ($actions == "*") {
00333 $permKeys = $this->_getAcoKeys($this->Aro->Permission->schema());
00334 $save = array_combine($permKeys, array_pad(array(), count($permKeys), $value));
00335 } else {
00336 if (!is_array($actions)) {
00337 $actions = array('_' . $actions);
00338 }
00339 if (is_array($actions)) {
00340 foreach ($actions as $action) {
00341 if ($action{0} != '_') {
00342 $action = '_' . $action;
00343 }
00344 if (in_array($action, $permKeys)) {
00345 $save[$action] = $value;
00346 }
00347 }
00348 }
00349 }
00350 list($save['aro_id'], $save['aco_id']) = array($perms['aro'], $perms['aco']);
00351
00352 if ($perms['link'] != null && count($perms['link']) > 0) {
00353 $save['id'] = $perms['link'][0][$this->Aro->Permission->alias]['id'];
00354 } else {
00355 unset($save['id']);
00356 $this->Aro->Permission->id = null;
00357 }
00358 return ($this->Aro->Permission->save($save) !== false);
00359 }
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369 function deny($aro, $aco, $action = "*") {
00370 return $this->allow($aro, $aco, $action, -1);
00371 }
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381 function inherit($aro, $aco, $action = "*") {
00382 return $this->allow($aro, $aco, $action, 0);
00383 }
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394 function grant($aro, $aco, $action = "*") {
00395 return $this->allow($aro, $aco, $action);
00396 }
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407 function revoke($aro, $aco, $action = "*") {
00408 return $this->deny($aro, $aco, $action);
00409 }
00410
00411
00412
00413
00414
00415
00416
00417
00418 function getAclLink($aro, $aco) {
00419 $obj = array();
00420 $obj['Aro'] = $this->Aro->node($aro);
00421 $obj['Aco'] = $this->Aco->node($aco);
00422
00423 if (empty($obj['Aro']) || empty($obj['Aco'])) {
00424 return false;
00425 }
00426
00427 return array(
00428 'aro' => Set::extract($obj, 'Aro.0.'.$this->Aro->alias.'.id'),
00429 'aco' => Set::extract($obj, 'Aco.0.'.$this->Aco->alias.'.id'),
00430 'link' => $this->Aro->Permission->find('all', array('conditions' => array(
00431 $this->Aro->Permission->alias . '.aro_id' => Set::extract($obj, 'Aro.0.'.$this->Aro->alias.'.id'),
00432 $this->Aro->Permission->alias . '.aco_id' => Set::extract($obj, 'Aco.0.'.$this->Aco->alias.'.id')
00433 )))
00434 );
00435 }
00436
00437
00438
00439
00440
00441
00442
00443 function _getAcoKeys($keys) {
00444 $newKeys = array();
00445 $keys = array_keys($keys);
00446 foreach ($keys as $key) {
00447 if (!in_array($key, array('id', 'aro_id', 'aco_id'))) {
00448 $newKeys[] = $key;
00449 }
00450 }
00451 return $newKeys;
00452 }
00453 }
00454
00455
00456
00457
00458
00459
00460 class IniAcl extends AclBase {
00461
00462
00463
00464
00465
00466
00467 var $config = null;
00468
00469
00470
00471
00472 function __construct() {
00473 }
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484 function check($aro, $aco, $aco_action = null) {
00485 if ($this->config == null) {
00486 $this->config = $this->readConfigFile(CONFIGS . 'acl.ini.php');
00487 }
00488 $aclConfig = $this->config;
00489
00490 if (isset($aclConfig[$aro]['deny'])) {
00491 $userDenies = $this->arrayTrim(explode(",", $aclConfig[$aro]['deny']));
00492
00493 if (array_search($aco, $userDenies)) {
00494 return false;
00495 }
00496 }
00497
00498 if (isset($aclConfig[$aro]['allow'])) {
00499 $userAllows = $this->arrayTrim(explode(",", $aclConfig[$aro]['allow']));
00500
00501 if (array_search($aco, $userAllows)) {
00502 return true;
00503 }
00504 }
00505
00506 if (isset($aclConfig[$aro]['groups'])) {
00507 $userGroups = $this->arrayTrim(explode(",", $aclConfig[$aro]['groups']));
00508
00509 foreach ($userGroups as $group) {
00510 if (array_key_exists($group, $aclConfig)) {
00511 if (isset($aclConfig[$group]['deny'])) {
00512 $groupDenies=$this->arrayTrim(explode(",", $aclConfig[$group]['deny']));
00513
00514 if (array_search($aco, $groupDenies)) {
00515 return false;
00516 }
00517 }
00518
00519 if (isset($aclConfig[$group]['allow'])) {
00520 $groupAllows = $this->arrayTrim(explode(",", $aclConfig[$group]['allow']));
00521
00522 if (array_search($aco, $groupAllows)) {
00523 return true;
00524 }
00525 }
00526 }
00527 }
00528 }
00529 return false;
00530 }
00531
00532
00533
00534
00535
00536
00537
00538 function readConfigFile($fileName) {
00539 $fileLineArray = file($fileName);
00540
00541 foreach ($fileLineArray as $fileLine) {
00542 $dataLine = trim($fileLine);
00543 $firstChar = substr($dataLine, 0, 1);
00544
00545 if ($firstChar != ';' && $dataLine != '') {
00546 if ($firstChar == '[' && substr($dataLine, -1, 1) == ']') {
00547 $sectionName = preg_replace('/[\[\]]/', '', $dataLine);
00548 } else {
00549 $delimiter = strpos($dataLine, '=');
00550
00551 if ($delimiter > 0) {
00552 $key = strtolower(trim(substr($dataLine, 0, $delimiter)));
00553 $value = trim(substr($dataLine, $delimiter + 1));
00554
00555 if (substr($value, 0, 1) == '"' && substr($value, -1) == '"') {
00556 $value = substr($value, 1, -1);
00557 }
00558
00559 $iniSetting[$sectionName][$key]=stripcslashes($value);
00560 } else {
00561 if (!isset($sectionName)) {
00562 $sectionName = '';
00563 }
00564
00565 $iniSetting[$sectionName][strtolower(trim($dataLine))]='';
00566 }
00567 }
00568 }
00569 }
00570
00571 return $iniSetting;
00572 }
00573
00574
00575
00576
00577
00578
00579
00580 function arrayTrim($array) {
00581 foreach ($array as $key => $value) {
00582 $array[$key] = trim($value);
00583 }
00584 array_unshift($array, "");
00585 return $array;
00586 }
00587 }
00588 ?>