Files
mstore/pkg/accntcli/grant.go
T
2026-03-07 19:11:20 +02:00

217 lines
4.9 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 accntcli
import (
"context"
"encoding/json"
"errors"
"mstore/app/accoper"
"mstore/app/handler"
"mstore/pkg/descr"
)
func (cli *Client) CreateGrantByAccountID(ctx context.Context, host string, accountID, right, pattern string) (string, error) {
var err error
var res string
params := accoper.CreateGrantParams{
AccountID: accountID,
Right: right,
Pattern: pattern,
}
reqdata, err := json.Marshal(params)
if err != nil {
return res, err
}
respdata, err := cli.doHTTPCall(ctx, host, "grant", "create", reqdata)
if err != nil {
return res, err
}
response := handler.NewResponse[accoper.CreateGrantResult]()
err = json.Unmarshal(respdata, response)
if err != nil {
return res, err
}
if response.Error {
err = errors.New(response.Message)
return res, err
}
res = response.Result.GrantID
return res, err
}
func (cli *Client) CreateGrantByUsername(ctx context.Context, host, username string, right string, pattern string) (string, error) {
var err error
var res string
params := accoper.CreateGrantParams{
Username: username,
Right: right,
Pattern: pattern,
}
reqdata, err := json.Marshal(params)
if err != nil {
return res, err
}
respdata, err := cli.doHTTPCall(ctx, host, "grant", "create", reqdata)
if err != nil {
return res, err
}
response := handler.NewResponse[accoper.CreateGrantResult]()
err = json.Unmarshal(respdata, response)
if err != nil {
return res, err
}
if response.Error {
err = errors.New(response.Message)
return res, err
}
res = response.Result.GrantID
return res, err
}
func (cli *Client) GetGrant(ctx context.Context, host, grantID string) (*descr.Grant, error) {
var err error
res := &descr.Grant{}
params := accoper.GetGrantParams{
GrantID: grantID,
}
reqdata, err := json.Marshal(params)
if err != nil {
return res, err
}
respdata, err := cli.doHTTPCall(ctx, host, "grant", "get", reqdata)
if err != nil {
return res, err
}
response := handler.NewResponse[accoper.GetGrantResult]()
err = json.Unmarshal(respdata, response)
if err != nil {
return res, err
}
if response.Error {
err = errors.New(response.Message)
return res, err
}
res = response.Result.Grant
return res, err
}
func (cli *Client) UpdateGrant(ctx context.Context, host, grantID, newPattern string) error {
var err error
params := accoper.UpdateGrantParams{
GrantID: grantID,
NewPattern: newPattern,
}
reqdata, err := json.Marshal(params)
if err != nil {
return err
}
respdata, err := cli.doHTTPCall(ctx, host, "grant", "update", reqdata)
if err != nil {
return err
}
response := handler.NewResponse[accoper.UpdateGrantResult]()
err = json.Unmarshal(respdata, response)
if err != nil {
return err
}
if response.Error {
err = errors.New(response.Message)
return err
}
return err
}
func (cli *Client) DeleteGrant(ctx context.Context, host, grantID string) error {
var err error
params := accoper.DeleteGrantParams{
GrantID: grantID,
}
reqdata, err := json.Marshal(params)
if err != nil {
return err
}
respdata, err := cli.doHTTPCall(ctx, host, "grant", "delete", reqdata)
if err != nil {
return err
}
response := handler.NewResponse[accoper.DeleteGrantResult]()
err = json.Unmarshal(respdata, response)
if err != nil {
return err
}
if response.Error {
err = errors.New(response.Message)
return err
}
return err
}
func (cli *Client) ListGrantsByAccountID(ctx context.Context, host, accountID string) ([]descr.Grant, error) {
var err error
res := make([]descr.Grant, 0)
params := accoper.ListGrantsParams{
AccountID: accountID,
}
reqdata, err := json.Marshal(params)
if err != nil {
return res, err
}
respdata, err := cli.doHTTPCall(ctx, host, "grants", "list", reqdata)
if err != nil {
return res, err
}
response := handler.NewResponse[accoper.ListGrantsResult]()
err = json.Unmarshal(respdata, response)
if err != nil {
return res, err
}
if response.Error {
err = errors.New(response.Message)
return res, err
}
res = response.Result.Grants
return res, err
}
func (cli *Client) ListGrantsByUsername(ctx context.Context, host, username string) ([]descr.Grant, error) {
var err error
res := make([]descr.Grant, 0)
params := accoper.ListGrantsParams{
Username: username,
}
reqdata, err := json.Marshal(params)
if err != nil {
return res, err
}
respdata, err := cli.doHTTPCall(ctx, host, "grants", "list", reqdata)
if err != nil {
return res, err
}
response := handler.NewResponse[accoper.ListGrantsResult]()
err = json.Unmarshal(respdata, response)
if err != nil {
return res, err
}
if response.Error {
err = errors.New(response.Message)
return res, err
}
res = response.Result.Grants
return res, err
}