working commit
This commit is contained in:
@@ -0,0 +1,232 @@
|
||||
package operator
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"mbase/pkg/descr"
|
||||
"mbase/pkg/auxuuid"
|
||||
"mbase/pkg/auxpwd"
|
||||
"mbase/pkg/mbctl"
|
||||
)
|
||||
|
||||
func (lg *Logic) ValidateAcount(ctx context.Context, username, password string) (bool, string, error) {
|
||||
var err error
|
||||
var accountID string
|
||||
var valid bool
|
||||
|
||||
lg.WaitRestoring()
|
||||
|
||||
accountExists, accountDescr, err := lg.db.GetAccountByUsername(ctx, username)
|
||||
if !accountExists {
|
||||
err := fmt.Errorf("Account not exists")
|
||||
return valid, accountID, err
|
||||
}
|
||||
if !auxpwd.PasswordMatchCompat([]byte(password), accountDescr.Passhash) {
|
||||
err := fmt.Errorf("Login data mismatch")
|
||||
return valid, accountID, err
|
||||
}
|
||||
valid = true
|
||||
accountID = accountDescr.ID
|
||||
return valid, accountID, err
|
||||
}
|
||||
|
||||
func (lg *Logic) CreateAccount(ctx context.Context, accountID string, params *mbctl.CreateAccountParams) (*mbctl.CreateAccountResult, error) {
|
||||
var err error
|
||||
res := &mbctl.CreateAccountResult{}
|
||||
|
||||
lg.WaitDumping()
|
||||
lg.WaitRestoring()
|
||||
|
||||
grantExists, _, err := lg.db.GetGrant(ctx, accountID, descr.GrantModifyUsers)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
if !grantExists {
|
||||
err := fmt.Errorf("Operation not allowed for the user")
|
||||
return res, err
|
||||
}
|
||||
if params.Username == "" {
|
||||
err := fmt.Errorf("Empty username parameters")
|
||||
return res, err
|
||||
}
|
||||
|
||||
if params.Password == "" {
|
||||
err := fmt.Errorf("Empty password parameter")
|
||||
return res, err
|
||||
}
|
||||
|
||||
accountExists, _, err := lg.db.GetAccountByUsername(ctx, params.Username)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
if accountExists {
|
||||
err := fmt.Errorf("Account with thist name already exists")
|
||||
return res, err
|
||||
}
|
||||
now := time.Now().Format(time.RFC3339)
|
||||
passhash := auxpwd.MakeSHA256Hash([]byte(params.Password))
|
||||
accountDescr := &descr.Account{
|
||||
ID: auxuuid.NewUUID(),
|
||||
Username: params.Username,
|
||||
Passhash: passhash,
|
||||
Disabled: false,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
err = lg.db.InsertAccount(ctx, accountDescr)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
res.AccountID = accountDescr.ID
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (lg *Logic) UpdateAccount(ctx context.Context, accountID string, params *mbctl.UpdateAccountParams) (*mbctl.UpdateAccountResult, error) {
|
||||
var err error
|
||||
res := &mbctl.UpdateAccountResult{}
|
||||
|
||||
lg.WaitRestoring()
|
||||
lg.WaitDumping()
|
||||
|
||||
grantExists, _, err := lg.db.GetGrant(ctx, accountID, descr.GrantModifyUsers)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
if !grantExists {
|
||||
err := fmt.Errorf("Operation not allowed for the user")
|
||||
return res, err
|
||||
}
|
||||
|
||||
var accountDescr *descr.Account
|
||||
var accountExists bool
|
||||
switch {
|
||||
case params.AccountID != "":
|
||||
accountExists, accountDescr, err = lg.db.GetAccountByID(ctx, params.AccountID)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
case params.Username != "":
|
||||
accountExists, accountDescr, err = lg.db.GetAccountByUsername(ctx, params.Username)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
}
|
||||
if !accountExists {
|
||||
err := fmt.Errorf("Account with this is or name dont exists")
|
||||
return res, err
|
||||
}
|
||||
now := time.Now().Format(time.RFC3339)
|
||||
if params.NewUsername != "" {
|
||||
accountDescr.UpdatedAt = now
|
||||
accountDescr.Username = params.NewUsername
|
||||
}
|
||||
if params.NewPassword != "" {
|
||||
passhash := auxpwd.MakeSHA256Hash([]byte(params.NewPassword))
|
||||
|
||||
accountDescr.UpdatedAt = now
|
||||
accountDescr.Passhash = passhash
|
||||
}
|
||||
if params.Disabled != accountDescr.Disabled {
|
||||
accountDescr.UpdatedAt = now
|
||||
accountDescr.Disabled = params.Disabled
|
||||
}
|
||||
|
||||
err = lg.db.UpdateAccountByID(ctx, accountDescr.ID, accountDescr)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (lg *Logic) DeleteAccount(ctx context.Context, accountID string, params *mbctl.DeleteAccountParams) (*mbctl.DeleteAccountResult, error) {
|
||||
var err error
|
||||
res := &mbctl.DeleteAccountResult{}
|
||||
|
||||
lg.WaitDumping()
|
||||
lg.WaitRestoring()
|
||||
|
||||
grantExists, _, err := lg.db.GetGrant(ctx, accountID, descr.GrantModifyUsers)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
if !grantExists {
|
||||
err := fmt.Errorf("Operation not allowed for the user")
|
||||
return res, err
|
||||
}
|
||||
|
||||
var accountDescr *descr.Account
|
||||
var accountExists bool
|
||||
switch {
|
||||
case params.AccountID != "":
|
||||
accountExists, accountDescr, err = lg.db.GetAccountByID(ctx, params.AccountID)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
case params.Username != "":
|
||||
accountExists, accountDescr, err = lg.db.GetAccountByUsername(ctx, params.Username)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
}
|
||||
if !accountExists {
|
||||
err := fmt.Errorf("Account with this is or name dont exists")
|
||||
return res, err
|
||||
}
|
||||
|
||||
err = lg.db.DeleteAllGrantsForAccountID(ctx, accountDescr.ID)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
err = lg.db.DeleteAccountByID(ctx, accountDescr.ID)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (lg *Logic) ListAccounts(ctx context.Context, accountID string, params *mbctl.ListAccountsParams) (*mbctl.ListAccountsResult, error) {
|
||||
var err error
|
||||
res := &mbctl.ListAccountsResult{
|
||||
Accounts: make([]*mbctl.AccountShortDescr, 0),
|
||||
}
|
||||
|
||||
lg.WaitRestoring()
|
||||
|
||||
grantExists, _, err := lg.db.GetGrant(ctx, accountID, descr.GrantModifyUsers)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
if !grantExists {
|
||||
err := fmt.Errorf("Operation not allowed for the user")
|
||||
return res, err
|
||||
}
|
||||
|
||||
accountDescrs, err := lg.db.ReducedListAccounts(ctx)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
for _, accountDescr := range accountDescrs {
|
||||
accountShortDescr := &mbctl.AccountShortDescr{
|
||||
Username: accountDescr.Username,
|
||||
Disabled: accountDescr.Disabled,
|
||||
CreatedAt: accountDescr.CreatedAt,
|
||||
UpdatedAt: accountDescr.UpdatedAt,
|
||||
Grants: make([]*mbctl.GrantShortDescr, 0),
|
||||
}
|
||||
grantDescrs, err := lg.db.ListGrantsByAccountID(ctx, accountDescr.ID)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
for _, grantDescrs := range grantDescrs {
|
||||
grantShortDescrs := &mbctl.GrantShortDescr{
|
||||
Operation: grantDescrs.Operation,
|
||||
CreatedAt: grantDescrs.CreatedAt,
|
||||
}
|
||||
accountShortDescr.Grants = append(accountShortDescr.Grants, grantShortDescrs)
|
||||
}
|
||||
res.Accounts = append(res.Accounts, accountShortDescr)
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
Reference in New Issue
Block a user