Skip to content
This repository has been archived by the owner on Nov 30, 2023. It is now read-only.

Commit

Permalink
Add new ROLE_GAME permissions for API
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeKESTEMAN committed Aug 25, 2022
1 parent 99c7364 commit aefed84
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 21 deletions.
10 changes: 7 additions & 3 deletions api/.env
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,13 @@ POSTGRES_DB=api
###> docker-compose/database ###

###< secrity-user_in_memory ###
USER_IN_MEMORY_USERNAME=admin
USER_IN_MEMORY_PASSWORD=admin
USER_IN_MEMORY_HASHED_PASSWORD='USER_IN_MEMORY_HASHED_PASSWORD'
USER_ADMIN_IN_MEMORY_USERNAME=admin
USER_ADMIN_IN_MEMORY_PASSWORD=admin
USER_ADMIN_IN_MEMORY_HASHED_PASSWORD='USER_ADMIN_IN_MEMORY_HASHED_PASSWORD'

USER_GAME_IN_MEMORY_USERNAME=game
USER_GAME_IN_MEMORY_PASSWORD=game
USER_GAME_IN_MEMORY_HASHED_PASSWORD='USER_GAME_IN_MEMORY_HASHED_PASSWORD'
###> secrity-user_in_memory ###

###> symfony/mercure-bundle ###
Expand Down
7 changes: 5 additions & 2 deletions api/.env.test
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ PANTHER_ERROR_SCREENSHOT_DIR=./var/error-screenshots
TRUSTED_HOSTS=^example\.com|localhost$

###< secrity-user_in_memory ###
USER_IN_MEMORY_USERNAME=admin
USER_IN_MEMORY_PASSWORD=admin
USER_ADMIN_IN_MEMORY_USERNAME=admin
USER_ADMIN_IN_MEMORY_PASSWORD=admin

USER_GAME_IN_MEMORY_USERNAME=game
USER_GAME_IN_MEMORY_PASSWORD=game
###> secrity-user_in_memory ###

