Create admin settings and moved signup disable setting there

This commit is contained in:
Thomas Miceli
2023-04-17 00:17:06 +02:00
parent dbdd38694d
commit dba3f4be44
10 changed files with 127 additions and 16 deletions

View File

@ -18,11 +18,10 @@ var C *config
// Not using nested structs because the library
// doesn't support dot notation in this case sadly
type config struct {
LogLevel string `yaml:"log-level"`
ExternalUrl string `yaml:"external-url"`
DisableSignup bool `yaml:"disable-signup"`
OpengistHome string `yaml:"opengist-home"`
DBFilename string `yaml:"db-filename"`
LogLevel string `yaml:"log-level"`
ExternalUrl string `yaml:"external-url"`
OpengistHome string `yaml:"opengist-home"`
DBFilename string `yaml:"db-filename"`
HttpHost string `yaml:"http.host"`
HttpPort string `yaml:"http.port"`
@ -46,7 +45,6 @@ func configWithDefaults() (*config, error) {
}
c.LogLevel = "warn"
c.DisableSignup = false
c.OpengistHome = filepath.Join(homeDir, ".opengist")
c.DBFilename = "opengist.db"

View File

@ -0,0 +1,56 @@
package models
import (
"errors"
"github.com/mattn/go-sqlite3"
"gorm.io/gorm/clause"
)
type AdminSetting struct {
Key string `gorm:"uniqueIndex"`
Value string
}
const (
SettingDisableSignup = "disable-signup"
)
func GetSetting(key string) (string, error) {
var setting AdminSetting
err := db.Where("key = ?", key).First(&setting).Error
return setting.Value, err
}
func UpdateSetting(key string, value string) error {
return db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "key"}}, // key colume
DoUpdates: clause.AssignmentColumns([]string{"value"}),
}).Create(&AdminSetting{
Key: key,
Value: value,
}).Error
}
func setSetting(key string, value string) error {
return db.Create(&AdminSetting{Key: key, Value: value}).Error
}
func initAdminSettings(settings map[string]string) error {
for key, value := range settings {
if err := setSetting(key, value); err != nil {
if !isUniqueConstraintViolation(err) {
return err
}
}
}
return nil
}
func isUniqueConstraintViolation(err error) bool {
var sqliteErr sqlite3.Error
if errors.As(err, &sqliteErr) && sqliteErr.ExtendedCode == sqlite3.ErrConstraintUnique {
return true
}
return false
}

View File

@ -17,11 +17,14 @@ func Setup(dbpath string) error {
return err
}
if err = db.AutoMigrate(&User{}, &SSHKey{}, &Gist{}); err != nil {
if err = db.AutoMigrate(&User{}, &SSHKey{}, &Gist{}, &AdminSetting{}); err != nil {
return err
}
return nil
// Default admin setting values
return initAdminSettings(map[string]string{
SettingDisableSignup: "0",
})
}
func CountAll(table interface{}) (int64, error) {

View File

@ -182,3 +182,16 @@ func adminSyncReposFromDB(ctx echo.Context) error {
}()
return redirect(ctx, "/admin-panel")
}
func adminSetSetting(ctx echo.Context) error {
key := ctx.FormValue("key")
value := ctx.FormValue("value")
if err := models.UpdateSetting(key, value); err != nil {
return errorRes(500, "Cannot set setting", err)
}
return ctx.JSON(200, map[string]interface{}{
"success": true,
})
}

View File

@ -5,7 +5,6 @@ import (
"github.com/labstack/echo/v4"
"github.com/rs/zerolog/log"
"gorm.io/gorm"
"opengist/internal/config"
"opengist/internal/models"
)
@ -16,7 +15,7 @@ func register(ctx echo.Context) error {
}
func processRegister(ctx echo.Context) error {
if config.C.DisableSignup {
if getData(ctx, "signupDisabled") == true {
return errorRes(403, "Signing up is disabled", nil)
}

View File

@ -183,6 +183,7 @@ func Start() {
g2.POST("/gists/:gist/delete", adminGistDelete)
g2.POST("/sync-fs", adminSyncReposFromFS)
g2.POST("/sync-db", adminSyncReposFromDB)
g2.PUT("/set-setting", adminSetSetting)
}
g1.GET("/all", allGists)
@ -236,7 +237,12 @@ func dataInit(next echo.HandlerFunc) echo.HandlerFunc {
ctxValue := context.WithValue(ctx.Request().Context(), "data", echo.Map{})
ctx.SetRequest(ctx.Request().WithContext(ctxValue))
setData(ctx, "loadStartTime", time.Now())
setData(ctx, "signupDisabled", config.C.DisableSignup)
disableSignup, err := models.GetSetting(models.SettingDisableSignup)
if err != nil {
return errorRes(500, "Cannot read setting from database", err)
}
setData(ctx, "signupDisabled", disableSignup == "1")
return next(ctx)
}