Skip to content

Commit

Permalink
soft deprecated apis.RequestData(c) in favor of apis.RequestInfo(c) a…
Browse files Browse the repository at this point in the history
…nd updated jsvm bindings
  • Loading branch information
ganigeorgiev committed Jul 17, 2023
1 parent 7d40172 commit 0110869
Show file tree
Hide file tree
Showing 22 changed files with 6,247 additions and 6,079 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
```
app.Dao().FindRecordsByFilter("posts", "title ~ 'lorem ipsum' && visible = true", "-created", 10)
app.Dao().FindFirstRecordByFilter("posts", "slug='test' && active=true")
app.Dao().CanAccessRecord(record, requestData, rule)
app.Dao().CanAccessRecord(record, requestInfo, rule)
```

- (@todo docs) Added `Dao.WithoutHooks()` helper to create a new `Dao` from the current one but without the create/update/delete hooks.
Expand Down Expand Up @@ -84,6 +84,9 @@

- (@todo docs) Added `record.ExpandedOne(rel)` and `record.ExpandedAll(rel)` helpers to retrieve casted single or multiple expand relations from the already loaded "expand" Record data.

- **!** renamed `models.RequestData` to `models.RequestInfo` and soft-deprecated `apis.RequestData(c)` to `apis.RequestInfo(c)` to avoid the stuttering with the `Data` field.
_The old `apis.RequestData()` method still works to minimize the breaking changes but it is recommended to replace it with `apis.RequestInfo(c)`._


## v0.16.10

Expand All @@ -92,7 +95,7 @@

## v0.16.9

