1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16:
17: App::uses('AppModel', 'Model');
18:
19: 20: 21: 22: 23:
24: class Permission extends AppModel {
25:
26: 27: 28: 29: 30:
31: public $cacheQueries = false;
32:
33: 34: 35: 36: 37:
38: public $useTable = 'aros_acos';
39:
40: 41: 42: 43: 44:
45: public $belongsTo = array('Aro', 'Aco');
46:
47: 48: 49: 50: 51:
52: public $actsAs = null;
53:
54: 55: 56: 57:
58: public function __construct() {
59: $config = Configure::read('Acl.database');
60: if (!empty($config)) {
61: $this->useDbConfig = $config;
62: }
63: parent::__construct();
64: }
65:
66: 67: 68: 69: 70: 71: 72: 73:
74: public function check($aro, $aco, $action = '*') {
75: if (!$aro || !$aco) {
76: return false;
77: }
78:
79: $permKeys = $this->getAcoKeys($this->schema());
80: $aroPath = $this->Aro->node($aro);
81: $acoPath = $this->Aco->node($aco);
82:
83: if (!$aroPath) {
84: $this->log(__d('cake_dev',
85: "%s - Failed ARO node lookup in permissions check. Node references:\nAro: %s\nAco: %s",
86: 'DbAcl::check()',
87: print_r($aro, true),
88: print_r($aco, true)),
89: E_USER_WARNING
90: );
91: return false;
92: }
93:
94: if (!$acoPath) {
95: $this->log(__d('cake_dev',
96: "%s - Failed ACO node lookup in permissions check. Node references:\nAro: %s\nAco: %s",
97: 'DbAcl::check()',
98: print_r($aro, true),
99: print_r($aco, true)),
100: E_USER_WARNING
101: );
102: return false;
103: }
104:
105: if ($action !== '*' && !in_array('_' . $action, $permKeys)) {
106: $this->log(__d('cake_dev', "ACO permissions key %s does not exist in %s", $action, 'DbAcl::check()'), E_USER_NOTICE);
107: return false;
108: }
109:
110: $acoIDs = Hash::extract($acoPath, '{n}.' . $this->Aco->alias . '.id');
111:
112: $count = count($aroPath);
113: $inherited = array();
114: for ($i = 0; $i < $count; $i++) {
115: $permAlias = $this->alias;
116:
117: $perms = $this->find('all', array(
118: 'conditions' => array(
119: "{$permAlias}.aro_id" => $aroPath[$i][$this->Aro->alias]['id'],
120: "{$permAlias}.aco_id" => $acoIDs
121: ),
122: 'order' => array($this->Aco->alias . '.lft' => 'desc'),
123: 'recursive' => 0
124: ));
125:
126: if (empty($perms)) {
127: continue;
128: }
129: $perms = Hash::extract($perms, '{n}.' . $this->alias);
130: foreach ($perms as $perm) {
131: if ($action === '*') {
132: if (empty($perm)) {
133: continue;
134: }
135: foreach ($permKeys as $key) {
136: if ($perm[$key] == -1 && !(isset($inherited[$key]) && $inherited[$key] == 1)) {
137:
138: return false;
139: } elseif ($perm[$key] == 1) {
140:
141: $inherited[$key] = $perm[$key];
142: }
143: }
144: } else {
145: switch ($perm['_' . $action]) {
146: case -1:
147: return false;
148: case 0:
149: continue;
150: case 1:
151: return true;
152: }
153: }
154: }
155:
156: if ($action === '*' && count($inherited) === count($permKeys)) {
157: return true;
158: }
159: }
160: return false;
161: }
162:
163: 164: 165: 166: 167: 168: 169: 170: 171: 172:
173: public function allow($aro, $aco, $actions = '*', $value = 1) {
174: $perms = $this->getAclLink($aro, $aco);
175: $permKeys = $this->getAcoKeys($this->schema());
176: $save = array();
177:
178: if (!$perms) {
179: $this->log(__d('cake_dev', '%s - Invalid node', 'DbAcl::allow()'), E_USER_WARNING);
180: return false;
181: }
182: if (isset($perms[0])) {
183: $save = $perms[0][$this->alias];
184: }
185:
186: if ($actions === '*') {
187: $save = array_combine($permKeys, array_pad(array(), count($permKeys), $value));
188: } else {
189: if (!is_array($actions)) {
190: $actions = array('_' . $actions);
191: }
192: foreach ($actions as $action) {
193: if ($action{0} !== '_') {
194: $action = '_' . $action;
195: }
196: if (!in_array($action, $permKeys, true)) {
197: throw new AclException(__d('cake_dev', 'Invalid permission key "%s"', $action));
198: }
199: $save[$action] = $value;
200: }
201: }
202: list($save['aro_id'], $save['aco_id']) = array($perms['aro'], $perms['aco']);
203:
204: if ($perms['link'] && !empty($perms['link'])) {
205: $save['id'] = $perms['link'][0][$this->alias]['id'];
206: } else {
207: unset($save['id']);
208: $this->id = null;
209: }
210: return ($this->save($save) !== false);
211: }
212:
213: 214: 215: 216: 217: 218: 219:
220: public function getAclLink($aro, $aco) {
221: $obj = array();
222: $obj['Aro'] = $this->Aro->node($aro);
223: $obj['Aco'] = $this->Aco->node($aco);
224:
225: if (empty($obj['Aro']) || empty($obj['Aco'])) {
226: return false;
227: }
228: $aro = Hash::extract($obj, 'Aro.0.' . $this->Aro->alias . '.id');
229: $aco = Hash::extract($obj, 'Aco.0.' . $this->Aco->alias . '.id');
230: $aro = current($aro);
231: $aco = current($aco);
232:
233: return array(
234: 'aro' => $aro,
235: 'aco' => $aco,
236: 'link' => $this->find('all', array('conditions' => array(
237: $this->alias . '.aro_id' => $aro,
238: $this->alias . '.aco_id' => $aco
239: )))
240: );
241: }
242:
243: 244: 245: 246: 247: 248:
249: public function getAcoKeys($keys) {
250: $newKeys = array();
251: $keys = array_keys($keys);
252: foreach ($keys as $key) {
253: if (!in_array($key, array('id', 'aro_id', 'aco_id'))) {
254: $newKeys[] = $key;
255: }
256: }
257: return $newKeys;
258: }
259: }
260: