Skip to content
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
14 changes: 14 additions & 0 deletions app/helpers/Recodex/RecodexApiHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -503,4 +503,18 @@ public function createTermGroup(string $instanceId, string $parentGroupId, strin

return null;
}

/**
* Set or unset archived flag of a group.
* @param string $groupId ReCodEx ID of a group which is being updated
* @param bool $archived true to set archived flag, false to unset
*/
public function setGroupArchivedFlag(string $groupId, bool $archived): void
{
Debugger::log(
"ReCodEx::setGroupArchivedFlag('$groupId', " . ($archived ? 'true' : 'false') . ")",
Debugger::INFO
);
$this->post("groups/$groupId/archived", [], ['value' => $archived]);
}
}
32 changes: 32 additions & 0 deletions app/presenters/GroupsPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Exceptions\BadRequestException;
use App\Exceptions\ForbiddenRequestException;
use App\Exceptions\NotFoundException;
use App\Exceptions\RecodexApiException;
use App\Helpers\RecodexGroup;
use App\Model\Entity\SisScheduleEvent;
use App\Model\Repository\SisScheduleEvents;
Expand Down Expand Up @@ -401,4 +402,35 @@ public function actionRemoveAttribute(string $id)
$this->recodexApi->removeAttribute($id, $key, $value);
$this->sendSuccessResponse("OK");
}

public function checkSetArchived()
{
if (!$this->groupAcl->canSetArchived()) {
throw new ForbiddenRequestException("You do not have permissions to set archived flag.");
}
}

/**
* Proxy to ReCodEx that sets or unsets the 'archived' flag of a group.
* @POST
* @Param(type="query", name="id", validation="string:1..",
* description="ReCodEx ID of a group for which the archived flag will be set or unset.")
* @Param(type="post", name="value", validation="bool",
* description="Boolean value indicating whether the group should be archived or not.")
*/
public function actionSetArchived(string $id)
{
$archived = $this->getRequest()->getPost('value');
if (!is_bool($archived)) {
throw new BadRequestException("Missing or invalid 'value' parameter (expected boolean).");
}

try {
$this->recodexApi->setGroupArchivedFlag($id, $archived);
} catch (RecodexApiException $e) {
throw new BadRequestException("Failed to set archived flag for group $id: " . $e->getMessage());
}

$this->sendSuccessResponse("OK");
}
}
1 change: 1 addition & 0 deletions app/router/RouterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ private static function createGroupsRoutes(string $prefix): RouteList
$router[] = new PostRoute("$prefix/<id>/add-attribute", "Groups:addAttribute");
$router[] = new PostRoute("$prefix/<id>/remove-attribute", "Groups:removeAttribute");
$router[] = new PostRoute("$prefix/<parentId>/create-term/<term>", "Groups:createTerm");
$router[] = new PostRoute("$prefix/<id>/archived", "Groups:setArchived");
return $router;
}
}
2 changes: 2 additions & 0 deletions app/security/ACL/IGroupPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ public function canViewTeacher(): bool;
public function canEditRawAttributes(): bool;

public function canCreateTermGroup(): bool;

public function canSetArchived(): bool;
}
24 changes: 24 additions & 0 deletions tests/Presenters/GroupsPresenter.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,30 @@ class TestGroupsPresenter extends Tester\TestCase
);
}, BadRequestException::class);
}

public function testSetArchivedFlag()
{
PresenterTestHelper::loginDefaultAdmin($this->container);

$this->client->shouldReceive("post")->with('groups/g1/archived', Mockery::any())
->andReturn(new Response(200, ['Content-Type' => 'application/json'], json_encode([
'success' => true,
'code' => 200,
'payload' => [
self::group('g1', null, 'Root', true, ['foo' => ['bar', 'baz']]),
]
])));

$payload = PresenterTestHelper::performPresenterRequest(
$this->presenter,
'Groups',
'POST',
['action' => 'setArchived', 'id' => 'g1'],
['value' => true]
);

Assert::equal("OK", $payload);
}
}

Debugger::$logDirectory = __DIR__ . '/../../log';
Expand Down