65 lines
2.6 KiB
Go
65 lines
2.6 KiB
Go
package handlers
|
|
|
|
import (
|
|
"Yimaru-Backend/internal/domain"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
func (h *Handler) canManageLMSPractices(c *fiber.Ctx) bool {
|
|
rn := string(c.Locals("role").(domain.Role))
|
|
return h.rbacSvc.HasPermission(rn, "practices.create") || h.rbacSvc.HasPermission(rn, "practices.update")
|
|
}
|
|
|
|
// hideUnpublishedPractices is true only for customer learner roles.
|
|
// Admin-panel staff (including custom RBAC roles with list permission) must see DRAFT shells
|
|
// on parent practice pages; gating create/update is handled separately by RequirePermission.
|
|
func (h *Handler) hideUnpublishedPractices(c *fiber.Ctx) bool {
|
|
role, _ := c.Locals("role").(domain.Role)
|
|
return role.IsCustomerLearnerRole()
|
|
}
|
|
|
|
func (h *Handler) canManageLessons(c *fiber.Ctx) bool {
|
|
rn := string(c.Locals("role").(domain.Role))
|
|
return h.rbacSvc.HasPermission(rn, "lessons.create") || h.rbacSvc.HasPermission(rn, "lessons.update")
|
|
}
|
|
|
|
func (h *Handler) canManageExamPrepPractices(c *fiber.Ctx) bool {
|
|
rn := string(c.Locals("role").(domain.Role))
|
|
return h.rbacSvc.HasPermission(rn, "exam_prep.practices.create") || h.rbacSvc.HasPermission(rn, "exam_prep.practices.update")
|
|
}
|
|
|
|
// forbidIfLinkedPracticeDraftForSubscriber returns 404 for draft LMS/exam-prep shells when the caller cannot manage content.
|
|
func (h *Handler) forbidIfLinkedPracticeDraftForSubscriber(c *fiber.Ctx, questionSetID int64) error {
|
|
if lp, ok, err := h.practiceSvc.TryGetByQuestionSetID(c.Context(), questionSetID); err != nil {
|
|
return err
|
|
} else if ok && !lp.VisibleToLearners() && !h.canManageLMSPractices(c) {
|
|
return fiber.NewError(fiber.StatusNotFound, "Practice not found")
|
|
}
|
|
|
|
if ep, ok, err := h.examPrepSvc.TryGetExamPrepPracticeByQuestionSetID(c.Context(), questionSetID); err != nil {
|
|
return err
|
|
} else if ok && !ep.VisibleToLearners() && !h.canManageExamPrepPractices(c) {
|
|
return fiber.NewError(fiber.StatusNotFound, "Practice not found")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// forbidCompletingDraftPractice blocks completion when an LMS/exam-prep shell is still in draft (learners/students path).
|
|
func (h *Handler) forbidCompletingDraftPractice(c *fiber.Ctx, questionSetID int64) error {
|
|
if lp, ok, err := h.practiceSvc.TryGetByQuestionSetID(c.Context(), questionSetID); err != nil {
|
|
return err
|
|
} else if ok && !lp.VisibleToLearners() {
|
|
return fiber.NewError(fiber.StatusForbidden, "Only published practices can be completed")
|
|
}
|
|
|
|
if ep, ok, err := h.examPrepSvc.TryGetExamPrepPracticeByQuestionSetID(c.Context(), questionSetID); err != nil {
|
|
return err
|
|
} else if ok && !ep.VisibleToLearners() {
|
|
return fiber.NewError(fiber.StatusForbidden, "Only published practices can be completed")
|
|
}
|
|
|
|
return nil
|
|
}
|