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
|
-- name: GetRoleByName :one
|
||||||
SELECT * FROM roles WHERE name = $1;
|
SELECT * FROM roles WHERE name = $1;
|
||||||
|
|
||||||
|
-- name: GetRoleByNameIgnoreCase :one
|
||||||
|
SELECT * FROM roles WHERE UPPER(TRIM(name)) = UPPER(TRIM($1));
|
||||||
|
|
||||||
-- name: ListRoles :many
|
-- name: ListRoles :many
|
||||||
SELECT
|
SELECT
|
||||||
COUNT(*) OVER () AS total_count,
|
COUNT(*) OVER () AS total_count,
|
||||||
|
|
|
||||||
|
|
@ -166,6 +166,24 @@ func (q *Queries) GetRoleByName(ctx context.Context, name string) (Role, error)
|
||||||
return i, err
|
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
|
const GetRolePermissions = `-- name: GetRolePermissions :many
|
||||||
SELECT p.id, p.key, p.name, p.description, p.group_name, p.created_at
|
SELECT p.id, p.key, p.name, p.description, p.group_name, p.created_at
|
||||||
FROM permissions p
|
FROM permissions p
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,17 @@
|
||||||
package domain
|
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 {
|
type Permission struct {
|
||||||
ID int64 `json:"id"`
|
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.
|
// 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 {
|
func (r TeamRole) RBACRoleForJWT() Role {
|
||||||
if r.IsBuiltinTeamRole() {
|
if r.IsBuiltinTeamRole() {
|
||||||
return r.PlatformRole()
|
return r.PlatformRole()
|
||||||
}
|
}
|
||||||
return Role(NormalizeTeamRoleKey(string(r)))
|
return Role(strings.TrimSpace(string(r)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r TeamRole) String() string {
|
func (r TeamRole) String() string {
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ type RBACStore interface {
|
||||||
CreateRole(ctx context.Context, name, description string, isSystem bool) (domain.RoleRecord, error)
|
CreateRole(ctx context.Context, name, description string, isSystem bool) (domain.RoleRecord, error)
|
||||||
GetRoleByID(ctx context.Context, id int64) (domain.RoleRecord, error)
|
GetRoleByID(ctx context.Context, id int64) (domain.RoleRecord, error)
|
||||||
GetRoleByName(ctx context.Context, name string) (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)
|
ListRoles(ctx context.Context, filter domain.RoleListFilter) ([]domain.RoleRecord, int64, error)
|
||||||
UpdateRole(ctx context.Context, id int64, name, description string) error
|
UpdateRole(ctx context.Context, id int64, name, description string) error
|
||||||
DeleteRole(ctx context.Context, id int64) 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
|
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) {
|
func (s *Store) ListRoles(ctx context.Context, filter domain.RoleListFilter) ([]domain.RoleRecord, int64, error) {
|
||||||
var queryParam pgtype.Text
|
var queryParam pgtype.Text
|
||||||
if filter.Query != "" {
|
if filter.Query != "" {
|
||||||
|
|
|
||||||
|
|
@ -105,6 +105,10 @@ func (s *Service) SeedDefaultRolePermissions(ctx context.Context) error {
|
||||||
// --- Role CRUD (pass-through to store + reload cache) ---
|
// --- Role CRUD (pass-through to store + reload cache) ---
|
||||||
|
|
||||||
func (s *Service) CreateRole(ctx context.Context, name, description string) (domain.RoleRecord, error) {
|
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)
|
role, err := s.store.CreateRole(ctx, name, description, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return domain.RoleRecord{}, err
|
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 {
|
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 {
|
if err := s.store.UpdateRole(ctx, id, name, description); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,9 +20,6 @@ func (s *Service) InviteTeamMember(ctx context.Context, req domain.InviteTeamMem
|
||||||
}
|
}
|
||||||
teamRole, err := s.resolveAndValidateTeamRole(ctx, req.TeamRole)
|
teamRole, err := s.resolveAndValidateTeamRole(ctx, req.TeamRole)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == domain.ErrInvalidTeamRole {
|
|
||||||
return domain.InviteTeamMemberRes{}, domain.ErrInvalidTeamRole
|
|
||||||
}
|
|
||||||
return domain.InviteTeamMemberRes{}, err
|
return domain.InviteTeamMemberRes{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,27 +4,55 @@ import (
|
||||||
"Yimaru-Backend/internal/domain"
|
"Yimaru-Backend/internal/domain"
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/jackc/pgx/v5"
|
"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.
|
// 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) {
|
func (s *Service) resolveAndValidateTeamRole(ctx context.Context, raw string) (domain.TeamRole, error) {
|
||||||
key := strings.TrimSpace(raw)
|
key := strings.TrimSpace(raw)
|
||||||
if key == "" {
|
if key == "" {
|
||||||
return "", domain.ErrInvalidTeamRole
|
return "", teamRoleNotFoundError(raw)
|
||||||
}
|
}
|
||||||
|
|
||||||
if id, err := strconv.ParseInt(key, 10, 64); err == nil && id > 0 {
|
if id, err := strconv.ParseInt(key, 10, 64); err == nil && id > 0 {
|
||||||
if s.rbacStore == nil {
|
if s.rbacStore == nil {
|
||||||
return "", domain.ErrInvalidTeamRole
|
return "", teamRoleNotFoundError(raw)
|
||||||
}
|
}
|
||||||
rec, err := s.rbacStore.GetRoleByID(ctx, id)
|
rec, err := s.rbacStore.GetRoleByID(ctx, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, pgx.ErrNoRows) {
|
if errors.Is(err, pgx.ErrNoRows) {
|
||||||
return "", domain.ErrInvalidTeamRole
|
return "", teamRoleNotFoundError(raw)
|
||||||
}
|
}
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
@ -33,7 +61,7 @@ func (s *Service) resolveAndValidateTeamRole(ctx context.Context, raw string) (d
|
||||||
|
|
||||||
normalized := domain.NormalizeTeamRoleKey(key)
|
normalized := domain.NormalizeTeamRoleKey(key)
|
||||||
if normalized == "" || domain.IsForbiddenTeamMemberRole(normalized) {
|
if normalized == "" || domain.IsForbiddenTeamMemberRole(normalized) {
|
||||||
return "", domain.ErrInvalidTeamRole
|
return "", teamRoleNotFoundError(raw)
|
||||||
}
|
}
|
||||||
|
|
||||||
teamRole := domain.TeamRole(normalized)
|
teamRole := domain.TeamRole(normalized)
|
||||||
|
|
@ -41,16 +69,11 @@ func (s *Service) resolveAndValidateTeamRole(ctx context.Context, raw string) (d
|
||||||
return teamRole, nil
|
return teamRole, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.rbacStore == nil {
|
rec, err := s.lookupRBACRole(ctx, key)
|
||||||
return "", domain.ErrInvalidTeamRole
|
if err != nil {
|
||||||
}
|
|
||||||
if _, err := s.rbacStore.GetRoleByName(ctx, normalized); err != nil {
|
|
||||||
if errors.Is(err, pgx.ErrNoRows) {
|
|
||||||
return "", domain.ErrInvalidTeamRole
|
|
||||||
}
|
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return teamRole, nil
|
return domain.TeamRole(rec.Name), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) IsAssignableTeamRole(ctx context.Context, raw string) bool {
|
func (s *Service) IsAssignableTeamRole(ctx context.Context, raw string) bool {
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,8 @@ package team
|
||||||
import (
|
import (
|
||||||
"Yimaru-Backend/internal/domain"
|
"Yimaru-Backend/internal/domain"
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/jackc/pgx/v5"
|
"github.com/jackc/pgx/v5"
|
||||||
|
|
@ -28,6 +30,15 @@ func (s *stubRBACStore) GetRoleByName(_ context.Context, name string) (domain.Ro
|
||||||
}
|
}
|
||||||
return domain.RoleRecord{}, pgx.ErrNoRows
|
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) {
|
func (s *stubRBACStore) ListRoles(context.Context, domain.RoleListFilter) ([]domain.RoleRecord, int64, error) {
|
||||||
panic("not implemented")
|
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) {
|
func TestResolveAndValidateTeamRole_UnknownRole(t *testing.T) {
|
||||||
svc := &Service{rbacStore: &stubRBACStore{roles: map[string]domain.RoleRecord{}}}
|
svc := &Service{rbacStore: &stubRBACStore{roles: map[string]domain.RoleRecord{}}}
|
||||||
|
|
||||||
_, err := svc.resolveAndValidateTeamRole(context.Background(), "NOT_A_ROLE")
|
_, 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)
|
t.Fatalf("want ErrInvalidTeamRole, got %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user