1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_Api2_Model_Acl_Global_Rule_Tree extends Mage_Core_Helper_Abstract
35: {
36: 37: 38:
39: const TYPE_ATTRIBUTE = 'attribute';
40: const TYPE_PRIVILEGE = 'privilege';
41:
42:
43: 44: 45:
46: const NAME_CHILDREN = 'children';
47: const NAME_PRIVILEGE = 'privilege';
48: const NAME_OPERATION = 'operation';
49: const NAME_ATTRIBUTE = 'attribute';
50: const NAME_RESOURCE = 'resource';
51: const NAME_RESOURCE_GROUPS = 'resource_groups';
52: const NAME_GROUP = 'group';
53:
54:
55: 56: 57:
58: const ID_SEPARATOR = '-';
59:
60: 61: 62: 63: 64:
65: protected $_role;
66:
67: 68: 69: 70: 71:
72: protected $_resourcesPermissions;
73:
74: 75: 76: 77: 78:
79: protected $_resourcesConfig;
80:
81: 82: 83: 84: 85:
86: protected $_existPrivileges;
87:
88: 89: 90: 91: 92:
93: protected $_existOperations;
94:
95: 96: 97: 98: 99:
100: protected $_type;
101:
102: 103: 104: 105: 106:
107: protected $_initialized = false;
108:
109: 110: 111: 112: 113:
114: protected $_hasEntityOnlyAttributes = false;
115:
116: 117: 118: 119: 120: 121: 122: 123: 124:
125: public function __construct($options)
126: {
127: $this->_type = $options['type'];
128:
129: switch ($this->_type) {
130: case self::TYPE_ATTRIBUTE:
131:
132: $operationSource = Mage::getModel('api2/acl_filter_attribute_operation');
133: $this->_existOperations = $operationSource->toArray();
134: break;
135:
136: case self::TYPE_PRIVILEGE:
137:
138: $privilegeSource = Mage::getModel('api2/acl_global_rule_privilege');
139: $this->_existPrivileges = $privilegeSource->toArray();
140: break;
141:
142: default:
143: throw new Exception(sprintf('Unknown tree type "%s".', $this->_type));
144: break;
145: }
146: }
147:
148: 149: 150: 151: 152: 153:
154: protected function _init()
155: {
156: if ($this->_initialized) {
157: return $this;
158: }
159:
160:
161: $config = Mage::getModel('api2/config');
162: $this->_resourcesConfig = $config->getResourceGroups();
163:
164: if ($this->_type == self::TYPE_ATTRIBUTE && !$this->_existOperations) {
165: throw new Exception('Operations is not set');
166: }
167:
168: if ($this->_type == self::TYPE_PRIVILEGE && !$this->_existPrivileges) {
169: throw new Exception('Privileges is not set.');
170: }
171:
172: return $this;
173: }
174:
175: 176: 177: 178: 179:
180: public function getPostResources()
181: {
182: $isAll = Mage::app()->getRequest()->getParam(Mage_Api2_Model_Acl_Global_Rule::RESOURCE_ALL);
183: $allow = Mage_Api2_Model_Acl_Global_Rule_Permission::TYPE_ALLOW;
184: if ($isAll) {
185: $resources = array(
186: Mage_Api2_Model_Acl_Global_Rule::RESOURCE_ALL => array(
187: null => $allow
188: )
189: );
190: } else {
191: $resources = array();
192: $checkedResources = explode(',', Mage::app()->getRequest()->getParam('resource'));
193: $prefixResource = self::NAME_RESOURCE . self::ID_SEPARATOR;
194: switch ($this->_type) {
195: case self::TYPE_PRIVILEGE:
196: $prefixPrivilege = self::NAME_PRIVILEGE . self::ID_SEPARATOR;
197: $nameResource = null;
198: foreach ($checkedResources as $i => $item) {
199: if (0 === strpos($item, $prefixResource)) {
200: $nameResource = substr($item, mb_strlen($prefixResource, 'UTF-8'));
201: $resources[$nameResource] = array();
202: } elseif (0 === strpos($item, $prefixPrivilege)) {
203: $name = substr($item, mb_strlen($prefixPrivilege, 'UTF-8'));
204: $namePrivilege = str_replace($nameResource . self::ID_SEPARATOR, '', $name);
205: $resources[$nameResource][$namePrivilege] = $allow;
206: } else {
207: unset($checkedResources[$i]);
208: }
209: }
210: break;
211:
212: case self::TYPE_ATTRIBUTE:
213: $prefixOperation = self::NAME_OPERATION . self::ID_SEPARATOR;
214: $prefixAttribute = self::NAME_ATTRIBUTE . self::ID_SEPARATOR;
215: $nameResource = null;
216: foreach ($checkedResources as $i => $item) {
217: if (0 === strpos($item, $prefixResource)) {
218: $nameResource = substr($item, mb_strlen($prefixResource, 'UTF-8'));
219: $resources[$nameResource] = array();
220: } elseif (0 === strpos($item, $prefixOperation)) {
221: $name = substr($item, mb_strlen($prefixOperation, 'UTF-8'));
222: $operationName = str_replace($nameResource . self::ID_SEPARATOR, '', $name);
223: $resources[$nameResource][$operationName] = array();
224: } elseif (0 === strpos($item, $prefixAttribute)) {
225: $name = substr($item, mb_strlen($prefixOperation, 'UTF-8'));
226: $attributeName = str_replace(
227: $nameResource . self::ID_SEPARATOR . $operationName . self::ID_SEPARATOR,
228: '',
229: $name
230: );
231: $resources[$nameResource][$operationName][$attributeName] = $allow;
232: } else {
233: unset($checkedResources[$i]);
234: }
235: }
236: break;
237:
238:
239: }
240: }
241: return $resources;
242: }
243:
244: 245: 246: 247: 248:
249: public function getEverythingAllowed()
250: {
251: $this->_init();
252:
253: $all = Mage_Api2_Model_Acl_Global_Rule::RESOURCE_ALL;
254: return !empty($this->_resourcesPermissions[$all]);
255: }
256:
257: 258: 259: 260: 261:
262: public function getTreeResources()
263: {
264: $this->_init();
265: $root = $this->_getTreeNode($this->_resourcesConfig, 1);
266: return isset($root[self::NAME_CHILDREN]) ? $root[self::NAME_CHILDREN] : array();
267: }
268:
269: 270: 271: 272: 273: 274: 275:
276: protected function _getTreeNode($node, $level = 0)
277: {
278: $item = array();
279:
280: $isResource = false;
281: $isGroup = false;
282: $name = null;
283:
284: if ($level != 0) {
285: $name = $node->getName();
286: if (!(int) $node->resource) {
287: if (self::NAME_RESOURCE_GROUPS != $name) {
288: $isGroup = true;
289: $item['id'] = self::NAME_GROUP . self::ID_SEPARATOR . $name;
290: }
291: $item['text'] = (string) $node->title;
292: } else {
293: $isResource = true;
294: $item['id'] = self::NAME_RESOURCE . self::ID_SEPARATOR . $name;
295: $item['text'] = $this->__('%s', (string) $node->title);
296: }
297: $item['checked'] = false;
298: $item['sort_order'] = isset($node->sort_order) ? (string) $node->sort_order : 0;
299: }
300: if (isset($node->children)) {
301: $children = $node->children->children();
302: } else {
303: $children = $node->children();
304: }
305:
306: if (empty($children)) {
307: 308: 309: 310:
311: return $item;
312: }
313:
314: $item[self::NAME_CHILDREN] = array();
315:
316: if ($isResource) {
317: if (self::TYPE_ATTRIBUTE == $this->_type) {
318: if (!$this->_addOperations($item, $node, $name)) {
319: return null;
320: }
321: } elseif (self::TYPE_PRIVILEGE == $this->_type) {
322: if (!$this->_addPrivileges($item, $node, $name)) {
323: return null;
324: }
325: }
326: }
327:
328:
329: foreach ($children as $child) {
330: if ($child->getName() != 'title' && $child->getName() != 'sort_order') {
331: if (!(string) $child->title) {
332: continue;
333: }
334:
335: if ($level != 0) {
336: $subNode = $this->_getTreeNode($child, $level + 1);
337: if (!$subNode) {
338: continue;
339: }
340:
341: if (!empty($subNode['checked'])) {
342: $item['checked'] = true;
343: }
344: $item[self::NAME_CHILDREN][] = $subNode;
345: } else {
346: $item = $this->_getTreeNode($child, $level + 1);
347: }
348: }
349: }
350: if (!empty($item[self::NAME_CHILDREN])) {
351: usort($item[self::NAME_CHILDREN], array($this, '_sortTree'));
352: } elseif ($isGroup) {
353:
354: return null;
355: }
356: return $item;
357: }
358:
359: 360: 361: 362: 363: 364: 365: 366:
367: protected function _addPrivileges(&$item, Varien_Simplexml_Element $node, $name)
368: {
369: $roleConfigNodeName = $this->getRole()->getConfigNodeName();
370: $possibleList = array();
371: if (isset($node->privileges)) {
372: $possibleRoles = $node->privileges->asArray();
373: if (isset($possibleRoles[$roleConfigNodeName])) {
374: $possibleList = $possibleRoles[$roleConfigNodeName];
375: }
376: }
377:
378: if (!$possibleList) {
379: return false;
380: }
381:
382: $cnt = 0;
383: foreach ($this->_existPrivileges as $key => $title) {
384: if (empty($possibleList[$key])) {
385: continue;
386: }
387: $checked = !empty($this->_resourcesPermissions[$name]['privileges'][$roleConfigNodeName][$key]);
388: $item['checked'] = $checked ? $checked : $item['checked'];
389: $subItem = array(
390: 'id' => self::NAME_PRIVILEGE . self::ID_SEPARATOR . $name . self::ID_SEPARATOR . $key,
391: 'text' => $title,
392: 'checked' => $checked,
393: 'sort_order' => ++$cnt,
394: );
395: $item[self::NAME_CHILDREN][] = $subItem;
396: }
397: return true;
398: }
399:
400: 401: 402: 403: 404: 405: 406: 407:
408: protected function _addOperations(&$item, Varien_Simplexml_Element $node, $name)
409: {
410: $cnt = 0;
411: foreach ($this->_existOperations as $key => $title) {
412: $subItem = array(
413: 'id' => self::NAME_OPERATION . self::ID_SEPARATOR . $name . self::ID_SEPARATOR . $key,
414: 'text' => $title,
415: 'checked' => false,
416: 'sort_order' => ++$cnt,
417: );
418:
419: if (!empty($this->_resourcesPermissions[$name]['operations'][$key]['attributes'])) {
420: if (!$this->_addAttribute($subItem, $node, $name, $key)) {
421: $cnt--;
422: continue;
423: }
424: } else {
425: $cnt--;
426: continue;
427: }
428: if (!empty($subItem['checked'])) {
429: $item['checked'] = true;
430: }
431: $item[self::NAME_CHILDREN][] = $subItem;
432: }
433: if (!$cnt) {
434: return false;
435: }
436: return true;
437: }
438:
439: 440: 441: 442: 443: 444: 445: 446: 447:
448: protected function _addAttribute(&$item, Varien_Simplexml_Element $node, $name, $privilege)
449: {
450: $cnt = 0;
451: foreach ($this->_resourcesPermissions[$name]['operations'][$privilege]['attributes'] as $key => $attribute) {
452: $title = $attribute['title'];
453: $status = $attribute['status'];
454:
455: $checked = $status == Mage_Api2_Model_Acl_Global_Rule_Permission::TYPE_ALLOW;
456: $item['checked'] = $checked ? $checked : $item['checked'];
457: $item[self::NAME_CHILDREN][] = array(
458: 'id' => self::NAME_ATTRIBUTE . self::ID_SEPARATOR . $name . self::ID_SEPARATOR . $privilege
459: . self::ID_SEPARATOR . $key,
460: 'text' => $title,
461: 'checked' => $checked,
462: 'sort_order' => ++$cnt,
463: );
464: }
465:
466: return true;
467: }
468:
469: 470: 471: 472: 473: 474: 475:
476: protected function _sortTree($a, $b)
477: {
478: return $a['sort_order'] < $b['sort_order'] ? -1 : ($a['sort_order'] > $b['sort_order'] ? 1 : 0);
479: }
480:
481: 482: 483: 484: 485: 486:
487: public function setRole($role)
488: {
489: $this->_role = $role;
490: return $this;
491: }
492:
493: 494: 495: 496: 497:
498: public function getRole()
499: {
500: return $this->_role;
501: }
502:
503: 504: 505: 506: 507: 508:
509: public function setResourcesPermissions($resourcesPermissions)
510: {
511: $this->_resourcesPermissions = $resourcesPermissions;
512: return $this;
513: }
514:
515: 516: 517: 518: 519:
520: public function getResourcesPermissions()
521: {
522: return $this->_resourcesPermissions;
523: }
524:
525: 526: 527: 528: 529: 530:
531: public function setHasEntityOnlyAttributes($hasEntityOnlyAttributes)
532: {
533: $this->_hasEntityOnlyAttributes = $hasEntityOnlyAttributes;
534: return $this;
535: }
536:
537: 538: 539: 540: 541:
542: public function getHasEntityOnlyAttributes()
543: {
544: return $this->_hasEntityOnlyAttributes;
545: }
546: }
547: