_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->cacheKey = $cacheKey; } /** * Populate ACL with roles from external storage * * @param \Magento\Framework\Acl $acl * @return void */ public function populateAcl(\Magento\Framework\Acl $acl) { foreach ($this->getRolesArray() as $role) { $parent = $role['parent_id'] > 0 ? $role['parent_id'] : null; switch ($role['role_type']) { case RoleGroup::ROLE_TYPE: $acl->addRole($this->_groupFactory->create(['roleId' => $role['role_id']]), $parent); break; case RoleUser::ROLE_TYPE: if (!$acl->hasRole($role['role_id'])) { $acl->addRole($this->_roleFactory->create(['roleId' => $role['role_id']]), $parent); } else { $acl->addRoleParent($role['role_id'], $parent); } break; } } } /** * Get application ACL roles array * * @return array */ private function getRolesArray() { $rolesCachedData = $this->aclDataCache->load($this->cacheKey); if ($rolesCachedData) { return $this->serializer->unserialize($rolesCachedData); } $roleTableName = $this->_resource->getTableName('authorization_role'); $connection = $this->_resource->getConnection(); $select = $connection->select() ->from($roleTableName) ->order('tree_level'); $rolesArray = $connection->fetchAll($select); $this->aclDataCache->save($this->serializer->serialize($rolesArray), $this->cacheKey); return $rolesArray; } }