Limit staff team roles to Super Admin, Admin, and Content Manager.

Add CONTENT_MANAGER RBAC defaults, migration, and Super Admin-only team email invitations; restrict assignable team_member roles without changing platform user roles.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Yared Yemane 2026-08-05 12:01:02 +03:00
parent 4cfc090d06
commit df4f4d8ade
10 changed files with 129 additions and 64 deletions

View File

@ -0,0 +1,4 @@
ALTER TABLE team_members
DROP CONSTRAINT IF EXISTS team_members_team_role_check;
DELETE FROM roles WHERE name = 'CONTENT_MANAGER';

View File

@ -0,0 +1,12 @@
-- Staff team roles: Super Admin, Admin, Content Manager only.
INSERT INTO roles (name, description, is_system)
VALUES ('CONTENT_MANAGER', 'Content manager — admin panel content section only', true)
ON CONFLICT (name) DO NOTHING;
UPDATE team_members
SET team_role = 'ADMIN'
WHERE team_role NOT IN ('SUPER_ADMIN', 'ADMIN', 'CONTENT_MANAGER');
ALTER TABLE team_members
ADD CONSTRAINT team_members_team_role_check
CHECK (team_role IN ('SUPER_ADMIN', 'ADMIN', 'CONTENT_MANAGER'));

View File

