Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the bookmark API and add tests for it #1425

Merged
merged 7 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions config/mbin_routes/bookmark_api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ api_subject_remove_bookmarks:
path: /api/rbo/{subject_id}/{subject_type}
requirements:
subject_type: "%default_subject_type_options%"
methods: [ PUT ]
methods: [ DELETE ]
format: json

api_subject_remove_bookmark_from_list:
controller: App\Controller\Api\Bookmark\BookmarkApiController::subjectRemoveBookmarkFromList
path: /api/rbol/{subject_id}/{subject_type}/{list_name}
requirements:
subject_type: "%default_subject_type_options%"
methods: [ PUT ]
methods: [ DELETE ]
format: json
14 changes: 7 additions & 7 deletions config/packages/league_oauth2_server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,17 @@ league_oauth2_server:
"post_comment:delete",
"post_comment:vote",
"post_comment:report",
"bookmark",
"bookmark:add",
"bookmark:remove",
"bookmark_list",
"bookmark_list:read",
"bookmark_list:edit",
"bookmark_list:delete",
"user",
"user:profile",
"user:profile:read",
"user:profile:edit",
"user:bookmark",
"user:bookmark:add",
"user:bookmark:remove",
"user:bookmark:list",
"user:bookmark:list:read",
"user:bookmark:list:edit",
"user:bookmark:list:delete",
"user:message",
"user:message:read",
"user:message:create",
Expand Down
26 changes: 15 additions & 11 deletions config/packages/security.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,17 @@ security:
'ROLE_OAUTH2_POST:EDIT',
'ROLE_OAUTH2_POST_COMMENT:CREATE',
'ROLE_OAUTH2_POST_COMMENT:EDIT',
'ROLE_OAUTH2_BOOKMARK:ADD',
'ROLE_OAUTH2_BOOKMARK:REMOVE',
'ROLE_OAUTH2_BOOKMARK_LIST:EDIT',
]
ROLE_OAUTH2_DELETE:
[
'ROLE_OAUTH2_ENTRY:DELETE',
'ROLE_OAUTH2_ENTRY_COMMENT:DELETE',
'ROLE_OAUTH2_POST:DELETE',
'ROLE_OAUTH2_POST_COMMENT:DELETE',
'ROLE_OAUTH2_BOOKMARK_LIST:DELETE',
]
ROLE_OAUTH2_REPORT:
[
Expand All @@ -165,6 +169,17 @@ security:
'ROLE_OAUTH2_MAGAZINE:SUBSCRIBE',
'ROLE_OAUTH2_USER:FOLLOW',
]
'ROLE_OAUTH2_BOOKMARK':
[
'ROLE_OAUTH2_BOOKMARK:ADD',
'ROLE_OAUTH2_BOOKMARK:REMOVE',
]
'ROLE_OAUTH2_BOOKMARK_LIST':
[
'ROLE_OAUTH2_BOOKMARK_LIST:READ',
'ROLE_OAUTH2_BOOKMARK_LIST:EDIT',
'ROLE_OAUTH2_BOOKMARK_LIST:DELETE',
]
ROLE_OAUTH2_BLOCK:
[
'ROLE_OAUTH2_DOMAIN:BLOCK',
Expand Down Expand Up @@ -230,17 +245,6 @@ security:
'ROLE_OAUTH2_USER:OAUTH_CLIENTS:READ',
'ROLE_OAUTH2_USER:OAUTH_CLIENTS:EDIT',
]
'ROLE_OAUTH2_USER:BOOKMARK':
[
'ROLE_OAUTH2_USER:BOOKMARK:ADD',
'ROLE_OAUTH2_USER:BOOKMARK:REMOVE',
]
'ROLE_OAUTH2_USER:BOOKMARK_LIST':
[
'ROLE_OAUTH2_USER:BOOKMARK_LIST:READ',
'ROLE_OAUTH2_USER:BOOKMARK_LIST:EDIT',
'ROLE_OAUTH2_USER:BOOKMARK_LIST:DELETE',
]
'ROLE_OAUTH2_MODERATE':
[
'ROLE_OAUTH2_MODERATE:ENTRY',
Expand Down
53 changes: 33 additions & 20 deletions src/Controller/Api/Bookmark/BookmarkApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Controller\Api\Bookmark;

use App\Controller\Api\BaseApi;
use App\DTO\BookmarksDto;
use App\Schema\Errors\NotFoundErrorSchema;
use App\Schema\Errors\TooManyRequestsErrorSchema;
use App\Schema\Errors\UnauthorizedErrorSchema;
Expand All @@ -27,7 +28,7 @@ class BookmarkApiController extends BaseApi
new OA\Header(header: 'X-RateLimit-Retry-After', description: 'Unix timestamp to retry the request after', schema: new OA\Schema(type: 'integer')),
new OA\Header(header: 'X-RateLimit-Limit', description: 'Number of requests available', schema: new OA\Schema(type: 'integer')),
],
content: null
content: new Model(type: BookmarksDto::class)
)]
#[OA\Response(
response: 401,
Expand Down Expand Up @@ -61,9 +62,9 @@ class BookmarkApiController extends BaseApi
in: 'path',
schema: new OA\Schema(type: 'string', enum: ['entry', 'entry_comment', 'post', 'post_comment'])
)]
#[OA\Tag(name: 'bookmark:list')]
#[Security(name: 'oauth2', scopes: ['user:bookmark:add'])]
#[IsGranted('ROLE_OAUTH2_USER:BOOKMARK:ADD')]
#[OA\Tag(name: 'bookmark')]
#[Security(name: 'oauth2', scopes: ['bookmark:add'])]
#[IsGranted('ROLE_OAUTH2_BOOKMARK:ADD')]
public function subjectBookmarkStandard(int $subject_id, string $subject_type, RateLimiterFactory $apiUpdateLimiter): JsonResponse
{
$user = $this->getUserOrThrow();
Expand All @@ -75,7 +76,10 @@ public function subjectBookmarkStandard(int $subject_id, string $subject_type, R
}
$this->bookmarkManager->addBookmarkToDefaultList($user, $subject);

return new JsonResponse(status: 200, headers: $headers);
$dto = new BookmarksDto();
$dto->bookmarks = $this->bookmarkListRepository->getBookmarksOfContentInterface($subject);

return new JsonResponse($dto, status: 200, headers: $headers);
}

