From 8d258c677b25b11c307095d8f4e0d1844afa0f8b Mon Sep 17 00:00:00 2001 From: ghost Date: Mon, 9 Oct 2023 19:31:25 +0300 Subject: [PATCH] implement user moderation tools --- public/asset/default/css/framework.css | 4 + src/Controller/UserController.php | 179 +++++++++++++++++++++++++ src/Service/TorrentService.php | 63 +++++++++ src/Service/UserService.php | 45 +++++++ templates/default/user/info.html.twig | 96 +++++++++---- 5 files changed, 363 insertions(+), 24 deletions(-) diff --git a/public/asset/default/css/framework.css b/public/asset/default/css/framework.css index c1101f2..e459f05 100644 --- a/public/asset/default/css/framework.css +++ b/public/asset/default/css/framework.css @@ -140,6 +140,10 @@ a.label-green:hover { top: 2px; } +.line-height-20-px { + line-height: 20px; +} + .line-height-26-px { line-height: 26px; } diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index 2e8c815..da71b25 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -11,6 +11,8 @@ use Symfony\Component\HttpFoundation\Request; use App\Service\ActivityService; use App\Service\UserService; +use App\Service\PageService; +use App\Service\TorrentService; class UserController extends AbstractController { @@ -307,6 +309,183 @@ class UserController extends AbstractController ); } + #[Route( + '/{_locale}/user/{userId}/moderator/toggle', + name: 'user_moderator_toggle', + requirements: + [ + 'userId' => '\d+', + ], + methods: + [ + 'GET' + ] + )] + public function toggleModerator( + Request $request, + TranslatorInterface $translator, + UserService $userService + ): Response + { + // Init user + $user = $userService->init( + $request->getClientIp() + ); + + if (!$user->isModerator()) + { + // @TODO + throw new \Exception( + $translator->trans('Access denied') + ); + } + + // Init target user + if (!$userTarget = $userService->getUser($request->get('userId'))) + { + throw $this->createNotFoundException(); + } + + // Update + $userService->toggleUserModerator( + $userTarget->getId() + ); + + // Redirect to info page created + return $this->redirectToRoute( + 'user_info', + [ + '_locale' => $request->get('_locale'), + 'userId' => $userTarget->getId() + ] + ); + } + + #[Route( + '/{_locale}/user/{userId}/status/toggle', + name: 'user_status_toggle', + requirements: + [ + 'userId' => '\d+', + ], + methods: + [ + 'GET' + ] + )] + public function toggleStatus( + Request $request, + TranslatorInterface $translator, + UserService $userService + ): Response + { + // Init user + $user = $userService->init( + $request->getClientIp() + ); + + if (!$user->isModerator()) + { + // @TODO + throw new \Exception( + $translator->trans('Access denied') + ); + } + + // Init target user + if (!$userTarget = $userService->getUser($request->get('userId'))) + { + throw $this->createNotFoundException(); + } + + // Update + $userService->toggleUserStatus( + $userTarget->getId() + ); + + // Redirect to info page created + return $this->redirectToRoute( + 'user_info', + [ + '_locale' => $request->get('_locale'), + 'userId' => $userTarget->getId() + ] + ); + } + + #[Route( + '/{_locale}/user/{userId}/approved/toggle', + name: 'user_approved_toggle', + requirements: + [ + 'userId' => '\d+', + ], + methods: + [ + 'GET' + ] + )] + public function toggleApproved( + Request $request, + TranslatorInterface $translator, + UserService $userService, + PageService $pageService, + TorrentService $torrentService + ): Response + { + // Init user + $user = $userService->init( + $request->getClientIp() + ); + + if (!$user->isModerator()) + { + // @TODO + throw new \Exception( + $translator->trans('Access denied') + ); + } + + // Init target user + if (!$userTarget = $userService->getUser($request->get('userId'))) + { + throw $this->createNotFoundException(); + } + + // Auto-approve all related content on user approve + if (!$userTarget->isApproved()) + { + $torrentService->setTorrentsApprovedByUserId( + $userTarget->getId(), + true + ); + + $torrentService->setTorrentLocalesApprovedByUserId( + $userTarget->getId(), + true + ); + + $torrentService->setTorrentSensitivesApprovedByUserId( + $userTarget->getId(), + true + ); + } + + // Update user approved + $userService->toggleUserApproved( + $userTarget->getId() + ); + + // Redirect to info page created + return $this->redirectToRoute( + 'user_info', + [ + '_locale' => $request->get('_locale'), + 'userId' => $userTarget->getId() + ] + ); + } + public function module(string $route = ''): Response { return $this->render( diff --git a/src/Service/TorrentService.php b/src/Service/TorrentService.php index 4c01c36..5bb5b32 100644 --- a/src/Service/TorrentService.php +++ b/src/Service/TorrentService.php @@ -280,6 +280,27 @@ class TorrentService ->getTorrentScrapeQueue(); } + public function setTorrentsApprovedByUserId( + int $userId, + bool $value + ): void + { + foreach ($this->entityManagerInterface + ->getRepository(Torrent::class) + ->findBy( + [ + 'userId' => $userId + ]) as $torrent) + { + $torrent->setApproved( + $value + ); + + $this->entityManagerInterface->persist($torrent); + $this->entityManagerInterface->flush(); + } + } + // Torrent locale public function getTorrentLocales(int $id): ?TorrentLocales { @@ -352,6 +373,27 @@ class TorrentService return $torrentLocales; } + public function setTorrentLocalesApprovedByUserId( + int $userId, + bool $value + ): void + { + foreach ($this->entityManagerInterface + ->getRepository(TorrentLocales::class) + ->findBy( + [ + 'userId' => $userId + ]) as $torrentLocales) + { + $torrentLocales->setApproved( + $value + ); + + $this->entityManagerInterface->persist($torrentLocales); + $this->entityManagerInterface->flush(); + } + } + // Torrent sensitive public function getTorrentSensitive(int $id): ?TorrentSensitive { @@ -424,6 +466,27 @@ class TorrentService return $torrentSensitive; } + public function setTorrentSensitivesApprovedByUserId( + int $userId, + bool $value + ): void + { + foreach ($this->entityManagerInterface + ->getRepository(TorrentSensitive::class) + ->findBy( + [ + 'userId' => $userId + ]) as $torrentSensitive) + { + $torrentSensitive->setApproved( + $value + ); + + $this->entityManagerInterface->persist($torrentSensitive); + $this->entityManagerInterface->flush(); + } + } + // Torrent star public function findTorrentStar( int $torrentId, diff --git a/src/Service/UserService.php b/src/Service/UserService.php index b070397..2752fa2 100644 --- a/src/Service/UserService.php +++ b/src/Service/UserService.php @@ -162,4 +162,49 @@ class UserService $this->entityManagerInterface->flush(); } } + + public function toggleUserModerator( + int $userId + ): void + { + if ($user = $this->getUser($userId)) + { + $user->setModerator( + !$user->isModerator() + ); + + $this->entityManagerInterface->persist($user); + $this->entityManagerInterface->flush(); + } + } + + public function toggleUserStatus( + int $userId + ): void + { + if ($user = $this->getUser($userId)) + { + $user->setStatus( + !$user->isStatus() + ); + + $this->entityManagerInterface->persist($user); + $this->entityManagerInterface->flush(); + } + } + + public function toggleUserApproved( + int $userId + ): void + { + if ($user = $this->getUser($userId)) + { + $user->setApproved( + !$user->isApproved() + ); + + $this->entityManagerInterface->persist($user); + $this->entityManagerInterface->flush(); + } + } } \ No newline at end of file diff --git a/templates/default/user/info.html.twig b/templates/default/user/info.html.twig index d56a6e8..f112700 100644 --- a/templates/default/user/info.html.twig +++ b/templates/default/user/info.html.twig @@ -31,47 +31,95 @@ - + - + - + - + - + - + @@ -81,7 +129,7 @@ -
{{ 'Common'|trans }}{{ 'Common'|trans }}
{{ 'Joined'|trans }} {{ user.added | format_ago }}
{{ 'Access'|trans }}{{ 'Access'|trans }}
{{ 'Status'|trans }} - {% if user.status %} - {{ 'active'|trans }} - {% else %} - {{ 'disabled'|trans }} - {% endif %} + {{ 'Status' | trans }} + + {% if user.status %} + {{ 'Active' | trans }} + {% if user.moderator %} + + + + + + {% endif %} + {% else %} + {{ 'Disabled' | trans }} + {% if user.moderator %} + + + + + + {% endif %} + {% endif %}
{{ 'Approved'|trans }} - {% if user.approved %} - {{ 'yes'|trans }} - {% else %} - {{ 'no'|trans }} - {% endif %} + {{ 'Approved' | trans }} + + {% if user.approved %} + {{ 'Yes'| trans }} + {% if user.moderator %} + + + + + + {% endif %} + {% else %} + {{ 'No'| trans }} + {% if user.moderator %} + + + + + + {% endif %} + {% endif %}
{{ 'Moderator'|trans }} - {% if user.moderator %} - {{ 'yes'|trans }} - {% else %} - {{ 'no'|trans }} - {% endif %} + {{ 'Moderator' | trans }} + + {% if user.moderator %} + {{ 'Yes'| trans }} + {% if user.moderator %} + + + + + + {% endif %} + {% else %} + {{ 'No'| trans }} + {% if user.moderator %} + + + + + + {% endif %} + {% endif %}
{{ 'Settings'|trans }}{{ 'Settings'|trans }}
{{ 'Interface'|trans }}
{{ 'Content'|trans }} + {% for i, locale in user.locales %}{% if i > 0 %},{% endif %} {{ locale|locale_name(locale)|u.title }}{% endfor %} {# {% for locale in user.locales %} @@ -100,9 +148,9 @@ {{ 'Sensitive'|trans }} {% if user.sensitive %} - {{ 'yes'|trans }} + {{ 'Yes'|trans }} {% else %} - {{ 'no'|trans }} + {{ 'No'|trans }} {% endif %}