@ -3,9 +3,10 @@ package domain
type Role string
const (
RoleSuperAdmin Role = "SUPER_ADMIN"
RoleAdmin Role = "ADMIN"
RoleStudent Role = "STUDENT"
RoleSuperAdmin Role = "SUPER_ADMIN"
RoleAdmin Role = "ADMIN"
RoleContentManager Role = "CONTENT_MANAGER"
RoleStudent Role = "STUDENT"
// RoleOpenLearner can consume LMS (Learn English) and exam-prep content without
// sequential/precedence locks or subscription gates (including premium tiers).
RoleOpenLearner Role = "OPEN_LEARNER"
@ -15,7 +16,7 @@ const (
func (r Role) IsValid() bool {
switch r {
case RoleSuperAdmin, RoleAdmin, RoleStudent, RoleOpenLearner, RoleInstructor, RoleSupport:
case RoleSuperAdmin, RoleAdmin, RoleContentManager, RoleStudent, RoleOpenLearner, RoleInstructor, RoleSupport:
return true
default:
return false
@ -42,7 +43,7 @@ func (r Role) IsCustomerLearnerRole() bool {
// BypassesSubscription is true when subscription middleware must not block access.
func (r Role) BypassesSubscription() bool {
switch r {
case RoleSuperAdmin, RoleAdmin, RoleInstructor, RoleSupport, RoleOpenLearner:
case RoleSuperAdmin, RoleAdmin, RoleContentManager, RoleInstructor, RoleSupport, RoleOpenLearner:
return true
default:
return false

View File

@ -88,17 +88,31 @@ func (r TeamRole) String() string {
return string(r)
}
// IsAssignableStaffTeamRole reports roles that may be assigned to admin-panel staff.
func IsAssignableStaffTeamRole(r TeamRole) bool {
switch r {
case TeamRoleSuperAdmin, TeamRoleAdmin, TeamRoleContentManager:
return true
default:
return false
}
}
// PlatformRole maps a team_members.team_role to the JWT platform Role used in API auth.
func (r TeamRole) PlatformRole() Role {
switch r {
case TeamRoleSuperAdmin:
return RoleSuperAdmin
case TeamRoleAdmin, TeamRoleContentManager, TeamRoleFinance, TeamRoleHR, TeamRoleAnalyst:
case TeamRoleAdmin:
return RoleAdmin
case TeamRoleContentManager:
return RoleContentManager
case TeamRoleInstructor:
return RoleInstructor
case TeamRoleSupportAgent:
return RoleSupport
case TeamRoleFinance, TeamRoleHR, TeamRoleAnalyst:
return RoleAdmin
default:
return RoleAdmin
}

View File

@ -12,8 +12,8 @@ func TestTeamRole_IsBuiltinTeamRole(t *testing.T) {
}
func TestTeamRole_RBACRoleForJWT(t *testing.T) {
if got := TeamRoleContentManager.RBACRoleForJWT(); got != RoleAdmin {
t.Fatalf("CONTENT_MANAGER JWT role = %q, want ADMIN", got)
if got := TeamRoleContentManager.RBACRoleForJWT(); got != RoleContentManager {
t.Fatalf("CONTENT_MANAGER JWT role = %q, want CONTENT_MANAGER", got)
}
if got := TeamRole("CONTENT_MANAGER_NEW").RBACRoleForJWT(); got != Role("CONTENT_MANAGER_NEW") {
t.Fatalf("custom JWT role = %q", got)

View File

@ -506,10 +506,10 @@ var DefaultRolePermissions = map[string][]string{
"vimeo.videos.list", "vimeo.videos.get", "vimeo.videos.embed", "vimeo.videos.status", "vimeo.videos.delete",
"vimeo.uploads.pull", "vimeo.uploads.tus",
// Team (full access)
"team.profile.get_mine", "team.profile.update_mine", "team.stats", "team.members.list", "team.members.export", "team.members.create", "team.members.invite",
// Team (full access except email invitations — Super Admin only)
"team.profile.get_mine", "team.profile.update_mine", "team.stats", "team.members.list", "team.members.export", "team.members.create",
"team.members.get", "team.members.update", "team.members.update_status", "team.members.delete", "team.members.change_password",
"team.invitations.list", "team.invitations.resend", "team.invitations.revoke",
"team.invitations.list", "team.invitations.revoke",
// Sub-course Prerequisites
"subcourse_prerequisites.add", "subcourse_prerequisites.list", "subcourse_prerequisites.remove",
@ -660,4 +660,41 @@ var DefaultRolePermissions = map[string][]string{
// Auth
"auth.logout",
},
"CONTENT_MANAGER": {
// Course & LMS content (full authoring)
"course_categories.create", "course_categories.list", "course_categories.get", "course_categories.update", "course_categories.delete", "course_categories.reorder",
"courses.create", "courses.get", "courses.list_by_category", "courses.list_by_program", "courses.update", "courses.upload_thumbnail", "courses.delete", "courses.reorder",
"subcourses.create", "subcourses.get", "subcourses.list_by_course", "subcourses.list_by_course_list", "subcourses.list_active",
"subcourses.update", "subcourses.upload_thumbnail", "subcourses.deactivate", "subcourses.delete", "subcourses.reorder",
"videos.create", "videos.create_vimeo", "videos.upload", "videos.import_vimeo", "videos.get",
"videos.list_by_subcourse", "videos.list_published", "videos.publish", "videos.update", "videos.delete", "videos.reorder", "videos.track_engagement",
"learning_tree.get", "practices.reorder",
"programs.create", "programs.list", "programs.get", "programs.update", "programs.delete", "programs.reorder",
"exam_prep.catalog_courses.create", "exam_prep.catalog_courses.list", "exam_prep.catalog_courses.get", "exam_prep.catalog_courses.update", "exam_prep.catalog_courses.delete", "exam_prep.catalog_courses.reorder",
"exam_prep.units.create", "exam_prep.units.list", "exam_prep.units.get", "exam_prep.units.update", "exam_prep.units.delete", "exam_prep.units.reorder",
"exam_prep.modules.create", "exam_prep.modules.list", "exam_prep.modules.get", "exam_prep.modules.update", "exam_prep.modules.delete", "exam_prep.modules.reorder",
"exam_prep.lessons.create", "exam_prep.lessons.list_by_module", "exam_prep.lessons.get", "exam_prep.lessons.update", "exam_prep.lessons.delete", "exam_prep.lessons.reorder",
"exam_prep.practices.create", "exam_prep.practices.list", "exam_prep.practices.list_by_catalog_course", "exam_prep.practices.list_by_unit", "exam_prep.practices.list_by_lesson", "exam_prep.practices.get", "exam_prep.practices.update", "exam_prep.practices.delete",
"lms.get_my_progress",
"modules.create", "modules.get", "modules.list_by_course", "modules.update", "modules.delete", "modules.reorder",
"lessons.create", "lessons.get", "lessons.list_by_module", "lessons.complete", "lessons.update", "lessons.delete", "lessons.reorder",
"practices.create", "practices.get", "practices.list", "practices.update", "practices.delete",
"personas.create", "personas.list", "personas.get", "personas.update", "personas.delete",
"questions.create", "questions.list", "questions.search", "questions.get", "questions.update", "questions.delete",
"question_sets.create", "question_sets.list", "question_sets.list_by_owner", "question_sets.get", "question_sets.update", "question_sets.delete",
"question_set_items.add", "question_set_items.list", "question_set_items.remove", "question_set_items.update_order",
"question_set_personas.list", "question_set_personas.add", "question_set_personas.remove",
"subcourse_prerequisites.add", "subcourse_prerequisites.list", "subcourse_prerequisites.remove",
"vimeo.videos.list", "vimeo.videos.get", "vimeo.videos.embed", "vimeo.videos.status", "vimeo.videos.delete",
"vimeo.uploads.pull", "vimeo.uploads.tus",
"team.profile.get_mine", "team.profile.update_mine", "team.members.change_password",
"notifications.ws_connect", "notifications.list_mine", "notifications.mark_read", "notifications.mark_all_read", "notifications.mark_unread", "notifications.mark_all_unread",
"notifications.delete_mine", "notifications.count_unread",
"users.update_self", "users.delete_self", "users.cancel_delete_self", "users.profile_completed", "users.upload_profile_picture", "users.user_profile",
"devices.register", "devices.unregister",
"progress.start", "progress.update", "progress.complete", "progress.check_access", "progress.get_course",
"ratings.submit", "ratings.list_by_target", "ratings.summary", "ratings.get_mine", "ratings.list_mine", "ratings.delete",
"auth.logout",
},
}

View File

@ -66,18 +66,22 @@ func (s *Service) resolveAndValidateTeamRole(ctx context.Context, raw string) (d
}
if domain.TeamRole(normalized).IsBuiltinTeamRole() {
return domain.TeamRole(normalized), nil
tr := domain.TeamRole(normalized)
if !domain.IsAssignableStaffTeamRole(tr) {
return "", teamRoleNotFoundError(raw)
}
return tr, nil
}
// Accept UI display labels for built-ins (e.g. "Super Admin" → SUPER_ADMIN).
if domain.TeamRole(canonical).IsBuiltinTeamRole() {
return domain.TeamRole(canonical), nil
tr := domain.TeamRole(canonical)
if !domain.IsAssignableStaffTeamRole(tr) {
return "", teamRoleNotFoundError(raw)
}
return tr, nil
}
rec, err := s.lookupRBACRole(ctx, key)
if err != nil {
return "", err
}
return domain.TeamRole(rec.Name), nil
return "", teamRoleNotFoundError(raw)
}
func (s *Service) IsAssignableTeamRole(ctx context.Context, raw string) bool {

View File

@ -71,7 +71,19 @@ func (s *stubRBACStore) GetAllRolesWithPermissions(context.Context) (map[string]
panic("not implemented")
}
func TestResolveAndValidateTeamRole_CustomRBACRole(t *testing.T) {
func TestResolveAndValidateTeamRole_BuiltinContentManager(t *testing.T) {
svc := &Service{rbacStore: &stubRBACStore{roles: map[string]domain.RoleRecord{}}}
got, err := svc.resolveAndValidateTeamRole(context.Background(), "CONTENT_MANAGER")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != domain.TeamRoleContentManager {
t.Fatalf("got %q", got)
}
}
func TestResolveAndValidateTeamRole_CustomRBACRoleRejected(t *testing.T) {
svc := &Service{
rbacStore: &stubRBACStore{
roles: map[string]domain.RoleRecord{
@ -83,51 +95,9 @@ func TestResolveAndValidateTeamRole_CustomRBACRole(t *testing.T) {
},
}
got, err := svc.resolveAndValidateTeamRole(context.Background(), "CONTENT_MANAGER_NEW")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != domain.TeamRole("CONTENT_MANAGER_NEW") {
t.Fatalf("got %q", got)
}
}
func TestResolveAndValidateTeamRole_ByRBACID(t *testing.T) {
svc := &Service{
rbacStore: &stubRBACStore{
roles: map[string]domain.RoleRecord{
"CONTENT_MANAGER_NEW": {ID: 9, Name: "CONTENT_MANAGER_NEW"},
},
byID: map[int64]domain.RoleRecord{
9: {ID: 9, Name: "CONTENT_MANAGER_NEW"},
},
},
}
got, err := svc.resolveAndValidateTeamRole(context.Background(), "9")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != domain.TeamRole("CONTENT_MANAGER_NEW") {
t.Fatalf("got %q", got)
}
}
func TestResolveAndValidateTeamRole_CaseInsensitiveRBACName(t *testing.T) {
svc := &Service{
rbacStore: &stubRBACStore{
roles: map[string]domain.RoleRecord{
"content_manager_new": {ID: 9, Name: "content_manager_new"},
},
},
}
got, err := svc.resolveAndValidateTeamRole(context.Background(), "CONTENT_MANAGER_NEW")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != domain.TeamRole("content_manager_new") {
t.Fatalf("got %q", got)
_, err := svc.resolveAndValidateTeamRole(context.Background(), "CONTENT_MANAGER_NEW")
if !errors.Is(err, domain.ErrInvalidTeamRole) {
t.Fatalf("want ErrInvalidTeamRole, got %v", err)
}
}

View File

@ -0,0 +1,17 @@
package handlers
import (
"Yimaru-Backend/internal/domain"
"github.com/gofiber/fiber/v2"
)
func requireSuperAdminForTeamInvitation(c *fiber.Ctx) error {
role, ok := c.Locals("role").(domain.Role)
if !ok || role != domain.RoleSuperAdmin {
return c.Status(fiber.StatusForbidden).JSON(domain.ErrorResponse{
Message: "Only super administrators can send team email invitations",
})
}
return nil
}

View File

@ -61,6 +61,9 @@ func mapTeamInvitationListItem(row domain.TeamInvitationWithMember) teamInvitati
// @Failure 500 {object} domain.ErrorResponse
// @Router /api/v1/team/members/invite [post]
func (h *Handler) InviteTeamMember(c *fiber.Ctx) error {
if err := requireSuperAdminForTeamInvitation(c); err != nil {
return err
}
var req domain.InviteTeamMemberReq
if err := c.BodyParser(&req); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
@ -114,6 +117,9 @@ func (h *Handler) InviteTeamMember(c *fiber.Ctx) error {
// @Failure 404 {object} domain.ErrorResponse
// @Router /api/v1/team/members/{id}/resend-invite [post]
func (h *Handler) ResendTeamInvitation(c *fiber.Ctx) error {
if err := requireSuperAdminForTeamInvitation(c); err != nil {
return err
}
memberID, err := strconv.ParseInt(c.Params("id"), 10, 64)
if err != nil || memberID <= 0 {
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{