working commit
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"mbase/app/descriptor"
|
||||
"mbase/pkg/auxid"
|
||||
"mbase/pkg/auxpwd"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultSeedUsername = "certman"
|
||||
defaultSeedPassword = "certman"
|
||||
)
|
||||
|
||||
func (lg *Logic) CleanDatabase(ctx context.Context) error {
|
||||
var err error
|
||||
err = lg.db.CleanDatabase(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (lg *Logic) SeedAccount(ctx context.Context) (int64, error) {
|
||||
var err error
|
||||
var accountID int64
|
||||
accountDescrs, err := lg.db.ReducedListAccounts(ctx)
|
||||
if err != nil {
|
||||
return accountID, err
|
||||
}
|
||||
lg.log.Debugf("Seed account")
|
||||
if len(accountDescrs) == 0 {
|
||||
now := time.Now().Format(time.RFC3339)
|
||||
passhash := auxpwd.MakeSHA256Hash([]byte(defaultSeedPassword))
|
||||
accountDescr := &descriptor.Account{
|
||||
ID: auxid.GenID(),
|
||||
Username: defaultSeedUsername,
|
||||
Passhash: passhash,
|
||||
Disabled: false,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
err = lg.db.InsertAccount(ctx, accountDescr)
|
||||
if err != nil {
|
||||
return accountID, err
|
||||
}
|
||||
accountID = accountDescr.ID
|
||||
grantTypes := []string{
|
||||
descriptor.GrantModifyUsers,
|
||||
descriptor.GrantModifyDatabase,
|
||||
}
|
||||
for _, grantType := range grantTypes {
|
||||
grantDescr := &descriptor.Grant{
|
||||
AccountID: accountDescr.ID,
|
||||
Operation: grantType,
|
||||
CreatedAt: now,
|
||||
}
|
||||
err = lg.db.InsertGrant(ctx, grantDescr)
|
||||
if err != nil {
|
||||
return accountID, err
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return accountID, err
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user