Files
gmbase/app/handler/account.go
T
2026-06-06 02:28:44 +02:00

81 lines
2.3 KiB
Go

package handler
import (
"context"
"mbase/pkg/mbctl"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
func (hand *Handler) Authentificate(ctx context.Context) (string, error) {
var err error
var accountID string
meta, _ := metadata.FromIncomingContext(ctx)
usernameArr := meta["username"]
passwordArr := meta["password"]
if len(usernameArr) == 0 || len(passwordArr) == 0 {
err := status.Errorf(codes.PermissionDenied, "Empty auth data")
return accountID, err
}
username := meta["username"][0]
password := meta["password"][0]
validated, accountID, err := hand.lg.ValidateAcount(ctx, username, password)
if !validated {
err := status.Errorf(codes.PermissionDenied, "Wrong auth data")
return accountID, err
}
return accountID, err
}
func (hand *Handler) CreateAccount(ctx context.Context, params *mbctl.CreateAccountParams) (*mbctl.CreateAccountResult, error) {
var err error
hand.log.Debugf("Handle CreateAccount call")
res := &mbctl.CreateAccountResult{}
accountID, err := hand.Authentificate(ctx)
if err != nil {
return res, err
}
res, err = hand.lg.CreateAccount(ctx, accountID, params)
return res, err
}
func (hand *Handler) DeleteAccount(ctx context.Context, params *mbctl.DeleteAccountParams) (*mbctl.DeleteAccountResult, error) {
var err error
hand.log.Debugf("Handle DeleteAccount call")
res := &mbctl.DeleteAccountResult{}
accountID, err := hand.Authentificate(ctx)
if err != nil {
return res, err
}
res, err = hand.lg.DeleteAccount(ctx, accountID, params)
return res, err
}
func (hand *Handler) ListAccounts(ctx context.Context, params *mbctl.ListAccountsParams) (*mbctl.ListAccountsResult, error) {
var err error
hand.log.Debugf("Handle ListAccounts call")
res := &mbctl.ListAccountsResult{}
accountID, err := hand.Authentificate(ctx)
if err != nil {
return res, err
}
res, err = hand.lg.ListAccounts(ctx, accountID, params)
return res, err
}
func (hand *Handler) UpdateAccount(ctx context.Context, params *mbctl.UpdateAccountParams) (*mbctl.UpdateAccountResult, error) {
var err error
hand.log.Debugf("Handle UpdateAccount call")
res := &mbctl.UpdateAccountResult{}
accountID, err := hand.Authentificate(ctx)
if err != nil {
return res, err
}
res, err = hand.lg.UpdateAccount(ctx, accountID, params)
return res, err
}