2023-09-03 00:30:57 +02:00
|
|
|
package db
|
2023-04-17 00:17:06 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"gorm.io/gorm/clause"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AdminSetting struct {
|
2024-09-20 16:01:09 +02:00
|
|
|
Key string `gorm:"index:,unique"`
|
2023-04-17 00:17:06 +02:00
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2024-05-12 14:40:11 -07:00
|
|
|
SettingDisableSignup = "disable-signup"
|
|
|
|
SettingRequireLogin = "require-login"
|
|
|
|
SettingAllowGistsWithoutLogin = "allow-gists-without-login"
|
|
|
|
SettingDisableLoginForm = "disable-login-form"
|
|
|
|
SettingDisableGravatar = "disable-gravatar"
|
2023-04-17 00:17:06 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func GetSetting(key string) (string, error) {
|
|
|
|
var setting AdminSetting
|
2024-11-17 21:25:59 +01:00
|
|
|
err := db.Where("`key` = ?", key).First(&setting).Error
|
2023-04-17 00:17:06 +02:00
|
|
|
return setting.Value, err
|
|
|
|
}
|
|
|
|
|
2023-04-28 20:31:10 +02:00
|
|
|
func GetSettings() (map[string]string, error) {
|
|
|
|
var settings []AdminSetting
|
|
|
|
err := db.Find(&settings).Error
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result := make(map[string]string)
|
|
|
|
for _, setting := range settings {
|
|
|
|
result[setting.Key] = setting.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2023-04-17 00:17:06 +02:00
|
|
|
func UpdateSetting(key string, value string) error {
|
|
|
|
return db.Clauses(clause.OnConflict{
|
2023-04-28 20:31:10 +02:00
|
|
|
Columns: []clause.Column{{Name: "key"}}, // key column
|
2023-04-17 00:17:06 +02:00
|
|
|
DoUpdates: clause.AssignmentColumns([]string{"value"}),
|
|
|
|
}).Create(&AdminSetting{
|
|
|
|
Key: key,
|
|
|
|
Value: value,
|
|
|
|
}).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func setSetting(key string, value string) error {
|
2024-09-20 16:01:09 +02:00
|
|
|
return db.FirstOrCreate(&AdminSetting{Key: key, Value: value}, &AdminSetting{Key: key}).Error
|
2023-04-17 00:17:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func initAdminSettings(settings map[string]string) error {
|
|
|
|
for key, value := range settings {
|
|
|
|
if err := setSetting(key, value); err != nil {
|
2023-04-17 14:25:39 +02:00
|
|
|
if !IsUniqueConstraintViolation(err) {
|
2023-04-17 00:17:06 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-05-12 14:40:11 -07:00
|
|
|
|
2024-09-20 16:01:09 +02:00
|
|
|
type AuthInfo struct{}
|
2024-05-12 14:40:11 -07:00
|
|
|
|
2024-09-20 16:01:09 +02:00
|
|
|
func (auth AuthInfo) RequireLogin() (bool, error) {
|
2024-05-12 14:40:11 -07:00
|
|
|
s, err := GetSetting(SettingRequireLogin)
|
|
|
|
if err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
return s == "1", nil
|
|
|
|
}
|
|
|
|
|
2024-09-20 16:01:09 +02:00
|
|
|
func (auth AuthInfo) AllowGistsWithoutLogin() (bool, error) {
|
2024-05-12 14:40:11 -07:00
|
|
|
s, err := GetSetting(SettingAllowGistsWithoutLogin)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return s == "1", nil
|
|
|
|
}
|