<?php
/**
* Shopware 5
* Copyright (c) shopware AG
*
* According to our dual licensing model, this program can be used either
* under the terms of the GNU Affero General Public License, version 3,
* or under a proprietary license.
*
* The texts of the GNU Affero General Public License with an additional
* permission and of our proprietary license can be found at and
* in the LICENSE file you have received along with this program.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* "Shopware" is a registered trademark of shopware AG.
* The licensing of the program under the AGPLv3 does not imply a
* trademark license. Therefore any rights, title and interest in
* our trademarks remain entirely with us.
*/
use Shopware\Components\Model\ModelManager;
/**
* Shopware ACL Components
*/
class Shopware_Components_Acl extends Zend_Acl
{
/**
* @var ModelManager
*/
private $em;
/**
* @param ModelManager $em
*/
public function __construct(ModelManager $em)
{
$this->em = $em;
$this->initShopwareAclTree();
}
/**
* Create shopware acl tree
*/
private function initShopwareAclTree()
{
$this->initAclResources()
->initAclRoles()
->initAclRoleConditions();
}
/**
* Get all resources from database and add to acl tree
*
* @return \Shopware_Components_Acl
*/
public function initAclResources()
{
$repository = $this->em->getRepository('Shopware\Models\User\Resource');
$resources = $repository->findAll();
/**@var $resource Shopware\Models\User\Resource */
foreach ($resources as $resource) {
$this->addResource($resource);
}
return $this;
}
/**
* Get all roles from s_core_auth_roles - check for parent role and add to tree
*
* @return \Shopware_Components_Acl
*/
public function initAclRoles()
{
$repository = $this->em->getRepository('Shopware\Models\User\Role');
$roles = $repository->findAll();
/** @var $role \Shopware\Models\User\Role */
foreach ($roles as $role) {
/** @var $parent \Shopware\Models\User\Role */
$parent = $role->getParent();
//parent exist and not already added?
if ($parent && !$this->hasRole($parent)) {
//register role and register role privileges
$this->addRole($parent);
}
//if parent exists, the pass the parent name to the addRole function
$this->addRole($role, $parent);
}
return $this;
}
/**
* Resolve all resources / privileges / role dependencies from database
* Should work as followed:
* If resourceID & privilegeID = null -> Global admin
* If resourceID != null AND privilegeID = null -> Module admin
* If resourceID & privilegeID is set - Grant permission to a certain privilege of a particular module
*
* @return \Shopware_Components_Acl
*/
public function initAclRoleConditions()
{
$rules = $this->em->getRepository('Shopware\Models\User\Rule')->findAll();
/** @var $rule \Shopware\Models\User\Rule */
foreach ($rules as $rule) {
$role = $rule->getRole();
$resource = $rule->getResource();
$privilege = $rule->getPrivilege();
if ($resource === null && $privilege === null) {
$this->allow($role);
} elseif ($privilege === null) {
$this->allow($role, $resource);
} else {
$this->allow($role, $resource, $privilege->getName());
}
}
return $this;
}
/**
* Is the resource identified by $resourceName already in database ?
* @param $resourceName
* @return bool
*/
public function hasResourceInDatabase($resourceName)
{
$repository = $this->em->getRepository('Shopware\Models\User\Resource');
$resource = $repository->findOneBy(array("name" => $resourceName));
return !empty($resource);
}
/**
* Create a new resource and optionally privileges, menu item relationships and plugin dependency
* @param $resourceName - unique identifier or resource key
* @param array|null $privileges - optionally array [a,b,c] of new privileges
* @param null $menuItemName - optionally s_core_menu.name item to link to this resource
* @param null $pluginID - optionally pluginID that implements this resource
* @throws Enlight_Exception
*/
public function createResource($resourceName, array $privileges = null, $menuItemName = null, $pluginID = null)
{
// Check if resource already exists
if ($this->hasResourceInDatabase($resourceName)) {
throw new Enlight_Exception("Resource $resourceName already exists");
}
$resource = new \Shopware\Models\User\Resource();
$resource->setName($resourceName);
$resource->setPluginId($pluginID);
$this->em->persist($resource);
$this->em->flush();
if (!empty($privileges)) {
foreach ($privileges as $privilege) {
$this->createPrivilege($resource->getId(), $privilege);
}
}
}
/**
* Create a privilege in a particular resource
* @param int $resourceId
* @param string $name
*/
public function createPrivilege($resourceId, $name)
{
$privilege = new \Shopware\Models\User\Privilege();
$privilege->setName($name);
$privilege->setResourceId($resourceId);
$this->em->persist($privilege);
$this->em->flush();
}
/**
* Delete resource and its privileges from database
* @param $resourceName
* @return bool
*/
public function deleteResource($resourceName)
{
$repository = $this->em->getRepository('Shopware\Models\User\Resource');
/** @var $resource \Shopware\Models\User\Resource */
$resource = $repository->findOneBy(array("name" => $resourceName));
if (empty($resource)) {
return false;
}
//The mapping table s_core_acl_roles must be cleared manually.
$this->em->getConnection()->executeUpdate(
"DELETE FROM s_core_acl_roles WHERE resourceID = ?",
[$resource->getId()]
);
//The privileges will be removed automatically
$this->em->remove($resource);
$this->em->flush();
return true;
}
}
| nama | waktu | ukuran | owner | perm | aksi |
|---|---|---|---|---|---|
| .. | |||||
| Api/ | 2016-11-07 16:02 | 287.29 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Auth/ | 2016-11-07 16:02 | 9.48 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Benchmark/ | 2016-11-07 15:59 | 2.97 KB | 76129f60960:kunden | dxwrx-rx-r | |
| CategoryHandling/ | 2016-11-07 15:59 | 6.7 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Check/ | 2016-11-07 16:02 | 137.23 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Compatibility/ | 2016-11-07 15:59 | 40.29 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Console/ | 2016-11-07 15:59 | 9.29 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Convert/ | 2016-11-07 15:59 | 21.31 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Cron/ | 2016-11-07 15:59 | 1.8 KB | 76129f60960:kunden | dxwrx-rx-r | |
| DependencyInjection/ | 2016-11-07 16:02 | 89.93 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Emotion/ | 2016-11-07 15:59 | 6.4 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Escaper/ | 2016-11-07 15:59 | 5.38 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Form/ | 2016-11-07 16:02 | 45.21 KB | 76129f60960:kunden | dxwrx-rx-r | |
| HttpCache/ | 2016-11-07 15:59 | 27.91 KB | 76129f60960:kunden | dxwrx-rx-r | |
| HttpClient/ | 2016-11-07 15:59 | 261.1 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Install/ | 2016-11-07 15:59 | 3.59 KB | 76129f60960:kunden | dxwrx-rx-r | |
| LegacyRequestWrapper/ | 2016-11-07 15:59 | 9.77 KB | 76129f60960:kunden | dxwrx-rx-r | |
| License/ | 2016-11-07 16:02 | 18.86 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Log/ | 2016-11-07 16:02 | 33.09 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Migrations/ | 2016-11-07 15:59 | 10.92 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Model/ | 2016-11-07 16:04 | 141.64 KB | 76129f60960:kunden | dxwrx-rx-r | |
| MultiEdit/ | 2016-11-07 16:02 | 98 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Password/ | 2016-11-07 16:02 | 18.5 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Plugin/ | 2016-11-07 16:02 | 119.2 KB | 76129f60960:kunden | dxwrx-rx-r | |
| ProductStream/ | 2016-11-07 15:59 | 19.08 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Routing/ | 2016-11-07 16:02 | 58.6 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Snippet/ | 2016-11-07 16:02 | 38.98 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Test/ | 2016-11-07 16:02 | 4.02 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Theme/ | 2016-11-07 16:02 | 130.93 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Thumbnail/ | 2016-11-07 16:02 | 21.71 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Validator/ | 2016-11-07 15:59 | 3.01 KB | 76129f60960:kunden | dxwrx-rx-r | |
| Acl.php | 2016-11-07 15:58 | 6.65 KB | 76129f60960:kunden | -xw-x----- | |
| AttributeSubscriber.php | 2016-11-07 15:58 | 4.73 KB | 76129f60960:kunden | -xw-x----- | |
| Auth.php | 2016-11-07 15:58 | 5.26 KB | 76129f60960:kunden | -xw-x----- | |
| CSRFTokenValidationException.php | 2016-11-07 15:58 | 1.08 KB | 76129f60960:kunden | -xw-x----- | |
| CSRFTokenValidator.php | 2016-11-07 15:58 | 5.52 KB | 76129f60960:kunden | -xw-x----- | |
| CSRFWhitelistAware.php | 2016-11-07 15:58 | 1.23 KB | 76129f60960:kunden | -xw-x----- | |
| CacheManager.php | 2016-11-07 15:58 | 12.56 KB | 76129f60960:kunden | -xw-x----- | |
| Config.php | 2016-11-07 15:58 | 7.9 KB | 76129f60960:kunden | -xw-x----- | |
| ConfigLoader.php | 2016-11-07 15:58 | 5.01 KB | 76129f60960:kunden | -xw-x----- | |
| ConfigWriter.php | 2016-11-07 15:58 | 4.32 KB | 76129f60960:kunden | -xw-x----- | |
| ContainerAwareEventManager.php | 2016-11-07 15:58 | 6.61 KB | 76129f60960:kunden | -xw-x----- | |
| CsvIterator.php | 2016-11-07 15:58 | 6.91 KB | 76129f60960:kunden | -xw-x----- | |
| Document.php | 2016-11-07 15:58 | 22.91 KB | 76129f60960:kunden | -xw-x----- | |
| Form.php | 2016-11-07 15:58 | 1.43 KB | 76129f60960:kunden | -xw-x----- | |
| HolidayTableUpdater.php | 2016-11-07 15:58 | 4.42 KB | 76129f60960:kunden | -xw-x----- | |
| Licence.php | 2016-11-07 15:58 | 1.25 KB | 76129f60960:kunden | -xw-x----- | |
| LicenseInstaller.php | 2016-11-07 15:58 | 3.04 KB | 76129f60960:kunden | -xw-x----- | |
| Logger.php | 2016-11-07 15:58 | 1.59 KB | 76129f60960:kunden | -xw-x----- | |
| MemoryLimit.php | 2016-11-07 15:58 | 2.04 KB | 76129f60960:kunden | -xw-x----- | |
| Modules.php | 2016-11-07 15:58 | 4.94 KB | 76129f60960:kunden | -xw-x----- | |
| NumberRangeIncrementer.php | 2016-11-07 15:58 | 2.16 KB | 76129f60960:kunden | -xw-x----- | |
| NumberRangeIncrementerInterface.php | 2016-11-07 15:58 | 1.65 KB | 76129f60960:kunden | -xw-x----- | |
| OpenSSLVerifier.php | 2016-11-07 15:58 | 2.82 KB | 76129f60960:kunden | -xw-x----- | |
| Plugin.php | 2016-11-07 15:58 | 5.31 KB | 76129f60960:kunden | -xw-x----- | |
| QueryAliasMapper.php | 2016-11-07 15:58 | 4.62 KB | 76129f60960:kunden | -xw-x----- | |
| Random.php | 2016-11-07 15:58 | 5.7 KB | 76129f60960:kunden | -xw-x----- | |
| ReflectionHelper.php | 2016-11-07 15:58 | 2.11 KB | 76129f60960:kunden | -xw-x----- | |
| SitePageMenu.php | 2016-11-07 15:58 | 5.13 KB | 76129f60960:kunden | -xw-x----- | |
| SitemapXMLRepository.php | 2016-11-07 15:58 | 10.86 KB | 76129f60960:kunden | -xw-x----- | |
| StringCompiler.php | 2016-11-07 15:58 | 4.64 KB | 76129f60960:kunden | -xw-x----- | |
| TemplateMail.php | 2016-11-07 15:58 | 7.54 KB | 76129f60960:kunden | -xw-x----- | |
| Theme.php | 2016-11-07 15:58 | 13.07 KB | 76129f60960:kunden | -xw-x----- | |
| Translation.php | 2016-11-07 15:58 | 16.25 KB | 76129f60960:kunden | -xw-x----- | |