diff --git a/.github/workflows/docker.yaml b/.github/workflows/docker.yaml index 23c30bb..0d55d27 100644 --- a/.github/workflows/docker.yaml +++ b/.github/workflows/docker.yaml @@ -4,6 +4,7 @@ on: push: branches: - master + - distro/* tags: - v* workflow_dispatch: diff --git a/config/packages/twig.yaml b/config/packages/twig.yaml index a058436..4af147b 100644 --- a/config/packages/twig.yaml +++ b/config/packages/twig.yaml @@ -4,6 +4,9 @@ twig: globals: umami_domain: "%env(UMAMI_DOMAIN)%" umami_website_id: "%env(UMAMI_WEBSITE_ID)%" + appfeatures: + hint: "%app.features.hint%" + comment: "%app.features.comment%" when@test: twig: diff --git a/config/services.yaml b/config/services.yaml index b4f0f26..f8450f0 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -8,6 +8,10 @@ parameters: app.redis_uri: "%env(REDIS_URI)%" app.openai_api_key: "%env(OPENAI_API_KEY)%" + app.features.hint: true + app.features.editable-profile: true + app.features.comment: true + services: # default configuration for services in *this* file _defaults: diff --git a/src/Controller/CommentsController.php b/src/Controller/CommentsController.php index c249c9e..8cee2a1 100644 --- a/src/Controller/CommentsController.php +++ b/src/Controller/CommentsController.php @@ -28,6 +28,10 @@ class CommentsController extends AbstractController #[Route('/', name: '')] public function index(#[CurrentUser] User $user, CommentRepository $commentRepository): Response { + if (!$this->isCommentFeatureEnabled()) { + throw $this->createNotFoundException('"Comment" feature is disabled.'); + } + $comments = $commentRepository->findUserComments($user); $likes = array_reduce($comments, fn (int $carry, Comment $comment) => $carry + $comment->getCommentLikeEvents()->count(), 0); @@ -47,6 +51,10 @@ public function likes( ChartBuilderInterface $chartBuilder, TranslatorInterface $translator, ): Response { + if (!$this->isCommentFeatureEnabled()) { + throw $this->createNotFoundException('"Comment" feature is disabled.'); + } + $q = $commentRepository->createQueryBuilder('c') ->select('c.id AS id, COUNT(cle.id) AS count') ->leftJoin('c.commentLikeEvents', 'cle') @@ -88,4 +96,14 @@ public function likes( 'chart' => $chart, ]); } + + private function isCommentFeatureEnabled(): bool + { + $comment = $this->getParameter('app.features.comment'); + if (!\is_bool($comment)) { + throw new \RuntimeException('The "app.features.comment" parameter must be a boolean.'); + } + + return $comment; + } } diff --git a/src/Controller/ProfileController.php b/src/Controller/ProfileController.php index e007028..ffc4b97 100644 --- a/src/Controller/ProfileController.php +++ b/src/Controller/ProfileController.php @@ -19,11 +19,20 @@ class ProfileController extends AbstractController { + public function isProfileEditable(): bool + { + $isProfileEditable = $this->getParameter('app.features.editable-profile'); + \assert(\is_bool($isProfileEditable)); + + return $isProfileEditable; + } + #[Route('/profile', name: 'app_profile')] public function index(#[CurrentUser] User $user): Response { return $this->render('profile/index.html.twig', [ 'user' => $user, + 'isProfileEditable' => $this->isProfileEditable(), ]); } @@ -35,6 +44,10 @@ public function editPassword( Request $request, #[CurrentUser] User $user, ): Response { + if (!$this->isProfileEditable()) { + throw $this->createNotFoundException('Feature "Editable Profile" is disabled.'); + } + $passwordChangeModel = new PasswordChangeModel(); $passwordChangeForm = $formFactory->createBuilder(PasswordChangeFormType::class, $passwordChangeModel) ->setAction($this->generateUrl('app_profile_edit_password')) @@ -66,6 +79,10 @@ public function editUsername( Request $request, #[CurrentUser] User $user, ): Response { + if (!$this->isProfileEditable()) { + throw $this->createNotFoundException('Feature "Editable Profile" is disabled.'); + } + $usernameChangeForm = $formFactory->createBuilder(NameChangeFormType::class, $user) ->setAction($this->generateUrl('app_profile_edit_username')) ->getForm(); diff --git a/src/Twig/Components/Challenge/CommentModule/CommentForm.php b/src/Twig/Components/Challenge/CommentModule/CommentForm.php index ae99741..ab3e2bf 100644 --- a/src/Twig/Components/Challenge/CommentModule/CommentForm.php +++ b/src/Twig/Components/Challenge/CommentModule/CommentForm.php @@ -9,8 +9,10 @@ use App\Entity\User as UserEntity; use App\Form\CommentCreateFormType; use Doctrine\ORM\EntityManagerInterface; +use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\Form\FormInterface; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\UX\LiveComponent\Attribute\AsLiveComponent; use Symfony\UX\LiveComponent\Attribute\LiveAction; use Symfony\UX\LiveComponent\Attribute\LiveProp; @@ -52,8 +54,15 @@ protected function instantiateForm(): FormInterface } #[LiveAction] - public function save(EntityManagerInterface $entityManager): void + public function save(EntityManagerInterface $entityManager, ParameterBagInterface $parameterBag): void { + $appFeatureComment = $parameterBag->get('app.features.comment'); + \assert(\is_bool($appFeatureComment)); + + if (!$appFeatureComment) { + throw new BadRequestHttpException('Comment feature is disabled.'); + } + $this->submitForm(); $comment = $this->getForm()->getData(); diff --git a/src/Twig/Components/Challenge/Instruction/Modal.php b/src/Twig/Components/Challenge/Instruction/Modal.php index d91f420..b2c91a2 100644 --- a/src/Twig/Components/Challenge/Instruction/Modal.php +++ b/src/Twig/Components/Challenge/Instruction/Modal.php @@ -11,6 +11,8 @@ use App\Service\PointCalculationService; use App\Service\PromptService; use Doctrine\ORM\EntityManagerInterface; +use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Serializer\SerializerInterface; use Symfony\Contracts\Translation\TranslatorInterface; use Symfony\UX\LiveComponent\Attribute\AsLiveComponent; @@ -51,7 +53,15 @@ public function instruct( TranslatorInterface $translator, SerializerInterface $serializer, EntityManagerInterface $entityManager, + ParameterBagInterface $parameterBag, ): void { + $appFeatureHint = $parameterBag->get('app.features.hint'); + \assert(\is_bool($appFeatureHint)); + + if (!$appFeatureHint) { + throw new BadRequestHttpException('Hint feature is disabled.'); + } + if ('' === $this->query) { return; } diff --git a/templates/components/Challenge/Comment.html.twig b/templates/components/Challenge/Comment.html.twig index 2dc225a..247b677 100644 --- a/templates/components/Challenge/Comment.html.twig +++ b/templates/components/Challenge/Comment.html.twig @@ -1,13 +1,15 @@ -
-
- -
+
+ {% if appfeatures.comment %} +
+ +
-
- {% for comment in this.comments %} - - {% else %} -
尚無留言。成為第一個留言者吧!
- {% endfor %} -
+
+ {% for comment in this.comments %} + + {% else %} +
尚無留言。成為第一個留言者吧!
+ {% endfor %} +
+ {% endif %}
diff --git a/templates/components/Challenge/ResultPresenterModule/AnswerPresenter.html.twig b/templates/components/Challenge/ResultPresenterModule/AnswerPresenter.html.twig index c3e7cf1..bbe3335 100644 --- a/templates/components/Challenge/ResultPresenterModule/AnswerPresenter.html.twig +++ b/templates/components/Challenge/ResultPresenterModule/AnswerPresenter.html.twig @@ -3,7 +3,7 @@ {% if not this.result.answer %} {% if this.result.same %} {% else %} diff --git a/templates/components/Navbar.html.twig b/templates/components/Navbar.html.twig index 612aa68..0d9bc31 100644 --- a/templates/components/Navbar.html.twig +++ b/templates/components/Navbar.html.twig @@ -6,26 +6,30 @@ name: '學習概況', icon: 'bi bi-bar-chart-fill', path: path('app_overview'), + disabled: false, }, { pageId: 'questions', name: '練習題目', icon: 'bi bi-pencil-fill', path: path('app_questions'), + disabled: false, }, { pageId: 'comments', name: '留言一覽', icon: 'bi bi-chat-left-text-fill', path: path('app_comments'), + disabled: not appfeatures.comment, }, { pageId: 'complementary', name: '補充資料', icon: 'bi bi-book-fill', path: path('app_complementary'), + disabled: false, }, -] %} +]|filter(el => not el.disabled) %}
diff --git a/templates/profile/index.html.twig b/templates/profile/index.html.twig index f05a4ae..3f00154 100644 --- a/templates/profile/index.html.twig +++ b/templates/profile/index.html.twig @@ -47,6 +47,7 @@ + {% if isProfileEditable %}

更新帳號資訊

@@ -64,5 +65,6 @@
+ {% endif %}
{% endblock %}