###> twitter-authentication ###
Expand Down
6 changes: 4 additions & 2 deletions api/config/packages/security.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ security:
app_user_provider:
memory:
users:
'%env(USER_IN_MEMORY_USERNAME)%': { password: '%env(USER_IN_MEMORY_HASHED_PASSWORD)%', roles: ['ROLE_ADMIN'] }
'%env(USER_ADMIN_IN_MEMORY_USERNAME)%': { password: '%env(USER_ADMIN_IN_MEMORY_HASHED_PASSWORD)%', roles: ['ROLE_ADMIN'] }
'%env(USER_GAME_IN_MEMORY_USERNAME)%': { password: '%env(USER_GAME_IN_MEMORY_HASHED_PASSWORD)%', roles: ['ROLE_GAME'] }
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
Expand Down Expand Up @@ -60,4 +61,5 @@ when@test:
app_user_provider:
memory:
users:
'%env(USER_IN_MEMORY_USERNAME)%': { password: '%env(USER_IN_MEMORY_PASSWORD)%', roles: [ 'ROLE_ADMIN' ] }
'%env(USER_ADMIN_IN_MEMORY_USERNAME)%': { password: '%env(USER_ADMIN_IN_MEMORY_PASSWORD)%', roles: [ 'ROLE_ADMIN' ] }
'%env(USER_GAME_IN_MEMORY_USERNAME)%': { password: '%env(USER_GAME_IN_MEMORY_PASSWORD)%', roles: ['ROLE_GAME'] }
4 changes: 2 additions & 2 deletions api/src/Entity/Game.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
ApiResource(
types: ['https://schema.org/VideoGame'],
operations: [
new GetCollection(),
new GetCollection(security: 'is_granted("ROLE_ADMIN") || is_granted("ROLE_GAME")'),
new Get(),
new Put(
denormalizationContext: ['groups' => ['put']],
security: 'is_granted("ROLE_ADMIN") && object.getScore() === null',
security: '(is_granted("ROLE_ADMIN") || is_granted("ROLE_GAME")) && object.getScore() === null',
validationContext: ['groups' => ['putValidation']],
processor: GamePutProcessor::class
),
Expand Down
2 changes: 1 addition & 1 deletion api/src/Entity/Lot.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
operations: [
new GetCollection(),
new Post(),
new Get(),
new Get(security: 'is_granted("ROLE_ADMIN") || is_granted("ROLE_GAME")'),
new Put(),
new Delete(validationContext: ['groups' => ['deleteValidation']], processor: LotProcessor::class),
],
Expand Down
2 changes: 1 addition & 1 deletion api/src/Entity/MediaObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
validationContext: ['groups' => ['Default', 'media_object_create']],
deserialize: false,
),
new Get(),
new Get(security: 'is_granted("ROLE_ADMIN") || is_granted("ROLE_GAME")'),
new Put(denormalizationContext: ['groups' => ['put']]),
new Delete(),
],
Expand Down
2 changes: 1 addition & 1 deletion api/src/Entity/Player.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#[ApiResource(
operations: [
new GetCollection(),
new Get(),
new Get(security: 'is_granted("ROLE_ADMIN") || is_granted("ROLE_GAME")'),
],
mercure: ['private' => true],
order: ['lastPlayDate' => 'DESC', 'name' => 'ASC'],
Expand Down
2 changes: 1 addition & 1 deletion api/src/Entity/TwitterAccountToFollow.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#[UniqueEntity('username')]
#[ApiResource(
operations: [
new GetCollection(),
new GetCollection(security: 'is_granted("ROLE_ADMIN") || is_granted("ROLE_GAME")'),
new Post(validationContext: ['groups' => new GroupSequence(['firstPostValidation', 'secondPostValidation'])], processor: TwitterAccountToFollowProcessor::class),
new Get(),
new Put(denormalizationContext: ['groups' => ['put']], processor: TwitterAccountToFollowProcessor::class),
Expand Down
2 changes: 1 addition & 1 deletion api/src/Entity/TwitterHashtag.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#[UniqueEntity('hashtag')]
#[ApiResource(
operations: [
new GetCollection(),
new GetCollection(security: 'is_granted("ROLE_ADMIN") || is_granted("ROLE_GAME")'),
new Post(),
new Get(),
new Put(denormalizationContext: ['groups' => ['put']]),
Expand Down
14 changes: 7 additions & 7 deletions api/tests/Security/LoginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ final class LoginTest extends ApiTestCase
*/
public function testLogin(): void
{
static::createClient()->request('POST', '/login', ['json' => [
'username' => $_ENV['USER_IN_MEMORY_USERNAME'],
'password' => $_ENV['USER_IN_MEMORY_PASSWORD'],
self::createClient()->request('POST', '/login', ['json' => [
'username' => $_ENV['USER_ADMIN_IN_MEMORY_USERNAME'],
'password' => $_ENV['USER_ADMIN_IN_MEMORY_PASSWORD'],
]]);

self::assertResponseIsSuccessful();
Expand All @@ -32,7 +32,7 @@ public function testLogin(): void
*/
public function testLoginFail(): void
{
static::createClient()->request('POST', '/login', ['json' => [
self::createClient()->request('POST', '/login', ['json' => [
'username' => 'invalid user',
'password' => 'invalid password',
]]);
Expand All @@ -50,9 +50,9 @@ public function testLoginFail(): void
*/
public static function getLoginToken(): string
{
$token = static::createClient()->request('POST', '/login', ['json' => [
'username' => $_ENV['USER_IN_MEMORY_USERNAME'],
'password' => $_ENV['USER_IN_MEMORY_PASSWORD'],
$token = self::createClient()->request('POST', '/login', ['json' => [
'username' => $_ENV['USER_ADMIN_IN_MEMORY_USERNAME'],
'password' => $_ENV['USER_ADMIN_IN_MEMORY_PASSWORD'],
]]);

self::assertResponseIsSuccessful();
Expand Down

0 comments on commit aefed84

Please sign in to comment.