-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(session): user in context and transaction as user
- Loading branch information
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package session | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/domonda/go-errs" | ||
"github.com/domonda/go-sqldb/db" | ||
"github.com/domonda/go-types/uu" | ||
) | ||
|
||
type userCtxKeyType struct{} | ||
|
||
var userCtxKey userCtxKeyType | ||
|
||
func ContextWithUser(ctx context.Context, userID uu.ID) context.Context { | ||
return context.WithValue(ctx, userCtxKey, userID) | ||
} | ||
|
||
func UserFromContext(ctx context.Context) (userID uu.ID, err error) { | ||
userIDVal := ctx.Value(userCtxKey) | ||
userID = uu.IDFrom(userIDVal) | ||
if userID == uu.IDNil { | ||
return uu.IDNil, errors.New("user not in context") | ||
} | ||
return userID, nil | ||
} | ||
|
||
func TransactionAsUser(ctx context.Context, txFunc func(ctx context.Context) error) (err error) { | ||
return db.Transaction(ctx, func(ctx context.Context) (err error) { | ||
userID, err := UserFromContext(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// impersonate user in transaction | ||
err = db.Conn(ctx).Exec(fmt.Sprintf("set local session.user_id to '%s'; set role viewer;", userID)) | ||
if err != nil { | ||
return err | ||
} | ||
defer func() { | ||
if !errors.Is(err, sql.ErrTxDone) { | ||
// for when this transaction is within another one | ||
e := db.Conn(ctx).Exec("reset session.user_id; reset role;") | ||
err = errs.Combine(err, e) | ||
} | ||
}() | ||
|
||
return txFunc(ctx) | ||
}) | ||
} |