Skip to content

Commit

Permalink
Merge pull request #12 from potievdev/issue/#11_enable_checking_forei…
Browse files Browse the repository at this point in the history
…gn_keys_after_remove_all

Enable checking foreign keys after remove all command
  • Loading branch information
potievdev authored Feb 24, 2019
2 parents 884747b + 963b011 commit 1c79649
Show file tree
Hide file tree
Showing 27 changed files with 367 additions and 157 deletions.
31 changes: 18 additions & 13 deletions src/Component/AuthManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Potievdev\SlimRbac\Exception\CyclicException;
use Potievdev\SlimRbac\Exception\DatabaseException;
use Potievdev\SlimRbac\Exception\NotUniqueException;
use Potievdev\SlimRbac\Models\Entity\Permission;
use Potievdev\SlimRbac\Models\Entity\Role;
Expand All @@ -23,6 +24,7 @@ class AuthManager extends BaseComponent
* @param integer $parentRoleId
* @param integer $childRoleId
* @throws CyclicException
* @throws \Doctrine\ORM\Query\QueryException
*/
private function checkForCyclicHierarchy($parentRoleId, $childRoleId)
{
Expand All @@ -37,6 +39,7 @@ private function checkForCyclicHierarchy($parentRoleId, $childRoleId)

/**
* Truncates all tables
* @throws DatabaseException
*/
public function removeAll()
{
Expand All @@ -51,13 +54,13 @@ public function removeAll()
$pdo->exec('TRUNCATE role');
$pdo->exec('TRUNCATE permission');
$pdo->exec('TRUNCATE user_role');
$pdo->exec('SET FOREIGN_KEY_CHECKS=0');
$pdo->exec('SET FOREIGN_KEY_CHECKS=1');

$pdo->commit();

} catch (\Exception $e) {
$pdo->rollBack();
throw new \Exception($e->getMessage());
throw new DatabaseException($e->getMessage());
}
}

