Files
upstream-mirror 7e385fc126
Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
snapshot of https://github.com/cortezaproject/corteza.git (2024.9.x, 2026-09-18; blobs > 131072B omitted)
2026-09-18 01:22:23 +05:00

185 lines
3.9 KiB
Go

package store
import (
"context"
"regexp"
"strconv"
"time"
"github.com/cortezaproject/corteza/server/pkg/envoy/resource"
"github.com/cortezaproject/corteza/server/pkg/expr"
"github.com/cortezaproject/corteza/server/pkg/id"
"github.com/cortezaproject/corteza/server/store"
)
type (
// genericFilter defines resource identifiers in a generic manner
genericFilter struct {
id uint64
identifiers []string
}
)
var (
// simple uint check.
// we'll use the pkg/handle to check for handles.
refy = regexp.MustCompile(`^[1-9](\d*)$`)
// wrapper around NextID that will aid service testing
NextID = func() uint64 {
return id.Next()
}
// wrapper around time.Now() that will aid testing
now = func() *time.Time {
c := time.Now().Round(time.Second)
return &c
}
// Global expression parser
exprP = expr.Parser()
)
func makeGenericFilter(ii resource.Identifiers) (f genericFilter) {
for _, i := range ii {
if i == "" {
continue
}
if refy.MatchString(i) && f.id <= 0 {
id, err := strconv.ParseUint(i, 10, 64)
if err != nil {
continue
}
f.id = id
} else {
f.identifiers = append(f.identifiers, i)
}
}
return f
}
// resolveUserstamps tries to resolve any missing userstamp using the store
//
// @todo rework based on the bellow userIndex
func resolveUserstamps(ctx context.Context, s store.Storer, rr []resource.Interface, us *resource.Userstamps) (*resource.Userstamps, error) {
if us == nil {
return nil, nil
}
fetch := func(us *resource.Userstamp) (*resource.Userstamp, error) {
if us == nil || (us.UserID == 0 && us.Ref == "") {
return nil, nil
}
// This one can be considered as valid
if us.Ref != "" && us.UserID > 0 && us.U != nil {
return us, nil
}
ii := resource.MakeIdentifiers()
if us.UserID > 0 {
ii = ii.Add(strconv.FormatUint(us.UserID, 10))
}
if us.Ref != "" {
ii = ii.Add(us.Ref)
}
u, err := findUser(ctx, s, rr, ii)
if err != nil {
return nil, err
}
if u == nil {
return nil, resource.UserErrUnresolved(ii)
}
return resource.MakeUserstamp(u), nil
}
var err error
us.CreatedBy, err = fetch(us.CreatedBy)
us.UpdatedBy, err = fetch(us.UpdatedBy)
us.DeletedBy, err = fetch(us.DeletedBy)
us.OwnedBy, err = fetch(us.OwnedBy)
us.RunAs, err = fetch(us.RunAs)
if err != nil {
return nil, err
}
return us, nil
}
// syncUserStamps tries to resolve any missing userstamp using the userIndex
func syncUserStamps(us *resource.Userstamps, ux *userIndex) {
if us.CreatedBy != nil && us.CreatedBy.UserID > 0 {
us.CreatedBy.U = ux.users[us.CreatedBy.UserID]
}
if us.UpdatedBy != nil && us.UpdatedBy.UserID > 0 {
us.UpdatedBy.U = ux.users[us.UpdatedBy.UserID]
}
if us.DeletedBy != nil && us.DeletedBy.UserID > 0 {
us.DeletedBy.U = ux.users[us.DeletedBy.UserID]
}
if us.OwnedBy != nil && us.OwnedBy.UserID > 0 {
us.OwnedBy.U = ux.users[us.OwnedBy.UserID]
}
if us.RunAs != nil && us.RunAs.UserID > 0 {
us.RunAs.U = ux.users[us.RunAs.UserID]
}
}
func mergeConfig(ec *EncoderConfig, rs *resource.EnvoyConfig) *EncoderConfig {
// Nothing we can do
if rs == nil {
return ec
}
// Take resource config as base
rr := &EncoderConfig{
OnExisting: rs.OnExisting,
SkipIf: rs.SkipIf,
}
// Default to store config
rr.Defer = ec.Defer
rr.DeferNok = ec.DeferNok
rr.DeferOk = ec.DeferOk
if rr.OnExisting == resource.Default {
rr.OnExisting = ec.OnExisting
}
if rr.SkipIf == "" {
rr.SkipIf = ec.SkipIf
}
return rr
}
func basicSkipEval(ctx context.Context, cfg *EncoderConfig, missing bool) (bool, error) {
if cfg == nil {
cfg = &EncoderConfig{}
}
if cfg.SkipIf != "" {
evl, err := exprP.NewEvaluable(cfg.SkipIf)
if err != nil {
return false, err
}
// @todo expand this
skip, err := evl.EvalBool(ctx, map[string]interface{}{
"missing": missing,
})
if err != nil {
return false, err
}
return skip, nil
}
// Don't skip by default
return false, nil
}