2019-03-18 19:05:41 +01:00
|
|
|
<?php
|
2020-02-22 18:14:36 +01:00
|
|
|
/**
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-01-05 15:46:58 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-03-18 19:05:41 +01:00
|
|
|
/**
|
2019-11-09 00:47:20 +01:00
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
2019-03-18 19:05:41 +01:00
|
|
|
*
|
2019-11-01 13:40:30 +01:00
|
|
|
* Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
|
2019-03-18 19:05:41 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
use App\Configuration\PermissionsConfiguration;
|
2020-02-01 19:42:28 +01:00
|
|
|
use App\Entity\UserSystem\Group;
|
2019-08-12 18:04:53 +02:00
|
|
|
use App\Entity\UserSystem\User;
|
2019-03-18 19:05:41 +01:00
|
|
|
use App\Security\Interfaces\HasPermissionsInterface;
|
2019-03-21 13:43:13 +01:00
|
|
|
use Symfony\Component\Config\ConfigCache;
|
2019-03-18 19:05:41 +01:00
|
|
|
use Symfony\Component\Config\Definition\Processor;
|
2019-03-21 13:43:13 +01:00
|
|
|
use Symfony\Component\Config\Resource\FileResource;
|
2019-03-18 19:05:41 +01:00
|
|
|
use Symfony\Component\Yaml\Yaml;
|
|
|
|
|
|
|
|
class PermissionResolver
|
|
|
|
{
|
|
|
|
protected $permission_structure;
|
|
|
|
|
2019-03-21 13:43:13 +01:00
|
|
|
protected $is_debug;
|
|
|
|
protected $cache_file;
|
|
|
|
|
2019-03-18 19:05:41 +01:00
|
|
|
/**
|
|
|
|
* PermissionResolver constructor.
|
|
|
|
*/
|
2020-03-29 23:36:53 +02:00
|
|
|
public function __construct(bool $kernel_debug, string $kernel_cache_dir)
|
2019-03-18 19:05:41 +01:00
|
|
|
{
|
2020-03-29 23:36:53 +02:00
|
|
|
$cache_dir = $kernel_cache_dir;
|
2019-03-21 13:43:13 +01:00
|
|
|
//Here the cached structure will be saved.
|
|
|
|
$this->cache_file = $cache_dir.'/permissions.php.cache';
|
2020-03-29 23:36:53 +02:00
|
|
|
$this->is_debug = $kernel_debug;
|
2019-03-18 19:05:41 +01:00
|
|
|
|
2019-09-10 17:12:56 +02:00
|
|
|
$this->permission_structure = $this->generatePermissionStructure();
|
2019-03-18 19:05:41 +01:00
|
|
|
}
|
|
|
|
|
2019-11-09 00:47:20 +01:00
|
|
|
public function getPermissionStructure(): array
|
2019-09-10 17:12:56 +02:00
|
|
|
{
|
|
|
|
return $this->permission_structure;
|
|
|
|
}
|
|
|
|
|
2019-03-18 19:05:41 +01:00
|
|
|
/**
|
|
|
|
* Check if a user/group is allowed to do the specified operation for the permission.
|
|
|
|
*
|
|
|
|
* See permissions.yaml for valid permission operation combinations.
|
|
|
|
*
|
2019-03-21 13:43:13 +01:00
|
|
|
* @param HasPermissionsInterface $user the user/group for which the operation should be checked
|
|
|
|
* @param string $permission the name of the permission for which should be checked
|
|
|
|
* @param string $operation the name of the operation for which should be checked
|
2019-03-20 23:16:07 +01:00
|
|
|
*
|
2019-03-21 13:43:13 +01:00
|
|
|
* @return bool|null true, if the user is allowed to do the operation (ALLOW), false if not (DISALLOW), and null,
|
|
|
|
* if the value is set to inherit
|
2019-03-18 19:05:41 +01:00
|
|
|
*/
|
2019-03-20 23:16:07 +01:00
|
|
|
public function dontInherit(HasPermissionsInterface $user, string $permission, string $operation): ?bool
|
2019-03-18 19:05:41 +01:00
|
|
|
{
|
|
|
|
//Get the permissions from the user
|
|
|
|
$perm_list = $user->getPermissions();
|
|
|
|
|
|
|
|
//Determine bit number using our configuration
|
|
|
|
$bit = $this->permission_structure['perms'][$permission]['operations'][$operation]['bit'];
|
|
|
|
|
|
|
|
return $perm_list->getPermissionValue($permission, $bit);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if a user is allowed to do the specified operation for the permission.
|
|
|
|
* In contrast to dontInherit() it tries to resolve the inherit values, of the user, by going upwards in the
|
|
|
|
* hierachy (user -> group -> parent group -> so on). But even in this case it is possible, that the inherit value
|
|
|
|
* could be resolved, and this function returns null.
|
|
|
|
*
|
|
|
|
* In that case the voter should set it manually to false by using ?? false.
|
|
|
|
*
|
2019-03-21 13:43:13 +01:00
|
|
|
* @param User $user the user for which the operation should be checked
|
|
|
|
* @param string $permission the name of the permission for which should be checked
|
|
|
|
* @param string $operation the name of the operation for which should be checked
|
2019-03-20 23:16:07 +01:00
|
|
|
*
|
2019-03-21 13:43:13 +01:00
|
|
|
* @return bool|null true, if the user is allowed to do the operation (ALLOW), false if not (DISALLOW), and null,
|
|
|
|
* if the value is set to inherit
|
2019-03-18 19:05:41 +01:00
|
|
|
*/
|
2019-03-20 23:16:07 +01:00
|
|
|
public function inherit(User $user, string $permission, string $operation): ?bool
|
2019-03-18 19:05:41 +01:00
|
|
|
{
|
|
|
|
//Check if we need to inherit
|
|
|
|
$allowed = $this->dontInherit($user, $permission, $operation);
|
|
|
|
|
2019-03-20 23:16:07 +01:00
|
|
|
if (null !== $allowed) {
|
2019-03-18 19:05:41 +01:00
|
|
|
//Just return the value of the user.
|
|
|
|
return $allowed;
|
|
|
|
}
|
|
|
|
|
2020-03-29 23:36:53 +02:00
|
|
|
/** @var Group $parent */
|
2019-03-18 19:05:41 +01:00
|
|
|
$parent = $user->getGroup();
|
2020-01-05 15:46:58 +01:00
|
|
|
while (null !== $parent) { //The top group, has parent == null
|
2019-03-18 19:05:41 +01:00
|
|
|
//Check if our current element gives a info about disallow/allow
|
|
|
|
$allowed = $this->dontInherit($parent, $permission, $operation);
|
2019-03-20 23:16:07 +01:00
|
|
|
if (null !== $allowed) {
|
2019-03-18 19:05:41 +01:00
|
|
|
return $allowed;
|
|
|
|
}
|
|
|
|
//Else go up in the hierachy.
|
|
|
|
$parent = $parent->getParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null; //The inherited value is never resolved. Should be treat as false, in Voters.
|
|
|
|
}
|
|
|
|
|
2019-09-10 17:12:56 +02:00
|
|
|
/**
|
2019-11-09 00:47:20 +01:00
|
|
|
* Sets the new value for the operation.
|
|
|
|
*
|
2020-01-04 20:24:09 +01:00
|
|
|
* @param HasPermissionsInterface $user the user or group for which the value should be changed
|
|
|
|
* @param string $permission the name of the permission that should be changed
|
|
|
|
* @param string $operation the name of the operation that should be changed
|
2019-11-09 00:47:20 +01:00
|
|
|
* @param bool|null $new_val The new value for the permission. true = ALLOW, false = DISALLOW, null = INHERIT
|
2019-09-10 17:12:56 +02:00
|
|
|
*/
|
2019-11-09 00:47:20 +01:00
|
|
|
public function setPermission(HasPermissionsInterface $user, string $permission, string $operation, ?bool $new_val): void
|
2019-09-10 17:12:56 +02:00
|
|
|
{
|
|
|
|
//Get the permissions from the user
|
|
|
|
$perm_list = $user->getPermissions();
|
|
|
|
|
|
|
|
//Determine bit number using our configuration
|
|
|
|
$bit = $this->permission_structure['perms'][$permission]['operations'][$operation]['bit'];
|
|
|
|
|
|
|
|
$perm_list->setPermissionValue($permission, $bit, $new_val);
|
|
|
|
}
|
|
|
|
|
2019-03-18 19:05:41 +01:00
|
|
|
/**
|
|
|
|
* Lists the names of all operations that is supported for the given permission.
|
|
|
|
*
|
|
|
|
* If the Permission is not existing at all, a exception is thrown.
|
|
|
|
*
|
|
|
|
* This function is useful for the support() function of the voters.
|
|
|
|
*
|
|
|
|
* @param string $permission The permission for which the
|
2019-03-20 23:16:07 +01:00
|
|
|
*
|
2019-03-18 19:05:41 +01:00
|
|
|
* @return string[] A list of all operations that are supported by the given
|
|
|
|
*/
|
2019-03-20 23:16:07 +01:00
|
|
|
public function listOperationsForPermission(string $permission): array
|
2019-03-18 19:05:41 +01:00
|
|
|
{
|
2020-01-05 15:46:58 +01:00
|
|
|
if (! $this->isValidPermission($permission)) {
|
2019-03-26 23:34:40 +01:00
|
|
|
throw new \InvalidArgumentException(sprintf('A permission with that name is not existing! Got %s.', $permission));
|
|
|
|
}
|
2019-03-18 19:05:41 +01:00
|
|
|
$operations = $this->permission_structure['perms'][$permission]['operations'];
|
|
|
|
|
|
|
|
return array_keys($operations);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the permission with the given name is existing.
|
|
|
|
*
|
2019-03-21 13:43:13 +01:00
|
|
|
* @param string $permission the name of the permission which we want to check
|
2019-03-20 23:16:07 +01:00
|
|
|
*
|
2019-03-18 19:05:41 +01:00
|
|
|
* @return bool True if a perm with that name is existing. False if not.
|
|
|
|
*/
|
2019-03-20 23:16:07 +01:00
|
|
|
public function isValidPermission(string $permission): bool
|
2019-03-18 19:05:41 +01:00
|
|
|
{
|
|
|
|
return isset($this->permission_structure['perms'][$permission]);
|
|
|
|
}
|
|
|
|
|
2019-03-19 18:36:05 +01:00
|
|
|
/**
|
|
|
|
* Checks if the permission operation combination with the given names is existing.
|
|
|
|
*
|
2019-03-21 13:43:13 +01:00
|
|
|
* @param string $permission the name of the permission which should be checked
|
|
|
|
* @param string $operation the name of the operation which should be checked
|
2019-03-20 23:16:07 +01:00
|
|
|
*
|
2019-03-21 13:43:13 +01:00
|
|
|
* @return bool true if the given permission operation combination is existing
|
2019-03-19 18:36:05 +01:00
|
|
|
*/
|
2019-03-20 23:16:07 +01:00
|
|
|
public function isValidOperation(string $permission, string $operation): bool
|
2019-03-19 18:36:05 +01:00
|
|
|
{
|
|
|
|
return $this->isValidPermission($permission) &&
|
|
|
|
isset($this->permission_structure['perms'][$permission]['operations'][$operation]);
|
|
|
|
}
|
2020-01-05 15:46:58 +01:00
|
|
|
|
|
|
|
protected function generatePermissionStructure()
|
|
|
|
{
|
|
|
|
$cache = new ConfigCache($this->cache_file, $this->is_debug);
|
|
|
|
|
|
|
|
//Check if the cache is fresh, else regenerate it.
|
|
|
|
if (! $cache->isFresh()) {
|
|
|
|
$permission_file = __DIR__.'/../../config/permissions.yaml';
|
|
|
|
|
|
|
|
//Read the permission config file...
|
|
|
|
$config = Yaml::parse(
|
|
|
|
file_get_contents($permission_file)
|
|
|
|
);
|
|
|
|
|
|
|
|
$configs = [$config];
|
|
|
|
|
|
|
|
//... And parse it
|
|
|
|
$processor = new Processor();
|
|
|
|
$databaseConfiguration = new PermissionsConfiguration();
|
|
|
|
$processedConfiguration = $processor->processConfiguration(
|
|
|
|
$databaseConfiguration,
|
|
|
|
$configs
|
|
|
|
);
|
|
|
|
|
|
|
|
//Permission file is our file resource (it is used to invalidate cache)
|
|
|
|
$resources = [];
|
|
|
|
$resources[] = new FileResource($permission_file);
|
|
|
|
|
|
|
|
//Var export the structure and write it to cache file.
|
|
|
|
$cache->write(
|
|
|
|
sprintf('<?php return %s;', var_export($processedConfiguration, true)),
|
|
|
|
$resources);
|
|
|
|
}
|
|
|
|
|
|
|
|
//In the most cases we just need to dump the cached PHP file.
|
|
|
|
return require $this->cache_file;
|
|
|
|
}
|
2019-03-20 23:16:07 +01:00
|
|
|
}
|