Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove deprecated Admin user multirole functionality #2876

Draft
wants to merge 7 commits into
base: next
Choose a base branch
from
Draft
98 changes: 32 additions & 66 deletions app/code/core/Mage/Admin/Model/Resource/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,56 +204,54 @@ public function delete(Mage_Core_Model_Abstract $user)
}

/**
* TODO: unify _saveRelations() and add() methods, they make same things
* Save admin user role
*
* @param Mage_Core_Model_Abstract|Mage_Admin_Model_User $user
* @return $this|Mage_Core_Model_Abstract
* @return $this
*/
public function _saveRelations(Mage_Core_Model_Abstract $user)
{
$rolesIds = $user->getRoleIds();
if (!is_array($rolesIds) || count($rolesIds) == 0) {
return $user;
$roleId = $user->getRoleId();
if (!$roleId) {
return $this;
}

$adapter = $this->_getWriteAdapter();
$adapter->beginTransaction();

try {
$conditions = [
'user_id = ?' => (int) $user->getId(),
];

$adapter->delete($this->getTable('admin/role'), $conditions);
foreach ($rolesIds as $rid) {
$rid = (int) $rid;
if ($rid > 0) {
$role = Mage::getModel('admin/role')->load($rid);
} else {
$role = new Varien_Object(['tree_level' => 0]);
}

$data = new Varien_Object([
'parent_id' => $rid,
'tree_level' => $role->getTreeLevel() + 1,
'sort_order' => 0,
'role_type' => Mage_Admin_Model_Acl::ROLE_TYPE_USER,
'user_id' => $user->getId(),
'role_name' => $user->getFirstname()
]);

$insertData = $this->_prepareDataForTable($data, $this->getTable('admin/role'));
$adapter->insert($this->getTable('admin/role'), $insertData);
$role = Mage::getModel('admin/role')->load($roleId);

$data = new Varien_Object([
'parent_id' => $roleId,
'tree_level' => (int)$role->getTreeLevel() + 1,
'sort_order' => 0,
'role_type' => Mage_Admin_Model_Acl::ROLE_TYPE_USER,
'user_id' => $user->getId(),
'role_name' => $user->getFirstname()
]);

$select = $adapter->select()
->from($this->getTable('admin/role'))
->where('user_id = ?', $user->getId());

$preparedData = $this->_prepareDataForTable($data, $this->getTable('admin/role'));

if ($adapter->fetchOne($select) === false) {
$adapter->insert($this->getTable('admin/role'), $preparedData);
} else {
$adapter->update(
$this->getTable('admin/role'),
$preparedData,
['user_id = ?' => $user->getId()]
);
}

if ($user->getId() > 0) {
// reload acl on next user http request
$this->saveReloadAclFlag($user, 1);
}
$adapter->commit();
} catch (Mage_Core_Exception $e) {
$adapter->rollBack();
throw $e;
} catch (Exception $e) {
$adapter->rollBack();
throw $e;
Expand Down Expand Up @@ -301,45 +299,13 @@ public function getRoles(Mage_Core_Model_Abstract $user)
/**
* Save user roles
*
* @deprecated Use {@see _saveRelations} instead.
* @param Mage_Core_Model_Abstract|Mage_Admin_Model_User $user
* @return $this
*/
public function add(Mage_Core_Model_Abstract $user)
{
$dbh = $this->_getWriteAdapter();
$aRoles = $this->hasAssigned2Role($user);
if (count($aRoles)) {
foreach ($aRoles as $idx => $data) {
$dbh->delete(
$this->getTable('admin/role'),
['role_id = ?' => $data['role_id']]
);
}
}

if ($user->getId() > 0) {
$role = Mage::getModel('admin/role')->load($user->getRoleId());
} else {
$role = new Varien_Object(['tree_level' => 0]);
}

$data = new Varien_Object([
'parent_id' => $user->getRoleId(),
'tree_level' => $role->getTreeLevel() + 1,
'sort_order' => 0,
'role_type' => Mage_Admin_Model_Acl::ROLE_TYPE_USER,
'user_id' => $user->getUserId(),
'role_name' => $user->getFirstname()
]);

$insertData = $this->_prepareDataForTable($data, $this->getTable('admin/role'));
$dbh->insert($this->getTable('admin/role'), $insertData);

if ($user->getId() > 0) {
// reload acl on next user http request
$this->saveReloadAclFlag($user, 1);
}

$this->_saveRelations($user);
return $this;
}

Expand Down
3 changes: 2 additions & 1 deletion app/code/core/Mage/Admin/Model/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,12 @@ public function roleUserExists()
/**
* Assign user to role
*
* @deprecated Use {@see saveRelations} instead.
* @return $this
*/
public function add()
{
$this->_getResource()->add($this);
$this->saveRelations();
return $this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,12 @@ protected function _prepareColumns()
'header_css_class' => 'a-center',
'header' => Mage::helper('adminhtml')->__('Assigned'),
'type' => 'radio',
'html_name' => 'roles[]',
'html_name' => 'role',
'values' => $this->_getSelectedRoles(),
'align' => 'center',
'index' => 'role_id'
]);

/*$this->addColumn('role_id', array(
'header' =>Mage::helper('adminhtml')->__('Role ID'),
'index' =>'role_id',
'align' => 'right',
'width' => '50px'
));*/

$this->addColumn('role_name', [
'header' => Mage::helper('adminhtml')->__('Role Name'),
'index' => 'role_name'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ protected function _addUserToRole($userId, $roleId)
if ($user->roleUserExists() === true) {
return false;
} else {
$user->add();
$user->saveRelations();
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,73 +107,76 @@ public function editAction()

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

//Validate current admin password
$currentPassword = $this->getRequest()->getParam('current_password', null);
$this->getRequest()->setParam('current_password', null);
unset($data['current_password']);
$result = $this->_validateCurrentPassword($currentPassword);
if (!$data) {
$this->_redirect('*/*/');
return;
}

$model->setData($data);
$id = $this->getRequest()->getParam('user_id');
$role = $this->getRequest()->getParam('role');

/*
* Unsetting new password and password confirmation if they are blank
*/
if ($model->hasNewPassword() && $model->getNewPassword() === '') {
$model->unsNewPassword();
}
if ($model->hasPasswordConfirmation() && $model->getPasswordConfirmation() === '') {
$model->unsPasswordConfirmation();
}
$user = Mage::getModel('admin/user')->load($id);
$isNew = $user->isObjectNew();

if ($id && !$user->getId()) {
$this->_getSession()->addError($this->__('This user no longer exists.'));
$this->_redirect('*/*/');
return;
}

$currentPassword = $this->getRequest()->getParam('current_password');
$this->getRequest()->setParam('current_password', null);
unset($data['current_password']);
$result = $this->_validateCurrentPassword($currentPassword);

$user->setData($data);

if (!is_array($result)) {
$result = $model->validate();
/*
* Unsetting new password and password confirmation if they are blank
*/
if ($user->hasNewPassword() && $user->getNewPassword() === '') {
$user->unsNewPassword();
}
if ($user->hasPasswordConfirmation() && $user->getPasswordConfirmation() === '') {
$user->unsPasswordConfirmation();
}

if (!is_array($result)) {
$result = $user->validate();
}

if (is_array($result)) {
$this->_getSession()->setUserData($data);
foreach ($result as $message) {
$this->_getSession()->addError($message);
}
if (is_array($result)) {
Mage::getSingleton('adminhtml/session')->setUserData($data);
foreach ($result as $message) {
Mage::getSingleton('adminhtml/session')->addError($message);
}
$this->_redirect('*/*/edit', ['_current' => true]);
return $this;
$this->_redirect('*/*/edit', ['_current' => true]);
return;
}

try {
$user->save();

// Send notification to General and additional contacts (if declared) that a new admin user was created.
if (Mage::getStoreConfigFlag('admin/security/crate_admin_user_notification') && $isNew) {
Mage::getModel('admin/user')->sendAdminNotification($user);
}

try {
$model->save();
// Send notification to General and additional contacts (if declared) that a new admin user was created.
if (Mage::getStoreConfigFlag('admin/security/crate_admin_user_notification') && $isNew) {
Mage::getModel('admin/user')->sendAdminNotification($model);
}
if ($uRoles = $this->getRequest()->getParam('roles', false)) {
if (is_array($uRoles) && (count($uRoles) >= 1)) {
// with fix for previous multi-roles logic
$model->setRoleIds(array_slice($uRoles, 0, 1))
->setRoleUserId($model->getUserId())
->saveRelations();
}
}
Mage::getSingleton('adminhtml/session')->addSuccess($this->__('The user has been saved.'));
Mage::getSingleton('adminhtml/session')->setUserData(false);
$this->_redirect('*/*/');
return;
} catch (Mage_Core_Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
Mage::getSingleton('adminhtml/session')->setUserData($data);
$this->_redirect('*/*/edit', ['user_id' => $model->getUserId()]);
return;
if ($role) {
$user->setRoleId((int)$role)
->setRoleUserId($user->getUserId())
->saveRelations();
}
$this->_getSession()->addSuccess($this->__('The user has been saved.'));
$this->_getSession()->setUserData(false);
$this->_redirect('*/*/');
} catch (Mage_Core_Exception $e) {
$this->_getSession()->addError($e->getMessage());
$this->_getSession()->setUserData($data);
$this->_redirect('*/*/edit', ['user_id' => $user->getUserId()]);
}
$this->_redirect('*/*/');
}

public function deleteAction()
Expand Down
2 changes: 1 addition & 1 deletion app/code/core/Mage/Install/Model/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public function createAdministrator($data)
//run time flag to force saving entered password
$data->setForceNewPassword(true);
$data->save();
$data->setRoleIds([1])->saveRelations();
$data->setRoleId(1)->saveRelations();

return true;
}
Expand Down
Loading