[#3106] always refresh the Admins UI initial admins counter cache when there are none
This commit is contained in:
parent
265dac45ce
commit
4a45ad91fa
|
@ -2,6 +2,10 @@
|
||||||
|
|
||||||
- Fixed `relation` "Cascade delete" tooltip message ([#3098](https://github.com/pocketbase/pocketbase/issues/3098)).
|
- Fixed `relation` "Cascade delete" tooltip message ([#3098](https://github.com/pocketbase/pocketbase/issues/3098)).
|
||||||
|
|
||||||
|
- Fixed jsvm error message prefix on failed migrations ([#3103]https://github.com/pocketbase/pocketbase/pull/3103).
|
||||||
|
|
||||||
|
- Disabled the initial Admin UI admins counter cache when there are no initial admins to allow detecting externally created accounts (eg. with the `admin` command) ([#3106](https://github.com/pocketbase/pocketbase/issues/3106)).
|
||||||
|
|
||||||
|
|
||||||
## v0.17.3
|
## v0.17.3
|
||||||
|
|
||||||
|
|
28
apis/base.go
28
apis/base.go
|
@ -207,15 +207,15 @@ func uiCacheControl() echo.MiddlewareFunc {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const totalAdminsCacheKey = "@totalAdmins"
|
const hasAdminsCacheKey = "@hasAdmins"
|
||||||
|
|
||||||
func updateTotalAdminsCache(app core.App) error {
|
func updateHasAdminsCache(app core.App) error {
|
||||||
total, err := app.Dao().TotalAdmins()
|
total, err := app.Dao().TotalAdmins()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
app.Cache().Set(totalAdminsCacheKey, total)
|
app.Cache().Set(hasAdminsCacheKey, total > 0)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -223,12 +223,13 @@ func updateTotalAdminsCache(app core.App) error {
|
||||||
// installerRedirect redirects the user to the installer admin UI page
|
// installerRedirect redirects the user to the installer admin UI page
|
||||||
// when the application needs some preliminary configurations to be done.
|
// when the application needs some preliminary configurations to be done.
|
||||||
func installerRedirect(app core.App) echo.MiddlewareFunc {
|
func installerRedirect(app core.App) echo.MiddlewareFunc {
|
||||||
// keep totalAdminsCacheKey value up-to-date
|
// keep hasAdminsCacheKey value up-to-date
|
||||||
app.OnAdminAfterCreateRequest().Add(func(data *core.AdminCreateEvent) error {
|
app.OnAdminAfterCreateRequest().Add(func(data *core.AdminCreateEvent) error {
|
||||||
return updateTotalAdminsCache(app)
|
return updateHasAdminsCache(app)
|
||||||
})
|
})
|
||||||
|
|
||||||
app.OnAdminAfterDeleteRequest().Add(func(data *core.AdminDeleteEvent) error {
|
app.OnAdminAfterDeleteRequest().Add(func(data *core.AdminDeleteEvent) error {
|
||||||
return updateTotalAdminsCache(app)
|
return updateHasAdminsCache(app)
|
||||||
})
|
})
|
||||||
|
|
||||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||||
|
@ -239,23 +240,24 @@ func installerRedirect(app core.App) echo.MiddlewareFunc {
|
||||||
return next(c)
|
return next(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
// load into cache (if not already)
|
hasAdmins := cast.ToBool(app.Cache().Get(hasAdminsCacheKey))
|
||||||
if !app.Cache().Has(totalAdminsCacheKey) {
|
|
||||||
if err := updateTotalAdminsCache(app); err != nil {
|
if !hasAdmins {
|
||||||
|
// update the cache to make sure that the admin wasn't created by another process
|
||||||
|
if err := updateHasAdminsCache(app); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
hasAdmins = cast.ToBool(app.Cache().Get(hasAdminsCacheKey))
|
||||||
}
|
}
|
||||||
|
|
||||||
totalAdmins := cast.ToInt(app.Cache().Get(totalAdminsCacheKey))
|
|
||||||
|
|
||||||
_, hasInstallerParam := c.Request().URL.Query()["installer"]
|
_, hasInstallerParam := c.Request().URL.Query()["installer"]
|
||||||
|
|
||||||
if totalAdmins == 0 && !hasInstallerParam {
|
if !hasAdmins && !hasInstallerParam {
|
||||||
// redirect to the installer page
|
// redirect to the installer page
|
||||||
return c.Redirect(http.StatusTemporaryRedirect, "?installer#")
|
return c.Redirect(http.StatusTemporaryRedirect, "?installer#")
|
||||||
}
|
}
|
||||||
|
|
||||||
if totalAdmins != 0 && hasInstallerParam {
|
if hasAdmins && hasInstallerParam {
|
||||||
// clear the installer param
|
// clear the installer param
|
||||||
return c.Redirect(http.StatusTemporaryRedirect, "?")
|
return c.Redirect(http.StatusTemporaryRedirect, "?")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue