Skip to content

Commit

Permalink
Fix: Login ID not available in trigger functions via API handler
Browse files Browse the repository at this point in the history
  • Loading branch information
r3-gabriel committed Aug 9, 2024
1 parent d3f6dd4 commit 62ebe85
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
8 changes: 8 additions & 0 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/url"
"r3/tools"
"r3/types"
"strconv"
"time"

pgxuuid "github.com/jackc/pgx-gofrs-uuid"
Expand Down Expand Up @@ -70,6 +71,13 @@ func Open(config types.FileTypeDb) error {
return Pool.Ping(context.Background())
}

// set transaction config parameters
// these are used by system functions, such as instance.get_login_id()
func SetSessionConfig_tx(ctx context.Context, tx pgx.Tx, loginId int64) error {
_, err := tx.Exec(ctx, `SELECT SET_CONFIG('r3.login_id',$1,TRUE)`, strconv.FormatInt(loginId, 10))
return err
}

func Close() {
Pool.Close()
Pool = nil
Expand Down
9 changes: 7 additions & 2 deletions handler/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ func Handler(w http.ResponseWriter, r *http.Request) {

/*
Parse URL, such as:
GET /api/lsw_invoices/contracts/v1?limit=10
GET /api/lsw_invoices/contracts/v1/45
GET /api/lsw_invoices/contracts/v1?limit=10
GET /api/lsw_invoices/contracts/v1/45
DELETE /api/lsw_invoices/contracts/v1/45
Rules:
Expand Down Expand Up @@ -209,6 +209,11 @@ func Handler(w http.ResponseWriter, r *http.Request) {
}
defer tx.Rollback(ctx)

if err := db.SetSessionConfig_tx(ctx, tx, loginId); err != nil {
abort(http.StatusServiceUnavailable, err, handler.ErrGeneral)
return
}

if isDelete {
if recordId < 1 {
abort(http.StatusBadRequest, nil, "record ID must be > 0")
Expand Down
4 changes: 1 addition & 3 deletions request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"r3/handler"
"r3/log"
"r3/types"
"strconv"
"time"

"github.com/jackc/pgx/v5"
Expand Down Expand Up @@ -52,8 +51,7 @@ func ExecTransaction(ctxClient context.Context, address string, loginId int64, i

// set local transaction configuration parameters
// these are used by system functions, such as instance.get_login_id()
if _, err := tx.Exec(ctx, `SELECT SET_CONFIG('r3.login_id',$1,TRUE)`, strconv.FormatInt(loginId, 10)); err != nil {

if err := db.SetSessionConfig_tx(ctx, tx, loginId); err != nil {
log.Error("websocket", fmt.Sprintf("TRANSACTION %d, transaction config failure (login ID %d)",
reqTrans.TransactionNr, loginId), err)

Expand Down

0 comments on commit 62ebe85

Please sign in to comment.