| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- <?php
- declare(strict_types=1);
- namespace App\Controllers;
- use App\ApiClient\AdminClient;
- use App\ApiClient\ApiException;
- use App\ApiClient\ApiNotFoundException;
- use App\Auth\SessionManager;
- use Psr\Http\Message\ResponseInterface;
- use Psr\Http\Message\ServerRequestInterface;
- use Slim\Views\Twig;
- /**
- * `/app/reporters` — list, edit, create, soft-delete.
- *
- * RBAC: list/show ⇒ Viewer; write ⇒ Admin.
- */
- final class ReportersController
- {
- use CrudControllerSupport;
- public function __construct(
- private readonly Twig $twigEngine,
- private readonly SessionManager $sessionManager,
- private readonly AdminClient $admin,
- ) {
- }
- protected function twig(): Twig
- {
- return $this->twigEngine;
- }
- protected function sessions(): SessionManager
- {
- return $this->sessionManager;
- }
- public function index(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
- {
- $redirect = $this->requireUser($request, $response);
- if ($redirect !== null) {
- return $redirect;
- }
- $user = $this->sessionManager->getUser();
- \assert($user !== null);
- try {
- $list = $this->admin->listReporters($user->userId);
- } catch (ApiException $e) {
- $list = ['data' => [], 'total' => 0];
- $this->flashFromException($e);
- }
- return $this->twigEngine->render($response, 'pages/reporters/index.twig', [
- 'active_section' => 'reporters',
- 'list' => $list,
- 'can_write' => $this->userIs($user, 'admin'),
- ]);
- }
- /**
- * @param array{id: string} $args
- */
- public function edit(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
- {
- $redirect = $this->requireUser($request, $response);
- if ($redirect !== null) {
- return $redirect;
- }
- $user = $this->sessionManager->getUser();
- \assert($user !== null);
- $id = $this->parseId($args['id']);
- if ($id === null) {
- return $this->twigEngine->render($response->withStatus(404), 'pages/error.twig', [
- 'status' => 404, 'is_client_error' => true, 'message' => 'Reporter not found',
- ]);
- }
- try {
- $reporter = $this->admin->getReporter($user->userId, $id);
- } catch (ApiNotFoundException) {
- return $this->twigEngine->render($response->withStatus(404), 'pages/error.twig', [
- 'status' => 404, 'is_client_error' => true, 'message' => 'Reporter not found',
- ]);
- } catch (ApiException $e) {
- $this->flashFromException($e);
- return $response->withStatus(303)->withHeader('Location', '/app/reporters');
- }
- return $this->twigEngine->render($response, 'pages/reporters/edit.twig', [
- 'active_section' => 'reporters',
- 'reporter' => $reporter,
- 'can_write' => $this->userIs($user, 'admin'),
- ]);
- }
- public function create(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
- {
- $redirect = $this->requireUser($request, $response);
- if ($redirect !== null) {
- return $redirect;
- }
- $user = $this->sessionManager->getUser();
- \assert($user !== null);
- $body = $this->formBody($request);
- $payload = ['name' => isset($body['name']) && is_string($body['name']) ? trim($body['name']) : ''];
- if (isset($body['description']) && is_string($body['description'])) {
- $payload['description'] = trim($body['description']) === '' ? null : trim($body['description']);
- }
- if (isset($body['trust_weight']) && is_numeric($body['trust_weight'])) {
- $payload['trust_weight'] = (float) $body['trust_weight'];
- }
- try {
- $created = $this->admin->createReporter($user->userId, $payload);
- $this->sessionManager->flash('success', 'Reporter created.');
- $newId = (int) ($created['id'] ?? 0);
- return $response->withStatus(303)->withHeader('Location', $newId > 0 ? '/app/reporters/' . $newId : '/app/reporters');
- } catch (ApiException $e) {
- $this->flashFromException($e);
- }
- return $response->withStatus(303)->withHeader('Location', '/app/reporters');
- }
- /**
- * @param array{id: string} $args
- */
- public function update(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
- {
- $redirect = $this->requireUser($request, $response);
- if ($redirect !== null) {
- return $redirect;
- }
- $user = $this->sessionManager->getUser();
- \assert($user !== null);
- $id = $this->parseId($args['id']);
- if ($id === null) {
- return $response->withStatus(303)->withHeader('Location', '/app/reporters');
- }
- $body = $this->formBody($request);
- $payload = [];
- if (isset($body['name']) && is_string($body['name'])) {
- $payload['name'] = trim($body['name']);
- }
- if (isset($body['description']) && is_string($body['description'])) {
- $payload['description'] = trim($body['description']) === '' ? null : trim($body['description']);
- }
- if (isset($body['trust_weight']) && is_numeric($body['trust_weight'])) {
- $payload['trust_weight'] = (float) $body['trust_weight'];
- }
- if (array_key_exists('is_active', $body)) {
- $payload['is_active'] = $this->formBool($body['is_active']);
- }
- try {
- $this->admin->updateReporter($user->userId, $id, $payload);
- $this->sessionManager->flash('success', 'Reporter saved.');
- } catch (ApiException $e) {
- $this->flashFromException($e);
- }
- return $response->withStatus(303)->withHeader('Location', '/app/reporters/' . $id);
- }
- /**
- * @param array{id: string} $args
- */
- public function delete(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
- {
- $redirect = $this->requireUser($request, $response);
- if ($redirect !== null) {
- return $redirect;
- }
- $user = $this->sessionManager->getUser();
- \assert($user !== null);
- $id = $this->parseId($args['id']);
- if ($id === null) {
- return $response->withStatus(303)->withHeader('Location', '/app/reporters');
- }
- try {
- $this->admin->deleteReporter($user->userId, $id);
- $this->sessionManager->flash('success', 'Reporter deactivated.');
- } catch (ApiException $e) {
- $this->flashFromException($e);
- }
- return $response->withStatus(303)->withHeader('Location', '/app/reporters');
- }
- }
|