Skip to content

Commit d0d3b1f

Browse files
committed
Refactor assigning role to admin user
1 parent a264cde commit d0d3b1f

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
@@ -211,56 +211,54 @@ public function delete(Mage_Core_Model_Abstract $user)
211211
}
212212

213213
/**
214-
* TODO: unify _saveRelations() and add() methods, they make same things
214+
* Save admin user role
215215
*
216216
* @param Mage_Core_Model_Abstract|Mage_Admin_Model_User $user
217-
* @return $this|Mage_Core_Model_Abstract
217+
* @return $this
218218
*/
219219
public function _saveRelations(Mage_Core_Model_Abstract $user)
220220
{
221-
$rolesIds = $user->getRoleIds();
222-
if (!is_array($rolesIds) || count($rolesIds) == 0) {
223-
return $user;
221+
$roleId = $user->getRoleId();
222+
if (!$roleId) {
223+
return $this;
224224
}
225225

226226
$adapter = $this->_getWriteAdapter();
227227
$adapter->beginTransaction();
228228

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

256257
if ($user->getId() > 0) {
257258
// reload acl on next user http request
258259
$this->saveReloadAclFlag($user, 1);
259260
}
260261
$adapter->commit();
261-
} catch (Mage_Core_Exception $e) {
262-
$adapter->rollBack();
263-
throw $e;
264262
} catch (Exception $e) {
265263
$adapter->rollBack();
266264
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)