#[OA\Response(
Expand All @@ -86,7 +90,7 @@ public function subjectBookmarkStandard(int $subject_id, string $subject_type, R
new OA\Header(header: 'X-RateLimit-Retry-After', description: 'Unix timestamp to retry the request after', schema: new OA\Schema(type: 'integer')),
new OA\Header(header: 'X-RateLimit-Limit', description: 'Number of requests available', schema: new OA\Schema(type: 'integer')),
],
content: null
content: new Model(type: BookmarksDto::class)
)]
#[OA\Response(
response: 401,
Expand Down Expand Up @@ -120,9 +124,9 @@ public function subjectBookmarkStandard(int $subject_id, string $subject_type, R
in: 'path',
schema: new OA\Schema(type: 'string', enum: ['entry', 'entry_comment', 'post', 'post_comment'])
)]
#[OA\Tag(name: 'bookmark:list')]
#[Security(name: 'oauth2', scopes: ['user:bookmark:add'])]
#[IsGranted('ROLE_OAUTH2_USER:BOOKMARK:ADD')]
#[OA\Tag(name: 'bookmark')]
#[Security(name: 'oauth2', scopes: ['bookmark:add'])]
#[IsGranted('ROLE_OAUTH2_BOOKMARK:ADD')]
public function subjectBookmarkToList(string $list_name, int $subject_id, string $subject_type, RateLimiterFactory $apiUpdateLimiter): JsonResponse
{
$user = $this->getUserOrThrow();
Expand All @@ -138,7 +142,10 @@ public function subjectBookmarkToList(string $list_name, int $subject_id, string
}
$this->bookmarkManager->addBookmark($user, $list, $subject);

return new JsonResponse(status: 200, headers: $headers);
$dto = new BookmarksDto();
$dto->bookmarks = $this->bookmarkListRepository->getBookmarksOfContentInterface($subject);

return new JsonResponse($dto, status: 200, headers: $headers);
}

#[OA\Response(
Expand All @@ -149,7 +156,7 @@ public function subjectBookmarkToList(string $list_name, int $subject_id, string
new OA\Header(header: 'X-RateLimit-Retry-After', description: 'Unix timestamp to retry the request after', schema: new OA\Schema(type: 'integer')),
new OA\Header(header: 'X-RateLimit-Limit', description: 'Number of requests available', schema: new OA\Schema(type: 'integer')),
],
content: null
content: new Model(type: BookmarksDto::class)
)]
#[OA\Response(
response: 401,
Expand Down Expand Up @@ -183,9 +190,9 @@ public function subjectBookmarkToList(string $list_name, int $subject_id, string
in: 'path',
schema: new OA\Schema(type: 'string', enum: ['entry', 'entry_comment', 'post', 'post_comment'])
)]
#[OA\Tag(name: 'bookmark:list')]
#[Security(name: 'oauth2', scopes: ['user:bookmark:remove'])]
#[IsGranted('ROLE_OAUTH2_USER:BOOKMARK:REMOVE')]
#[OA\Tag(name: 'bookmark')]
#[Security(name: 'oauth2', scopes: ['bookmark:remove'])]
#[IsGranted('ROLE_OAUTH2_BOOKMARK:REMOVE')]
public function subjectRemoveBookmarkFromList(string $list_name, int $subject_id, string $subject_type, RateLimiterFactory $apiUpdateLimiter): JsonResponse
{
$user = $this->getUserOrThrow();
Expand All @@ -201,7 +208,10 @@ public function subjectRemoveBookmarkFromList(string $list_name, int $subject_id
}
$this->bookmarkRepository->removeBookmarkFromList($user, $list, $subject);

return new JsonResponse(status: 200, headers: $headers);
$dto = new BookmarksDto();
$dto->bookmarks = $this->bookmarkListRepository->getBookmarksOfContentInterface($subject);

return new JsonResponse($dto, status: 200, headers: $headers);
}

