diff --git a/internal/domain/lms_access.go b/internal/domain/lms_access.go index ef85de49..4a701e96 100644 --- a/internal/domain/lms_access.go +++ b/internal/domain/lms_access.go @@ -2,7 +2,7 @@ package domain // LMSEntityAccess describes learner gating for a program, course, module, or lesson. // Included for STUDENT and OPEN_LEARNER; omitted (nil) for staff roles in API responses. -// OPEN_LEARNER always has is_accessible and is_completed true; STUDENT reflects real progress and gating. +// OPEN_LEARNER always has is_accessible and is_completed true and bypasses subscription gates; STUDENT reflects real progress and gating. // Progress fields count completed published practices vs total published practices in the // entity's scope. progress_percent keeps the legacy whole-number value; use // progress_percent_precise for decimal precision in learner UIs. diff --git a/internal/domain/role.go b/internal/domain/role.go index 62c358be..69fb3c81 100644 --- a/internal/domain/role.go +++ b/internal/domain/role.go @@ -6,7 +6,7 @@ const ( RoleSuperAdmin Role = "SUPER_ADMIN" RoleAdmin Role = "ADMIN" RoleStudent Role = "STUDENT" - // RoleOpenLearner can consume LMS content like a learner without sequential locks; access APIs show all content accessible and completed. + // RoleOpenLearner can consume LMS and exam-prep content without sequential locks or subscription gates. RoleOpenLearner Role = "OPEN_LEARNER" RoleInstructor Role = "INSTRUCTOR" RoleSupport Role = "SUPPORT" @@ -31,6 +31,16 @@ func (r Role) IsCustomerLearnerRole() bool { return r == RoleStudent || r == RoleOpenLearner } +// BypassesSubscription is true when subscription middleware must not block access. +func (r Role) BypassesSubscription() bool { + switch r { + case RoleSuperAdmin, RoleAdmin, RoleInstructor, RoleSupport, RoleOpenLearner: + return true + default: + return false + } +} + func (r Role) Value() string { return string(r) } diff --git a/internal/web_server/middleware.go b/internal/web_server/middleware.go index 951087ea..615eff4f 100644 --- a/internal/web_server/middleware.go +++ b/internal/web_server/middleware.go @@ -175,7 +175,7 @@ func (a *App) OnlyAdminAndAbove(c *fiber.Ctx) error { } // RequireActiveSubscription enforces an active subscription for learner accounts. -// Staff roles (SUPER_ADMIN, ADMIN, INSTRUCTOR, SUPPORT) bypass this check. +// Staff roles and OPEN_LEARNER bypass this check. STUDENT is checked when this middleware applies. // Use after authMiddleware on routes that deliver paid learning content. func (a *App) RequireActiveSubscription() fiber.Handler { return func(c *fiber.Ctx) error { @@ -183,10 +183,11 @@ func (a *App) RequireActiveSubscription() fiber.Handler { if !ok { return fiber.NewError(fiber.StatusForbidden, "Role not found in context") } - switch role { - case domain.RoleSuperAdmin, domain.RoleAdmin, domain.RoleInstructor, domain.RoleSupport: + if role.BypassesSubscription() { return c.Next() - case domain.RoleStudent, domain.RoleOpenLearner: + } + switch role { + case domain.RoleStudent: userID, ok := c.Locals("user_id").(int64) if !ok || userID == 0 { return fiber.NewError(fiber.StatusUnauthorized, "Unauthorized") @@ -345,12 +346,7 @@ func isReadOnlyHTTPMethod(method string) bool { } func bypassSubscriptionForRole(role domain.Role) bool { - switch role { - case domain.RoleSuperAdmin, domain.RoleAdmin, domain.RoleInstructor, domain.RoleSupport: - return true - default: - return false - } + return role.BypassesSubscription() } func humanizeSubscriptionCategory(category domain.SubscriptionCategory) string {