1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Admin
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27:
28: /**
29: * Admin rule resource model
30: *
31: * @category Mage
32: * @package Mage_Admin
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Admin_Model_Resource_Rules extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: /**
38: * Define main table
39: *
40: */
41: protected function _construct()
42: {
43: $this->_init('admin/rule', 'rule_id');
44: }
45:
46: /**
47: * Save ACL resources
48: *
49: * @param Mage_Admin_Model_Rules $rule
50: */
51: public function saveRel(Mage_Admin_Model_Rules $rule)
52: {
53: try {
54: $adapter = $this->_getWriteAdapter();
55: $adapter->beginTransaction();
56: $roleId = $rule->getRoleId();
57:
58: $condition = array(
59: 'role_id = ?' => (int) $roleId,
60: );
61:
62: $adapter->delete($this->getMainTable(), $condition);
63:
64: $postedResources = $rule->getResources();
65: if ($postedResources) {
66: $row = array(
67: 'role_type' => 'G',
68: 'resource_id' => 'all',
69: 'privileges' => '', // not used yet
70: 'assert_id' => 0,
71: 'role_id' => $roleId,
72: 'permission' => 'allow'
73: );
74:
75: // If all was selected save it only and nothing else.
76: if ($postedResources === array('all')) {
77: $insertData = $this->_prepareDataForTable(new Varien_Object($row), $this->getMainTable());
78:
79: $adapter->insert($this->getMainTable(), $insertData);
80: } else {
81: foreach (Mage::getModel('admin/roles')->getResourcesList2D() as $index => $resName) {
82: $row['permission'] = (in_array($resName, $postedResources) ? 'allow' : 'deny');
83: $row['resource_id'] = trim($resName, '/');
84:
85: $insertData = $this->_prepareDataForTable(new Varien_Object($row), $this->getMainTable());
86: $adapter->insert($this->getMainTable(), $insertData);
87: }
88: }
89: }
90:
91: $adapter->commit();
92: } catch (Mage_Core_Exception $e) {
93: $adapter->rollBack();
94: throw $e;
95: } catch (Exception $e){
96: $adapter->rollBack();
97: Mage::logException($e);
98: }
99: }
100: }
101: