Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make org and project name case insensitive #867

Merged
merged 2 commits into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions core/authenticate/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
frontiersession "github.com/raystack/frontier/core/authenticate/session"
"github.com/raystack/frontier/core/serviceuser"
"github.com/raystack/frontier/internal/bootstrap/schema"
"github.com/raystack/frontier/internal/metrics"
"github.com/raystack/frontier/pkg/errors"

"github.com/lestrrat-go/jwx/v2/jwk"
Expand Down Expand Up @@ -739,6 +740,11 @@ func (s Service) getOrCreateUser(ctx context.Context, email, title string) (user
}

func (s Service) GetPrincipal(ctx context.Context, assertions ...ClientAssertion) (Principal, error) {
if metrics.ServiceOprLatency != nil {
promCollect := metrics.ServiceOprLatency("authenticate", "GetPrincipal")
defer promCollect()
}

var currentPrincipal Principal
if len(assertions) == 0 {
// check all assertions
Expand Down
6 changes: 6 additions & 0 deletions core/organization/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/raystack/frontier/core/relation"
"github.com/raystack/frontier/core/user"
"github.com/raystack/frontier/internal/bootstrap/schema"
"github.com/raystack/frontier/internal/metrics"
)

type Repository interface {
Expand Down Expand Up @@ -229,6 +230,11 @@ func (s Service) Update(ctx context.Context, org Organization) (Organization, er
}

func (s Service) ListByUser(ctx context.Context, principal authenticate.Principal, filter Filter) ([]Organization, error) {
if metrics.ServiceOprLatency != nil {
promCollect := metrics.ServiceOprLatency("organization", "ListByUser")
defer promCollect()
}

subjectIDs, err := s.relationService.LookupResources(ctx, relation.Relation{
Object: relation.Object{
Namespace: schema.OrganizationNamespace,
Expand Down
1 change: 1 addition & 0 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
func Init() {
initStripe()
initDB()
initService()
}

type HistogramFunc func(labelValue ...string) func()
Expand Down
16 changes: 16 additions & 0 deletions internal/metrics/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package metrics

import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)

var ServiceOprLatency HistogramFunc

func initService() {
ServiceOprLatency = createMeasureTime(promauto.NewHistogramVec(prometheus.HistogramOpts{
Name: "service_operation_latency",
Help: "Time taken for service operation to complete",
Buckets: []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10, 30, 60},
}, []string{"service", "operation"}))
}
1 change: 1 addition & 0 deletions internal/store/postgres/organization_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ func (r OrganizationRepository) Create(ctx context.Context, org organization.Org
if strings.TrimSpace(org.Name) == "" {
return organization.Organization{}, organization.ErrInvalidDetail
}
org.Name = strings.ToLower(org.Name)

marshaledMetadata, err := json.Marshal(org.Metadata)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions internal/store/postgres/organization_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,14 @@ func (s *OrganizationRepositoryTestSuite) TestCreate() {
},
ErrString: organization.ErrConflict.Error(),
},
{
Description: "should return error if organization name already exist case sensitive",
OrganizationToCreate: organization.Organization{
Name: "ORG-1",
Metadata: metadata.Metadata{},
},
ErrString: organization.ErrConflict.Error(),
},
{
Description: "should return error if organization name is empty",
OrganizationToCreate: organization.Organization{
Expand Down
1 change: 1 addition & 0 deletions internal/store/postgres/project_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func (r ProjectRepository) Create(ctx context.Context, prj project.Project) (pro
if strings.TrimSpace(prj.Name) == "" {
return project.Project{}, project.ErrInvalidDetail
}
prj.Name = strings.ToLower(prj.Name)

marshaledMetadata, err := json.Marshal(prj.Metadata)
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions internal/store/postgres/project_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,16 @@ func (s *ProjectRepositoryTestSuite) TestCreate() {
},
ErrString: project.ErrConflict.Error(),
},
{
Description: "should return error if project slug already exist case sensitive",
ProjectToCreate: project.Project{
Name: "PROJECT-2",
Organization: organization.Organization{
ID: s.orgs[0].ID,
},
},
ErrString: project.ErrConflict.Error(),
},
{
Description: "should return error if org id not an uuid",
ProjectToCreate: project.Project{
Expand Down
Loading