package logic import ( "context" "fmt" "time" "mbase/app/descr" "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, 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 != 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 := &descr.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, 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 != 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 }