snapshot of https://github.com/cortezaproject/corteza.git (2024.9.x, 2026-09-18; blobs > 131072B omitted)
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
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
This commit is contained in:
135
server/pkg/provision/users.go
Normal file
135
server/pkg/provision/users.go
Normal file
@@ -0,0 +1,135 @@
|
||||
package provision
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/cortezaproject/corteza/server/pkg/auth"
|
||||
"github.com/cortezaproject/corteza/server/pkg/filter"
|
||||
"github.com/cortezaproject/corteza/server/pkg/id"
|
||||
"github.com/cortezaproject/corteza/server/pkg/logger"
|
||||
"github.com/cortezaproject/corteza/server/pkg/sass"
|
||||
"github.com/cortezaproject/corteza/server/store"
|
||||
"github.com/cortezaproject/corteza/server/system/types"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// SystemUsers creates or updates system users
|
||||
func SystemUsers(ctx context.Context, log *zap.Logger, s store.Users) (uu []*types.User, err error) {
|
||||
uu = types.UserSet{
|
||||
&types.User{
|
||||
Email: "provision@corteza.local",
|
||||
Name: "Corteza Provisioner",
|
||||
Handle: auth.ProvisionUserHandle,
|
||||
Kind: types.SystemUser,
|
||||
},
|
||||
&types.User{
|
||||
Email: "service@corteza.local",
|
||||
Name: "Corteza Service",
|
||||
Handle: auth.ServiceUserHandle,
|
||||
Kind: types.SystemUser,
|
||||
},
|
||||
&types.User{
|
||||
Email: "federation@corteza.local",
|
||||
Name: "Corteza Federation",
|
||||
Handle: auth.FederationUserHandle,
|
||||
Kind: types.SystemUser,
|
||||
},
|
||||
}
|
||||
|
||||
m, err := loadSystemUsers(ctx, s)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for i := range uu {
|
||||
u := uu[i]
|
||||
if m[u.Handle] == nil {
|
||||
// this is a new user
|
||||
u.ID = id.Next()
|
||||
u.CreatedAt = *now()
|
||||
|
||||
if err := store.UpsertUser(ctx, s, u); err != nil {
|
||||
return nil, fmt.Errorf("failed to provision system user %s: %w", u.Handle, err)
|
||||
}
|
||||
|
||||
log.Info("creating system user", zap.String("handle", u.Handle), logger.Uint64("ID", u.ID))
|
||||
} else {
|
||||
|
||||
u.ID = m[u.Handle].ID
|
||||
|
||||
// There is no need to update system users if they are unchanged
|
||||
if m[u.Handle].UpdatedAt == nil &&
|
||||
m[u.Handle].SuspendedAt == nil &&
|
||||
m[u.Handle].DeletedAt == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
// Make sure all values are as they should be
|
||||
u.CreatedAt = m[u.Handle].CreatedAt
|
||||
u.Email = m[u.Handle].Email
|
||||
u.Name = m[u.Handle].Name
|
||||
u.UpdatedAt = nil
|
||||
u.SuspendedAt = nil
|
||||
u.DeletedAt = nil
|
||||
|
||||
if err := store.UpsertUser(ctx, s, u); err != nil {
|
||||
return nil, fmt.Errorf("failed to provision system user %s: %w", u.Handle, err)
|
||||
}
|
||||
|
||||
log.Info("updating system user", zap.String("handle", u.Handle), logger.Uint64("ID", u.ID))
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func loadSystemUsers(ctx context.Context, s store.Users) (m map[string]*types.User, err error) {
|
||||
var (
|
||||
f = types.UserFilter{
|
||||
Suspended: filter.StateInclusive,
|
||||
Deleted: filter.StateInclusive,
|
||||
Kind: types.SystemUser,
|
||||
}
|
||||
)
|
||||
|
||||
m = make(map[string]*types.User)
|
||||
if set, _, err := store.SearchUsers(ctx, s, f); err == nil {
|
||||
for _, r := range set {
|
||||
m[r.Handle] = r
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func setUsersTheme(ctx context.Context, log *zap.Logger, s store.Users) (err error) {
|
||||
log.Info("provision start")
|
||||
defer log.Info("provision end")
|
||||
|
||||
var (
|
||||
f = types.UserFilter{
|
||||
Deleted: filter.StateInclusive,
|
||||
}
|
||||
|
||||
users []*types.User
|
||||
)
|
||||
|
||||
if set, _, err := store.SearchUsers(ctx, s, f); err == nil {
|
||||
for _, r := range set {
|
||||
if r.Meta.Theme == "" {
|
||||
r.Meta.Theme = sass.LightTheme
|
||||
|
||||
users = append(users, r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
err = store.UpdateUser(ctx, s, users...)
|
||||
if err != nil {
|
||||
log.Error("error provisioning users theme", zap.Error(err))
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user