Files
wehub-resource-sync 75f3dd141c
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (actions) (push) Has been cancelled
CodeQL / Analyze (javascript-typescript) (push) Has been cancelled
CI and Release / lint-frontend (push) Has been cancelled
CI and Release / dockerfile-scan (push) Has been cancelled
CI and Release / test-frontend (push) Has been cancelled
CI and Release / lint-verification-agent (push) Has been cancelled
CI and Release / test-verification-agent (push) Has been cancelled
CI and Release / e2e-verification-agent (push) Has been cancelled
CI and Release / test-backend (push) Has been cancelled
CI and Release / build-dev-image (push) Has been cancelled
CI and Release / push-dev-image (push) Has been cancelled
CI and Release / build-image (push) Has been cancelled
CI and Release / build-verification-image (push) Has been cancelled
CI and Release / determine-version (push) Has been cancelled
CI and Release / push-image (push) Has been cancelled
CI and Release / push-verification-image (12) (push) Has been cancelled
CI and Release / lint-backend (push) Has been cancelled
CI and Release / push-verification-image (17) (push) Has been cancelled
CI and Release / push-verification-image (18) (push) Has been cancelled
CI and Release / release (push) Has been cancelled
CI and Release / publish-helm-chart (push) Has been cancelled
CI and Release / push-verification-image (13) (push) Has been cancelled
CI and Release / push-verification-image (14) (push) Has been cancelled
CI and Release / push-verification-image (15) (push) Has been cancelled
CI and Release / push-verification-image (16) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:11:07 +08:00

81 lines
2.3 KiB
Go

package users_services
import (
"fmt"
users_interfaces "databasus-backend/internal/features/users/interfaces"
users_models "databasus-backend/internal/features/users/models"
users_repositories "databasus-backend/internal/features/users/repositories"
)
type SettingsService struct {
userSettingsRepository *users_repositories.UsersSettingsRepository
auditLogWriter users_interfaces.AuditLogWriter
}
func (s *SettingsService) SetAuditLogWriter(writer users_interfaces.AuditLogWriter) {
s.auditLogWriter = writer
}
func (s *SettingsService) GetSettings() (*users_models.UsersSettings, error) {
return s.userSettingsRepository.GetSettings()
}
func (s *SettingsService) UpdateSettings(
request users_models.UsersSettings,
updatedBy *users_models.User,
) (*users_models.UsersSettings, error) {
if !updatedBy.CanUpdateSettings() {
return nil, fmt.Errorf("insufficient permissions to update settings")
}
existingSettings, err := s.userSettingsRepository.GetSettings()
if err != nil {
return nil, fmt.Errorf("failed to get current settings: %w", err)
}
auditLogMessages := []string{}
if request.IsAllowExternalRegistrations != existingSettings.IsAllowExternalRegistrations {
existingSettings.IsAllowExternalRegistrations = request.IsAllowExternalRegistrations
auditLogMessages = append(
auditLogMessages,
fmt.Sprintf(
"isAllowExternalRegistrations: %t -> %t",
existingSettings.IsAllowExternalRegistrations,
request.IsAllowExternalRegistrations,
),
)
}
if request.IsAllowMemberInvitations != existingSettings.IsAllowMemberInvitations {
existingSettings.IsAllowMemberInvitations = request.IsAllowMemberInvitations
auditLogMessages = append(
auditLogMessages,
fmt.Sprintf(
"isAllowMemberInvitations: %t -> %t",
existingSettings.IsAllowMemberInvitations,
request.IsAllowMemberInvitations,
),
)
}
if request.IsMemberAllowedToCreateWorkspaces != existingSettings.IsMemberAllowedToCreateWorkspaces {
existingSettings.IsMemberAllowedToCreateWorkspaces = request.IsMemberAllowedToCreateWorkspaces
}
if err := s.userSettingsRepository.UpdateSettings(existingSettings); err != nil {
return nil, fmt.Errorf("failed to update settings: %w", err)
}
for _, message := range auditLogMessages {
s.auditLogWriter.WriteAuditLog(
message,
&updatedBy.ID,
nil,
)
}
return existingSettings, nil
}