- Register the `eagerRequestDataCache` middleware only for the internal `api` group routes to avoid conflicts with custom route handlers ([#2914](https://github.com/pocketbase/pocketbase/issues/2914)).
- Register the `eagerRequestInfoCache` middleware only for the internal `api` group routes to avoid conflicts with custom route handlers ([#2914](https://github.com/pocketbase/pocketbase/issues/2914)).


## v0.16.8
Expand Down
2 changes: 1 addition & 1 deletion apis/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func InitApi(app core.App) (*echo.Echo, error) {
bindStaticAdminUI(app, e)

// default routes
api := e.Group("/api", eagerRequestDataCache(app))
api := e.Group("/api", eagerRequestInfoCache(app))
bindSettingsApi(app, api)
bindAdminApi(app, api)
bindCollectionApi(app, api)
Expand Down
12 changes: 6 additions & 6 deletions apis/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func TestRemoveTrailingSlashMiddleware(t *testing.T) {
}
}

func TestEagerRequestDataCache(t *testing.T) {
func TestEagerRequestInfoCache(t *testing.T) {

scenarios := []tests.ApiScenario{
{
Expand All @@ -236,7 +236,7 @@ func TestEagerRequestDataCache(t *testing.T) {

// since the unknown method is not eager cache support
// it should fail reading the json body twice
r := apis.RequestData(c)
r := apis.RequestInfo(c)
if v := cast.ToString(r.Data["name"]); v != "" {
t.Fatalf("Expected empty request data body, got, %v", r.Data)
}
Expand All @@ -256,7 +256,7 @@ func TestEagerRequestDataCache(t *testing.T) {
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
// it is not important whether the route handler return an error since
// we just need to ensure that the eagerRequestDataCache was registered
// we just need to ensure that the eagerRequestInfoCache was registered
next(c)

// ensure that the body was read at least once
Expand All @@ -267,7 +267,7 @@ func TestEagerRequestDataCache(t *testing.T) {

// since the unknown method is not eager cache support
// it should fail reading the json body twice
r := apis.RequestData(c)
r := apis.RequestInfo(c)
if v := cast.ToString(r.Data["name"]); v != "" {
t.Fatalf("Expected empty request data body, got, %v", r.Data)
}
Expand All @@ -287,7 +287,7 @@ func TestEagerRequestDataCache(t *testing.T) {
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
// it is not important whether the route handler return an error since
// we just need to ensure that the eagerRequestDataCache was registered
// we just need to ensure that the eagerRequestInfoCache was registered
next(c)

// ensure that the body was read at least once
Expand All @@ -297,7 +297,7 @@ func TestEagerRequestDataCache(t *testing.T) {
c.Bind(data)

// try to read the body again
r := apis.RequestData(c)
r := apis.RequestInfo(c)
fmt.Println(r)
if v := cast.ToString(r.Data["name"]); v != "test123" {
t.Fatalf("Expected request data with name %q, got, %q", "test123", v)
Expand Down
12 changes: 6 additions & 6 deletions apis/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,18 @@ func (api *fileApi) download(c echo.Context) error {
adminOrAuthRecord, _ := api.findAdminOrAuthRecordByFileToken(token)

// create a copy of the cached request data and adjust it for the current auth model
requestData := *RequestData(c)
requestData.Admin = nil
requestData.AuthRecord = nil
requestInfo := *RequestInfo(c)
requestInfo.Admin = nil
requestInfo.AuthRecord = nil
if adminOrAuthRecord != nil {
if admin, _ := adminOrAuthRecord.(*models.Admin); admin != nil {
requestData.Admin = admin
requestInfo.Admin = admin
} else if record, _ := adminOrAuthRecord.(*models.Record); record != nil {
requestData.AuthRecord = record
requestInfo.AuthRecord = record
}
}

if ok, _ := api.app.Dao().CanAccessRecord(record, &requestData, record.Collection().ViewRule); !ok {
if ok, _ := api.app.Dao().CanAccessRecord(record, &requestInfo, record.Collection().ViewRule); !ok {
return NewForbiddenError("Insufficient permissions to access the file resource.", nil)
}
}
Expand Down
6 changes: 3 additions & 3 deletions apis/middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,15 +393,15 @@ func realUserIp(r *http.Request, fallbackIp string) string {
return fallbackIp
}

// eagerRequestDataCache ensures that the request data is cached in the request
// eagerRequestInfoCache ensures that the request data is cached in the request
// context to allow reading for example the json request body data more than once.
func eagerRequestDataCache(app core.App) echo.MiddlewareFunc {
func eagerRequestInfoCache(app core.App) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
switch c.Request().Method {
// currently we are eagerly caching only the requests with body
case "POST", "PUT", "PATCH", "DELETE":
RequestData(c)
RequestInfo(c)
}

return next(c)
Expand Down
6 changes: 3 additions & 3 deletions apis/realtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,12 +347,12 @@ func (api *realtimeApi) canAccessRecord(client subscriptions.Client, record *mod
}

// mock request data
requestData := &models.RequestData{
requestInfo := &models.RequestInfo{
Method: "GET",
}
requestData.AuthRecord, _ = client.Get(ContextAuthRecordKey).(*models.Record)
requestInfo.AuthRecord, _ = client.Get(ContextAuthRecordKey).(*models.Record)

resolver := resolvers.NewRecordFieldResolver(api.app.Dao(), record.Collection(), requestData, true)
resolver := resolvers.NewRecordFieldResolver(api.app.Dao(), record.Collection(), requestInfo, true)
expr, err := search.FilterData(*accessRule).BuildExpr(resolver)
if err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions apis/record_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ func (api *recordAuthApi) authWithOAuth2(c echo.Context) error {
return createForm.DrySubmit(func(txDao *daos.Dao) error {
event.IsNewRecord = true
// clone the current request data and assign the form create data as its body data
requestData := *RequestData(c)
requestData.Data = form.CreateData
requestInfo := *RequestInfo(c)
requestInfo.Data = form.CreateData

createRuleFunc := func(q *dbx.SelectQuery) error {
admin, _ := c.Get(ContextAdminKey).(*models.Admin)
Expand All @@ -205,7 +205,7 @@ func (api *recordAuthApi) authWithOAuth2(c echo.Context) error {
}

if *collection.CreateRule != "" {
resolver := resolvers.NewRecordFieldResolver(txDao, collection, &requestData, true)
resolver := resolvers.NewRecordFieldResolver(txDao, collection, &requestInfo, true)
expr, err := search.FilterData(*collection.CreateRule).BuildExpr(resolver)
if err != nil {
return err
Expand Down
60 changes: 30 additions & 30 deletions apis/record_crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,19 @@ func (api *recordApi) list(c echo.Context) error {
return err
}

requestData := RequestData(c)
requestInfo := RequestInfo(c)

if requestData.Admin == nil && collection.ListRule == nil {
if requestInfo.Admin == nil && collection.ListRule == nil {
// only admins can access if the rule is nil
return NewForbiddenError("Only admins can perform this action.", nil)
}

fieldsResolver := resolvers.NewRecordFieldResolver(
api.app.Dao(),
collection,
requestData,
requestInfo,
// hidden fields are searchable only by admins
requestData.Admin != nil,
requestInfo.Admin != nil,
)

searchProvider := search.NewProvider(fieldsResolver).
Expand All @@ -73,7 +73,7 @@ func (api *recordApi) list(c echo.Context) error {
searchProvider.CountCol("id")
}

if requestData.Admin == nil && collection.ListRule != nil {
if requestInfo.Admin == nil && collection.ListRule != nil {
searchProvider.AddFilter(search.FilterData(*collection.ListRule))
}

Expand Down Expand Up @@ -110,16 +110,16 @@ func (api *recordApi) view(c echo.Context) error {
return NewNotFoundError("", nil)
}

requestData := RequestData(c)
requestInfo := RequestInfo(c)

if requestData.Admin == nil && collection.ViewRule == nil {
if requestInfo.Admin == nil && collection.ViewRule == nil {
// only admins can access if the rule is nil
return NewForbiddenError("Only admins can perform this action.", nil)
}

ruleFunc := func(q *dbx.SelectQuery) error {
if requestData.Admin == nil && collection.ViewRule != nil && *collection.ViewRule != "" {
resolver := resolvers.NewRecordFieldResolver(api.app.Dao(), collection, requestData, true)
if requestInfo.Admin == nil && collection.ViewRule != nil && *collection.ViewRule != "" {
resolver := resolvers.NewRecordFieldResolver(api.app.Dao(), collection, requestInfo, true)
expr, err := search.FilterData(*collection.ViewRule).BuildExpr(resolver)
if err != nil {
return err
Expand Down Expand Up @@ -155,23 +155,23 @@ func (api *recordApi) create(c echo.Context) error {
return NewNotFoundError("", "Missing collection context.")
}

requestData := RequestData(c)
requestInfo := RequestInfo(c)

if requestData.Admin == nil && collection.CreateRule == nil {
if requestInfo.Admin == nil && collection.CreateRule == nil {
// only admins can access if the rule is nil
return NewForbiddenError("Only admins can perform this action.", nil)
}

hasFullManageAccess := requestData.Admin != nil
hasFullManageAccess := requestInfo.Admin != nil

// temporary save the record and check it against the create rule
if requestData.Admin == nil && collection.CreateRule != nil {
if requestInfo.Admin == nil && collection.CreateRule != nil {
testRecord := models.NewRecord(collection)

// replace modifiers fields so that the resolved value is always
// available when accessing requestData.Data using just the field name
if requestData.HasModifierDataKeys() {
requestData.Data = testRecord.ReplaceModifers(requestData.Data)
// available when accessing requestInfo.Data using just the field name
if requestInfo.HasModifierDataKeys() {
requestInfo.Data = testRecord.ReplaceModifers(requestInfo.Data)
}

testForm := forms.NewRecordUpsert(api.app, testRecord)
Expand All @@ -185,7 +185,7 @@ func (api *recordApi) create(c echo.Context) error {
return nil // no create rule to resolve
}

resolver := resolvers.NewRecordFieldResolver(api.app.Dao(), collection, requestData, true)
resolver := resolvers.NewRecordFieldResolver(api.app.Dao(), collection, requestInfo, true)
expr, err := search.FilterData(*collection.CreateRule).BuildExpr(resolver)
if err != nil {
return err
Expand All @@ -200,7 +200,7 @@ func (api *recordApi) create(c echo.Context) error {
if err != nil {
return fmt.Errorf("DrySubmit create rule failure: %w", err)
}
hasFullManageAccess = hasAuthManageAccess(txDao, foundRecord, requestData)
hasFullManageAccess = hasAuthManageAccess(txDao, foundRecord, requestInfo)
return nil
})

Expand Down Expand Up @@ -259,26 +259,26 @@ func (api *recordApi) update(c echo.Context) error {
return NewNotFoundError("", nil)
}

requestData := RequestData(c)
requestInfo := RequestInfo(c)

if requestData.Admin == nil && collection.UpdateRule == nil {
if requestInfo.Admin == nil && collection.UpdateRule == nil {
// only admins can access if the rule is nil
return NewForbiddenError("Only admins can perform this action.", nil)
}

// eager fetch the record so that the modifier field values are replaced
// and available when accessing requestData.Data using just the field name
if requestData.HasModifierDataKeys() {
// and available when accessing requestInfo.Data using just the field name
if requestInfo.HasModifierDataKeys() {
record, err := api.app.Dao().FindRecordById(collection.Id, recordId)
if err != nil || record == nil {
return NewNotFoundError("", err)
}
requestData.Data = record.ReplaceModifers(requestData.Data)
requestInfo.Data = record.ReplaceModifers(requestInfo.Data)
}

ruleFunc := func(q *dbx.SelectQuery) error {
if requestData.Admin == nil && collection.UpdateRule != nil && *collection.UpdateRule != "" {
resolver := resolvers.NewRecordFieldResolver(api.app.Dao(), collection, requestData, true)
if requestInfo.Admin == nil && collection.UpdateRule != nil && *collection.UpdateRule != "" {
resolver := resolvers.NewRecordFieldResolver(api.app.Dao(), collection, requestInfo, true)
expr, err := search.FilterData(*collection.UpdateRule).BuildExpr(resolver)
if err != nil {
return err
Expand All @@ -296,7 +296,7 @@ func (api *recordApi) update(c echo.Context) error {
}

form := forms.NewRecordUpsert(api.app, record)
form.SetFullManageAccess(requestData.Admin != nil || hasAuthManageAccess(api.app.Dao(), record, requestData))
form.SetFullManageAccess(requestInfo.Admin != nil || hasAuthManageAccess(api.app.Dao(), record, requestInfo))

// load request
if err := form.LoadRequest(c.Request(), ""); err != nil {
Expand Down Expand Up @@ -344,16 +344,16 @@ func (api *recordApi) delete(c echo.Context) error {
return NewNotFoundError("", nil)
}

requestData := RequestData(c)
requestInfo := RequestInfo(c)

if requestData.Admin == nil && collection.DeleteRule == nil {
if requestInfo.Admin == nil && collection.DeleteRule == nil {
// only admins can access if the rule is nil
return NewForbiddenError("Only admins can perform this action.", nil)
}

ruleFunc := func(q *dbx.SelectQuery) error {
if requestData.Admin == nil && collection.DeleteRule != nil && *collection.DeleteRule != "" {
resolver := resolvers.NewRecordFieldResolver(api.app.Dao(), collection, requestData, true)
if requestInfo.Admin == nil && collection.DeleteRule != nil && *collection.DeleteRule != "" {
resolver := resolvers.NewRecordFieldResolver(api.app.Dao(), collection, requestInfo, true)
expr, err := search.FilterData(*collection.DeleteRule).BuildExpr(resolver)
if err != nil {
return err
Expand Down
Loading

0 comments on commit 0110869

Please sign in to comment.