mirror of
https://github.com/thomiceli/opengist.git
synced 2025-01-03 16:22:40 +00:00
Create admin settings and moved signup disable setting there
This commit is contained in:
parent
dbdd38694d
commit
dba3f4be44
10 changed files with 127 additions and 16 deletions
|
@ -5,9 +5,6 @@ log-level: warn
|
||||||
# If not set, uses the URL from the request
|
# If not set, uses the URL from the request
|
||||||
external-url:
|
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/
|
# Directory where Opengist will store its data. Default: ~/.opengist/
|
||||||
opengist-home:
|
opengist-home:
|
||||||
|
|
||||||
|
|
|
@ -18,11 +18,10 @@ var C *config
|
||||||
// Not using nested structs because the library
|
// Not using nested structs because the library
|
||||||
// doesn't support dot notation in this case sadly
|
// doesn't support dot notation in this case sadly
|
||||||
type config struct {
|
type config struct {
|
||||||
LogLevel string `yaml:"log-level"`
|
LogLevel string `yaml:"log-level"`
|
||||||
ExternalUrl string `yaml:"external-url"`
|
ExternalUrl string `yaml:"external-url"`
|
||||||
DisableSignup bool `yaml:"disable-signup"`
|
OpengistHome string `yaml:"opengist-home"`
|
||||||
OpengistHome string `yaml:"opengist-home"`
|
DBFilename string `yaml:"db-filename"`
|
||||||
DBFilename string `yaml:"db-filename"`
|
|
||||||
|
|
||||||
HttpHost string `yaml:"http.host"`
|
HttpHost string `yaml:"http.host"`
|
||||||
HttpPort string `yaml:"http.port"`
|
HttpPort string `yaml:"http.port"`
|
||||||
|
@ -46,7 +45,6 @@ func configWithDefaults() (*config, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
c.LogLevel = "warn"
|
c.LogLevel = "warn"
|
||||||
c.DisableSignup = false
|
|
||||||
c.OpengistHome = filepath.Join(homeDir, ".opengist")
|
c.OpengistHome = filepath.Join(homeDir, ".opengist")
|
||||||
c.DBFilename = "opengist.db"
|
c.DBFilename = "opengist.db"
|
||||||
|
|
||||||
|
|
56
internal/models/admin_setting.go
Normal file
56
internal/models/admin_setting.go
Normal 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
|
||||||
|
}
|
|
@ -17,11 +17,14 @@ func Setup(dbpath string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = db.AutoMigrate(&User{}, &SSHKey{}, &Gist{}); err != nil {
|
if err = db.AutoMigrate(&User{}, &SSHKey{}, &Gist{}, &AdminSetting{}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
// Default admin setting values
|
||||||
|
return initAdminSettings(map[string]string{
|
||||||
|
SettingDisableSignup: "0",
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func CountAll(table interface{}) (int64, error) {
|
func CountAll(table interface{}) (int64, error) {
|
||||||
|
|
|
@ -182,3 +182,16 @@ func adminSyncReposFromDB(ctx echo.Context) error {
|
||||||
}()
|
}()
|
||||||
return redirect(ctx, "/admin-panel")
|
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,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
@ -5,7 +5,6 @@ import (
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"opengist/internal/config"
|
|
||||||
"opengist/internal/models"
|
"opengist/internal/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -16,7 +15,7 @@ func register(ctx echo.Context) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func processRegister(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)
|
return errorRes(403, "Signing up is disabled", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -183,6 +183,7 @@ func Start() {
|
||||||
g2.POST("/gists/:gist/delete", adminGistDelete)
|
g2.POST("/gists/:gist/delete", adminGistDelete)
|
||||||
g2.POST("/sync-fs", adminSyncReposFromFS)
|
g2.POST("/sync-fs", adminSyncReposFromFS)
|
||||||
g2.POST("/sync-db", adminSyncReposFromDB)
|
g2.POST("/sync-db", adminSyncReposFromDB)
|
||||||
|
g2.PUT("/set-setting", adminSetSetting)
|
||||||
}
|
}
|
||||||
|
|
||||||
g1.GET("/all", allGists)
|
g1.GET("/all", allGists)
|
||||||
|
@ -236,7 +237,12 @@ func dataInit(next echo.HandlerFunc) echo.HandlerFunc {
|
||||||
ctxValue := context.WithValue(ctx.Request().Context(), "data", echo.Map{})
|
ctxValue := context.WithValue(ctx.Request().Context(), "data", echo.Map{})
|
||||||
ctx.SetRequest(ctx.Request().WithContext(ctxValue))
|
ctx.SetRequest(ctx.Request().WithContext(ctxValue))
|
||||||
setData(ctx, "loadStartTime", time.Now())
|
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)
|
return next(ctx)
|
||||||
}
|
}
|
||||||
|
|
22
public/admin.ts
Normal file
22
public/admin.ts
Normal 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');
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
17
templates/pages/admin_index.html
vendored
17
templates/pages/admin_index.html
vendored
|
@ -71,7 +71,24 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</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>
|
</div>
|
||||||
|
|
||||||
|
<script type="module" src="{{ asset "admin.ts" }}"></script>
|
||||||
|
|
||||||
{{ template "admin_footer" .}}
|
{{ template "admin_footer" .}}
|
||||||
{{ template "footer" .}}
|
{{ template "footer" .}}
|
||||||
|
|
|
@ -9,7 +9,7 @@ export default defineConfig({
|
||||||
assetsDir: 'assets',
|
assetsDir: 'assets',
|
||||||
manifest: true,
|
manifest: true,
|
||||||
rollupOptions: {
|
rollupOptions: {
|
||||||
input: ['./public/main.ts', './public/editor.ts']
|
input: ['./public/main.ts', './public/editor.ts', './public/admin.ts']
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue