diff --git a/src/Repository/UserRepository.php b/src/Repository/UserRepository.php index f48e036e..8e021bed 100644 --- a/src/Repository/UserRepository.php +++ b/src/Repository/UserRepository.php @@ -49,33 +49,4 @@ class UserRepository extends ServiceEntityRepository 'id' => User::ID_ANONYMOUS, ]); } - - // /** - // * @return User[] Returns an array of User objects - // */ - /* - public function findByExampleField($value) - { - return $this->createQueryBuilder('u') - ->andWhere('u.exampleField = :val') - ->setParameter('val', $value) - ->orderBy('u.id', 'ASC') - ->setMaxResults(10) - ->getQuery() - ->getResult() - ; - } - */ - - /* - public function findOneBySomeField($value): ?User - { - return $this->createQueryBuilder('u') - ->andWhere('u.exampleField = :val') - ->setParameter('val', $value) - ->getQuery() - ->getOneOrNullResult() - ; - } - */ } diff --git a/src/Security/Annotations/ColumnSecurity.php b/src/Security/Annotations/ColumnSecurity.php index 381e62a7..85915d00 100644 --- a/src/Security/Annotations/ColumnSecurity.php +++ b/src/Security/Annotations/ColumnSecurity.php @@ -89,8 +89,9 @@ class ColumnSecurity if ($object instanceof NamedDBElement) { if (\is_string($this->placeholder) && '' !== $this->placeholder) { $object->setName($this->placeholder); + } else { + $object->setName('???'); } - $object->setName('???'); } return $object; @@ -99,6 +100,7 @@ class ColumnSecurity if (null === $this->placeholder) { switch ($this->type) { case 'integer': + case 'int': return 0; case 'float': return 0.0; @@ -109,11 +111,10 @@ class ColumnSecurity case 'collection': return new ArrayCollection(); case 'boolean': + case 'bool': return false; case 'datetime': - $date = new \DateTime(); - - return $date->setTimestamp(0); + return (new \DateTime())->setTimestamp(0); default: throw new InvalidArgumentException('Unknown type! You have to specify a placeholder!'); } diff --git a/src/Security/UserChecker.php b/src/Security/UserChecker.php index 17216bb6..a1328875 100644 --- a/src/Security/UserChecker.php +++ b/src/Security/UserChecker.php @@ -30,11 +30,9 @@ use Symfony\Contracts\Translation\TranslatorInterface; class UserChecker implements UserCheckerInterface { - protected $translator; - - public function __construct(TranslatorInterface $translator) + public function __construct() { - $this->translator = $translator; + } /** diff --git a/src/Security/Voter/ExtendedVoter.php b/src/Security/Voter/ExtendedVoter.php index 63b2efbb..e886a3a3 100644 --- a/src/Security/Voter/ExtendedVoter.php +++ b/src/Security/Voter/ExtendedVoter.php @@ -56,7 +56,8 @@ abstract class ExtendedVoter extends Voter // if the user is anonymous, we use the anonymous user. if (!$user instanceof User) { - $user = $this->entityManager->find(User::class, User::ID_ANONYMOUS); + $repo = $this->entityManager->getRepository(User::class); + $user = $repo->getAnonymousUser(); if (null === $user) { return false; } @@ -71,6 +72,7 @@ abstract class ExtendedVoter extends Voter * * @param $attribute * @param $subject + * @return bool */ abstract protected function voteOnUser($attribute, $subject, User $user): bool; } diff --git a/tests/Security/Annotations/ColumnSecurityTest.php b/tests/Security/Annotations/ColumnSecurityTest.php new file mode 100644 index 00000000..b867853f --- /dev/null +++ b/tests/Security/Annotations/ColumnSecurityTest.php @@ -0,0 +1,107 @@ +assertEquals('read', $annotation->getReadOperationName(), 'A new annotation must return read'); + $annotation->read = 'overwritten'; + $this->assertEquals('overwritten', $annotation->getReadOperationName()); + $annotation->prefix = 'prefix'; + $this->assertEquals('prefix.overwritten', $annotation->getReadOperationName()); + } + + public function testGetEditOperation() + { + $annotation = new ColumnSecurity(); + $this->assertEquals('edit', $annotation->getEditOperationName(), 'A new annotation must return read'); + $annotation->edit = 'overwritten'; + $this->assertEquals('overwritten', $annotation->getEditOperationName()); + $annotation->prefix = 'prefix'; + $this->assertEquals('prefix.overwritten', $annotation->getEditOperationName()); + } + + public function placeholderScalarDataProvider() : array + { + return [ + ['string', '???'], + ['integer', 0], + ['int', 0], + ['float', 0.0], + ['object', null], + ['bool', false], + ['boolean', false], + //['datetime', (new \DateTime())->setTimestamp(0)] + ]; + } + + /** + * @dataProvider placeholderScalarDataProvider + * @param string $type + * @param $expected_value + */ + public function testGetPlaceholderScalar(string $type, $expected_value) + { + $annotation = new ColumnSecurity(); + $annotation->type = $type; + $this->assertEquals($expected_value, $annotation->getPlaceholder()); + } + + public function testGetPlaceholderSpecifiedValue() + { + $annotation = new ColumnSecurity(); + $annotation->placeholder = 3434; + $this->assertEquals(3434, $annotation->getPlaceholder()); + + $annotation->placeholder = [323]; + $this->assertCount(1, $annotation->getPlaceholder()); + + //If a placeholder is specified we allow every type + $annotation->type = "type2"; + $annotation->placeholder = 'invalid'; + $this->assertEquals('invalid', $annotation->getPlaceholder()); + } + + public function testGetPlaceholderDBElement() + { + $annotation = new ColumnSecurity(); + $annotation->type = AttachmentType::class; + + /** @var AttachmentType $placeholder */ + $placeholder = $annotation->getPlaceholder(); + $this->assertInstanceOf(AttachmentType::class, $placeholder); + $this->assertEquals('???', $placeholder->getName()); + + $annotation->placeholder = 'test'; + $placeholder = $annotation->getPlaceholder(); + $this->assertInstanceOf(AttachmentType::class, $placeholder); + $this->assertEquals('test', $placeholder->getName()); + } +} \ No newline at end of file diff --git a/tests/Security/UserCheckerTest.php b/tests/Security/UserCheckerTest.php new file mode 100644 index 00000000..283fa2d9 --- /dev/null +++ b/tests/Security/UserCheckerTest.php @@ -0,0 +1,52 @@ +service = new UserChecker(); + } + + public function testThrowDisabledException() + { + $user = new User(); + $user->setDisabled(false); + + //An user that is not disabled should not throw an exception + $this->service->checkPostAuth($user); + + //An disabled user must throw an exception + $user->setDisabled(true); + $this->expectException(DisabledException::class); + $this->service->checkPostAuth($user); + } +} \ No newline at end of file