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

Added methods to access different sessions #4362

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
  •  
  •  
  •  
24 changes: 6 additions & 18 deletions .phpstan.dist.baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2862,6 +2862,12 @@ parameters:
count: 1
path: app/code/core/Mage/Checkout/Block/Cart/Shipping.php

-
message: '#^Call to function is_array\(\) with non\-empty\-array will always evaluate to true\.$#'
identifier: function.alreadyNarrowedType
count: 1
path: app/code/core/Mage/Checkout/Block/Multishipping/Success.php

-
message: '#^Method Mage_Checkout_Block_Onepage_Billing\:\:_getTaxvat\(\) should return Mage_Customer_Block_Widget_Taxvat but returns Mage_Core_Block_Abstract\|false\.$#'
identifier: return.type
Expand Down Expand Up @@ -4800,18 +4806,6 @@ parameters:
count: 1
path: app/code/core/Mage/Install/Model/Config.php

-
message: '#^Method Mage_Install_Model_Installer\:\:getDataModel\(\) should return Mage_Install_Model_Session but returns Mage_Install_Model_Installer_Data\|null\.$#'
identifier: return.type
count: 1
path: app/code/core/Mage/Install/Model/Installer.php

-
message: '#^Parameter \#1 \$model of method Mage_Install_Model_Installer\:\:setDataModel\(\) expects Mage_Install_Model_Installer_Data, Mage_Install_Model_Session given\.$#'
identifier: argument.type
count: 1
path: app/code/core/Mage/Install/Model/Installer.php

-
message: '#^Parameter \#2 \$value of method Mage_Core_Model_Resource_Setup\:\:setConfigData\(\) expects string, int given\.$#'
identifier: argument.type
Expand Down Expand Up @@ -5250,12 +5244,6 @@ parameters:
count: 1
path: app/code/core/Mage/ProductAlert/controllers/AddController.php

