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