feat: support bind user to tenants
This commit is contained in:
10
backend/database/migrations/20241212111730_alter_tenants.sql
Normal file
10
backend/database/migrations/20241212111730_alter_tenants.sql
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
-- +goose Up
|
||||||
|
-- +goose StatementBegin
|
||||||
|
-- add column bind_user_id to tenants
|
||||||
|
ALTER TABLE tenants ADD COLUMN bind_user_id INT8 NOT NULL DEFAULT 0;
|
||||||
|
-- +goose StatementEnd
|
||||||
|
|
||||||
|
-- +goose Down
|
||||||
|
-- +goose StatementBegin
|
||||||
|
DROP COLUMN bind_user_id FROM tenants;
|
||||||
|
-- +goose StatementEnd
|
||||||
@@ -19,4 +19,5 @@ type Tenants struct {
|
|||||||
ExpireAt time.Time `json:"expire_at"`
|
ExpireAt time.Time `json:"expire_at"`
|
||||||
CreatedAt time.Time `json:"created_at"`
|
CreatedAt time.Time `json:"created_at"`
|
||||||
UpdatedAt time.Time `json:"updated_at"`
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
|
BindUserID int64 `json:"bind_user_id"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ type tenantsTable struct {
|
|||||||
ExpireAt postgres.ColumnTimestamp
|
ExpireAt postgres.ColumnTimestamp
|
||||||
CreatedAt postgres.ColumnTimestamp
|
CreatedAt postgres.ColumnTimestamp
|
||||||
UpdatedAt postgres.ColumnTimestamp
|
UpdatedAt postgres.ColumnTimestamp
|
||||||
|
BindUserID postgres.ColumnInteger
|
||||||
|
|
||||||
AllColumns postgres.ColumnList
|
AllColumns postgres.ColumnList
|
||||||
MutableColumns postgres.ColumnList
|
MutableColumns postgres.ColumnList
|
||||||
@@ -71,8 +72,9 @@ func newTenantsTableImpl(schemaName, tableName, alias string) tenantsTable {
|
|||||||
ExpireAtColumn = postgres.TimestampColumn("expire_at")
|
ExpireAtColumn = postgres.TimestampColumn("expire_at")
|
||||||
CreatedAtColumn = postgres.TimestampColumn("created_at")
|
CreatedAtColumn = postgres.TimestampColumn("created_at")
|
||||||
UpdatedAtColumn = postgres.TimestampColumn("updated_at")
|
UpdatedAtColumn = postgres.TimestampColumn("updated_at")
|
||||||
allColumns = postgres.ColumnList{IDColumn, NameColumn, SlugColumn, DescriptionColumn, ExpireAtColumn, CreatedAtColumn, UpdatedAtColumn}
|
BindUserIDColumn = postgres.IntegerColumn("bind_user_id")
|
||||||
mutableColumns = postgres.ColumnList{NameColumn, SlugColumn, DescriptionColumn, ExpireAtColumn, CreatedAtColumn, UpdatedAtColumn}
|
allColumns = postgres.ColumnList{IDColumn, NameColumn, SlugColumn, DescriptionColumn, ExpireAtColumn, CreatedAtColumn, UpdatedAtColumn, BindUserIDColumn}
|
||||||
|
mutableColumns = postgres.ColumnList{NameColumn, SlugColumn, DescriptionColumn, ExpireAtColumn, CreatedAtColumn, UpdatedAtColumn, BindUserIDColumn}
|
||||||
)
|
)
|
||||||
|
|
||||||
return tenantsTable{
|
return tenantsTable{
|
||||||
@@ -86,6 +88,7 @@ func newTenantsTableImpl(schemaName, tableName, alias string) tenantsTable {
|
|||||||
ExpireAt: ExpireAtColumn,
|
ExpireAt: ExpireAtColumn,
|
||||||
CreatedAt: CreatedAtColumn,
|
CreatedAt: CreatedAtColumn,
|
||||||
UpdatedAt: UpdatedAtColumn,
|
UpdatedAt: UpdatedAtColumn,
|
||||||
|
BindUserID: BindUserIDColumn,
|
||||||
|
|
||||||
AllColumns: allColumns,
|
AllColumns: allColumns,
|
||||||
MutableColumns: mutableColumns,
|
MutableColumns: mutableColumns,
|
||||||
|
|||||||
39
backend/modules/commands/tenant/bind.go
Normal file
39
backend/modules/commands/tenant/bind.go
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
package tenant
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"backend/modules/users"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
// @provider
|
||||||
|
type Bind struct {
|
||||||
|
userSvc *users.Service
|
||||||
|
log *log.Entry `inject:"false"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prepare
|
||||||
|
func (d *Bind) Prepare() error {
|
||||||
|
d.log = log.WithField("module", "tenants.bind")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Bind) RunE(slug string, userID int64) error {
|
||||||
|
d.log.Infof("bind tenant %s with user %d", slug, userID)
|
||||||
|
|
||||||
|
tenant, err := d.userSvc.GetTenantBySlug(context.Background(), slug)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "get tenant: %s", slug)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = d.userSvc.SetTenantBindUserID(context.Background(), tenant.ID, userID)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "bind tenant: %s with user %d", tenant.ID, userID)
|
||||||
|
}
|
||||||
|
|
||||||
|
d.log.Infof("bind tenant success: %s(%d) with user %d", slug, tenant.ID, userID)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -8,6 +8,20 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Provide(opts ...opt.Option) error {
|
func Provide(opts ...opt.Option) error {
|
||||||
|
if err := container.Container.Provide(func(
|
||||||
|
userSvc *users.Service,
|
||||||
|
) (*Bind, error) {
|
||||||
|
obj := &Bind{
|
||||||
|
userSvc: userSvc,
|
||||||
|
}
|
||||||
|
if err := obj.Prepare(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return obj, nil
|
||||||
|
}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if err := container.Container.Provide(func(
|
if err := container.Container.Provide(func(
|
||||||
userSvc *users.Service,
|
userSvc *users.Service,
|
||||||
) (*Create, error) {
|
) (*Create, error) {
|
||||||
|
|||||||
@@ -396,3 +396,20 @@ func (svc *Service) GetTenantUserBalance(ctx context.Context, tenantID, userID i
|
|||||||
}
|
}
|
||||||
return result.Balance, nil
|
return result.Balance, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetTenantBindUserID
|
||||||
|
func (svc *Service) SetTenantBindUserID(ctx context.Context, tenantID, adminUserID int64) error {
|
||||||
|
log := svc.log.WithField("method", "SetTenantBindUserID")
|
||||||
|
|
||||||
|
tbl := table.Tenants
|
||||||
|
stmt := tbl.
|
||||||
|
UPDATE(tbl.BindUserID).
|
||||||
|
SET(Int64(adminUserID)).
|
||||||
|
WHERE(tbl.ID.EQ(Int64(tenantID)))
|
||||||
|
log.Debug(stmt.DebugSql())
|
||||||
|
|
||||||
|
if _, err := stmt.ExecContext(ctx, db.FromContext(ctx, svc.db)); err != nil {
|
||||||
|
return errors.Wrap(err, "failed to set tenant admin user id")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import (
|
|||||||
"git.ipao.vip/rogeecn/atom"
|
"git.ipao.vip/rogeecn/atom"
|
||||||
"git.ipao.vip/rogeecn/atom/container"
|
"git.ipao.vip/rogeecn/atom/container"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"github.com/spf13/cast"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -72,5 +73,31 @@ func Command() atom.Option {
|
|||||||
})
|
})
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
|
|
||||||
|
atom.Command(
|
||||||
|
atom.Name("bind"),
|
||||||
|
atom.Short("设置租户的管理员ID"),
|
||||||
|
atom.Example("bind slug user_id"),
|
||||||
|
atom.Providers(defaultProviders().With(
|
||||||
|
medias.Provide,
|
||||||
|
users.Provide,
|
||||||
|
tenant.Provide,
|
||||||
|
)),
|
||||||
|
atom.Arguments(func(cmd *cobra.Command) {
|
||||||
|
}),
|
||||||
|
atom.RunE(func(cmd *cobra.Command, args []string) error {
|
||||||
|
return container.Container.Invoke(func(t *tenant.Bind) error {
|
||||||
|
slug := args[0]
|
||||||
|
userID, err := cast.ToInt64E(args[1]) // format 2024-01-01
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "parse user_id failed: %s", args[1])
|
||||||
|
}
|
||||||
|
|
||||||
|
// parse expire string as time.Time
|
||||||
|
|
||||||
|
return t.RunE(slug, userID)
|
||||||
|
})
|
||||||
|
}),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user