mirror of
https://github.com/paralus/paralus.git
synced 2026-05-06 00:17:19 +00:00
Basic entitlements working
This commit is contained in:
@@ -3,12 +3,12 @@ package dao
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/RafayLabs/rcloud-base/internal/models"
|
||||
"github.com/google/uuid"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
func GetProjectOrganization(ctx context.Context, db bun.IDB, id uuid.UUID) (string, string, error) {
|
||||
// Could possibly union them later for some speedup
|
||||
type projectOrg struct {
|
||||
Project string
|
||||
Organization string
|
||||
@@ -27,3 +27,39 @@ func GetProjectOrganization(ctx context.Context, db bun.IDB, id uuid.UUID) (stri
|
||||
}
|
||||
return r.Project, r.Organization, nil
|
||||
}
|
||||
|
||||
func GetFileteredProjects(ctx context.Context, db bun.IDB, account, partner, org uuid.UUID) ([]models.Project, error) {
|
||||
ids := []uuid.UUID{}
|
||||
sp := []models.SentryPermission{}
|
||||
err := db.NewSelect().Model(&sp).
|
||||
Where("sentry_permission.partner_id = ?", partner).
|
||||
Where("sentry_permission.organization_id = ?", org).
|
||||
Where("sentry_permission.account_id = ?", account).
|
||||
Scan(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
all := false
|
||||
for _, p := range sp {
|
||||
if p.ProjectId == uuid.Nil {
|
||||
all = true
|
||||
break
|
||||
}
|
||||
ids = append(ids, p.ProjectId)
|
||||
}
|
||||
|
||||
prjs := []models.Project{}
|
||||
if !all && len(ids) == 0 {
|
||||
return prjs, nil
|
||||
}
|
||||
q := db.NewSelect().Model(&prjs).
|
||||
Where("project.partner_id = ?", partner).
|
||||
Where("project.organization_id = ?", org).
|
||||
Where("project.trash = ?", false)
|
||||
if !all {
|
||||
q = q.Where("project.id IN (?)", bun.In(ids))
|
||||
}
|
||||
err = q.Scan(ctx)
|
||||
return prjs, err
|
||||
}
|
||||
|
||||
@@ -237,13 +237,21 @@ func (s *projectService) List(ctx context.Context, project *systemv3.Project) (*
|
||||
if err != nil {
|
||||
return &systemv3.ProjectList{}, err
|
||||
}
|
||||
var projs []models.Project
|
||||
entities, err := dao.List(ctx, s.db, uuid.NullUUID{UUID: part.ID, Valid: true}, uuid.NullUUID{UUID: org.ID, Valid: true}, &projs)
|
||||
|
||||
// name := project.GetMetadata().GetName() // TODO: get this working
|
||||
name := "user2.name@provider.com"
|
||||
|
||||
entity, err := dao.GetByTraits(ctx, s.db, name, &models.KratosIdentities{})
|
||||
if err != nil {
|
||||
return &systemv3.ProjectList{}, err
|
||||
}
|
||||
if projs, ok := entities.(*[]models.Project); ok {
|
||||
for _, proj := range *projs {
|
||||
|
||||
if usr, ok := entity.(*models.KratosIdentities); ok {
|
||||
projs, err := dao.GetFileteredProjects(ctx, s.db, usr.ID, part.ID, org.ID)
|
||||
if err != nil {
|
||||
return &systemv3.ProjectList{}, err
|
||||
}
|
||||
for _, proj := range projs {
|
||||
labels := make(map[string]string)
|
||||
labels["organization"] = proj.OrganizationId.String()
|
||||
labels["partner"] = proj.PartnerId.String()
|
||||
@@ -268,10 +276,8 @@ func (s *projectService) List(ctx context.Context, project *systemv3.Project) (*
|
||||
Count: int64(len(projects)),
|
||||
}
|
||||
projectList.Items = projects
|
||||
return projectList, nil
|
||||
}
|
||||
|
||||
} else {
|
||||
return projectList, fmt.Errorf("missing organization id in metadata")
|
||||
}
|
||||
return projectList, nil
|
||||
return projectList, fmt.Errorf("missing organization id in metadata")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user