Skip to content

Commit

Permalink
Cleanup ObjectManager usage - Magento_Authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
Bartlomiejsz committed Feb 27, 2020
1 parent 9f093a3 commit b26b64e
Show file tree
Hide file tree
Showing 6 changed files with 330 additions and 256 deletions.
47 changes: 27 additions & 20 deletions app/code/Magento/Authorization/Model/Acl/Loader/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,46 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Authorization\Model\Acl\Loader;

use Magento\Authorization\Model\Acl\Role\Group as RoleGroup;
use Magento\Authorization\Model\Acl\Role\GroupFactory;
use Magento\Authorization\Model\Acl\Role\User as RoleUser;
use Magento\Framework\App\ObjectManager;
use Magento\Authorization\Model\Acl\Role\UserFactory;
use Magento\Framework\Acl\Data\CacheInterface;
use Magento\Framework\Acl\LoaderInterface;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Serialize\Serializer\Json;

class Role implements \Magento\Framework\Acl\LoaderInterface
/**
* Acl Role Loader
*/
class Role implements LoaderInterface
{
/**
* Cache key for ACL roles cache
*/
const ACL_ROLES_CACHE_KEY = 'authorization_role_cached_data';

/**
* @var \Magento\Framework\App\ResourceConnection
* @var ResourceConnection
*/
protected $_resource;

/**
* @var \Magento\Authorization\Model\Acl\Role\GroupFactory
* @var GroupFactory
*/
protected $_groupFactory;

/**
* @var \Magento\Authorization\Model\Acl\Role\UserFactory
* @var UserFactory
*/
protected $_roleFactory;

/**
* @var \Magento\Framework\Acl\Data\CacheInterface
* @var CacheInterface
*/
private $aclDataCache;

Expand All @@ -48,28 +57,26 @@ class Role implements \Magento\Framework\Acl\LoaderInterface
private $cacheKey;

/**
* @param \Magento\Authorization\Model\Acl\Role\GroupFactory $groupFactory
* @param \Magento\Authorization\Model\Acl\Role\UserFactory $roleFactory
* @param \Magento\Framework\App\ResourceConnection $resource
* @param \Magento\Framework\Acl\Data\CacheInterface $aclDataCache
* @param GroupFactory $groupFactory
* @param UserFactory $roleFactory
* @param ResourceConnection $resource
* @param CacheInterface $aclDataCache
* @param Json $serializer
* @param string $cacheKey
*/
public function __construct(
\Magento\Authorization\Model\Acl\Role\GroupFactory $groupFactory,
\Magento\Authorization\Model\Acl\Role\UserFactory $roleFactory,
\Magento\Framework\App\ResourceConnection $resource,
\Magento\Framework\Acl\Data\CacheInterface $aclDataCache = null,
Json $serializer = null,
GroupFactory $groupFactory,
UserFactory $roleFactory,
ResourceConnection $resource,
CacheInterface $aclDataCache,
Json $serializer,
$cacheKey = self::ACL_ROLES_CACHE_KEY
) {
$this->_resource = $resource;
$this->_groupFactory = $groupFactory;
$this->_roleFactory = $roleFactory;
$this->aclDataCache = $aclDataCache ?: ObjectManager::getInstance()->get(
\Magento\Framework\Acl\Data\CacheInterface::class
);
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
$this->_resource = $resource;
$this->aclDataCache = $aclDataCache;
$this->serializer = $serializer;
$this->cacheKey = $cacheKey;
}

Expand Down
44 changes: 25 additions & 19 deletions app/code/Magento/Authorization/Model/Acl/Loader/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,38 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Authorization\Model\Acl\Loader;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\Acl\Data\CacheInterface;
use Magento\Framework\Acl\LoaderInterface;
use Magento\Framework\Acl\RootResource;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Serialize\Serializer\Json;

class Rule implements \Magento\Framework\Acl\LoaderInterface
/**
* Acl Rule Loader
*/
class Rule implements LoaderInterface
{
/**
* Rules array cache key
*/
const ACL_RULE_CACHE_KEY = 'authorization_rule_cached_data';

/**
* @var \Magento\Framework\App\ResourceConnection
* @var ResourceConnection
*/
protected $_resource;

/**
* @var \Magento\Framework\Acl\RootResource
* @var RootResource
*/
private $_rootResource;

/**
* @var \Magento\Framework\Acl\Data\CacheInterface
* @var CacheInterface
*/
private $aclDataCache;

Expand All @@ -41,28 +49,26 @@ class Rule implements \Magento\Framework\Acl\LoaderInterface
private $cacheKey;

/**
* @param \Magento\Framework\Acl\RootResource $rootResource
* @param \Magento\Framework\App\ResourceConnection $resource
* @param array $data
* @param \Magento\Framework\Acl\Data\CacheInterface $aclDataCache
* @param RootResource $rootResource
* @param ResourceConnection $resource
* @param CacheInterface $aclDataCache
* @param Json $serializer
* @param array $data
* @param string $cacheKey
* @SuppressWarnings(PHPMD.UnusedFormalParameter):
*/
public function __construct(
\Magento\Framework\Acl\RootResource $rootResource,
\Magento\Framework\App\ResourceConnection $resource,
RootResource $rootResource,
ResourceConnection $resource,
CacheInterface $aclDataCache,
Json $serializer,
array $data = [],
\Magento\Framework\Acl\Data\CacheInterface $aclDataCache = null,
Json $serializer = null,
$cacheKey = self::ACL_RULE_CACHE_KEY
) {
$this->_resource = $resource;
$this->_rootResource = $rootResource;
$this->aclDataCache = $aclDataCache ?: ObjectManager::getInstance()->get(
\Magento\Framework\Acl\Data\CacheInterface::class
);
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
$this->_resource = $resource;
$this->aclDataCache = $aclDataCache;
$this->serializer = $serializer;
$this->cacheKey = $cacheKey;
}

Expand Down Expand Up @@ -104,7 +110,7 @@ private function getRulesArray()
return $this->serializer->unserialize($rulesCachedData);
}

$ruleTable = $this->_resource->getTableName("authorization_rule");
$ruleTable = $this->_resource->getTableName('authorization_rule');
$connection = $this->_resource->getConnection();
$select = $connection->select()
->from(['r' => $ruleTable]);
Expand Down
51 changes: 28 additions & 23 deletions app/code/Magento/Authorization/Model/ResourceModel/Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,58 +6,63 @@

namespace Magento\Authorization\Model\ResourceModel;

use Magento\Framework\App\ObjectManager;
use Magento\Backend\App\AbstractAction;
use Magento\Framework\Acl\Builder;
use Magento\Framework\Acl\Data\CacheInterface;
use Magento\Framework\Acl\RootResource;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
use Magento\Framework\Model\ResourceModel\Db\Context;
use Psr\Log\LoggerInterface;

/**
* Admin rule resource model
*/
class Rules extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
class Rules extends AbstractDb
{
/**
* Root ACL resource
*
* @var \Magento\Framework\Acl\RootResource
* @var RootResource
*/
protected $_rootResource;

/**
* @var \Magento\Framework\Acl\Builder
* @var Builder
*/
protected $_aclBuilder;

/**
* @var \Psr\Log\LoggerInterface
* @var LoggerInterface
*/
protected $_logger;

/**
* @var \Magento\Framework\Acl\Data\CacheInterface
* @var CacheInterface
*/
private $aclDataCache;

/**
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
* @param \Magento\Framework\Acl\Builder $aclBuilder
* @param \Psr\Log\LoggerInterface $logger
* @param \Magento\Framework\Acl\RootResource $rootResource
* @param Context $context
* @param Builder $aclBuilder
* @param LoggerInterface $logger
* @param RootResource $rootResource
* @param CacheInterface $aclDataCache
* @param string $connectionName
* @param \Magento\Framework\Acl\Data\CacheInterface $aclDataCache
*/
public function __construct(
\Magento\Framework\Model\ResourceModel\Db\Context $context,
\Magento\Framework\Acl\Builder $aclBuilder,
\Psr\Log\LoggerInterface $logger,
\Magento\Framework\Acl\RootResource $rootResource,
$connectionName = null,
\Magento\Framework\Acl\Data\CacheInterface $aclDataCache = null
Context $context,
Builder $aclBuilder,
LoggerInterface $logger,
RootResource $rootResource,
CacheInterface $aclDataCache,
$connectionName = null
) {
$this->_aclBuilder = $aclBuilder;
parent::__construct($context, $connectionName);
$this->_rootResource = $rootResource;
$this->_logger = $logger;
$this->aclDataCache = $aclDataCache ?: ObjectManager::getInstance()->get(
\Magento\Framework\Acl\Data\CacheInterface::class
);
$this->aclDataCache = $aclDataCache;
}

/**
Expand All @@ -75,7 +80,7 @@ protected function _construct()
*
* @param \Magento\Authorization\Model\Rules $rule
* @return void
* @throws \Magento\Framework\Exception\LocalizedException
* @throws LocalizedException
*/
public function saveRel(\Magento\Authorization\Model\Rules $rule)
{
Expand Down Expand Up @@ -107,7 +112,7 @@ public function saveRel(\Magento\Authorization\Model\Rules $rule)
$connection->insert($this->getMainTable(), $insertData);
} else {
/** Give basic admin permissions to any admin */
$postedResources[] = \Magento\Backend\App\AbstractAction::ADMIN_RESOURCE;
$postedResources[] = AbstractAction::ADMIN_RESOURCE;
$acl = $this->_aclBuilder->getAcl();
/** @var $resource \Magento\Framework\Acl\AclResource */
foreach ($acl->getResources() as $resourceId) {
Expand All @@ -125,7 +130,7 @@ public function saveRel(\Magento\Authorization\Model\Rules $rule)

$connection->commit();
$this->aclDataCache->clean();
} catch (\Magento\Framework\Exception\LocalizedException $e) {
} catch (LocalizedException $e) {
$connection->rollBack();
throw $e;
} catch (\Exception $e) {
Expand Down
Loading

0 comments on commit b26b64e

Please sign in to comment.