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_Api2
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: * API2 global ACL role resource model
29: *
30: * @category Mage
31: * @package Mage_Api2
32: * @author Magento Core Team <core@magentocommerce.com>
33: * @method int getId
34: * @method string getRoleName
35: */
36: class Mage_Api2_Model_Resource_Acl_Global_Role extends Mage_Core_Model_Resource_Db_Abstract
37: {
38: /**
39: * Initialize resource model
40: *
41: * @return void
42: */
43: protected function _construct()
44: {
45: $this->_init('api2/acl_role', 'entity_id');
46: }
47:
48: /**
49: * Create/update relation row of admin user to API2 role
50: *
51: * @param int $adminId Admin user id
52: * @param int $roleId API2 role id
53: * @return Mage_Api2_Model_Resource_Acl_Global_Role
54: */
55: public function saveAdminToRoleRelation($adminId, $roleId)
56: {
57: if (Mage_Api2_Model_Acl_Global_Role::ROLE_GUEST_ID == $roleId
58: || Mage_Api2_Model_Acl_Global_Role::ROLE_CUSTOMER_ID == $roleId
59: ) {
60: Mage::throwException(
61: Mage::helper('api2')->__('The role is a special one and not for assigning it to admin users.')
62: );
63: }
64:
65: $read = $this->_getReadAdapter();
66: $select = $read->select()
67: ->from($this->getTable('api2/acl_user'), 'admin_id')
68: ->where('admin_id = ?', $adminId, Zend_Db::INT_TYPE);
69:
70: $write = $this->_getWriteAdapter();
71: $table = $this->getTable('api2/acl_user');
72:
73: if (false === $read->fetchOne($select)) {
74: $write->insert($table, array('admin_id' => $adminId, 'role_id' => $roleId));
75: } else {
76: $write->update($table, array('role_id' => $roleId), array('admin_id = ?' => $adminId));
77: }
78:
79: return $this;
80: }
81:
82: /**
83: * delete relation row of admin user to API2 role
84: *
85: * @param int $adminId Admin user id
86: * @param int $roleId API2 role id
87: * @return Mage_Api2_Model_Resource_Acl_Global_Role
88: */
89: public function deleteAdminToRoleRelation($adminId, $roleId)
90: {
91: $write = $this->_getWriteAdapter();
92: $table = $this->getTable('api2/acl_user');
93:
94: $where = array(
95: 'role_id = ?' => $roleId,
96: 'admin_id = ?' => $adminId
97: );
98:
99: $write->delete($table, $where);
100:
101: return $this;
102: }
103:
104: /**
105: * Get users
106: *
107: * @param Mage_Api2_Model_Acl_Global_Role $role
108: * @return array
109: */
110: public function getRoleUsers(Mage_Api2_Model_Acl_Global_Role $role)
111: {
112: $adapter = $this->_getReadAdapter();
113: $select = $adapter->select()
114: ->from($this->getTable('api2/acl_user'))
115: ->where('role_id=?', $role->getId());
116:
117: $users = $adapter->fetchCol($select);
118:
119: return $users;
120: }
121: }
122: