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
No known key found for this signature in database
GPG key ID: D86C6F6390AF050F
10 changed files with 127 additions and 16 deletions

View file

@ -5,9 +5,6 @@ log-level: warn
# If not set, uses the URL from the request
external-url:
# Prevents the creation of new accounts (either `true` or `false`). Default: false
disable-signup: false
# Directory where Opengist will store its data. Default: ~/.opengist/
opengist-home:

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)
}

22
public/admin.ts Normal file
View file

@ -0,0 +1,22 @@
document.addEventListener('DOMContentLoaded', () => {
registerDomSetting(document.getElementById('disable-signup') as HTMLInputElement);
});
const setSetting = (key: string, value: string) => {
const data = new URLSearchParams();
data.append('key', key);
data.append('value', value);
data.append('_csrf', ((document.getElementsByName('_csrf')[0] as HTMLInputElement).value));
fetch('/admin-panel/set-setting', {
method: 'PUT',
credentials: 'same-origin',
body: data,
});
};
const registerDomSetting = (el: HTMLInputElement) => {
el.addEventListener('change', () => {
setSetting(el.id, el.checked ? '1' : '0');
});
};

View file

@ -71,7 +71,24 @@
</div>
</div>
</div>
<div class="sm:overflow-hidden ">
<div class="space-y-2 bg-gray-800 py-6 px-6 rounded-md border border-gray-700">
<div>
<span class="text-base font-bold leading-6 text-slate-300">Settings</span>
</div>
{{ .csrfHtml }}
<div class="space-y-2">
<div>
<label for="disable-signup" class="text-sm text-slate-300">Disable signup</label>
<input type="checkbox" id="disable-signup" name="disable-signup" {{ if .signupDisabled }}checked="checked"{{ end }} class="ml-1 h-4 w-4 rounded border-gray-300 text-primary-600 focus:ring-primary-600" />
</div>
</div>
</div>
</div>
</div>
<script type="module" src="{{ asset "admin.ts" }}"></script>
{{ template "admin_footer" .}}
{{ template "footer" .}}

View file

@ -9,7 +9,7 @@ export default defineConfig({
assetsDir: 'assets',
manifest: true,
rollupOptions: {
input: ['./public/main.ts', './public/editor.ts']
input: ['./public/main.ts', './public/editor.ts', './public/admin.ts']
}
}
})
})