71 lines
1.6 KiB
Go
71 lines
1.6 KiB
Go
/*
|
|
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
|
|
*
|
|
* This work is published and licensed under a Creative Commons
|
|
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
|
|
*
|
|
* Distribution of this work is permitted, but commercial use and
|
|
* modifications are strictly prohibited.
|
|
*/
|
|
package accoper
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"mstore/pkg/auxpwd"
|
|
"mstore/pkg/auxtool"
|
|
"mstore/pkg/auxuuid"
|
|
"mstore/pkg/descr"
|
|
)
|
|
|
|
type CreateAccountParams struct {
|
|
Username string `json:"username"`
|
|
Password string `json:"password"`
|
|
}
|
|
type CreateAccountResult struct {
|
|
AccountID string `json:"accountId"`
|
|
}
|
|
|
|
func (oper *Operator) CreateAccount(ctx context.Context, operatorID string, params *CreateAccountParams) (*CreateAccountResult, error) {
|
|
var err error
|
|
res := &CreateAccountResult{}
|
|
|
|
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 := oper.mdb.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 := auxtool.TimeNow()
|
|
passhash := auxpwd.MakeSHA256Hash([]byte(params.Password))
|
|
accountDescr := &descr.Account{
|
|
ID: auxuuid.NewUUID(),
|
|
Username: params.Username,
|
|
Passhash: passhash,
|
|
Disabled: false,
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
CreatedBy: operatorID,
|
|
UpdatedBy: operatorID,
|
|
}
|
|
err = oper.mdb.InsertAccount(ctx, accountDescr)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
res.AccountID = accountDescr.ID
|
|
return res, err
|
|
}
|