fix: resolve custom team roles case-insensitively against RBAC
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
dbdea622d4
commit
180448d688
|
|
@ -9,6 +9,9 @@ SELECT * FROM roles WHERE id = $1;
|
|||
-- name: GetRoleByName :one
|
||||
SELECT * FROM roles WHERE name = $1;
|
||||
|
||||
-- name: GetRoleByNameIgnoreCase :one
|
||||
SELECT * FROM roles WHERE UPPER(TRIM(name)) = UPPER(TRIM($1));
|
||||
|
||||
-- name: ListRoles :many
|
||||
SELECT
|
||||
COUNT(*) OVER () AS total_count,
|
||||
|
|
|
|||
|
|
@ -166,6 +166,24 @@ func (q *Queries) GetRoleByName(ctx context.Context, name string) (Role, error)
|
|||
return i, err
|
||||
}
|
||||
|
||||
const GetRoleByNameIgnoreCase = `-- name: GetRoleByNameIgnoreCase :one
|
||||
SELECT id, name, description, is_system, created_at, updated_at FROM roles WHERE UPPER(TRIM(name)) = UPPER(TRIM($1))
|
||||
`
|
||||
|
||||
func (q *Queries) GetRoleByNameIgnoreCase(ctx context.Context, btrim string) (Role, error) {
|
||||
row := q.db.QueryRow(ctx, GetRoleByNameIgnoreCase, btrim)
|
||||
var i Role
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Name,
|
||||
&i.Description,
|
||||
&i.IsSystem,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const GetRolePermissions = `-- name: GetRolePermissions :many
|
||||
SELECT p.id, p.key, p.name, p.description, p.group_name, p.created_at
|
||||
FROM permissions p
|
||||
|
|
|
|||
|
|
@ -1,6 +1,17 @@
|
|||
package domain
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// NormalizeRBACRoleName canonicalizes custom RBAC role names (uppercase, underscores).
|
||||
func NormalizeRBACRoleName(name string) string {
|
||||
s := strings.TrimSpace(name)
|
||||
s = strings.ReplaceAll(s, " ", "_")
|
||||
s = strings.ReplaceAll(s, "-", "_")
|
||||
return strings.ToUpper(s)
|
||||
}
|
||||
|
||||
type Permission struct {
|
||||
ID int64 `json:"id"`
|
||||
|
|
|
|||
|
|
@ -65,12 +65,12 @@ func IsForbiddenTeamMemberRole(roleKey string) bool {
|
|||
}
|
||||
|
||||
// RBACRoleForJWT maps a stored team_role to the role name used in JWTs and permission checks.
|
||||
// Built-in team roles keep their legacy platform mapping; custom RBAC roles use their own name.
|
||||
// Built-in team roles keep their legacy platform mapping; custom RBAC roles use the stored RBAC name.
|
||||
func (r TeamRole) RBACRoleForJWT() Role {
|
||||
if r.IsBuiltinTeamRole() {
|
||||
return r.PlatformRole()
|
||||
}
|
||||
return Role(NormalizeTeamRoleKey(string(r)))
|
||||
return Role(strings.TrimSpace(string(r)))
|
||||
}
|
||||
|
||||
func (r TeamRole) String() string {
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ type RBACStore interface {
|
|||
CreateRole(ctx context.Context, name, description string, isSystem bool) (domain.RoleRecord, error)
|
||||
GetRoleByID(ctx context.Context, id int64) (domain.RoleRecord, error)
|
||||
GetRoleByName(ctx context.Context, name string) (domain.RoleRecord, error)
|
||||
GetRoleByNameIgnoreCase(ctx context.Context, name string) (domain.RoleRecord, error)
|
||||
ListRoles(ctx context.Context, filter domain.RoleListFilter) ([]domain.RoleRecord, int64, error)
|
||||
UpdateRole(ctx context.Context, id int64, name, description string) error
|
||||
DeleteRole(ctx context.Context, id int64) error
|
||||
|
|
|
|||
|
|
@ -39,6 +39,14 @@ func (s *Store) GetRoleByName(ctx context.Context, name string) (domain.RoleReco
|
|||
return mapRole(r), nil
|
||||
}
|
||||
|
||||
func (s *Store) GetRoleByNameIgnoreCase(ctx context.Context, name string) (domain.RoleRecord, error) {
|
||||
r, err := s.queries.GetRoleByNameIgnoreCase(ctx, name)
|
||||
if err != nil {
|
||||
return domain.RoleRecord{}, err
|
||||
}
|
||||
return mapRole(r), nil
|
||||
}
|
||||
|
||||
func (s *Store) ListRoles(ctx context.Context, filter domain.RoleListFilter) ([]domain.RoleRecord, int64, error) {
|
||||
var queryParam pgtype.Text
|
||||
if filter.Query != "" {
|
||||
|
|
|
|||
|
|
@ -105,6 +105,10 @@ func (s *Service) SeedDefaultRolePermissions(ctx context.Context) error {
|
|||
// --- Role CRUD (pass-through to store + reload cache) ---
|
||||
|
||||
func (s *Service) CreateRole(ctx context.Context, name, description string) (domain.RoleRecord, error) {
|
||||
name = domain.NormalizeRBACRoleName(name)
|
||||
if name == "" {
|
||||
return domain.RoleRecord{}, fmt.Errorf("role name is required")
|
||||
}
|
||||
role, err := s.store.CreateRole(ctx, name, description, false)
|
||||
if err != nil {
|
||||
return domain.RoleRecord{}, err
|
||||
|
|
@ -122,6 +126,10 @@ func (s *Service) ListRoles(ctx context.Context, filter domain.RoleListFilter) (
|
|||
}
|
||||
|
||||
func (s *Service) UpdateRole(ctx context.Context, id int64, name, description string) error {
|
||||
name = domain.NormalizeRBACRoleName(name)
|
||||
if name == "" {
|
||||
return fmt.Errorf("role name is required")
|
||||
}
|
||||
if err := s.store.UpdateRole(ctx, id, name, description); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,9 +20,6 @@ func (s *Service) InviteTeamMember(ctx context.Context, req domain.InviteTeamMem
|
|||
}
|
||||
teamRole, err := s.resolveAndValidateTeamRole(ctx, req.TeamRole)
|
||||
if err != nil {
|
||||
if err == domain.ErrInvalidTeamRole {
|
||||
return domain.InviteTeamMemberRes{}, domain.ErrInvalidTeamRole
|
||||
}
|
||||
return domain.InviteTeamMemberRes{}, err
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,27 +4,55 @@ 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 "", domain.ErrInvalidTeamRole
|
||||
return "", teamRoleNotFoundError(raw)
|
||||
}
|
||||
|
||||
if id, err := strconv.ParseInt(key, 10, 64); err == nil && id > 0 {
|
||||
if s.rbacStore == nil {
|
||||
return "", domain.ErrInvalidTeamRole
|
||||
return "", teamRoleNotFoundError(raw)
|
||||
}
|
||||
rec, err := s.rbacStore.GetRoleByID(ctx, id)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return "", domain.ErrInvalidTeamRole
|
||||
return "", teamRoleNotFoundError(raw)
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
|
|
@ -33,7 +61,7 @@ func (s *Service) resolveAndValidateTeamRole(ctx context.Context, raw string) (d
|
|||
|
||||
normalized := domain.NormalizeTeamRoleKey(key)
|
||||
if normalized == "" || domain.IsForbiddenTeamMemberRole(normalized) {
|
||||
return "", domain.ErrInvalidTeamRole
|
||||
return "", teamRoleNotFoundError(raw)
|
||||
}
|
||||
|
||||
teamRole := domain.TeamRole(normalized)
|
||||
|
|
@ -41,16 +69,11 @@ func (s *Service) resolveAndValidateTeamRole(ctx context.Context, raw string) (d
|
|||
return teamRole, nil
|
||||
}
|
||||
|
||||
if s.rbacStore == nil {
|
||||
return "", domain.ErrInvalidTeamRole
|
||||
}
|
||||
if _, err := s.rbacStore.GetRoleByName(ctx, normalized); err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return "", domain.ErrInvalidTeamRole
|
||||
}
|
||||
rec, err := s.lookupRBACRole(ctx, key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return teamRole, nil
|
||||
return domain.TeamRole(rec.Name), nil
|
||||
}
|
||||
|
||||
func (s *Service) IsAssignableTeamRole(ctx context.Context, raw string) bool {
|
||||
|
|
|
|||
|
|
@ -3,6 +3,8 @@ package team
|
|||
import (
|
||||
"Yimaru-Backend/internal/domain"
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
|
|
@ -28,6 +30,15 @@ func (s *stubRBACStore) GetRoleByName(_ context.Context, name string) (domain.Ro
|
|||
}
|
||||
return domain.RoleRecord{}, pgx.ErrNoRows
|
||||
}
|
||||
func (s *stubRBACStore) GetRoleByNameIgnoreCase(_ context.Context, name string) (domain.RoleRecord, error) {
|
||||
upper := strings.ToUpper(strings.TrimSpace(name))
|
||||
for k, r := range s.roles {
|
||||
if strings.ToUpper(strings.TrimSpace(k)) == upper {
|
||||
return r, nil
|
||||
}
|
||||
}
|
||||
return domain.RoleRecord{}, pgx.ErrNoRows
|
||||
}
|
||||
func (s *stubRBACStore) ListRoles(context.Context, domain.RoleListFilter) ([]domain.RoleRecord, int64, error) {
|
||||
panic("not implemented")
|
||||
}
|
||||
|
|
@ -102,11 +113,29 @@ func TestResolveAndValidateTeamRole_ByRBACID(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveAndValidateTeamRole_UnknownRole(t *testing.T) {
|
||||
svc := &Service{rbacStore: &stubRBACStore{roles: map[string]domain.RoleRecord{}}}
|
||||
|
||||
_, err := svc.resolveAndValidateTeamRole(context.Background(), "NOT_A_ROLE")
|
||||
if err != domain.ErrInvalidTeamRole {
|
||||
if !errors.Is(err, domain.ErrInvalidTeamRole) {
|
||||
t.Fatalf("want ErrInvalidTeamRole, got %v", err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user