Files
gmbase/app/operator/grants.go
T
2026-06-06 17:49:33 +02:00

149 lines
3.3 KiB
Go

package operator
import (
"context"
"fmt"
"time"
"mbase/pkg/descr"
"mbase/pkg/auxuuid"
"mbase/pkg/mbctl"
)
func (lg *Logic) SetGrant(ctx context.Context, accountID string, params *mbctl.SetGrantParams) (*mbctl.SetGrantResult, error) {
var err error
res := &mbctl.SetGrantResult{}
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
}
grantTypes := []string{
descr.GrantModifyUsers,
descr.GrantModifyDatabase,
}
var grantOk bool
for _, grantType := range grantTypes {
if grantType == params.Operation {
grantOk = true
break
}
}
if !grantOk {
err := fmt.Errorf("Unknown grant type")
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 || accountDescr == nil {
err := fmt.Errorf("Account with this id or name dont exists")
return res, err
}
grantExists, _, err = lg.db.GetGrant(ctx, accountDescr.ID, params.Operation)
if err != nil {
return res, err
}
if grantExists {
err := fmt.Errorf("Grant %s for the user already exists", params.Operation)
return res, err
}
now := time.Now().Format(time.RFC3339)
grantDescr := &descr.Grant{
ID: auxuuid.NewUUID(),
AccountID: accountDescr.ID,
CreatedAt: now,
Operation: params.Operation,
}
err = lg.db.InsertGrant(ctx, grantDescr)
if err != nil {
return res, err
}
return res, err
}
func (lg *Logic) DeleteGrant(ctx context.Context, accountID string, params *mbctl.DeleteGrantParams) (*mbctl.DeleteGrantResult, error) {
var err error
res := &mbctl.DeleteGrantResult{}
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
}
grantTypes := []string{
descr.GrantModifyUsers,
descr.GrantModifyDatabase,
}
var grantOk bool
for _, grantType := range grantTypes {
if grantType == params.Operation {
grantOk = true
break
}
}
if !grantOk {
err := fmt.Errorf("Unknown grant type")
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 || accountDescr == nil {
err := fmt.Errorf("Account with this id or name dont exists")
return res, err
}
grantExists, _, err = lg.db.GetGrant(ctx, accountDescr.ID, params.Operation)
if err != nil {
return res, err
}
if !grantExists {
err := fmt.Errorf("Requested grant for the user not found")
return res, err
}
err = lg.db.DeleteGrantByAccountID(ctx, accountDescr.ID, params.Operation)
if err != nil {
return res, err
}
return res, err
}