working commit
This commit is contained in:
@@ -22,6 +22,7 @@ type Account struct {
|
||||
}
|
||||
|
||||
type AccountShort struct {
|
||||
ID string `json:"id"`
|
||||
Username string `json:"username"`
|
||||
Disabled bool `json:"disabled"`
|
||||
CreatedAt string `json:"createdAt"`
|
||||
|
||||
@@ -10,6 +10,8 @@
|
||||
|
||||
package descr
|
||||
|
||||
const AnonymousID = "10000000-0000-0000-0000-000000000001"
|
||||
|
||||
type Grant struct {
|
||||
ID string `json:"id" db:"id"`
|
||||
AccountID string `json:"accountID" db:"account_id"`
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
//"fmt"
|
||||
|
||||
//"mstore/app/descr"
|
||||
"mstore/app/descr"
|
||||
"mstore/app/operator"
|
||||
"mstore/app/router"
|
||||
)
|
||||
@@ -12,13 +10,15 @@ import (
|
||||
func (hand *Handler) CreateAccount(rctx *router.Context) {
|
||||
var err error
|
||||
|
||||
operatorID := descr.AnonymousID
|
||||
|
||||
params := &operator.CreateAccountParams{}
|
||||
err = rctx.BindJSON(params)
|
||||
if err != nil {
|
||||
hand.SendError(rctx, err)
|
||||
return
|
||||
}
|
||||
res, err := hand.oper.CreateAccount(rctx.Ctx, params)
|
||||
res, err := hand.oper.CreateAccount(rctx.Ctx, operatorID, params)
|
||||
if err != nil {
|
||||
hand.logg.Errorf("CreateAccount error: %v", err)
|
||||
hand.SendError(rctx, err)
|
||||
|
||||
+10
-2
@@ -37,7 +37,7 @@ type CreateAccountResult struct {
|
||||
AccountID string `json:"accountId"`
|
||||
}
|
||||
|
||||
func (oper *Operator) CreateAccount(ctx context.Context, params *CreateAccountParams) (*CreateAccountResult, error) {
|
||||
func (oper *Operator) CreateAccount(ctx context.Context, operatorID string, params *CreateAccountParams) (*CreateAccountResult, error) {
|
||||
var err error
|
||||
res := &CreateAccountResult{}
|
||||
|
||||
@@ -68,6 +68,8 @@ func (oper *Operator) CreateAccount(ctx context.Context, params *CreateAccountPa
|
||||
Disabled: false,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
CreatedBy: operatorID,
|
||||
UpdatedBy: operatorID,
|
||||
}
|
||||
err = oper.mdb.InsertAccount(ctx, accountDescr)
|
||||
if err != nil {
|
||||
@@ -112,10 +114,13 @@ func (oper *Operator) GetAccount(ctx context.Context, params *GetAccountParams)
|
||||
}
|
||||
}
|
||||
accountShort := &descr.AccountShort{
|
||||
ID: accountDescr.ID,
|
||||
Username: accountDescr.Username,
|
||||
Disabled: accountDescr.Disabled,
|
||||
CreatedAt: accountDescr.CreatedAt,
|
||||
UpdatedAt: accountDescr.UpdatedAt,
|
||||
CreatedBy: accountDescr.CreatedBy,
|
||||
UpdatedBy: accountDescr.UpdatedBy,
|
||||
Disabled: accountDescr.Disabled,
|
||||
Grants: make([]descr.GrantShort, 0),
|
||||
}
|
||||
grantDescrs, err := oper.mdb.ListGrantsByAccountID(ctx, accountDescr.ID)
|
||||
@@ -252,10 +257,13 @@ func (oper *Operator) ListAccounts(ctx context.Context, params *ListAccountsPara
|
||||
}
|
||||
for _, accountDescr := range accountDescrs {
|
||||
accountShort := descr.AccountShort{
|
||||
ID: accountDescr.ID,
|
||||
Username: accountDescr.Username,
|
||||
Disabled: accountDescr.Disabled,
|
||||
CreatedAt: accountDescr.CreatedAt,
|
||||
UpdatedAt: accountDescr.UpdatedAt,
|
||||
CreatedBy: accountDescr.CreatedBy,
|
||||
UpdatedBy: accountDescr.UpdatedBy,
|
||||
Grants: make([]descr.GrantShort, 0),
|
||||
}
|
||||
grantDescrs, err := oper.mdb.ListGrantsByAccountID(ctx, accountDescr.ID)
|
||||
|
||||
+37
-6
@@ -13,7 +13,6 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/url"
|
||||
|
||||
"mstore/app/descr"
|
||||
"mstore/app/handler"
|
||||
@@ -54,9 +53,9 @@ func (cli *Client) CreateAccount(ctx context.Context, hosturi, username, passwor
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (cli *Client) GetAccountByID(ctx context.Context, hosturi, id string) (descr.AccountShort, error) {
|
||||
func (cli *Client) GetAccountByID(ctx context.Context, hosturi, id string) (*descr.AccountShort, error) {
|
||||
var err error
|
||||
var res descr.AccountShort
|
||||
res := &descr.AccountShort{}
|
||||
|
||||
apipath, err := setApiPath(hosturi, "/v3/api/account/get")
|
||||
if err != nil {
|
||||
@@ -84,13 +83,14 @@ func (cli *Client) GetAccountByID(ctx context.Context, hosturi, id string) (desc
|
||||
err = fmt.Errorf("%s", operRes.Message)
|
||||
return res, err
|
||||
}
|
||||
res = operRes.Result.Account
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (cli *Client) GetAccountByName(ctx context.Context, hosturi, username string) error {
|
||||
var err error
|
||||
|
||||
apipath, err := url.JoinPath(hosturi, "/v3/api/account/get")
|
||||
apipath, err := setApiPath(hosturi, "/v3/api/account/get")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -121,7 +121,7 @@ func (cli *Client) GetAccountByName(ctx context.Context, hosturi, username strin
|
||||
func (cli *Client) UpdateAccount(ctx context.Context, hosturi, id, username, newUsername, newPassword string) error {
|
||||
var err error
|
||||
|
||||
apipath, err := url.JoinPath(hosturi, "/v3/api/account/update")
|
||||
apipath, err := setApiPath(hosturi, "/v3/api/account/update")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -154,7 +154,7 @@ func (cli *Client) UpdateAccount(ctx context.Context, hosturi, id, username, new
|
||||
func (cli *Client) DeleteAccount(ctx context.Context, hosturi, id, username string) error {
|
||||
var err error
|
||||
|
||||
apipath, err := url.JoinPath(hosturi, "/v3/api/account/delete")
|
||||
apipath, err := setApiPath(hosturi, "/v3/api/account/delete")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -182,3 +182,34 @@ func (cli *Client) DeleteAccount(ctx context.Context, hosturi, id, username stri
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (cli *Client) ListAccounts(ctx context.Context, hosturi string) ([]descr.AccountShort, error) {
|
||||
var err error
|
||||
res := make([]descr.AccountShort, 0)
|
||||
|
||||
apipath, err := setApiPath(hosturi, "/v3/api/accounts/list")
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
operParams := operator.ListAccountsParams{}
|
||||
paramsJson, err := json.Marshal(operParams)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
respBytes, err := doHTTPCall(ctx, apipath, paramsJson)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
operRes := handler.NewResponse[operator.ListAccountsResult]()
|
||||
err = json.Unmarshal(respBytes, operRes)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
if operRes.Error {
|
||||
err = fmt.Errorf("%s", operRes.Message)
|
||||
return res, err
|
||||
}
|
||||
res = operRes.Result.Accounts
|
||||
return res, err
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ import (
|
||||
"mstore/app/server"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
func TestAccountLife(t *testing.T) {
|
||||
@@ -95,10 +96,11 @@ func TestAccountLife(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
ctx, _ = context.WithTimeout(ctx, 1*time.Second)
|
||||
|
||||
_, err = cli.GetAccountByID(ctx, srvaddr, accountID)
|
||||
accountDescr, err := cli.GetAccountByID(ctx, srvaddr, accountID)
|
||||
require.NoError(t, err)
|
||||
accountYAML, err := yaml.Marshal(accountDescr)
|
||||
fmt.Printf("account:\n%s\n", string(accountYAML))
|
||||
}
|
||||
/*
|
||||
{
|
||||
// ListAccounts
|
||||
fmt.Printf("=== ListAccounts ===\n")
|
||||
@@ -106,10 +108,14 @@ func TestAccountLife(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
ctx, _ = context.WithTimeout(ctx, 1*time.Second)
|
||||
|
||||
files, err := cli.ListAccounts(ctx, srvaddr+"/")
|
||||
accountDescrs, err := cli.ListAccounts(ctx, srvaddr+"/")
|
||||
require.NoError(t, err)
|
||||
require.NotZero(t, len(files))
|
||||
require.NotZero(t, len(accountDescrs))
|
||||
accountsYAML, err := yaml.Marshal(accountDescrs)
|
||||
fmt.Printf("accounts:\n%s\n", string(accountsYAML))
|
||||
|
||||
}
|
||||
/*
|
||||
|
||||
{
|
||||
// DeleteAccount
|
||||
|
||||
+4
-5
@@ -13,7 +13,6 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/url"
|
||||
|
||||
"mstore/app/handler"
|
||||
"mstore/app/operator"
|
||||
@@ -22,7 +21,7 @@ import (
|
||||
func (cli *Client) CreateGrant(ctx context.Context, hosturi, accountID, operation, pattern string) error {
|
||||
var err error
|
||||
|
||||
apiuri, err := url.JoinPath(hosturi, "/v3/api/grant/create")
|
||||
apiuri, err := setApiPath(hosturi, "/v3/api/grant/create")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -55,7 +54,7 @@ func (cli *Client) CreateGrant(ctx context.Context, hosturi, accountID, operatio
|
||||
func (cli *Client) GetGrant(ctx context.Context, hosturi, id, username string) error {
|
||||
var err error
|
||||
|
||||
apipath, err := url.JoinPath(hosturi, "/v3/api/grant/get")
|
||||
apipath, err := setApiPath(hosturi, "/v3/api/grant/get")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -86,7 +85,7 @@ func (cli *Client) GetGrant(ctx context.Context, hosturi, id, username string) e
|
||||
func (cli *Client) UpdateGrant(ctx context.Context, hosturi, grantID, newPattern string) error {
|
||||
var err error
|
||||
|
||||
apipath, err := url.JoinPath(hosturi, "/v3/api/grant/update")
|
||||
apipath, err := setApiPath(hosturi, "/v3/api/grant/update")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -117,7 +116,7 @@ func (cli *Client) UpdateGrant(ctx context.Context, hosturi, grantID, newPattern
|
||||
func (cli *Client) DeleteGrant(ctx context.Context, hosturi, grantID string) error {
|
||||
var err error
|
||||
|
||||
apipath, err := url.JoinPath(hosturi, "/v3/api/grant/delete")
|
||||
apipath, err := setApiPath(hosturi, "/v3/api/grant/delete")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user