mirror of
https://github.com/seriousm4x/UpSnap.git
synced 2026-03-31 06:24:06 -04:00
* fix: allow users with create permissions to scan for devices * fix: show scan tab in frontend
63 lines
1.6 KiB
Go
63 lines
1.6 KiB
Go
package pb
|
|
|
|
import (
|
|
"github.com/pocketbase/dbx"
|
|
"github.com/pocketbase/pocketbase/apis"
|
|
"github.com/pocketbase/pocketbase/core"
|
|
"github.com/pocketbase/pocketbase/tools/hook"
|
|
)
|
|
|
|
func RequireScanDevicesPermission() *hook.Handler[*core.RequestEvent] {
|
|
return &hook.Handler[*core.RequestEvent]{
|
|
Func: func(e *core.RequestEvent) error {
|
|
if e.HasSuperuserAuth() {
|
|
return e.Next()
|
|
}
|
|
|
|
user := e.Auth
|
|
if user == nil {
|
|
return apis.NewUnauthorizedError("The request requires superuser or record authorization token to be set.", nil)
|
|
}
|
|
|
|
res, err := e.App.FindFirstRecordByFilter(
|
|
"permissions",
|
|
"user.id = {:userId} && create = true",
|
|
dbx.Params{"userId": user.Id},
|
|
)
|
|
if res == nil || err != nil {
|
|
return apis.NewForbiddenError("You are not allowed to perform this request.", nil)
|
|
}
|
|
|
|
return e.Next()
|
|
},
|
|
}
|
|
}
|
|
|
|
func RequireUpSnapPermission() *hook.Handler[*core.RequestEvent] {
|
|
return &hook.Handler[*core.RequestEvent]{
|
|
Func: func(e *core.RequestEvent) error {
|
|
if e.HasSuperuserAuth() {
|
|
return e.Next()
|
|
}
|
|
|
|
user := e.Auth
|
|
if user == nil {
|
|
return apis.NewUnauthorizedError("The request requires superuser or record authorization token to be set.", nil)
|
|
}
|
|
|
|
deviceId := e.Request.PathValue("id")
|
|
|
|
// find record where user has device with power permission
|
|
res, err := e.App.FindFirstRecordByFilter("permissions", "user.id = {:userId} && power.id ?= {:deviceId}", dbx.Params{
|
|
"userId": user.Id,
|
|
"deviceId": deviceId,
|
|
})
|
|
if res == nil || err != nil {
|
|
return apis.NewForbiddenError("You are not allowed to perform this request.", nil)
|
|
}
|
|
|
|
return e.Next()
|
|
},
|
|
}
|
|
}
|