reCoreD-UI/controllers/settings.go

90 lines
2.1 KiB
Go
Raw Normal View History

2024-04-09 08:28:18 +00:00
package controllers
import (
2024-04-11 03:41:33 +00:00
"errors"
2024-04-09 08:28:18 +00:00
"reCoreD-UI/database"
"reCoreD-UI/models"
"strings"
2024-04-11 02:51:50 +00:00
"github.com/sirupsen/logrus"
2024-04-09 08:28:18 +00:00
)
const dnsSep = ","
type settingsDAO struct {
2024-04-11 02:51:50 +00:00
database.BaseDAO[models.ISettings]
2024-04-09 08:28:18 +00:00
}
func SetupDNS(dns ...string) error {
settings := models.Settings{Key: models.SettingsKeyDNSServer, Value: strings.Join(dns, dnsSep)}
2024-04-11 03:41:33 +00:00
if _, err := (settingsDAO{}).UpdateOrCreate(database.Client, &settings, &models.Settings{Key: models.SettingsKeyDNSServer}); err != nil {
2024-04-09 08:28:18 +00:00
return err
}
return nil
}
func GetDNS() ([]string, error) {
2024-04-11 03:41:33 +00:00
settings, err := (settingsDAO{}).GetOne(database.Client, &models.Settings{Key: models.SettingsKeyDNSServer})
2024-04-09 08:28:18 +00:00
if err != nil {
return nil, err
}
2024-04-11 03:41:33 +00:00
d, ok := settings.(*models.Settings)
if !ok {
return nil, errors.New("cannot get dns config")
}
2024-04-09 08:28:18 +00:00
2024-04-11 03:41:33 +00:00
return strings.Split(d.Value, dnsSep), nil
2024-04-09 08:28:18 +00:00
}
func SetupAdmin(username, password string) error {
2024-04-11 03:41:33 +00:00
logrus.Debugf("got %s: %s", username, password)
2024-04-11 02:51:50 +00:00
2024-04-09 08:28:18 +00:00
settingUsername := models.Settings{
Key: models.SettingsKeyAdminUsername,
Value: username,
}
settingPassword := models.Settings{
Key: models.SettingsKeyAdminPassword,
Value: password,
}
tx := database.Client.Begin()
2024-04-11 03:41:33 +00:00
if _, err := (settingsDAO{}).UpdateOrCreate(tx, &settingUsername, &models.Settings{Key: models.SettingsKeyAdminUsername}); err != nil {
2024-04-09 08:28:18 +00:00
tx.Rollback()
return err
}
2024-04-11 03:41:33 +00:00
if _, err := (settingsDAO{}).UpdateOrCreate(tx, &settingPassword, &models.Settings{Key: models.SettingsKeyAdminPassword}); err != nil {
2024-04-09 08:28:18 +00:00
tx.Rollback()
return err
}
2024-04-11 02:51:50 +00:00
return tx.Commit().Error
2024-04-09 08:28:18 +00:00
}
func GetAdmin() (string, string, error) {
2024-04-11 03:41:33 +00:00
settings, err := (settingsDAO{}).GetOne(database.Client, &models.Settings{Key: models.SettingsKeyAdminUsername})
2024-04-09 08:28:18 +00:00
if err != nil {
return "", "", err
}
2024-04-11 03:41:33 +00:00
u, ok := settings.(*models.Settings)
if !ok {
return "", "", errors.New("cannot get admin username")
}
username := u.Value
2024-04-09 08:28:18 +00:00
2024-04-11 03:41:33 +00:00
settings, err = (settingsDAO{}).GetOne(database.Client, &models.Settings{Key: models.SettingsKeyAdminPassword})
2024-04-09 08:28:18 +00:00
if err != nil {
return "", "", err
}
2024-04-11 03:41:33 +00:00
p, ok := settings.(*models.Settings)
password := p.Value
2024-04-09 08:28:18 +00:00
2024-04-11 03:41:33 +00:00
if !ok {
return "", "", errors.New("cannot get admin password")
}
2024-04-09 08:28:18 +00:00
return username, password, nil
}