Add session variable for user id

This commit is contained in:
Vikram Rangnekar
2019-09-08 01:54:38 -04:00
parent fe4d1107ac
commit 65921d4d42
9 changed files with 81 additions and 57 deletions

View File

@ -60,10 +60,9 @@ type config struct {
Defaults struct {
Filter []string
Blacklist []string
Blocklist []string
}
Fields []configTable
Tables []configTable
} `mapstructure:"database"`
}
@ -72,7 +71,7 @@ type configTable struct {
Name string
Filter []string
Table string
Blacklist []string
Blocklist []string
Remotes []configRemote
}

View File

@ -262,10 +262,30 @@ func (c *coreContext) resolvePreparedSQL(gql string) ([]byte, *preparedItem, err
var root json.RawMessage
vars := varList(c, ps.args)
_, err := ps.stmt.QueryOne(pg.Scan(&root), vars...)
tx, err := db.Begin()
if err != nil {
return nil, nil, err
}
defer tx.Rollback()
if v := c.Value(userIDKey); v != nil {
_, err = tx.Exec(`SET LOCAL SESSION "user.id" = ?`, v)
if err != nil {
return nil, nil, err
}
}
_, err = tx.Stmt(ps.stmt).QueryOne(pg.Scan(&root), vars...)
if err != nil {
return nil, nil, err
}
if err := tx.Commit(); err != nil {
return nil, nil, err
}
// w.WriteString(`SET LOCAL SESSION "user.id" = '{{user_id}}'; `)
fmt.Printf("PRE: %v\n", ps.stmt)
@ -314,15 +334,33 @@ func (c *coreContext) resolveSQL(qc *qcode.QCode) (
st = time.Now()
}
tx, err := db.Begin()
if err != nil {
return nil, 0, err
}
defer tx.Rollback()
if v := c.Value(userIDKey); v != nil {
_, err = tx.Exec(`SET LOCAL SESSION "user.id" = ?`, v)
if err != nil {
return nil, 0, err
}
}
fmt.Printf("RAW: %#v\n", finalSQL)
var root json.RawMessage
_, err = db.QueryOne(pg.Scan(&root), finalSQL)
_, err = tx.QueryOne(pg.Scan(&root), finalSQL)
if err != nil {
return nil, 0, err
}
if err := tx.Commit(); err != nil {
return nil, 0, err
}
if conf.EnableTracing && len(qc.Selects) != 0 {
c.addTrace(
qc.Selects,

View File

@ -99,10 +99,6 @@ func initConf(path string) (*config, error) {
flect.AddPlural(k, v)
}
if len(c.DB.Tables) == 0 {
c.DB.Tables = c.DB.Fields
}
for i := range c.DB.Tables {
t := c.DB.Tables[i]
t.Name = flect.Pluralize(strings.ToLower(t.Name))
@ -159,7 +155,7 @@ func initCompilers(c *config) (*qcode.Compiler, *psql.Compiler, error) {
qc, err := qcode.NewCompiler(qcode.Config{
DefaultFilter: c.DB.Defaults.Filter,
FilterMap: c.getFilterMap(),
Blacklist: c.DB.Defaults.Blacklist,
Blocklist: c.DB.Defaults.Blocklist,
KeepArgs: false,
})