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>
91 lines
2.4 KiB
Go
91 lines
2.4 KiB
Go
package team
|
|
|
|
import (
|
|
"Yimaru-Backend/internal/domain"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
)
|
|
|
|
func teamRoleNotFoundError(raw string) error {
|
|
return fmt.Errorf("%w: %q (use a built-in team role or an existing RBAC role name/id)", domain.ErrInvalidTeamRole, strings.TrimSpace(raw))
|
|
}
|
|
|
|
func (s *Service) lookupRBACRole(ctx context.Context, name string) (domain.RoleRecord, error) {
|
|
if s.rbacStore == nil {
|
|
return domain.RoleRecord{}, teamRoleNotFoundError(name)
|
|
}
|
|
|
|
rec, err := s.rbacStore.GetRoleByName(ctx, name)
|
|
if err == nil {
|
|
return rec, nil
|
|
}
|
|
if !errors.Is(err, pgx.ErrNoRows) {
|
|
return domain.RoleRecord{}, err
|
|
}
|
|
|
|
rec, err = s.rbacStore.GetRoleByNameIgnoreCase(ctx, name)
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return domain.RoleRecord{}, teamRoleNotFoundError(name)
|
|
}
|
|
return domain.RoleRecord{}, err
|
|
}
|
|
return rec, nil
|
|
}
|
|
|
|
// resolveAndValidateTeamRole accepts a built-in team role key, an RBAC role name, or a decimal RBAC roles.id.
|
|
func (s *Service) resolveAndValidateTeamRole(ctx context.Context, raw string) (domain.TeamRole, error) {
|
|
key := strings.TrimSpace(raw)
|
|
if key == "" {
|
|
return "", teamRoleNotFoundError(raw)
|
|
}
|
|
|
|
if id, err := strconv.ParseInt(key, 10, 64); err == nil && id > 0 {
|
|
if s.rbacStore == nil {
|
|
return "", teamRoleNotFoundError(raw)
|
|
}
|
|
rec, err := s.rbacStore.GetRoleByID(ctx, id)
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return "", teamRoleNotFoundError(raw)
|
|
}
|
|
return "", err
|
|
}
|
|
key = rec.Name
|
|
}
|
|
|
|
normalized := domain.NormalizeTeamRoleKey(key)
|
|
canonical := domain.CanonicalTeamRoleKey(key)
|
|
if normalized == "" || domain.IsForbiddenTeamMemberRole(normalized) || domain.IsForbiddenTeamMemberRole(canonical) {
|
|
return "", teamRoleNotFoundError(raw)
|
|
}
|
|
|
|
if domain.TeamRole(normalized).IsBuiltinTeamRole() {
|
|
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() {
|
|
tr := domain.TeamRole(canonical)
|
|
if !domain.IsAssignableStaffTeamRole(tr) {
|
|
return "", teamRoleNotFoundError(raw)
|
|
}
|
|
return tr, nil
|
|
}
|
|
|
|
return "", teamRoleNotFoundError(raw)
|
|
}
|
|
|
|
func (s *Service) IsAssignableTeamRole(ctx context.Context, raw string) bool {
|
|
_, err := s.resolveAndValidateTeamRole(ctx, raw)
|
|
return err == nil
|
|
}
|