working commit

This commit is contained in:
2026-06-05 18:25:03 +02:00
commit 7d8abba003
82 changed files with 21863 additions and 0 deletions
+148
View File
@@ -0,0 +1,148 @@
package logic
import (
"context"
"fmt"
"time"
"mbase/app/descriptor"
"mbase/pkg/auxid"
"mbase/pkg/mbctl"
)
func (lg *Logic) SetGrant(ctx context.Context, accountID int64, params *mbctl.SetGrantParams) (*mbctl.SetGrantResult, error) {
var err error
res := &mbctl.SetGrantResult{}
lg.WaitDumping()
grantExists, _, err := lg.db.GetGrant(ctx, accountID, descriptor.GrantModifyUsers)
if err != nil {
return res, err
}
if !grantExists {
err := fmt.Errorf("Operation not allowed for the user")
return res, err
}
grantTypes := []string{
descriptor.GrantModifyUsers,
descriptor.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 *descriptor.Account
var accountExists bool
switch {
case params.AccountID != 0:
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 := &descriptor.Grant{
ID: auxid.GenID(),
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 int64, params *mbctl.DeleteGrantParams) (*mbctl.DeleteGrantResult, error) {
var err error
res := &mbctl.DeleteGrantResult{}
lg.WaitDumping()
grantExists, _, err := lg.db.GetGrant(ctx, accountID, descriptor.GrantModifyUsers)
if err != nil {
return res, err
}
if !grantExists {
err := fmt.Errorf("Operation not allowed for the user")
return res, err
}
grantTypes := []string{
descriptor.GrantModifyUsers,
descriptor.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 *descriptor.Account
var accountExists bool
switch {
case params.AccountID != 0:
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
}