User roles and permission management via Admin UI (#1231)
This commit is contained in:
@@ -14,27 +14,53 @@ use App\Entity\Configuration;
|
||||
use App\Form\Model\SystemConfiguration;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Doctrine\ORM\ORMException;
|
||||
use Doctrine\ORM\Query;
|
||||
|
||||
class ConfigurationRepository extends EntityRepository implements ConfigLoaderInterface
|
||||
{
|
||||
private static $cacheByPrefix = null;
|
||||
private static $cacheAll = [];
|
||||
|
||||
private function clearCache()
|
||||
{
|
||||
static::$cacheByPrefix = null;
|
||||
}
|
||||
|
||||
private function prefillCache()
|
||||
{
|
||||
if (null !== static::$cacheByPrefix) {
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var Configuration[] $configs */
|
||||
$configs = $this->findAll();
|
||||
static::$cacheByPrefix = [];
|
||||
foreach ($configs as $config) {
|
||||
$key = substr($config->getName(), 0, strpos($config->getName(), '.'));
|
||||
if (!array_key_exists($key, static::$cacheByPrefix)) {
|
||||
static::$cacheByPrefix[$key] = [];
|
||||
}
|
||||
static::$cacheByPrefix[$key][] = $config;
|
||||
static::$cacheAll[] = $config;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $prefix
|
||||
* @return Configuration[]
|
||||
*/
|
||||
public function getConfiguration(?string $prefix = null): array
|
||||
{
|
||||
$this->prefillCache();
|
||||
|
||||
if (null === $prefix) {
|
||||
return $this->findAll();
|
||||
return static::$cacheAll;
|
||||
}
|
||||
|
||||
$qb = $this->createQueryBuilder('c');
|
||||
$qb
|
||||
->select('c')
|
||||
->where($qb->expr()->like('c.name', ':prefix'))
|
||||
->setParameter(':prefix', $prefix . '%');
|
||||
if (!array_key_exists($prefix, static::$cacheByPrefix)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $qb->getQuery()->getResult(Query::HYDRATE_OBJECT);
|
||||
return static::$cacheByPrefix[$prefix];
|
||||
}
|
||||
|
||||
public function saveSystemConfiguration(SystemConfiguration $model)
|
||||
@@ -68,5 +94,7 @@ class ConfigurationRepository extends EntityRepository implements ConfigLoaderIn
|
||||
$em->rollback();
|
||||
throw $ex;
|
||||
}
|
||||
|
||||
$this->clearCache();
|
||||
}
|
||||
}
|
||||
|
||||
40
src/Repository/RolePermissionRepository.php
Normal file
40
src/Repository/RolePermissionRepository.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Repository;
|
||||
|
||||
use App\Entity\Role;
|
||||
use App\Entity\RolePermission;
|
||||
use Doctrine\ORM\AbstractQuery;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
|
||||
class RolePermissionRepository extends EntityRepository
|
||||
{
|
||||
public function saveRolePermission(RolePermission $permission)
|
||||
{
|
||||
$entityManager = $this->getEntityManager();
|
||||
$entityManager->persist($permission);
|
||||
$entityManager->flush();
|
||||
}
|
||||
|
||||
public function findRolePermission(Role $role, string $permission)
|
||||
{
|
||||
return $this->findOneBy(['role' => $role, 'permission' => $permission]);
|
||||
}
|
||||
|
||||
public function getAllAsArray()
|
||||
{
|
||||
$qb = $this->createQueryBuilder('rp');
|
||||
|
||||
$qb->select('r.name as role,rp.permission,rp.allowed')
|
||||
->leftJoin('rp.role', 'r');
|
||||
|
||||
return $qb->getQuery()->execute([], AbstractQuery::HYDRATE_ARRAY);
|
||||
}
|
||||
}
|
||||
47
src/Repository/RoleRepository.php
Normal file
47
src/Repository/RoleRepository.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Repository;
|
||||
|
||||
use App\Entity\Role;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Doctrine\ORM\ORMException;
|
||||
|
||||
class RoleRepository extends EntityRepository
|
||||
{
|
||||
/**
|
||||
* @return Role[]
|
||||
*/
|
||||
public function findAll()
|
||||
{
|
||||
return parent::findAll();
|
||||
}
|
||||
|
||||
public function saveRole(Role $role)
|
||||
{
|
||||
$entityManager = $this->getEntityManager();
|
||||
$entityManager->persist($role);
|
||||
$entityManager->flush();
|
||||
}
|
||||
|
||||
public function deleteRole(Role $role)
|
||||
{
|
||||
$em = $this->getEntityManager();
|
||||
$em->beginTransaction();
|
||||
|
||||
try {
|
||||
$em->remove($role);
|
||||
$em->flush();
|
||||
$em->commit();
|
||||
} catch (ORMException $ex) {
|
||||
$em->rollback();
|
||||
throw $ex;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user