#[OA\Response(
Expand All @@ -212,7 +222,7 @@ public function subjectRemoveBookmarkFromList(string $list_name, int $subject_id
new OA\Header(header: 'X-RateLimit-Retry-After', description: 'Unix timestamp to retry the request after', schema: new OA\Schema(type: 'integer')),
new OA\Header(header: 'X-RateLimit-Limit', description: 'Number of requests available', schema: new OA\Schema(type: 'integer')),
],
content: null
content: new Model(type: BookmarksDto::class)
)]
#[OA\Response(
response: 401,
Expand Down Expand Up @@ -246,9 +256,9 @@ public function subjectRemoveBookmarkFromList(string $list_name, int $subject_id
in: 'path',
schema: new OA\Schema(type: 'string', enum: ['entry', 'entry_comment', 'post', 'post_comment'])
)]
#[OA\Tag(name: 'bookmark:list')]
#[Security(name: 'oauth2', scopes: ['user:bookmark:remove'])]
#[IsGranted('ROLE_OAUTH2_USER:BOOKMARK:REMOVE')]
#[OA\Tag(name: 'bookmark')]
#[Security(name: 'oauth2', scopes: ['bookmark:remove'])]
#[IsGranted('ROLE_OAUTH2_BOOKMARK:REMOVE')]
public function subjectRemoveBookmarks(int $subject_id, string $subject_type, RateLimiterFactory $apiUpdateLimiter): JsonResponse
{
$user = $this->getUserOrThrow();
Expand All @@ -260,6 +270,9 @@ public function subjectRemoveBookmarks(int $subject_id, string $subject_type, Ra
}
$this->bookmarkRepository->removeAllBookmarksForContent($user, $subject);

return new JsonResponse(status: 200, headers: $headers);
$dto = new BookmarksDto();
$dto->bookmarks = $this->bookmarkListRepository->getBookmarksOfContentInterface($subject);

return new JsonResponse($dto, status: 200, headers: $headers);
}
}
46 changes: 21 additions & 25 deletions src/Controller/Api/Bookmark/BookmarkListApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ enum: [...Entry::ENTRY_TYPE_OPTIONS, 'all']
in: 'query',
schema: new OA\Schema(type: 'integer', default: EntryRepository::PER_PAGE, maximum: self::MAX_PER_PAGE, minimum: self::MIN_PER_PAGE)
)]
#[OA\Tag(name: 'bookmark:list')]
#[Security(name: 'oauth2', scopes: ['user:bookmark:list:read'])]
#[IsGranted('ROLE_OAUTH2_USER:BOOKMARK_LIST:READ')]
#[OA\Tag(name: 'bookmark_list')]
#[Security(name: 'oauth2', scopes: ['bookmark_list:read'])]
#[IsGranted('ROLE_OAUTH2_BOOKMARK_LIST:READ')]
public function front(
#[MapQueryParameter] ?int $list_id,
#[MapQueryParameter] ?string $sort,
Expand Down Expand Up @@ -204,9 +204,9 @@ public function front(
],
content: new OA\JsonContent(ref: new Model(type: TooManyRequestsErrorSchema::class))
)]
#[OA\Tag(name: 'bookmark:list')]
#[Security(name: 'oauth2', scopes: ['user:bookmark:list:read'])]
#[IsGranted('ROLE_OAUTH2_USER:BOOKMARK_LIST:READ')]
#[OA\Tag(name: 'bookmark_list')]
#[Security(name: 'oauth2', scopes: ['bookmark_list:read'])]
#[IsGranted('ROLE_OAUTH2_BOOKMARK_LIST:READ')]
public function list(RateLimiterFactory $apiReadLimiter): JsonResponse
{
$user = $this->getUserOrThrow();
Expand All @@ -227,7 +227,7 @@ public function list(RateLimiterFactory $apiReadLimiter): JsonResponse
new OA\Header(header: 'X-RateLimit-Retry-After', description: 'Unix timestamp to retry the request after', schema: new OA\Schema(type: 'integer')),
new OA\Header(header: 'X-RateLimit-Limit', description: 'Number of requests available', schema: new OA\Schema(type: 'integer')),
],
content: null
content: new Model(type: BookmarkListDto::class),
)]
#[OA\Response(
response: 401,
Expand Down Expand Up @@ -255,9 +255,9 @@ public function list(RateLimiterFactory $apiReadLimiter): JsonResponse
in: 'path',
schema: new OA\Schema(type: 'string')
)]
#[OA\Tag(name: 'bookmark:list')]
#[Security(name: 'oauth2', scopes: ['user:bookmark:list:edit'])]
#[IsGranted('ROLE_OAUTH2_USER:BOOKMARK_LIST:EDIT')]
#[OA\Tag(name: 'bookmark_list')]
#[Security(name: 'oauth2', scopes: ['bookmark_list:edit'])]
#[IsGranted('ROLE_OAUTH2_BOOKMARK_LIST:EDIT')]
public function makeDefault(string $list_name, RateLimiterFactory $apiUpdateLimiter): JsonResponse
{
$user = $this->getUserOrThrow();
Expand All @@ -268,7 +268,7 @@ public function makeDefault(string $list_name, RateLimiterFactory $apiUpdateLimi
}
$this->bookmarkListRepository->makeListDefault($user, $list);

return new JsonResponse(status: 200, headers: $headers);
return new JsonResponse(BookmarkListDto::fromList($list), status: 200, headers: $headers);
}

#[OA\Response(
Expand Down Expand Up @@ -307,13 +307,10 @@ public function makeDefault(string $list_name, RateLimiterFactory $apiUpdateLimi
in: 'path',
schema: new OA\Schema(type: 'string')
)]
#[OA\RequestBody(content: new Model(
type: BookmarkListDto::class,
groups: ['common']
))]
#[OA\Tag(name: 'bookmark:list')]
#[Security(name: 'oauth2', scopes: ['user:bookmark:list:edit'])]
#[IsGranted('ROLE_OAUTH2_USER:BOOKMARK_LIST:EDIT')]
#[OA\RequestBody(content: new Model(type: BookmarkListDto::class))]
#[OA\Tag(name: 'bookmark_list')]
#[Security(name: 'oauth2', scopes: ['bookmark_list:edit'])]
#[IsGranted('ROLE_OAUTH2_BOOKMARK_LIST:EDIT')]
public function editList(string $list_name, #[MapRequestPayload] BookmarkListDto $dto, RateLimiterFactory $apiUpdateLimiter): JsonResponse
{
$user = $this->getUserOrThrow();
Expand All @@ -323,7 +320,6 @@ public function editList(string $list_name, #[MapRequestPayload] BookmarkListDto
throw new NotFoundHttpException(headers: $headers);
}
$this->bookmarkListRepository->editList($user, $list, $dto);
$list = $this->bookmarkListRepository->findOneBy(['id' => $list->getId()]);

return new JsonResponse(BookmarkListDto::fromList($list), status: 200, headers: $headers);
}
Expand Down Expand Up @@ -364,9 +360,9 @@ public function editList(string $list_name, #[MapRequestPayload] BookmarkListDto
in: 'path',
schema: new OA\Schema(type: 'string')
)]
#[OA\Tag(name: 'bookmark:list')]
#[Security(name: 'oauth2', scopes: ['user:bookmark:list:edit'])]
#[IsGranted('ROLE_OAUTH2_USER:BOOKMARK_LIST:EDIT')]
#[OA\Tag(name: 'bookmark_list')]
#[Security(name: 'oauth2', scopes: ['bookmark_list:edit'])]
#[IsGranted('ROLE_OAUTH2_BOOKMARK_LIST:EDIT')]
public function createList(string $list_name, RateLimiterFactory $apiUpdateLimiter): JsonResponse
{
$user = $this->getUserOrThrow();
Expand Down Expand Up @@ -416,9 +412,9 @@ public function createList(string $list_name, RateLimiterFactory $apiUpdateLimit
in: 'path',
schema: new OA\Schema(type: 'string')
)]
#[OA\Tag(name: 'bookmark:list')]
#[Security(name: 'oauth2', scopes: ['user:bookmark:list:delete'])]
#[IsGranted('ROLE_OAUTH2_USER:BOOKMARK_LIST:DELETE')]
#[OA\Tag(name: 'bookmark_list')]
#[Security(name: 'oauth2', scopes: ['bookmark_list:delete'])]
#[IsGranted('ROLE_OAUTH2_BOOKMARK_LIST:DELETE')]
public function deleteList(string $list_name, RateLimiterFactory $apiDeleteLimiter): JsonResponse
{
$user = $this->getUserOrThrow();
Expand Down
13 changes: 13 additions & 0 deletions src/DTO/BookmarksDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace App\DTO;

class BookmarksDto
{
/**
* @var string[]|null
*/
public ?array $bookmarks = null;
}
Loading
Loading