-
message: '#^PHPDoc tag @var with type Mage_Catalog_Model_Product is not subtype of native type 0\|0\.0\|''''\|''0''\|array\{\}\|false\|null\.$#'
identifier: varTag.nativeType
count: 1
path: app/code/core/Mage/ProductAlert/controllers/AddController.php

-
message: '#^Method Mage_Rating_Model_Resource_Rating\:\:getEntityIdByCode\(\) should return int but returns string\|false\|null\.$#'
identifier: return.type
Expand Down
20 changes: 8 additions & 12 deletions app/code/core/Mage/Admin/Model/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* @category Mage
* @package Mage_Admin
*/
class Mage_Admin_Model_Observer
class Mage_Admin_Model_Observer extends Mage_Core_Model_Observer
{
public const FLAG_NO_LOGIN = 'no-login';

Expand All @@ -31,8 +31,7 @@ class Mage_Admin_Model_Observer
*/
public function actionPreDispatchAdmin($observer)
{
/** @var Mage_Admin_Model_Session $session */
$session = Mage::getSingleton('admin/session');
$session = $this->getAdminSession();

$request = Mage::app()->getRequest();
$user = $session->getUser();
Expand All @@ -53,22 +52,19 @@ public function actionPreDispatchAdmin($observer)
}
if (!$user || !$user->getId()) {
if ($request->getPost('login')) {
/** @var Mage_Core_Model_Session $coreSession */
$coreSession = Mage::getSingleton('core/session');
$coreSession = $this->getCoreSession();

if ($coreSession->validateFormKey($request->getPost('form_key'))) {
$postLogin = $request->getPost('login');
$username = $postLogin['username'] ?? '';
$password = $postLogin['password'] ?? '';
$session->login($username, $password, $request);
$request->setPost('login', null);
} else {
if (!$request->getParam('messageSent')) {
Mage::getSingleton('adminhtml/session')->addError(
Mage::helper('adminhtml')->__('Invalid Form Key. Please refresh the page.'),
);
$request->setParam('messageSent', true);
}
} elseif (!$request->getParam('messageSent')) {
$this->getAdminhtmlSession()->addError(
Mage::helper('adminhtml')->__('Invalid Form Key. Please refresh the page.'),
);
$request->setParam('messageSent', true);
}

$coreSession->renewFormKey();
Expand Down
4 changes: 2 additions & 2 deletions app/code/core/Mage/Admin/Model/Resource/Acl.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ public function loadRules(Mage_Admin_Model_Acl $acl, array $rulesArr)
}
}

if ($orphanedResources !== [] && $acl->isAllowed(Mage::getSingleton('admin/session')->getUser()->getAclRole(), 'admin/system/acl/orphaned_resources')) {
Mage::getSingleton('adminhtml/session')->addNotice(
if ($orphanedResources !== [] && $acl->isAllowed($this->getAdminSession()->getUser()->getAclRole(), 'admin/system/acl/orphaned_resources')) {
$this->getAdminhtmlSession()->addNotice(
Mage::helper('adminhtml')->__(
'The following role resources are no longer available in the system: %s. You can delete them by <a href="%s">clicking here</a>.',
implode(', ', $orphanedResources),
Expand Down
8 changes: 4 additions & 4 deletions app/code/core/Mage/Admin/Model/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public function login($username, $password, $request = null)
$this->setUser($user);
$this->setAcl(Mage::getResourceModel('admin/acl')->loadAcl());
if ($backendLocale = $user->getBackendLocale()) {
Mage::getSingleton('adminhtml/session')->setLocale($backendLocale);
$this->getAdminhtmlSession()->setLocale($backendLocale);
}

$alternativeUrl = $this->_getRequestUri($request);
Expand Down Expand Up @@ -218,8 +218,8 @@ public function refreshAcl($user = null)
/**
* Check current user permission on resource and privilege
*
* Mage::getSingleton('admin/session')->isAllowed('admin/catalog')
* Mage::getSingleton('admin/session')->isAllowed('catalog')
* $this->getAdminSession()->isAllowed('admin/catalog')
* $this->getAdminSession()->isAllowed('catalog')
*
* @param string $resource
* @param string $privilege
Expand Down Expand Up @@ -320,7 +320,7 @@ protected function _loginFailed($e, $request, $username, $message)
}

if ($request && !$request->getParam('messageSent')) {
Mage::getSingleton('adminhtml/session')->addError($message);
$this->getAdminhtmlSession()->addError($message);
$request->setParam('messageSent', true);
}
}
Expand Down
20 changes: 11 additions & 9 deletions app/code/core/Mage/Admin/Model/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,9 @@ protected function _beforeSave()
// Change user password
$data['password'] = $this->_getEncodedPassword($this->getNewPassword());
$data['new_password'] = $data['password'];
$sessionUser = $this->getSession()->getUser();
$sessionUser = $this->getAdminSession()->getUser();
if ($sessionUser && $sessionUser->getId() == $this->getId()) {
$this->getSession()->setUserPasswordChanged(true);
$this->getAdminSession()->setUserPasswordChanged(true);
}
} elseif ($this->getPassword() && $this->getPassword() != $this->getOrigData('password')) {
// New user password
Expand All @@ -183,10 +183,12 @@ protected function _beforeSave()

/**
* @return Mage_Admin_Model_Session
*/
* @deprecated
* @see getAdminSession()
*/
protected function getSession()
{
return Mage::getSingleton('admin/session');
return $this->getAdminSession();
}

/**
Expand Down Expand Up @@ -435,7 +437,7 @@ public function login($username, $password)

if ($this->authenticate($username, $password)) {
$this->getResource()->recordLogin($this);
Mage::getSingleton('core/session')->renewFormKey();
$this->getCoreSession()->renewFormKey();
}
return $this;
}
Expand All @@ -451,11 +453,11 @@ public function reload()
$oldPassword = $this->getPassword();
$this->setId(null);
$this->load($id);
$isUserPasswordChanged = $this->getSession()->getUserPasswordChanged();
$isUserPasswordChanged = $this->getAdminSession()->getUserPasswordChanged();
if (!$isUserPasswordChanged && $this->getPassword() !== $oldPassword) {
$this->setId(null);
} elseif ($isUserPasswordChanged) {
$this->getSession()->setUserPasswordChanged(false);
$this->getAdminSession()->setUserPasswordChanged(false);
}
return $this;
}
Expand Down Expand Up @@ -520,7 +522,7 @@ public function findFirstAvailableMenu($parent = null, $path = '', $level = 0)
}
foreach ($parent->children() as $childName => $child) {
$aclResource = 'admin/' . $path . $childName;
if (Mage::getSingleton('admin/session')->isAllowed($aclResource)) {
if ($this->getAdminSession()->isAllowed($aclResource)) {
if (!$child->children) {
return (string) $child->action;
} elseif ($child->children) {
Expand Down Expand Up @@ -564,7 +566,7 @@ public function getStartupPageUrl()
{
$startupPage = Mage::getStoreConfig(self::XML_PATH_STARTUP_PAGE);
$aclResource = 'admin/' . $startupPage;
if (Mage::getSingleton('admin/session')->isAllowed($aclResource)) {
if ($this->getAdminSession()->isAllowed($aclResource)) {
$nodePath = 'menu/' . implode('/children/', explode('/', $startupPage)) . '/action';
$url = (string) Mage::getSingleton('admin/config')->getAdminhtmlConfig()->getNode($nodePath);
if ($url) {
Expand Down
7 changes: 3 additions & 4 deletions app/code/core/Mage/AdminNotification/Model/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,16 @@
* @category Mage
* @package Mage_AdminNotification
*/
class Mage_AdminNotification_Model_Observer
class Mage_AdminNotification_Model_Observer extends Mage_Core_Model_Observer
{
/**
* Predispath admin action controller
*/
public function preDispatch(Varien_Event_Observer $observer)
{
if (Mage::getSingleton('admin/session')->isLoggedIn()) {
$feedModel = Mage::getModel('adminnotification/feed');
if ($this->getAdminSession()->isLoggedIn()) {
/** @var Mage_AdminNotification_Model_Feed $feedModel */

$feedModel = Mage::getModel('adminnotification/feed');
$feedModel->checkUpdate();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ protected function _prepareForm()
]);
}

if (Mage::getSingleton('admin/session')->getUser()->getId() != $model->getUserId()) {
if ($this->getAdminSession()->getUser()->getId() != $model->getUserId()) {
$fieldset->addField('is_active', 'select', [
'name' => 'is_active',
'label' => Mage::helper('adminhtml')->__('This account is'),
Expand Down
2 changes: 1 addition & 1 deletion app/code/core/Mage/Adminhtml/Block/Cache/Notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function getManageUrl()
*/
protected function _toHtml()
{
if (Mage::getSingleton('admin/session')->isAllowed('system/cache')) {
if ($this->getAdminSession()->isAllowed('system/cache')) {
return parent::_toHtml();
}
return '';
Expand Down
4 changes: 2 additions & 2 deletions app/code/core/Mage/Adminhtml/Block/Catalog/Category/Tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function getStoreSwitcherHtml()
public function getLoadTreeUrl($expanded = null)
{
$params = ['_current' => true, 'id' => null,'store' => null];
if ((is_null($expanded) && Mage::getSingleton('admin/session')->getIsTreeWasExpanded())
if ((is_null($expanded) && $this->getAdminSession()->getIsTreeWasExpanded())
|| $expanded == true
) {
$params['expand_all'] = true;
Expand All @@ -149,7 +149,7 @@ public function getSwitchTreeUrl()

public function getIsWasExpanded()
{
return Mage::getSingleton('admin/session')->getIsTreeWasExpanded();
return $this->getAdminSession()->getIsTreeWasExpanded();
}

public function getMoveUrl()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ protected function _prepareForm()

// Add new attribute button if it is not an image tab
if (!$form->getElement('media_gallery')
&& Mage::getSingleton('admin/session')->isAllowed('catalog/attributes/attributes')
&& $this->getAdminSession()->isAllowed('catalog/attributes/attributes')
) {
$headerBar = $this->getLayout()->createBlock('adminhtml/catalog_product_edit_tab_attributes_create');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ protected function _prepareLayout()

if ($this->getRequest()->getParam('id', false)) {
if ($this->isModuleEnabled('Mage_Review', 'catalog')) {
if (Mage::getSingleton('admin/session')->isAllowed('admin/catalog/reviews_ratings')) {
if ($this->getAdminSession()->isAllowed('admin/catalog/reviews_ratings')) {
$this->addTab('reviews', [
'label' => Mage::helper('catalog')->__('Product Reviews'),
'url' => $this->getUrl('*/*/reviews', ['_current' => true]),
Expand All @@ -141,7 +141,7 @@ protected function _prepareLayout()
}
}
if ($this->isModuleEnabled('Mage_Tag', 'catalog')) {
if (Mage::getSingleton('admin/session')->isAllowed('admin/catalog/tag')) {
if ($this->getAdminSession()->isAllowed('admin/catalog/tag')) {
$this->addTab('tags', [
'label' => Mage::helper('catalog')->__('Product Tags'),
'url' => $this->getUrl('*/*/tagGrid', ['_current' => true]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ protected function _prepareMassaction()
],
]);

if (Mage::getSingleton('admin/session')->isAllowed('catalog/update_attributes')) {
if ($this->getAdminSession()->isAllowed('catalog/update_attributes')) {
$this->getMassactionBlock()->addItem(MassAction::ATTRIBUTES, [
'label' => Mage::helper('catalog')->__('Update Attributes'),
'url' => $this->getUrl('*/catalog_product_action_attribute/edit', ['_current' => true]),
Expand Down
2 changes: 1 addition & 1 deletion app/code/core/Mage/Adminhtml/Block/Cms/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ public function __construct()
*/
protected function _isAllowedAction($action)
{
return Mage::getSingleton('admin/session')->isAllowed('cms/page/' . $action);
return $this->getAdminSession()->isAllowed('cms/page/' . $action);
}
}
2 changes: 1 addition & 1 deletion app/code/core/Mage/Adminhtml/Block/Cms/Page/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function getHeaderText()
*/
protected function _isAllowedAction($action)
{
return Mage::getSingleton('admin/session')->isAllowed('cms/page/' . $action);
return $this->getAdminSession()->isAllowed('cms/page/' . $action);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,6 @@ public function isHidden()
*/
protected function _isAllowedAction($action)
{
return Mage::getSingleton('admin/session')->isAllowed('cms/page/' . $action);
return $this->getAdminSession()->isAllowed('cms/page/' . $action);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,6 @@ public function isHidden()
*/
protected function _isAllowedAction($action)
{
return Mage::getSingleton('admin/session')->isAllowed('cms/page/' . $action);
return $this->getAdminSession()->isAllowed('cms/page/' . $action);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,6 @@ public function isHidden()
*/
protected function _isAllowedAction($action)
{
return Mage::getSingleton('admin/session')->isAllowed('cms/page/' . $action);
return $this->getAdminSession()->isAllowed('cms/page/' . $action);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,6 @@ public function isHidden()
*/
protected function _isAllowedAction($action)
{
return Mage::getSingleton('admin/session')->isAllowed('cms/page/' . $action);
return $this->getAdminSession()->isAllowed('cms/page/' . $action);
}
}
2 changes: 1 addition & 1 deletion app/code/core/Mage/Adminhtml/Block/Customer/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct()
$this->_controller = 'customer';

if ($this->getCustomerId() &&
Mage::getSingleton('admin/session')->isAllowed('sales/order/actions/create')
$this->getAdminSession()->isAllowed('sales/order/actions/create')
) {
$this->_addButton('order', [
'label' => Mage::helper('customer')->__('Create Order'),
Expand Down
14 changes: 4 additions & 10 deletions app/code/core/Mage/Adminhtml/Block/Customer/Edit/Tabs.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected function _beforeToHtml()
// load: Orders, Shopping Cart, Wishlist, Product Reviews, Product Tags - with ajax

if (Mage::registry('current_customer')->getId()) {
if (Mage::getSingleton('admin/session')->isAllowed('sales/order/actions/view')) {
if ($this->getAdminSession()->isAllowed('sales/order/actions/view')) {
$this->addTab('orders', [
'label' => Mage::helper('customer')->__('Orders'),
'class' => 'ajax',
Expand All @@ -74,9 +74,7 @@ protected function _beforeToHtml()
'url' => $this->getUrl('*/*/wishlist', ['_current' => true]),
]);

if ($this->isModuleOutputEnabled('Mage_Newsletter')
&& Mage::getSingleton('admin/session')->isAllowed('newsletter/subscriber')
) {
if ($this->isModuleOutputEnabled('Mage_Newsletter') && $this->getAdminSession()->isAllowed('newsletter/subscriber')) {
/** @var Mage_Adminhtml_Block_Customer_Edit_Tab_Newsletter $block */
$block = $this->getLayout()->createBlock('adminhtml/customer_edit_tab_newsletter');
$this->addTab('newsletter', [
Expand All @@ -85,19 +83,15 @@ protected function _beforeToHtml()
]);
}

if ($this->isModuleOutputEnabled('Mage_Review')
&& Mage::getSingleton('admin/session')->isAllowed('catalog/reviews_ratings')
) {
if ($this->isModuleOutputEnabled('Mage_Review') && $this->getAdminSession()->isAllowed('catalog/reviews_ratings')) {
$this->addTab('reviews', [
'label' => Mage::helper('customer')->__('Product Reviews'),
'class' => 'ajax',
'url' => $this->getUrl('*/*/productReviews', ['_current' => true]),
]);
}

if ($this->isModuleEnabled('Mage_Tag')
&& Mage::getSingleton('admin/session')->isAllowed('catalog/tag')
) {
if ($this->isModuleEnabled('Mage_Tag') && $this->getAdminSession()->isAllowed('catalog/tag')) {
$this->addTab('tags', [
'label' => Mage::helper('customer')->__('Product Tags'),
'class' => 'ajax',
Expand Down
Loading
Loading