70 lines
1.4 KiB
Go
70 lines
1.4 KiB
Go
package operator
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"mbase/pkg/auxpwd"
|
|
"mbase/pkg/auxuuid"
|
|
"mbase/pkg/descr"
|
|
)
|
|
|
|
const (
|
|
defaultSeedUsername = "mbase"
|
|
defaultSeedPassword = "mbase"
|
|
)
|
|
|
|
func (oper *Operator) CleanDatabase(ctx context.Context) error {
|
|
var err error
|
|
err = oper.db.CleanDatabase(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (oper *Operator) SeedAccount(ctx context.Context) (string, error) {
|
|
var err error
|
|
var accountID string
|
|
accountDescrs, err := oper.db.ReducedListAccounts(ctx)
|
|
if err != nil {
|
|
return accountID, err
|
|
}
|
|
oper.log.Debugf("Seed account")
|
|
if len(accountDescrs) == 0 {
|
|
now := time.Now().Format(time.RFC3339)
|
|
passhash := auxpwd.MakeSHA256Hash([]byte(defaultSeedPassword))
|
|
accountDescr := &descr.Account{
|
|
ID: auxuuid.NewUUID(),
|
|
Username: defaultSeedUsername,
|
|
Passhash: passhash,
|
|
Disabled: false,
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
err = oper.db.InsertAccount(ctx, accountDescr)
|
|
if err != nil {
|
|
return accountID, err
|
|
}
|
|
accountID = accountDescr.ID
|
|
grantTypes := []string{
|
|
descr.GrantModifyUsers,
|
|
descr.GrantModifyDatabase,
|
|
}
|
|
for _, grantType := range grantTypes {
|
|
grantDescr := &descr.Grant{
|
|
AccountID: accountDescr.ID,
|
|
Grantname: grantType,
|
|
CreatedAt: now,
|
|
}
|
|
err = oper.db.InsertGrant(ctx, grantDescr)
|
|
if err != nil {
|
|
return accountID, err
|
|
}
|
|
|
|
}
|
|
}
|
|
return accountID, err
|
|
|
|
}
|