Skip to content

Commit e6e5b9b

Browse files
committed
Refactor assigning role to admin user
1 parent a5ae514 commit e6e5b9b

File tree

4 files changed

+93
-99
lines changed

4 files changed

+93
-99
lines changed

app/code/core/Mage/Admin/Model/Resource/User.php

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -213,56 +213,54 @@ public function delete(Mage_Core_Model_Abstract $user)
213213
}
214214

215215
/**
216-
* TODO: unify _saveRelations() and add() methods, they make same things
216+
* Save admin user role
217217
*
218218
* @param Mage_Core_Model_Abstract|Mage_Admin_Model_User $user
219-
* @return $this|Mage_Core_Model_Abstract
219+
* @return $this
220220
*/
221221
public function _saveRelations(Mage_Core_Model_Abstract $user)
222222
{
223-
$rolesIds = $user->getRoleIds();
224-
if (!is_array($rolesIds) || count($rolesIds) == 0) {
225-
return $user;
223+
$roleId = $user->getRoleId();
224+
if (!$roleId) {
225+
return $this;
226226
}
227227

228228
$adapter = $this->_getWriteAdapter();
229229
$adapter->beginTransaction();
230230

231231
try {
232-
$conditions = [
233-
'user_id = ?' => (int) $user->getId(),
234-
];
235-
236-
$adapter->delete($this->getTable('admin/role'), $conditions);
237-
foreach ($rolesIds as $rid) {
238-
$rid = (int) $rid;
239-
if ($rid > 0) {
240-
$role = Mage::getModel('admin/role')->load($rid);
241-
} else {
242-
$role = new Varien_Object(['tree_level' => 0]);
243-
}
244-
245-
$data = new Varien_Object([
246-
'parent_id' => $rid,
247-
'tree_level' => $role->getTreeLevel() + 1,
248-
'sort_order' => 0,
249-
'role_type' => Mage_Admin_Model_Acl::ROLE_TYPE_USER,
250-
'user_id' => $user->getId(),
251-
'role_name' => $user->getFirstname()
252-
]);
253-
254-
$insertData = $this->_prepareDataForTable($data, $this->getTable('admin/role'));
255-
$adapter->insert($this->getTable('admin/role'), $insertData);
232+
$role = Mage::getModel('admin/role')->load($roleId);
233+
234+
$data = new Varien_Object([
235+
'parent_id' => $roleId,
236+
'tree_level' => (int)$role->getTreeLevel() + 1,
237+
'sort_order' => 0,
238+
'role_type' => Mage_Admin_Model_Acl::ROLE_TYPE_USER,
239+
'user_id' => $user->getId(),
240+
'role_name' => $user->getFirstname()
241+
]);
242+
243+
$select = $adapter->select()
244+
->from($this->getTable('admin/role'))
245+
->where('user_id = ?', $user->getId());
246+
247+
$preparedData = $this->_prepareDataForTable($data, $this->getTable('admin/role'));
248+
249+
if ($adapter->fetchOne($select) === false) {
250+
$adapter->insert($this->getTable('admin/role'), $preparedData);
251+
} else {
252+
$adapter->update(
253+
$this->getTable('admin/role'),
254+
$preparedData,
255+
['user_id = ?' => $user->getId()]
256+
);
256257
}
257258

258259
if ($user->getId() > 0) {
259260
// reload acl on next user http request
260261
$this->saveReloadAclFlag($user, 1);
261262
}
262263
$adapter->commit();
263-
} catch (Mage_Core_Exception $e) {
264-
$adapter->rollBack();
265-
throw $e;
266264
} catch (Exception $e) {
267265
$adapter->rollBack();
268266
throw $e;

app/code/core/Mage/Adminhtml/Block/Permissions/User/Edit/Tab/Roles.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,12 @@ protected function _prepareColumns()
7171
'header_css_class' => 'a-center',
7272
'header' => Mage::helper('adminhtml')->__('Assigned'),
7373
'type' => 'radio',
74-
'html_name' => 'roles[]',
74+
'html_name' => 'role',
7575
'values' => $this->_getSelectedRoles(),
7676
'align' => 'center',
7777
'index' => 'role_id'
7878
]);
7979

80-
/*$this->addColumn('role_id', array(
81-
'header' =>Mage::helper('adminhtml')->__('Role ID'),
82-
'index' =>'role_id',
83-
'align' => 'right',
84-
'width' => '50px'
85-
));*/
86-
8780
$this->addColumn('role_name', [
8881
'header' => Mage::helper('adminhtml')->__('Role Name'),
8982
'index' => 'role_name'

app/code/core/Mage/Adminhtml/controllers/Permissions/UserController.php

Lines changed: 61 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -114,73 +114,76 @@ public function editAction()
114114

115115
public function saveAction()
116116
{
117-
if ($data = $this->getRequest()->getPost()) {
118-
$id = $this->getRequest()->getParam('user_id');
119-
$model = Mage::getModel('admin/user')->load($id);
120-
// @var $isNew flag for detecting new admin user creation.
121-
$isNew = !$model->getId() ? true : false;
122-
if (!$model->getId() && $id) {
123-
Mage::getSingleton('adminhtml/session')->addError($this->__('This user no longer exists.'));
124-
$this->_redirect('*/*/');
125-
return;
126-
}
117+
$data = $this->getRequest()->getPost();
127118

128-
//Validate current admin password
129-
$currentPassword = $this->getRequest()->getParam('current_password', null);
130-
$this->getRequest()->setParam('current_password', null);
131-
unset($data['current_password']);
132-
$result = $this->_validateCurrentPassword($currentPassword);
119+
if (!$data) {
120+
$this->_redirect('*/*/');
121+
return;
122+
}
133123

134-
$model->setData($data);
124+
$id = $this->getRequest()->getParam('user_id');
125+
$role = $this->getRequest()->getParam('role');
135126

136-
/*
137-
* Unsetting new password and password confirmation if they are blank
138-
*/
139-
if ($model->hasNewPassword() && $model->getNewPassword() === '') {
140-
$model->unsNewPassword();
141-
}
142-
if ($model->hasPasswordConfirmation() && $model->getPasswordConfirmation() === '') {
143-
$model->unsPasswordConfirmation();
144-
}
127+
$user = Mage::getModel('admin/user')->load($id);
128+
$isNew = $user->isObjectNew();
129+
130+
if ($id && !$user->getId()) {
131+
$this->_getSession()->addError($this->__('This user no longer exists.'));
132+
$this->_redirect('*/*/');
133+
return;
134+
}
135+
136+
$currentPassword = $this->getRequest()->getParam('current_password');
137+
$this->getRequest()->setParam('current_password', null);
138+
unset($data['current_password']);
139+
$result = $this->_validateCurrentPassword($currentPassword);
140+
141+
$user->setData($data);
145142

146-
if (!is_array($result)) {
147-
$result = $model->validate();
143+
/*
144+
* Unsetting new password and password confirmation if they are blank
145+
*/
146+
if ($user->hasNewPassword() && $user->getNewPassword() === '') {
147+
$user->unsNewPassword();
148+
}
149+
if ($user->hasPasswordConfirmation() && $user->getPasswordConfirmation() === '') {
150+
$user->unsPasswordConfirmation();
151+
}
152+
153+
if (!is_array($result)) {
154+
$result = $user->validate();
155+
}
156+
157+
if (is_array($result)) {
158+
$this->_getSession()->setUserData($data);
159+
foreach ($result as $message) {
160+
$this->_getSession()->addError($message);
148161
}
149-
if (is_array($result)) {
150-
Mage::getSingleton('adminhtml/session')->setUserData($data);
151-
foreach ($result as $message) {
152-
Mage::getSingleton('adminhtml/session')->addError($message);
153-
}
154-
$this->_redirect('*/*/edit', ['_current' => true]);
155-
return $this;
162+
$this->_redirect('*/*/edit', ['_current' => true]);
163+
return;
164+
}
165+
166+
try {
167+
$user->save();
168+
169+
// Send notification to General and additional contacts (if declared) that a new admin user was created.
170+
if (Mage::getStoreConfigFlag('admin/security/crate_admin_user_notification') && $isNew) {
171+
Mage::getModel('admin/user')->sendAdminNotification($user);
156172
}
157173

158-
try {
159-
$model->save();
160-
// Send notification to General and additional contacts (if declared) that a new admin user was created.
161-
if (Mage::getStoreConfigFlag('admin/security/crate_admin_user_notification') && $isNew) {
162-
Mage::getModel('admin/user')->sendAdminNotification($model);
163-
}
164-
if ($uRoles = $this->getRequest()->getParam('roles', false)) {
165-
if (is_array($uRoles) && (count($uRoles) >= 1)) {
166-
// with fix for previous multi-roles logic
167-
$model->setRoleIds(array_slice($uRoles, 0, 1))
168-
->setRoleUserId($model->getUserId())
169-
->saveRelations();
170-
}
171-
}
172-
Mage::getSingleton('adminhtml/session')->addSuccess($this->__('The user has been saved.'));
173-
Mage::getSingleton('adminhtml/session')->setUserData(false);
174-
$this->_redirect('*/*/');
175-
return;
176-
} catch (Mage_Core_Exception $e) {
177-
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
178-
Mage::getSingleton('adminhtml/session')->setUserData($data);
179-
$this->_redirect('*/*/edit', ['user_id' => $model->getUserId()]);
180-
return;
174+
if ($role) {
175+
$user->setRoleId((int)$role)
176+
->setRoleUserId($user->getUserId())
177+
->saveRelations();
181178
}
179+
$this->_getSession()->addSuccess($this->__('The user has been saved.'));
180+
$this->_getSession()->setUserData(false);
181+
$this->_redirect('*/*/');
182+
} catch (Mage_Core_Exception $e) {
183+
$this->_getSession()->addError($e->getMessage());
184+
$this->_getSession()->setUserData($data);
185+
$this->_redirect('*/*/edit', ['user_id' => $user->getUserId()]);
182186
}
183-
$this->_redirect('*/*/');
184187
}
185188

186189
public function deleteAction()

app/code/core/Mage/Install/Model/Installer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ public function createAdministrator($data)
237237
//run time flag to force saving entered password
238238
$data->setForceNewPassword(true);
239239
$data->save();
240-
$data->setRoleIds([1])->saveRelations();
240+
$data->setRoleId(1)->saveRelations();
241241

242242
return true;
243243
}

0 commit comments

Comments
 (0)