Skip to content

Commit

Permalink
Merge pull request #3151 from nextcloud/ref/attributes
Browse files Browse the repository at this point in the history
use attributes instead of annotations
  • Loading branch information
dartcafe authored Nov 9, 2023
2 parents ad90a8f + ff5a17b commit 6be960d
Show file tree
Hide file tree
Showing 22 changed files with 240 additions and 203 deletions.
5 changes: 2 additions & 3 deletions lib/Controller/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use OCA\Polls\AppConstants;
use OCA\Polls\Service\PollService;
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\Collaboration\Resources\LoadAdditionalScriptsEvent;
Expand All @@ -46,9 +47,7 @@ public function __construct(
parent::__construct($appName, $request, $session);
}

/**
* @NoCSRFRequired
*/
#[NoCSRFRequired]
public function index(): TemplateResponse {
Util::addScript(AppConstants::APP_ID, 'polls-main');
$this->eventDispatcher->dispatchTyped(new LoadAdditionalScriptsEvent());
Expand Down
9 changes: 5 additions & 4 deletions lib/Controller/BaseApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use OCP\AppFramework\ApiController;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;

Expand All @@ -45,8 +46,8 @@ public function __construct(

/**
* response
* @NoAdminRequired
*/
#[NoAdminRequired]
protected function response(Closure $callback): JSONResponse {
try {
return new JSONResponse($callback(), Http::STATUS_OK);
Expand All @@ -57,8 +58,8 @@ protected function response(Closure $callback): JSONResponse {

/**
* response
* @NoAdminRequired
*/
#[NoAdminRequired]
protected function responseLong(Closure $callback): JSONResponse {
try {
return new JSONResponse($callback(), Http::STATUS_OK);
Expand All @@ -69,8 +70,8 @@ protected function responseLong(Closure $callback): JSONResponse {

/**
* responseCreate
* @NoAdminRequired
*/
#[NoAdminRequired]
protected function responseCreate(Closure $callback): JSONResponse {
try {
return new JSONResponse($callback(), Http::STATUS_CREATED);
Expand All @@ -81,8 +82,8 @@ protected function responseCreate(Closure $callback): JSONResponse {

/**
* responseDeleteTolerant
* @NoAdminRequired
*/
#[NoAdminRequired]
protected function responseDeleteTolerant(Closure $callback): JSONResponse {
try {
return new JSONResponse($callback(), Http::STATUS_OK);
Expand Down
9 changes: 5 additions & 4 deletions lib/Controller/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use OCP\AppFramework\Controller;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;
use OCP\ISession;
Expand All @@ -44,8 +45,8 @@ public function __construct(

/**
* response
* @NoAdminRequired
*/
#[NoAdminRequired]
protected function response(Closure $callback, string $token = ''): JSONResponse {
if ($token) {
$this->session->set('publicPollToken', $token);
Expand All @@ -60,8 +61,8 @@ protected function response(Closure $callback, string $token = ''): JSONResponse

/**
* response
* @NoAdminRequired
*/
#[NoAdminRequired]
protected function responseLong(Closure $callback, string $token = ''): JSONResponse {
if ($token) {
$this->session->set('publicPollToken', $token);
Expand All @@ -76,8 +77,8 @@ protected function responseLong(Closure $callback, string $token = ''): JSONResp

/**
* responseCreate
* @NoAdminRequired
*/
#[NoAdminRequired]
protected function responseCreate(Closure $callback, string $token = ''): JSONResponse {
if ($token) {
$this->session->set('publicPollToken', $token);
Expand All @@ -92,8 +93,8 @@ protected function responseCreate(Closure $callback, string $token = ''): JSONRe

/**
* responseDeleteTolerant
* @NoAdminRequired
*/
#[NoAdminRequired]
protected function responseDeleteTolerant(Closure $callback, string $token = ''): JSONResponse {
if ($token) {
$this->session->set('publicPollToken', $token);
Expand Down
21 changes: 12 additions & 9 deletions lib/Controller/CommentApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
use \OCA\Polls\Db\Comment;
use OCA\Polls\Model\Acl;
use OCA\Polls\Service\CommentService;
use OCP\AppFramework\Http\Attribute\CORS;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;

Expand All @@ -41,10 +44,10 @@ public function __construct(

/**
* Read all comments of a poll based on the poll id and return list as array
* @NoAdminRequired
* @CORS
* @NoCSRFRequired
*/
#[CORS]
#[NoAdminRequired]
#[NoCSRFRequired]
public function list(int $pollId): JSONResponse {
return $this->response(fn () => [
'comments' => $this->commentService->list($this->acl->setPollId($pollId))
Expand All @@ -53,10 +56,10 @@ public function list(int $pollId): JSONResponse {

/**
* Add comment
* @NoAdminRequired
* @CORS
* @NoCSRFRequired
*/
#[CORS]
#[NoAdminRequired]
#[NoCSRFRequired]
public function add(int $pollId, string $comment): JSONResponse {
return $this->response(fn () => [
'comment' => $this->commentService->add($comment, $this->acl->setPollId($pollId, Acl::PERMISSION_COMMENT_ADD))
Expand All @@ -65,10 +68,10 @@ public function add(int $pollId, string $comment): JSONResponse {

/**
* Delete comment
* @NoAdminRequired
* @CORS
* @NoCSRFRequired
*/
#[CORS]
#[NoAdminRequired]
#[NoCSRFRequired]
public function delete(int $commentId): JSONResponse {
return $this->responseDeleteTolerant(fn () => [
'comment' => $this->deleteComment($commentId)
Expand Down
7 changes: 4 additions & 3 deletions lib/Controller/CommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use OCA\Polls\Model\Acl;
use OCA\Polls\Service\CommentService;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;
use OCP\ISession;
Expand All @@ -42,8 +43,8 @@ public function __construct(

/**
* Write a new comment to the db and returns the new comment as array
* @NoAdminRequired
*/
#[NoAdminRequired]
public function list(int $pollId): JSONResponse {
return $this->response(fn () => [
'comments' => $this->commentService->list($this->acl->setPollId($pollId))
Expand All @@ -52,8 +53,8 @@ public function list(int $pollId): JSONResponse {

/**
* Write a new comment to the db and returns the new comment as array
* @NoAdminRequired
*/
#[NoAdminRequired]
public function add(int $pollId, string $message): JSONResponse {
return $this->response(fn () => [
'comment' => $this->commentService->add($message, $this->acl->setPollId($pollId, Acl::PERMISSION_COMMENT_ADD))
Expand All @@ -62,8 +63,8 @@ public function add(int $pollId, string $message): JSONResponse {

/**
* Delete Comment
* @NoAdminRequired
*/
#[NoAdminRequired]
public function delete(int $commentId): JSONResponse {
$comment = $this->commentService->get($commentId);

Expand Down
39 changes: 21 additions & 18 deletions lib/Controller/OptionApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
namespace OCA\Polls\Controller;

use OCA\Polls\Service\OptionService;
use OCP\AppFramework\Http\Attribute\CORS;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;

Expand All @@ -38,61 +41,61 @@ public function __construct(

/**
* Get all options of given poll
* @NoAdminRequired
* @CORS
* @NoCSRFRequired
*/
#[CORS]
#[NoAdminRequired]
#[NoCSRFRequired]
public function list(int $pollId): JSONResponse {
return $this->response(fn () => ['options' => $this->optionService->list($pollId)]);
}

/**
* Add a new option
* @NoAdminRequired
* @CORS
* @NoCSRFRequired
*/
#[CORS]
#[NoAdminRequired]
#[NoCSRFRequired]
public function add(int $pollId, int $timestamp = 0, string $pollOptionText = '', int $duration = 0): JSONResponse {
return $this->responseCreate(fn () => ['option' => $this->optionService->add($pollId, $timestamp, $pollOptionText, $duration)]);
}


/**
* Update option
* @NoAdminRequired
* @CORS
* @NoCSRFRequired
*/
#[CORS]
#[NoAdminRequired]
#[NoCSRFRequired]
public function update(int $optionId, int $timestamp = 0, string $pollOptionText = '', int $duration = 0): JSONResponse {
return $this->response(fn () => ['option' => $this->optionService->update($optionId, $timestamp, $pollOptionText, $duration)]);
}

/**
* Delete option
* @NoAdminRequired
* @CORS
* @NoCSRFRequired
*/
#[CORS]
#[NoAdminRequired]
#[NoCSRFRequired]
public function delete(int $optionId): JSONResponse {
return $this->responseDeleteTolerant(fn () => ['option' => $this->optionService->delete($optionId)]);
}

/**
* Switch option confirmation
* @NoAdminRequired
* @CORS
* @NoCSRFRequired
*/
#[CORS]
#[NoAdminRequired]
#[NoCSRFRequired]
public function confirm(int $optionId): JSONResponse {
return $this->response(fn () => ['option' => $this->optionService->confirm($optionId)]);
}

/**
* Set order position for option
* @NoAdminRequired
* @CORS
* @NoCSRFRequired
*/
#[CORS]
#[NoAdminRequired]
#[NoCSRFRequired]
public function setOrder(int $optionId, int $order): JSONResponse {
return $this->response(fn () => ['option' => $this->optionService->setOrder($optionId, $order)]);
}
Expand Down
21 changes: 11 additions & 10 deletions lib/Controller/OptionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use OCA\Polls\Service\CalendarService;
use OCA\Polls\Service\OptionService;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;
use OCP\ISession;
Expand All @@ -42,8 +43,8 @@ public function __construct(

/**
* Get all options of given poll
* @NoAdminRequired
*/
#[NoAdminRequired]
public function list(int $pollId): JSONResponse {
return $this->response(function () use ($pollId) {
return ['options' => $this->optionService->list($pollId)];
Expand All @@ -52,72 +53,72 @@ public function list(int $pollId): JSONResponse {

/**
* Add a new option
* @NoAdminRequired
*/
#[NoAdminRequired]
public function add(int $pollId, int $timestamp = 0, string $text = '', int $duration = 0): JSONResponse {
return $this->responseCreate(fn () => ['option' => $this->optionService->add($pollId, $timestamp, $text, $duration)]);
}

/**
* Add mulitple new option
* @NoAdminRequired
*/
#[NoAdminRequired]
public function addBulk(int $pollId, string $text = ''): JSONResponse {
return $this->responseCreate(fn () => ['options' => $this->optionService->addBulk($pollId, $text)]);
}

/**
* Update option
* @NoAdminRequired
*/
#[NoAdminRequired]
public function update(int $optionId, int $timestamp, string $text, int $duration): JSONResponse {
return $this->response(fn () => ['option' => $this->optionService->update($optionId, $timestamp, $text, $duration)]);
}

/**
* Delete option
* @NoAdminRequired
*/
#[NoAdminRequired]
public function delete(int $optionId): JSONResponse {
return $this->responseDeleteTolerant(fn () => ['option' => $this->optionService->delete($optionId)]);
}

/**
* Switch option confirmation
* @NoAdminRequired
*/
#[NoAdminRequired]
public function confirm(int $optionId): JSONResponse {
return $this->response(fn () => ['option' => $this->optionService->confirm($optionId)]);
}

/**
* Reorder options
* @NoAdminRequired
*/
#[NoAdminRequired]
public function reorder(int $pollId, array $options): JSONResponse {
return $this->response(fn () => ['options' => $this->optionService->reorder($pollId, $options)]);
}

/**
* Reorder options
* @NoAdminRequired
*/
#[NoAdminRequired]
public function sequence(int $optionId, int $step, string $unit, int $amount): JSONResponse {
return $this->response(fn () => ['options' => $this->optionService->sequence($optionId, $step, $unit, $amount)]);
}

/**
* Reorder options
* @NoAdminRequired
*/
#[NoAdminRequired]
public function shift(int $pollId, int $step, string $unit): JSONResponse {
return $this->response(fn () => ['options' => $this->optionService->shift($pollId, $step, $unit)]);
}

/**
* findCalendarEvents
* @NoAdminRequired
*/
#[NoAdminRequired]
public function findCalendarEvents(int $optionId, string $tz): JSONResponse {
return $this->response(fn () => ['events' => $this->calendarService->getEvents($optionId)]);
}
Expand Down
Loading

0 comments on commit 6be960d

Please sign in to comment.