Expand Down Expand Up @@ -91,12 +94,12 @@ public function createRole($roleName)
* Save permission in database
* @param Permission $permission
* @throws NotUniqueException
* @throws DatabaseException
*/
public function addPermission(Permission $permission)
{
try {
$this->entityManager->persist($permission);
$this->entityManager->flush();
$this->saveEntity($permission);
} catch (UniqueConstraintViolationException $e) {
throw new NotUniqueException('Permission with name ' . $permission->getName() . ' already created');
}
Expand All @@ -106,12 +109,12 @@ public function addPermission(Permission $permission)
* Save role in database
* @param Role $role
* @throws NotUniqueException
* @throws DatabaseException
*/
public function addRole(Role $role)
{
try {
$this->entityManager->persist($role);
$this->entityManager->flush();
$this->saveEntity($role);
} catch (UniqueConstraintViolationException $e) {
throw new NotUniqueException('Role with name ' . $role->getName() . ' already created');
}
Expand All @@ -121,6 +124,7 @@ public function addRole(Role $role)
* Add permission to role
* @param Role $role
* @param Permission $permission
* @throws DatabaseException
* @throws NotUniqueException
*/
public function addChildPermission(Role $role, Permission $permission)
Expand All @@ -131,8 +135,7 @@ public function addChildPermission(Role $role, Permission $permission)
$rolePermission->setRole($role);

try {
$this->entityManager->persist($rolePermission);
$this->entityManager->flush();
$this->saveEntity($rolePermission);
} catch (UniqueConstraintViolationException $e) {
throw new NotUniqueException('Permission ' . $permission->getName() . ' is already assigned to role ' . $role->getName());
}
Expand All @@ -142,7 +145,10 @@ public function addChildPermission(Role $role, Permission $permission)
* Add child role to role
* @param Role $parentRole
* @param Role $childRole
* @throws CyclicException
* @throws DatabaseException
* @throws NotUniqueException
* @throws \Doctrine\ORM\Query\QueryException
*/
public function addChildRole(Role $parentRole, Role $childRole)
{
Expand All @@ -154,8 +160,7 @@ public function addChildRole(Role $parentRole, Role $childRole)
$this->checkForCyclicHierarchy($childRole->getId(), $parentRole->getId());

try {
$this->entityManager->persist($roleHierarchy);
$this->entityManager->flush();
$this->saveEntity($roleHierarchy);
} catch (UniqueConstraintViolationException $e) {
throw new NotUniqueException('Child role ' . $childRole->getName() . ' is already has parent role ' . $parentRole->getName());
}
Expand All @@ -166,6 +171,7 @@ public function addChildRole(Role $parentRole, Role $childRole)
* @param Role $role
* @param integer $userId
* @throws NotUniqueException
* @throws DatabaseException
*/
public function assign(Role $role, $userId)
{
Expand All @@ -175,10 +181,9 @@ public function assign(Role $role, $userId)
$userRole->setRole($role);

try {
$this->entityManager->persist($userRole);
$this->entityManager->flush();
$this->saveEntity($userRole);
} catch (UniqueConstraintViolationException $e) {
throw new NotUniqueException('Role ' . $role->getName() . 'is already assigned to user with identifier ' . $userId);
}
}
}
}
16 changes: 9 additions & 7 deletions src/Component/AuthMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ class AuthMiddleware extends BaseComponent
{
/**
* Check access
* @param ServerRequestInterface $request PSR7 request
* @param ResponseInterface $response PSR7 response
* @param callable $next Next middleware
* @param ServerRequestInterface $request PSR7 request
* @param ResponseInterface $response PSR7 response
* @param callable $next Next middleware
*
* @return \Psr\Http\Message\ResponseInterface
* @throws \Doctrine\ORM\Query\QueryException
* @throws \Potievdev\SlimRbac\Exception\InvalidArgumentException
*/
public function __invoke($request, $response, $next)
{
Expand All @@ -30,16 +32,16 @@ public function __invoke($request, $response, $next)
switch ($storageType) {

case AuthOptions::ATTRIBUTE_STORAGE_TYPE:
$userId = $request->getAttribute($variableName);
$userId = intval($request->getAttribute($variableName));
break;

case AuthOptions::HEADER_STORAGE_TYPE:
$userId = $request->getHeaderLine($variableName);
$userId = intval($request->getHeaderLine($variableName));
break;

case AuthOptions::COOKIE_STORAGE_TYPE:
$params = $request->getCookieParams();
$userId = $params[$variableName];
$userId = intval($params[$variableName]);
break;
}

Expand All @@ -58,4 +60,4 @@ public function __invoke($request, $response, $next)

return $response;
}
}
}
24 changes: 22 additions & 2 deletions src/Component/BaseComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Potievdev\SlimRbac\Component;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\OptimisticLockException;
use Potievdev\SlimRbac\Exception\DatabaseException;
use Potievdev\SlimRbac\Exception\InvalidArgumentException;
use Potievdev\SlimRbac\Helper\ValidatorHelper;
use Potievdev\SlimRbac\Models\RepositoryRegistry;
Expand Down Expand Up @@ -34,12 +36,30 @@ public function __construct(AuthOptions $authOptions)
$this->repositoryRegistry = new RepositoryRegistry($this->entityManager);
}

/**
* Insert or update entity
* @param object $entity
* @return object
* @throws DatabaseException
*/
protected function saveEntity($entity)
{
try {
$this->entityManager->persist($entity);
$this->entityManager->flush($entity);
return $entity;
} catch (OptimisticLockException $e) {
throw new DatabaseException($e->getMessage());
}
}

/**
* Checks access status
* @param integer $userId
* @param string $permissionName
* @return bool
* @throws \Exception
* @throws InvalidArgumentException
* @throws \Doctrine\ORM\Query\QueryException
*/
public function checkAccess($userId, $permissionName)
{
Expand Down Expand Up @@ -74,4 +94,4 @@ public function checkAccess($userId, $permissionName)

return false;
}
}
}
2 changes: 1 addition & 1 deletion src/Console/Command/BaseDatabaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,4 @@ public function execute(InputInterface $input, OutputInterface $output)

$this->config = new Config($configArray);
}
}
}
2 changes: 1 addition & 1 deletion src/Console/Command/CreateConfigCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ public function execute(InputInterface $input, OutputInterface $output)
file_put_contents($currentDir . '/sr-config.php', $configFile);
$output->writeln("File sr-config.php created in directory: $currentDir");
}
}
}
2 changes: 1 addition & 1 deletion src/Console/Command/MigrateDatabaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ public function execute(InputInterface $input, OutputInterface $output)
$manager = new Manager($this->config, $input, $output);
$manager->migrate($this->config->getDefaultEnvironment());
}
}
}
2 changes: 1 addition & 1 deletion src/Console/Command/RollbackDatabaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ public function execute(InputInterface $input, OutputInterface $output)
$manager = new Manager($this->config, $input, $output);
$manager->rollback($this->config->getDefaultEnvironment());
}
}
}
3 changes: 2 additions & 1 deletion src/Console/SlimRbacConsoleApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function __construct($version = '1.0.0')
* @param InputInterface $input An Input instance
* @param OutputInterface $output An Output instance
* @return integer 0 if everything went fine, or an error code
* @throws \Throwable
*/
public function doRun(InputInterface $input, OutputInterface $output)
{
Expand All @@ -45,4 +46,4 @@ public function doRun(InputInterface $input, OutputInterface $output)

return parent::doRun($input, $output);
}
}
}
4 changes: 2 additions & 2 deletions src/Exception/CyclicException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace Potievdev\SlimRbac\Exception;

/**
* This throws when cyclic line detected
* This throws when cyclic line detected in roles hierarchy
* Class CyclicException
* @package Potievdev\SlimRbac\Exception
*/
class CyclicException extends \Exception
{
protected $message = 'Cyclic role three detected';
}
}
12 changes: 12 additions & 0 deletions src/Exception/DatabaseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Potievdev\SlimRbac\Exception;

/**
* Exception occurs when something goes wrong with database
* Class DatabaseException
* @package Potievdev\SlimRbac\Exception
*/
class DatabaseException extends \Exception
{
}
2 changes: 1 addition & 1 deletion src/Exception/ForbiddenException.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
class ForbiddenException extends \Exception
{
protected $message = 'Access denied';
}
}
2 changes: 1 addition & 1 deletion src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
class InvalidArgumentException extends \Exception
{
protected $message = 'The invalid argument passed';
}
}
6 changes: 5 additions & 1 deletion src/Exception/NotUniqueException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

namespace Potievdev\SlimRbac\Exception;

/**
* Class NotUniqueException
* @package Potievdev\SlimRbac\Exception
*/
class NotUniqueException extends \Exception
{
protected $message = 'Not unique value';
}
}
7 changes: 5 additions & 2 deletions src/Helper/ArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace Potievdev\SlimRbac\Helper;

/**
* Class ArrayHelper
* @package Potievdev\SlimRbac\Helper
*/
class ArrayHelper
{
public static function merge(array $array1, array $array2)
{
return array_merge($array1, $array2);
}

}
}
2 changes: 1 addition & 1 deletion src/Helper/ValidatorHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ public static function isInteger($number)
return is_integer($number);
}

}
}
10 changes: 7 additions & 3 deletions src/Models/Entity/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,19 @@ public function setDescription($description)
$this->description = $description;
}

/** @ORM\PrePersist */
/** @ORM\PrePersist
* @throws \Exception
*/
public function prePersist()
{
$this->createdAt = new \DateTime();
}

/** @ORM\PreUpdate */
/** @ORM\PreUpdate
* @throws \Exception
*/
public function preUpdate()
{
$this->updatedAt = new \DateTime();
}
}
}
10 changes: 7 additions & 3 deletions src/Models/Entity/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,19 @@ public function setDescription($description)
$this->description = $description;
}

/** @ORM\PrePersist */
/** @ORM\PrePersist
* @throws \Exception
*/
public function prePersist()
{
$this->createdAt = new \DateTime();
}

/** @ORM\PreUpdate */
/** @ORM\PreUpdate
* @throws \Exception
*/
public function preUpdate()
{
$this->updatedAt = new \DateTime();
}
}
}
Loading

0 comments on commit 1c79649

Please sign in to comment.