2024-08-17 21:57:14 +02:00
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
import (
|
2025-04-06 06:04:08 -07:00
|
|
|
"context"
|
|
|
|
|
"errors"
|
2025-01-19 06:02:07 -06:00
|
|
|
"log"
|
|
|
|
|
"mime/multipart"
|
|
|
|
|
"os"
|
|
|
|
|
"reflect"
|
|
|
|
|
|
2025-02-05 18:08:01 +01:00
|
|
|
"github.com/pocket-id/pocket-id/backend/internal/common"
|
|
|
|
|
"github.com/pocket-id/pocket-id/backend/internal/dto"
|
|
|
|
|
"github.com/pocket-id/pocket-id/backend/internal/model"
|
|
|
|
|
"github.com/pocket-id/pocket-id/backend/internal/utils"
|
2024-08-17 21:57:14 +02:00
|
|
|
"gorm.io/gorm"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type AppConfigService struct {
|
|
|
|
|
DbConfig *model.AppConfig
|
|
|
|
|
db *gorm.DB
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-06 06:04:08 -07:00
|
|
|
func NewAppConfigService(ctx context.Context, db *gorm.DB) *AppConfigService {
|
2024-08-17 21:57:14 +02:00
|
|
|
service := &AppConfigService{
|
|
|
|
|
DbConfig: &defaultDbConfig,
|
|
|
|
|
db: db,
|
|
|
|
|
}
|
2025-04-06 06:04:08 -07:00
|
|
|
|
|
|
|
|
err := service.InitDbConfig(ctx)
|
|
|
|
|
if err != nil {
|
2024-08-17 21:57:14 +02:00
|
|
|
log.Fatalf("Failed to initialize app config service: %v", err)
|
|
|
|
|
}
|
2025-02-25 12:10:20 -06:00
|
|
|
|
2024-08-17 21:57:14 +02:00
|
|
|
return service
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var defaultDbConfig = model.AppConfig{
|
2025-01-19 06:02:07 -06:00
|
|
|
// General
|
2024-08-17 21:57:14 +02:00
|
|
|
AppName: model.AppConfigVariable{
|
2024-10-26 00:15:31 +02:00
|
|
|
Key: "appName",
|
|
|
|
|
Type: "string",
|
|
|
|
|
IsPublic: true,
|
|
|
|
|
DefaultValue: "Pocket ID",
|
2024-08-17 21:57:14 +02:00
|
|
|
},
|
|
|
|
|
SessionDuration: model.AppConfigVariable{
|
2024-10-26 00:15:31 +02:00
|
|
|
Key: "sessionDuration",
|
|
|
|
|
Type: "number",
|
|
|
|
|
DefaultValue: "60",
|
2024-08-17 21:57:14 +02:00
|
|
|
},
|
2024-10-25 21:33:54 +02:00
|
|
|
EmailsVerified: model.AppConfigVariable{
|
2024-10-26 00:15:31 +02:00
|
|
|
Key: "emailsVerified",
|
|
|
|
|
Type: "bool",
|
|
|
|
|
DefaultValue: "false",
|
2024-10-25 21:33:54 +02:00
|
|
|
},
|
2024-10-28 18:45:27 +01:00
|
|
|
AllowOwnAccountEdit: model.AppConfigVariable{
|
|
|
|
|
Key: "allowOwnAccountEdit",
|
|
|
|
|
Type: "bool",
|
|
|
|
|
IsPublic: true,
|
|
|
|
|
DefaultValue: "true",
|
|
|
|
|
},
|
2025-01-19 06:02:07 -06:00
|
|
|
// Internal
|
2024-08-17 21:57:14 +02:00
|
|
|
BackgroundImageType: model.AppConfigVariable{
|
2024-10-26 00:15:31 +02:00
|
|
|
Key: "backgroundImageType",
|
|
|
|
|
Type: "string",
|
|
|
|
|
IsInternal: true,
|
|
|
|
|
DefaultValue: "jpg",
|
2024-08-17 21:57:14 +02:00
|
|
|
},
|
2024-10-03 11:27:31 +02:00
|
|
|
LogoLightImageType: model.AppConfigVariable{
|
2024-10-26 00:15:31 +02:00
|
|
|
Key: "logoLightImageType",
|
|
|
|
|
Type: "string",
|
|
|
|
|
IsInternal: true,
|
|
|
|
|
DefaultValue: "svg",
|
2024-10-03 11:27:31 +02:00
|
|
|
},
|
|
|
|
|
LogoDarkImageType: model.AppConfigVariable{
|
2024-10-26 00:15:31 +02:00
|
|
|
Key: "logoDarkImageType",
|
|
|
|
|
Type: "string",
|
|
|
|
|
IsInternal: true,
|
|
|
|
|
DefaultValue: "svg",
|
2024-08-17 21:57:14 +02:00
|
|
|
},
|
2025-01-20 18:50:58 +08:00
|
|
|
// Email
|
2024-09-09 10:29:41 +02:00
|
|
|
SmtpHost: model.AppConfigVariable{
|
|
|
|
|
Key: "smtpHost",
|
|
|
|
|
Type: "string",
|
|
|
|
|
},
|
|
|
|
|
SmtpPort: model.AppConfigVariable{
|
|
|
|
|
Key: "smtpPort",
|
|
|
|
|
Type: "number",
|
|
|
|
|
},
|
|
|
|
|
SmtpFrom: model.AppConfigVariable{
|
|
|
|
|
Key: "smtpFrom",
|
|
|
|
|
Type: "string",
|
|
|
|
|
},
|
|
|
|
|
SmtpUser: model.AppConfigVariable{
|
|
|
|
|
Key: "smtpUser",
|
|
|
|
|
Type: "string",
|
|
|
|
|
},
|
|
|
|
|
SmtpPassword: model.AppConfigVariable{
|
|
|
|
|
Key: "smtpPassword",
|
|
|
|
|
Type: "string",
|
|
|
|
|
},
|
2024-11-28 12:13:23 +01:00
|
|
|
SmtpTls: model.AppConfigVariable{
|
|
|
|
|
Key: "smtpTls",
|
2025-02-25 12:10:20 -06:00
|
|
|
Type: "string",
|
|
|
|
|
DefaultValue: "none",
|
2024-11-28 12:13:23 +01:00
|
|
|
},
|
2024-11-21 18:24:01 +01:00
|
|
|
SmtpSkipCertVerify: model.AppConfigVariable{
|
|
|
|
|
Key: "smtpSkipCertVerify",
|
|
|
|
|
Type: "bool",
|
|
|
|
|
DefaultValue: "false",
|
|
|
|
|
},
|
2025-01-20 18:50:58 +08:00
|
|
|
EmailLoginNotificationEnabled: model.AppConfigVariable{
|
2025-01-19 15:30:31 +01:00
|
|
|
Key: "emailLoginNotificationEnabled",
|
|
|
|
|
Type: "bool",
|
|
|
|
|
DefaultValue: "false",
|
|
|
|
|
},
|
|
|
|
|
EmailOneTimeAccessEnabled: model.AppConfigVariable{
|
|
|
|
|
Key: "emailOneTimeAccessEnabled",
|
|
|
|
|
Type: "bool",
|
|
|
|
|
IsPublic: true,
|
|
|
|
|
DefaultValue: "false",
|
|
|
|
|
},
|
2025-01-19 06:02:07 -06:00
|
|
|
// LDAP
|
|
|
|
|
LdapEnabled: model.AppConfigVariable{
|
|
|
|
|
Key: "ldapEnabled",
|
|
|
|
|
Type: "bool",
|
2025-02-03 08:58:20 +01:00
|
|
|
IsPublic: true,
|
2025-01-19 06:02:07 -06:00
|
|
|
DefaultValue: "false",
|
|
|
|
|
},
|
|
|
|
|
LdapUrl: model.AppConfigVariable{
|
|
|
|
|
Key: "ldapUrl",
|
|
|
|
|
Type: "string",
|
|
|
|
|
},
|
|
|
|
|
LdapBindDn: model.AppConfigVariable{
|
|
|
|
|
Key: "ldapBindDn",
|
|
|
|
|
Type: "string",
|
|
|
|
|
},
|
|
|
|
|
LdapBindPassword: model.AppConfigVariable{
|
|
|
|
|
Key: "ldapBindPassword",
|
|
|
|
|
Type: "string",
|
|
|
|
|
},
|
|
|
|
|
LdapBase: model.AppConfigVariable{
|
|
|
|
|
Key: "ldapBase",
|
|
|
|
|
Type: "string",
|
|
|
|
|
},
|
2025-02-08 11:16:57 -06:00
|
|
|
LdapUserSearchFilter: model.AppConfigVariable{
|
|
|
|
|
Key: "ldapUserSearchFilter",
|
|
|
|
|
Type: "string",
|
|
|
|
|
DefaultValue: "(objectClass=person)",
|
|
|
|
|
},
|
|
|
|
|
LdapUserGroupSearchFilter: model.AppConfigVariable{
|
|
|
|
|
Key: "ldapUserGroupSearchFilter",
|
|
|
|
|
Type: "string",
|
|
|
|
|
DefaultValue: "(objectClass=groupOfNames)",
|
|
|
|
|
},
|
2025-01-19 06:02:07 -06:00
|
|
|
LdapSkipCertVerify: model.AppConfigVariable{
|
|
|
|
|
Key: "ldapSkipCertVerify",
|
|
|
|
|
Type: "bool",
|
|
|
|
|
DefaultValue: "false",
|
|
|
|
|
},
|
|
|
|
|
LdapAttributeUserUniqueIdentifier: model.AppConfigVariable{
|
|
|
|
|
Key: "ldapAttributeUserUniqueIdentifier",
|
|
|
|
|
Type: "string",
|
|
|
|
|
},
|
|
|
|
|
LdapAttributeUserUsername: model.AppConfigVariable{
|
|
|
|
|
Key: "ldapAttributeUserUsername",
|
|
|
|
|
Type: "string",
|
|
|
|
|
},
|
|
|
|
|
LdapAttributeUserEmail: model.AppConfigVariable{
|
|
|
|
|
Key: "ldapAttributeUserEmail",
|
|
|
|
|
Type: "string",
|
|
|
|
|
},
|
|
|
|
|
LdapAttributeUserFirstName: model.AppConfigVariable{
|
|
|
|
|
Key: "ldapAttributeUserFirstName",
|
|
|
|
|
Type: "string",
|
|
|
|
|
},
|
|
|
|
|
LdapAttributeUserLastName: model.AppConfigVariable{
|
|
|
|
|
Key: "ldapAttributeUserLastName",
|
|
|
|
|
Type: "string",
|
|
|
|
|
},
|
2025-02-19 14:28:45 +01:00
|
|
|
LdapAttributeUserProfilePicture: model.AppConfigVariable{
|
|
|
|
|
Key: "ldapAttributeUserProfilePicture",
|
|
|
|
|
Type: "string",
|
|
|
|
|
},
|
2025-02-16 11:27:07 -06:00
|
|
|
LdapAttributeGroupMember: model.AppConfigVariable{
|
|
|
|
|
Key: "ldapAttributeGroupMember",
|
|
|
|
|
Type: "string",
|
|
|
|
|
DefaultValue: "member",
|
|
|
|
|
},
|
2025-01-19 06:02:07 -06:00
|
|
|
LdapAttributeGroupUniqueIdentifier: model.AppConfigVariable{
|
|
|
|
|
Key: "ldapAttributeGroupUniqueIdentifier",
|
|
|
|
|
Type: "string",
|
|
|
|
|
},
|
|
|
|
|
LdapAttributeGroupName: model.AppConfigVariable{
|
|
|
|
|
Key: "ldapAttributeGroupName",
|
|
|
|
|
Type: "string",
|
|
|
|
|
},
|
|
|
|
|
LdapAttributeAdminGroup: model.AppConfigVariable{
|
|
|
|
|
Key: "ldapAttributeAdminGroup",
|
|
|
|
|
Type: "string",
|
|
|
|
|
},
|
2024-08-17 21:57:14 +02:00
|
|
|
}
|
|
|
|
|
|
2025-04-06 06:04:08 -07:00
|
|
|
func (s *AppConfigService) UpdateAppConfig(ctx context.Context, input dto.AppConfigUpdateDto) ([]model.AppConfigVariable, error) {
|
2025-02-12 14:20:52 +01:00
|
|
|
if common.EnvConfig.UiConfigDisabled {
|
|
|
|
|
return nil, &common.UiConfigDisabledError{}
|
|
|
|
|
}
|
2024-08-17 21:57:14 +02:00
|
|
|
|
|
|
|
|
tx := s.db.Begin()
|
2025-04-06 06:04:08 -07:00
|
|
|
defer func() {
|
|
|
|
|
tx.Rollback()
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
2024-08-17 21:57:14 +02:00
|
|
|
rt := reflect.ValueOf(input).Type()
|
|
|
|
|
rv := reflect.ValueOf(input)
|
|
|
|
|
|
2025-04-06 06:04:08 -07:00
|
|
|
savedConfigVariables := make([]model.AppConfigVariable, 0, rt.NumField())
|
|
|
|
|
for i := range rt.NumField() {
|
2024-08-17 21:57:14 +02:00
|
|
|
field := rt.Field(i)
|
|
|
|
|
key := field.Tag.Get("json")
|
|
|
|
|
value := rv.FieldByName(field.Name).String()
|
|
|
|
|
|
2025-01-19 15:30:31 +01:00
|
|
|
// If the emailEnabled is set to false, disable the emailOneTimeAccessEnabled
|
|
|
|
|
if key == s.DbConfig.EmailOneTimeAccessEnabled.Key {
|
|
|
|
|
if rv.FieldByName("EmailEnabled").String() == "false" {
|
|
|
|
|
value = "false"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-09 10:29:41 +02:00
|
|
|
var appConfigVariable model.AppConfigVariable
|
2025-04-06 06:04:08 -07:00
|
|
|
err = tx.
|
|
|
|
|
WithContext(ctx).
|
|
|
|
|
First(&appConfigVariable, "key = ? AND is_internal = false", key).
|
|
|
|
|
Error
|
|
|
|
|
if err != nil {
|
2024-08-17 21:57:14 +02:00
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-09 10:29:41 +02:00
|
|
|
appConfigVariable.Value = value
|
2025-04-06 06:04:08 -07:00
|
|
|
err = tx.
|
|
|
|
|
WithContext(ctx).
|
|
|
|
|
Save(&appConfigVariable).
|
|
|
|
|
Error
|
|
|
|
|
if err != nil {
|
2024-08-17 21:57:14 +02:00
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-09 10:29:41 +02:00
|
|
|
savedConfigVariables = append(savedConfigVariables, appConfigVariable)
|
2024-08-17 21:57:14 +02:00
|
|
|
}
|
|
|
|
|
|
2025-04-06 06:04:08 -07:00
|
|
|
err = tx.Commit().Error
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2024-08-17 21:57:14 +02:00
|
|
|
|
2025-04-06 06:04:08 -07:00
|
|
|
err = s.LoadDbConfigFromDb()
|
|
|
|
|
if err != nil {
|
2024-08-17 21:57:14 +02:00
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return savedConfigVariables, nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-06 06:04:08 -07:00
|
|
|
func (s *AppConfigService) updateImageType(ctx context.Context, imageName string, fileType string) error {
|
|
|
|
|
key := imageName + "ImageType"
|
|
|
|
|
err := s.db.
|
|
|
|
|
WithContext(ctx).
|
|
|
|
|
Model(&model.AppConfigVariable{}).
|
|
|
|
|
Where("key = ?", key).
|
|
|
|
|
Update("value", fileType).
|
|
|
|
|
Error
|
2024-08-17 21:57:14 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-26 00:15:31 +02:00
|
|
|
return s.LoadDbConfigFromDb()
|
2024-08-17 21:57:14 +02:00
|
|
|
}
|
|
|
|
|
|
2025-04-06 06:04:08 -07:00
|
|
|
func (s *AppConfigService) ListAppConfig(ctx context.Context, showAll bool) (configuration []model.AppConfigVariable, err error) {
|
2024-08-17 21:57:14 +02:00
|
|
|
if showAll {
|
2025-04-06 06:04:08 -07:00
|
|
|
err = s.db.
|
|
|
|
|
WithContext(ctx).
|
|
|
|
|
Find(&configuration).
|
|
|
|
|
Error
|
2024-08-17 21:57:14 +02:00
|
|
|
} else {
|
2025-04-06 06:04:08 -07:00
|
|
|
err = s.db.
|
|
|
|
|
WithContext(ctx).
|
|
|
|
|
Find(&configuration, "is_public = true").
|
|
|
|
|
Error
|
2024-08-17 21:57:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-26 00:15:31 +02:00
|
|
|
for i := range configuration {
|
2025-02-12 14:20:52 +01:00
|
|
|
if common.EnvConfig.UiConfigDisabled {
|
|
|
|
|
// Set the value to the environment variable if the UI config is disabled
|
|
|
|
|
configuration[i].Value = s.getConfigVariableFromEnvironmentVariable(configuration[i].Key, configuration[i].DefaultValue)
|
|
|
|
|
} else if configuration[i].Value == "" && configuration[i].DefaultValue != "" {
|
|
|
|
|
// Set the value to the default value if it is empty
|
2024-10-26 00:15:31 +02:00
|
|
|
configuration[i].Value = configuration[i].DefaultValue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-17 21:57:14 +02:00
|
|
|
return configuration, nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-06 06:04:08 -07:00
|
|
|
func (s *AppConfigService) UpdateImage(ctx context.Context, uploadedFile *multipart.FileHeader, imageName string, oldImageType string) (err error) {
|
2024-08-17 21:57:14 +02:00
|
|
|
fileType := utils.GetFileExtension(uploadedFile.Filename)
|
|
|
|
|
mimeType := utils.GetImageMimeType(fileType)
|
|
|
|
|
if mimeType == "" {
|
2024-10-28 18:11:54 +01:00
|
|
|
return &common.FileTypeNotSupportedError{}
|
2024-08-17 21:57:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Delete the old image if it has a different file type
|
|
|
|
|
if fileType != oldImageType {
|
2025-04-06 06:04:08 -07:00
|
|
|
oldImagePath := common.EnvConfig.UploadPath + "/application-images/" + imageName + "." + oldImageType
|
|
|
|
|
err = os.Remove(oldImagePath)
|
|
|
|
|
if err != nil {
|
2024-08-17 21:57:14 +02:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-06 06:04:08 -07:00
|
|
|
imagePath := common.EnvConfig.UploadPath + "/application-images/" + imageName + "." + fileType
|
|
|
|
|
err = utils.SaveFile(uploadedFile, imagePath)
|
|
|
|
|
if err != nil {
|
2024-08-17 21:57:14 +02:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update the file type in the database
|
2025-04-06 06:04:08 -07:00
|
|
|
err = s.updateImageType(ctx, imageName, fileType)
|
|
|
|
|
if err != nil {
|
2024-08-17 21:57:14 +02:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// InitDbConfig creates the default configuration values in the database if they do not exist,
|
|
|
|
|
// updates existing configurations if they differ from the default, and deletes any configurations
|
|
|
|
|
// that are not in the default configuration.
|
2025-04-06 06:04:08 -07:00
|
|
|
func (s *AppConfigService) InitDbConfig(ctx context.Context) (err error) {
|
|
|
|
|
tx := s.db.Begin()
|
|
|
|
|
defer func() {
|
|
|
|
|
tx.Rollback()
|
|
|
|
|
}()
|
|
|
|
|
|
2024-08-17 21:57:14 +02:00
|
|
|
// Reflect to get the underlying value of DbConfig and its default configuration
|
|
|
|
|
defaultConfigReflectValue := reflect.ValueOf(defaultDbConfig)
|
|
|
|
|
defaultKeys := make(map[string]struct{})
|
|
|
|
|
|
|
|
|
|
// Iterate over the fields of DbConfig
|
2025-04-06 06:04:08 -07:00
|
|
|
for i := range defaultConfigReflectValue.NumField() {
|
2024-08-17 21:57:14 +02:00
|
|
|
defaultConfigVar := defaultConfigReflectValue.Field(i).Interface().(model.AppConfigVariable)
|
|
|
|
|
|
|
|
|
|
defaultKeys[defaultConfigVar.Key] = struct{}{}
|
|
|
|
|
|
|
|
|
|
var storedConfigVar model.AppConfigVariable
|
2025-04-06 06:04:08 -07:00
|
|
|
err = tx.
|
|
|
|
|
WithContext(ctx).
|
|
|
|
|
First(&storedConfigVar, "key = ?", defaultConfigVar.Key).
|
|
|
|
|
Error
|
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
2024-08-17 21:57:14 +02:00
|
|
|
// If the configuration does not exist, create it
|
2025-04-06 06:04:08 -07:00
|
|
|
err = tx.
|
|
|
|
|
WithContext(ctx).
|
|
|
|
|
Create(&defaultConfigVar).
|
|
|
|
|
Error
|
|
|
|
|
if err != nil {
|
2024-08-17 21:57:14 +02:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
continue
|
2025-04-06 06:04:08 -07:00
|
|
|
} else if err != nil {
|
|
|
|
|
return err
|
2024-08-17 21:57:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update existing configuration if it differs from the default
|
2025-04-06 06:04:08 -07:00
|
|
|
if storedConfigVar.Type != defaultConfigVar.Type ||
|
|
|
|
|
storedConfigVar.IsPublic != defaultConfigVar.IsPublic ||
|
|
|
|
|
storedConfigVar.IsInternal != defaultConfigVar.IsInternal ||
|
|
|
|
|
storedConfigVar.DefaultValue != defaultConfigVar.DefaultValue {
|
|
|
|
|
// Set values
|
2024-08-17 21:57:14 +02:00
|
|
|
storedConfigVar.Type = defaultConfigVar.Type
|
|
|
|
|
storedConfigVar.IsPublic = defaultConfigVar.IsPublic
|
|
|
|
|
storedConfigVar.IsInternal = defaultConfigVar.IsInternal
|
2024-10-26 00:15:31 +02:00
|
|
|
storedConfigVar.DefaultValue = defaultConfigVar.DefaultValue
|
2025-04-06 06:04:08 -07:00
|
|
|
|
|
|
|
|
err = tx.
|
|
|
|
|
WithContext(ctx).
|
|
|
|
|
Save(&storedConfigVar).
|
|
|
|
|
Error
|
|
|
|
|
if err != nil {
|
2024-08-17 21:57:14 +02:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Delete any configurations not in the default keys
|
|
|
|
|
var allConfigVars []model.AppConfigVariable
|
2025-04-06 06:04:08 -07:00
|
|
|
err = tx.
|
|
|
|
|
WithContext(ctx).
|
|
|
|
|
Find(&allConfigVars).
|
|
|
|
|
Error
|
|
|
|
|
if err != nil {
|
2024-08-17 21:57:14 +02:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, config := range allConfigVars {
|
2025-04-06 06:04:08 -07:00
|
|
|
if _, exists := defaultKeys[config.Key]; exists {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = tx.
|
|
|
|
|
WithContext(ctx).
|
|
|
|
|
Delete(&config).
|
|
|
|
|
Error
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2024-08-17 21:57:14 +02:00
|
|
|
}
|
|
|
|
|
}
|
2025-04-06 06:04:08 -07:00
|
|
|
|
|
|
|
|
// Commit the changes
|
|
|
|
|
err = tx.Commit().Error
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reload the configuration
|
|
|
|
|
err = s.LoadDbConfigFromDb()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
2024-08-17 21:57:14 +02:00
|
|
|
}
|
|
|
|
|
|
2024-10-26 00:15:31 +02:00
|
|
|
// LoadDbConfigFromDb loads the configuration values from the database into the DbConfig struct.
|
|
|
|
|
func (s *AppConfigService) LoadDbConfigFromDb() error {
|
2025-04-06 06:04:08 -07:00
|
|
|
return s.db.Transaction(func(tx *gorm.DB) error {
|
|
|
|
|
dbConfigReflectValue := reflect.ValueOf(s.DbConfig).Elem()
|
|
|
|
|
|
|
|
|
|
for i := range dbConfigReflectValue.NumField() {
|
|
|
|
|
dbConfigField := dbConfigReflectValue.Field(i)
|
|
|
|
|
currentConfigVar := dbConfigField.Interface().(model.AppConfigVariable)
|
|
|
|
|
var storedConfigVar model.AppConfigVariable
|
|
|
|
|
err := tx.First(&storedConfigVar, "key = ?", currentConfigVar.Key).Error
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2024-08-17 21:57:14 +02:00
|
|
|
|
2025-04-06 06:04:08 -07:00
|
|
|
if common.EnvConfig.UiConfigDisabled {
|
|
|
|
|
storedConfigVar.Value = s.getConfigVariableFromEnvironmentVariable(currentConfigVar.Key, storedConfigVar.DefaultValue)
|
|
|
|
|
} else if storedConfigVar.Value == "" && storedConfigVar.DefaultValue != "" {
|
|
|
|
|
storedConfigVar.Value = storedConfigVar.DefaultValue
|
|
|
|
|
}
|
2024-08-17 21:57:14 +02:00
|
|
|
|
2025-04-06 06:04:08 -07:00
|
|
|
dbConfigField.Set(reflect.ValueOf(storedConfigVar))
|
2024-10-26 00:15:31 +02:00
|
|
|
}
|
|
|
|
|
|
2025-04-06 06:04:08 -07:00
|
|
|
return nil
|
|
|
|
|
})
|
2024-08-17 21:57:14 +02:00
|
|
|
}
|
2025-02-12 14:20:52 +01:00
|
|
|
|
|
|
|
|
func (s *AppConfigService) getConfigVariableFromEnvironmentVariable(key, fallbackValue string) string {
|
|
|
|
|
environmentVariableName := utils.CamelCaseToScreamingSnakeCase(key)
|
|
|
|
|
|
|
|
|
|
if value, exists := os.LookupEnv(environmentVariableName); exists {
|
|
|
|
|
return value
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fallbackValue
|
|
|
|
|
}
|