feat: let OPEN_LEARNER bypass subscription gates on LMS and exam-prep routes

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Yared Yemane 2026-06-27 01:41:40 -07:00
parent 51c8cad698
commit bfa96b7372
3 changed files with 18 additions and 12 deletions

View File

@ -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.

View File

@ -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)
}

View File

@ -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 {