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 role list for admin user permissions
29: *
30: * @category Mage
31: * @package Mage_Api2
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Api2_Block_Adminhtml_Permissions_User_Edit_Tab_Roles
35: extends Mage_Adminhtml_Block_Widget_Grid
36: implements Mage_Adminhtml_Block_Widget_Tab_Interface
37: {
38: /**
39: * Selected API2 roles for grid
40: *
41: * @var array
42: */
43: protected $_selectedRoles;
44:
45: /**
46: * Constructor
47: * Prepare grid parameters
48: */
49: public function __construct()
50: {
51: parent::__construct();
52:
53: $this->setId('api2_roles_section')
54: ->setDefaultSort('sort_order')
55: ->setDefaultDir(Varien_Db_Select::SQL_ASC)
56: ->setTitle($this->__('REST Roles Information'))
57: ->setUseAjax(true);
58: }
59:
60: /**
61: * Prepare grid collection object
62: *
63: * @return Mage_Api2_Block_Adminhtml_Permissions_User_Edit_Tab_Roles
64: */
65: protected function _prepareCollection()
66: {
67: /** @var $collection Mage_Api2_Model_Resource_Acl_Global_Role_Collection */
68: $collection = Mage::getResourceModel('api2/acl_global_role_collection');
69: $collection->addFieldToFilter('entity_id', array('nin' => Mage_Api2_Model_Acl_Global_Role::getSystemRoles()));
70:
71: $this->setCollection($collection);
72:
73: return parent::_prepareCollection();
74: }
75:
76: /**
77: * Prepare grid columns
78: *
79: * @return Mage_Api2_Block_Adminhtml_Permissions_User_Edit_Tab_Roles
80: */
81: protected function _prepareColumns()
82: {
83: $this->addColumn('assigned_user_role', array(
84: 'header_css_class' => 'a-center',
85: 'header' => $this->__('Assigned'),
86: 'type' => 'radio',
87: 'html_name' => 'api2_roles[]',
88: 'values' => $this->_getSelectedRoles(),
89: 'align' => 'center',
90: 'index' => 'entity_id'
91: ));
92:
93: $this->addColumn('role_name', array(
94: 'header' => $this->__('Role Name'),
95: 'index' => 'role_name'
96: ));
97:
98: return parent::_prepareColumns();
99: }
100:
101: /**
102: * Add custom column filter to collection
103: *
104: * @param Mage_Adminhtml_Block_Widget_Grid_Column $column
105: * @return Mage_Api2_Block_Adminhtml_Permissions_User_Edit_Tab_Roles
106: */
107: protected function _addColumnFilterToCollection($column)
108: {
109: if ($column->getId() == 'assigned_user_role') {
110: $userRoles = $this->_getSelectedRoles();
111: if ($column->getFilter()->getValue()) {
112: $this->getCollection()->addFieldToFilter('entity_id', array('in' => $userRoles));
113: } elseif (!empty($userRoles)) {
114: $this->getCollection()->addFieldToFilter('entity_id', array('nin' => $userRoles));
115: } else {
116: $this->getCollection();
117: }
118: } else {
119: parent::_addColumnFilterToCollection($column);
120: }
121:
122: return $this;
123: }
124:
125: /**
126: * Get selected API2 roles for grid
127: *
128: * @return array
129: */
130: protected function _getSelectedRoles()
131: {
132: if (null === $this->_selectedRoles) {
133: $userRoles = array();
134:
135: /* @var $user Mage_Admin_Model_User */
136: $user = Mage::registry('permissions_user');
137: if ($user->getId()) {
138: /** @var $collection Mage_Api2_Model_Resource_Acl_Global_Role_Collection */
139: $collection = Mage::getResourceModel('api2/acl_global_role_collection');
140: $collection->addFilterByAdminId($user->getId());
141:
142: $userRoles = $collection->getAllIds();
143: }
144:
145: $this->_selectedRoles = $userRoles;
146: }
147:
148: return $this->_selectedRoles;
149: }
150:
151: /**
152: * Prepare label for tab
153: *
154: * @return string
155: */
156: public function getTabLabel()
157: {
158: return $this->__('REST Role');
159: }
160:
161: /**
162: * Prepare title for tab
163: *
164: * @return string
165: */
166: public function getTabTitle()
167: {
168: return $this->__('REST Role');
169: }
170:
171: /**
172: * Returns status flag about this tab can be shown or not
173: *
174: * @return true
175: */
176: public function canShowTab()
177: {
178: return true;
179: }
180:
181: /**
182: * Returns status flag about this tab hidden or not
183: *
184: * @return true
185: */
186: public function isHidden()
187: {
188: return false;
189: }
190:
191: /**
192: * Get controller action url for grid ajax actions
193: *
194: * @return string
195: */
196: public function getGridUrl()
197: {
198: return $this->getUrl(
199: '*/api2_role/rolesGrid',
200: array('user_id' => Mage::registry('permissions_user')->getUserId())
201: );
202: }
203: }
204: