Skip to content

Commit

Permalink
Adding endpoint to edit ref. solution note (description).
Browse files Browse the repository at this point in the history
  • Loading branch information
krulis-martin committed Jun 12, 2024
1 parent ebd0bf9 commit 2a945fd
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 0 deletions.
27 changes: 27 additions & 0 deletions app/V1Module/presenters/ReferenceExerciseSolutionsPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,33 @@ public function actionDetail(string $solutionId)
$this->sendSuccessResponse($this->referenceSolutionViewFactory->getReferenceSolution($solution));
}

public function checkUpdate(string $solutionId)
{
$solution = $this->referenceSolutions->findOrThrow($solutionId);
if (!$this->referenceSolutionAcl->canUpdate($solution)) {
throw new ForbiddenRequestException("You cannot update the ref. solution");
}
}

/**
* Update details about the ref. solution (note, etc...)
* @POST
* @Param(type="post", name="note", validation="string:0..65535",
* description="A description by the author of the solution")
* @param string $solutionId Identifier of the solution
* @throws NotFoundException
* @throws InternalServerException
*/
public function actionUpdate(string $solutionId)
{
$req = $this->getRequest();
$solution = $this->referenceSolutions->findOrThrow($solutionId);
$solution->setDescription($req->getPost("note"));

$this->referenceSolutions->flush();
$this->sendSuccessResponse($this->referenceSolutionViewFactory->getReferenceSolution($solution));
}

public function checkDeleteReferenceSolution(string $solutionId)
{
$solution = $this->referenceSolutions->findOrThrow($solutionId);
Expand Down
1 change: 1 addition & 0 deletions app/V1Module/router/RouterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ private static function createReferenceSolutionsRoutes(string $prefix): RouteLis
);

$router[] = new GetRoute("$prefix/<solutionId>", "ReferenceExerciseSolutions:detail");
$router[] = new PostRoute("$prefix/<solutionId>", "ReferenceExerciseSolutions:update");
$router[] = new DeleteRoute("$prefix/<solutionId>", "ReferenceExerciseSolutions:deleteReferenceSolution");
$router[] = new PostRoute("$prefix/<id>/resubmit", "ReferenceExerciseSolutions:resubmit");
$router[] = new GetRoute("$prefix/<solutionId>/submissions", "ReferenceExerciseSolutions:submissions");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ interface IReferenceExerciseSolutionPermissions
{
public function canViewDetail(ReferenceExerciseSolution $referenceExerciseSolution): bool;

public function canUpdate(ReferenceExerciseSolution $referenceExerciseSolution): bool;

public function canDelete(ReferenceExerciseSolution $referenceExerciseSolution): bool;

public function canEvaluate(ReferenceExerciseSolution $referenceExerciseSolution): bool;
Expand Down
15 changes: 15 additions & 0 deletions app/config/permissions.neon
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,7 @@ permissions:
resource: referenceExerciseSolution
actions:
- viewDetail
- update
- promote

- allow: true
Expand Down Expand Up @@ -918,6 +919,20 @@ permissions:
- referenceExerciseSolution.isExerciseSuperGroupAdmin
- referenceExerciseSolution.isExerciseSubGroupNonStudentMember

- allow: true
role: supervisor-student
resource: referenceExerciseSolution
actions:
- update
conditions:
- or:
- referenceExerciseSolution.isAuthor
- and:
- referenceExerciseSolution.isPublic
- or:
- referenceExerciseSolution.isExerciseAuthorOrAdmin
- referenceExerciseSolution.isExerciseSuperGroupAdmin

- allow: true
role: supervisor-student
resource: referenceExerciseSolution
Expand Down
5 changes: 5 additions & 0 deletions app/model/entity/ReferenceExerciseSolution.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ public function getDescription(): string
return $this->description;
}

public function setDescription(string $desccription): void
{
$this->description = $desccription;
}

public function getSolution(): Solution
{
return $this->solution;
Expand Down
48 changes: 48 additions & 0 deletions tests/Presenters/ReferenceExerciseSolutionsPresenter.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,54 @@ class TestReferenceExerciseSolutionsPresenter extends Tester\TestCase
Assert::equal(ReferenceExerciseSolution::VISIBILITY_PROMOTED, $payload['visibility']);
Assert::equal(ReferenceExerciseSolution::VISIBILITY_PROMOTED, $solution->getVisibility());
}

public function testUpdateDesription()
{
PresenterTestHelper::login($this->container, PresenterTestHelper::ANOTHER_SUPERVISOR_LOGIN);
$solution = current(array_filter($this->referenceSolutions->findAll(), function ($rs) {
// get solutions of logged-in user
return $rs->getSolution()->getAuthor()->getEmail() === PresenterTestHelper::ANOTHER_SUPERVISOR_LOGIN;
}));
Assert::truthy($solution);

$newNote = 'new-description';

$payload = PresenterTestHelper::performPresenterRequest(
$this->presenter,
'V1:ReferenceExerciseSolutions',
'POST',
[ 'action' => 'update', 'solutionId' => $solution->getId() ],
[ 'note' => $newNote ]
);

Assert::equal($newNote, $payload['description']);
$this->referenceSolutions->refresh($solution);
Assert::equal($newNote, $solution->getDescription());
}

public function testUpdateDesriptionUnauthorized()
{
PresenterTestHelper::login($this->container, PresenterTestHelper::ANOTHER_SUPERVISOR_LOGIN);
$solution = current(array_filter($this->referenceSolutions->findAll(), function ($rs) {
// get solutions the do not belong to the logged-in user
return $rs->getSolution()->getAuthor()->getEmail() !== PresenterTestHelper::ANOTHER_SUPERVISOR_LOGIN;
}));
Assert::truthy($solution);

// another supervisor is not the author, nor admin of the exercise/group...
Assert::exception(
function () use ($solution) {
$payload = PresenterTestHelper::performPresenterRequest(
$this->presenter,
'V1:ReferenceExerciseSolutions',
'POST',
[ 'action' => 'update', 'solutionId' => $solution->getId() ],
[ 'note' => 'new-description' ]
);
},
ForbiddenRequestException::class
);
}
}

$testCase = new TestReferenceExerciseSolutionsPresenter();
Expand Down

0 comments on commit 2a945fd

Please sign in to comment.