mirror of
https://github.com/hanzoai/o11y.git
synced 2026-10-15 12:58:33 +00:00
o11y carried a second identity system: email/password sign-in, Google,
OIDC and SAML callbacks, self-registration, invites, reset-password
tokens, roles-on-users, SSO auth domains, and a pair of tokenizers that
minted and stored its own sessions. Hanzo IAM answers all of it, once,
at the edge. Two credential stores is one too many, and the one nobody
signs into is the one that rots.
What is left is Identity: WHO a request is, resolved from what the edge
asserted or from a service-account key, never issued here. The routes are
not merely unrouted — the handlers, stores, tables and templates behind
them are gone, and anonymous_test.go names each dead address so that
re-exempting one fails even though no route can come back.
The census moves with it: 366 addresses to 329, 353 typed ops to 319, ten
escape hatches to seven. zipdoc_gen.go loses the 34 descriptions whose
addresses left, because zip keys its doc registry by method and path, so
a description outliving its declaration attaches this module's prose to
whatever a host serves at that address. zipdoc_test.go pins that.
Cherry-picked from delete/o11y-identity 89854a68ae.
157 lines
3.6 KiB
Go
157 lines
3.6 KiB
Go
package impluser
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/hanzoai/o11y/pkg/factory"
|
|
"github.com/hanzoai/o11y/pkg/sqlstore"
|
|
"github.com/hanzoai/o11y/pkg/types"
|
|
"github.com/hanzoai/o11y/pkg/valuer"
|
|
"github.com/uptrace/bun"
|
|
)
|
|
|
|
type store struct {
|
|
sqlstore sqlstore.SQLStore
|
|
settings factory.ProviderSettings
|
|
}
|
|
|
|
func NewStore(sqlstore sqlstore.SQLStore, settings factory.ProviderSettings) types.UserStore {
|
|
return &store{sqlstore: sqlstore, settings: settings}
|
|
}
|
|
|
|
func (store *store) CreateUser(ctx context.Context, user *types.User) error {
|
|
_, err := store.
|
|
sqlstore.
|
|
BunDBCtx(ctx).
|
|
NewInsert().
|
|
Model(user).
|
|
Exec(ctx)
|
|
if err != nil {
|
|
return store.sqlstore.WrapAlreadyExistsErrf(err, types.ErrUserAlreadyExists, "user with email %s already exists in org %s", user.Email, user.OrgID)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (store *store) GetByOrgIDAndID(ctx context.Context, orgID valuer.UUID, id valuer.UUID) (*types.User, error) {
|
|
user := new(types.User)
|
|
|
|
err := store.
|
|
sqlstore.
|
|
BunDBCtx(ctx).
|
|
NewSelect().
|
|
Model(user).
|
|
Where("org_id = ?", orgID).
|
|
Where("id = ?", id).
|
|
Scan(ctx)
|
|
if err != nil {
|
|
return nil, store.sqlstore.WrapNotFoundErrf(err, types.ErrCodeUserNotFound, "user with id %s does not exist", id)
|
|
}
|
|
|
|
return user, nil
|
|
}
|
|
|
|
func (store *store) ListUsersByOrgID(ctx context.Context, orgID valuer.UUID) ([]*types.User, error) {
|
|
users := []*types.User{}
|
|
|
|
err := store.
|
|
sqlstore.
|
|
BunDBCtx(ctx).
|
|
NewSelect().
|
|
Model(&users).
|
|
Where("org_id = ?", orgID).
|
|
Scan(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return users, nil
|
|
}
|
|
|
|
func (store *store) CountByOrgID(ctx context.Context, orgID valuer.UUID) (int64, error) {
|
|
user := new(types.User)
|
|
|
|
count, err := store.
|
|
sqlstore.
|
|
BunDB().
|
|
NewSelect().
|
|
Model(user).
|
|
Where("org_id = ?", orgID).
|
|
Count(ctx)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
return int64(count), nil
|
|
}
|
|
|
|
func (store *store) CountByOrgIDAndStatuses(ctx context.Context, orgID valuer.UUID, statuses []string) (map[valuer.String]int64, error) {
|
|
user := new(types.User)
|
|
var results []struct {
|
|
Status valuer.String `bun:"status"`
|
|
Count int64 `bun:"count"`
|
|
}
|
|
|
|
err := store.
|
|
sqlstore.
|
|
BunDBCtx(ctx).
|
|
NewSelect().
|
|
Model(user).
|
|
ColumnExpr("status").
|
|
ColumnExpr("COUNT(*) AS count").
|
|
Where("org_id = ?", orgID.StringValue()).
|
|
Where("status IN (?)", bun.In(statuses)).
|
|
GroupExpr("status").
|
|
Scan(ctx, &results)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
counts := make(map[valuer.String]int64, len(results))
|
|
for _, r := range results {
|
|
counts[r.Status] = r.Count
|
|
}
|
|
|
|
return counts, nil
|
|
}
|
|
|
|
func (store *store) RunInTx(ctx context.Context, cb func(ctx context.Context) error) error {
|
|
return store.sqlstore.RunInTxCtx(ctx, nil, func(ctx context.Context) error {
|
|
return cb(ctx)
|
|
})
|
|
}
|
|
|
|
func (store *store) GetRootUserByOrgID(ctx context.Context, orgID valuer.UUID) (*types.User, error) {
|
|
user := new(types.User)
|
|
err := store.
|
|
sqlstore.
|
|
BunDBCtx(ctx).
|
|
NewSelect().
|
|
Model(user).
|
|
Where("org_id = ?", orgID).
|
|
Where("is_root = ?", true).
|
|
Scan(ctx)
|
|
if err != nil {
|
|
return nil, store.sqlstore.WrapNotFoundErrf(err, types.ErrCodeUserNotFound, "root user for org %s not found", orgID)
|
|
}
|
|
return user, nil
|
|
}
|
|
|
|
func (store *store) GetUsersByOrgIDAndRoleID(ctx context.Context, orgID valuer.UUID, roleID valuer.UUID) ([]*types.User, error) {
|
|
users := []*types.User{}
|
|
|
|
err := store.
|
|
sqlstore.
|
|
BunDBCtx(ctx).
|
|
NewSelect().
|
|
Model(&users).
|
|
Join(`JOIN user_role ON user_role.user_id = "users".id`).
|
|
Where(`"users".org_id = ?`, orgID).
|
|
Where("user_role.role_id = ?", roleID).
|
|
Scan(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return users, nil
|
|
}
|