mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-12 21:34:25 +00:00
fix cs and add test
This commit is contained in:
parent
fdbaa8cbef
commit
9993bfc96f
@ -1,5 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Chill is a software for social workers
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view
|
||||||
|
* the LICENSE file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
namespace Chill\MainBundle\Controller;
|
namespace Chill\MainBundle\Controller;
|
||||||
|
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
@ -9,10 +16,13 @@ use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
|||||||
use Symfony\Component\Routing\Annotation\Route;
|
use Symfony\Component\Routing\Annotation\Route;
|
||||||
use Symfony\Component\Security\Core\Security;
|
use Symfony\Component\Security\Core\Security;
|
||||||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
||||||
|
use function array_key_exists;
|
||||||
|
use function json_decode;
|
||||||
|
|
||||||
class PermissionApiController extends AbstractController
|
class PermissionApiController extends AbstractController
|
||||||
{
|
{
|
||||||
private DenormalizerInterface $denormalizer;
|
private DenormalizerInterface $denormalizer;
|
||||||
|
|
||||||
private Security $security;
|
private Security $security;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
@ -25,25 +35,29 @@ class PermissionApiController extends AbstractController
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @Route("/api/1.0/main/permissions/info.json", methods={"POST"})
|
* @Route("/api/1.0/main/permissions/info.json", methods={"POST"})
|
||||||
|
*
|
||||||
* @throws \Symfony\Component\Serializer\Exception\ExceptionInterface
|
* @throws \Symfony\Component\Serializer\Exception\ExceptionInterface
|
||||||
*/
|
*/
|
||||||
public function getPermissions(Request $request): JsonResponse
|
public function getPermissions(Request $request): JsonResponse
|
||||||
{
|
{
|
||||||
$this->denyAccessUnlessGranted('ROLE_USER');
|
$this->denyAccessUnlessGranted('ROLE_USER');
|
||||||
|
|
||||||
$data = \json_decode($request->getContent(), true);
|
$data = json_decode($request->getContent(), true);
|
||||||
|
|
||||||
if (null === $data) {
|
if (null === $data) {
|
||||||
throw new BadRequestHttpException(sprintf(
|
throw new BadRequestHttpException(sprintf(
|
||||||
"Could not decode json received, or data invalid: %s, %s", \json_last_error(), \json_last_error_msg()
|
'Could not decode json received, or data invalid: %s, %s',
|
||||||
|
json_last_error(),
|
||||||
|
json_last_error_msg()
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!\array_key_exists('object', $data)) {
|
if (!array_key_exists('object', $data)) {
|
||||||
throw new BadRequestHttpException("the object key is not present");
|
throw new BadRequestHttpException('the object key is not present');
|
||||||
}
|
}
|
||||||
if (!\array_key_exists('class', $data)) {
|
|
||||||
throw new BadRequestHttpException("the class key is not present");
|
if (!array_key_exists('class', $data)) {
|
||||||
|
throw new BadRequestHttpException('the class key is not present');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (null !== $data['object']) {
|
if (null !== $data['object']) {
|
||||||
@ -54,14 +68,13 @@ class PermissionApiController extends AbstractController
|
|||||||
$roles = [];
|
$roles = [];
|
||||||
|
|
||||||
foreach (($data['roles'] ?? []) as $role) {
|
foreach (($data['roles'] ?? []) as $role) {
|
||||||
$roles[$role] = $this->security->isGranted($role, $object);
|
$roles[$role] = $this->security->isGranted($role, $object);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->json(
|
return $this->json(
|
||||||
['roles' => $roles, ],
|
['roles' => $roles],
|
||||||
200,
|
200,
|
||||||
[],
|
[],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,51 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Chill is a software for social workers
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view
|
||||||
|
* the LICENSE file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
namespace Controller;
|
namespace Controller;
|
||||||
|
|
||||||
use Chill\MainBundle\Test\PrepareClientTrait;
|
use Chill\MainBundle\Test\PrepareClientTrait;
|
||||||
|
use DateTime;
|
||||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
* @coversNothing
|
||||||
|
*/
|
||||||
class PermissionApiControllerTest extends WebTestCase
|
class PermissionApiControllerTest extends WebTestCase
|
||||||
{
|
{
|
||||||
use PrepareClientTrait;
|
use PrepareClientTrait;
|
||||||
|
|
||||||
|
public function testDenormalizingObject()
|
||||||
|
{
|
||||||
|
$client = $this->getClientAuthenticated();
|
||||||
|
|
||||||
|
$client->request(
|
||||||
|
'POST',
|
||||||
|
'/api/1.0/main/permissions/info.json',
|
||||||
|
[], // parameters
|
||||||
|
[], // files
|
||||||
|
[], // server
|
||||||
|
json_encode([
|
||||||
|
'object' => [
|
||||||
|
'datetime' => '1969-07-09T00:00:00+0100',
|
||||||
|
],
|
||||||
|
'class' => DateTime::class,
|
||||||
|
'roles' => ['FOO_ROLE'],
|
||||||
|
])
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertResponseIsSuccessful();
|
||||||
|
|
||||||
|
$data = json_decode($client->getResponse()->getContent(), true);
|
||||||
|
$this->assertFalse($data['roles']['FOO_ROLE']);
|
||||||
|
}
|
||||||
|
|
||||||
public function testNullObject()
|
public function testNullObject()
|
||||||
{
|
{
|
||||||
$client = $this->getClientAuthenticated();
|
$client = $this->getClientAuthenticated();
|
||||||
@ -19,18 +56,17 @@ class PermissionApiControllerTest extends WebTestCase
|
|||||||
[], // parameters
|
[], // parameters
|
||||||
[], // files
|
[], // files
|
||||||
[], // server
|
[], // server
|
||||||
\json_encode([
|
json_encode([
|
||||||
'object' => null,
|
'object' => null,
|
||||||
'class' => null,
|
'class' => null,
|
||||||
'roles' => ['ROLE_USER', 'ROLE_ADMIN']
|
'roles' => ['ROLE_USER', 'ROLE_ADMIN'],
|
||||||
])
|
])
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertResponseIsSuccessful();
|
$this->assertResponseIsSuccessful();
|
||||||
|
|
||||||
$data = \json_decode($client->getResponse()->getContent(), true);
|
$data = json_decode($client->getResponse()->getContent(), true);
|
||||||
$this->assertTrue($data['roles']['ROLE_USER']);
|
$this->assertTrue($data['roles']['ROLE_USER']);
|
||||||
$this->assertFalse($data['roles']['ROLE_ADMIN']);
|
$this->assertFalse($data['roles']['ROLE_ADMIN']);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user