2024-08-17 21:57:14 +02:00
|
|
|
package controller
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
2025-03-27 10:20:39 -07:00
|
|
|
"strconv"
|
2025-02-05 18:08:01 +01:00
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"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/middleware"
|
|
|
|
|
"github.com/pocket-id/pocket-id/backend/internal/service"
|
2024-08-17 21:57:14 +02:00
|
|
|
)
|
|
|
|
|
|
2025-03-11 14:16:42 -05:00
|
|
|
// NewAppConfigController creates a new controller for application configuration endpoints
|
|
|
|
|
// @Summary Create a new application configuration controller
|
|
|
|
|
// @Description Initialize routes for application configuration
|
|
|
|
|
// @Tags Application Configuration
|
2024-08-23 17:04:19 +02:00
|
|
|
func NewAppConfigController(
|
2024-08-17 21:57:14 +02:00
|
|
|
group *gin.RouterGroup,
|
2025-03-11 14:16:42 -05:00
|
|
|
authMiddleware *middleware.AuthMiddleware,
|
2024-11-21 18:24:01 +01:00
|
|
|
appConfigService *service.AppConfigService,
|
|
|
|
|
emailService *service.EmailService,
|
2025-01-19 06:02:07 -06:00
|
|
|
ldapService *service.LdapService,
|
2024-11-21 18:24:01 +01:00
|
|
|
) {
|
2024-08-17 21:57:14 +02:00
|
|
|
|
2024-08-23 17:04:19 +02:00
|
|
|
acc := &AppConfigController{
|
2024-08-17 21:57:14 +02:00
|
|
|
appConfigService: appConfigService,
|
2024-11-21 18:24:01 +01:00
|
|
|
emailService: emailService,
|
2025-01-19 06:02:07 -06:00
|
|
|
ldapService: ldapService,
|
2024-08-17 21:57:14 +02:00
|
|
|
}
|
2024-09-09 10:29:41 +02:00
|
|
|
group.GET("/application-configuration", acc.listAppConfigHandler)
|
2025-03-11 14:16:42 -05:00
|
|
|
group.GET("/application-configuration/all", authMiddleware.Add(), acc.listAllAppConfigHandler)
|
|
|
|
|
group.PUT("/application-configuration", authMiddleware.Add(), acc.updateAppConfigHandler)
|
2024-08-17 21:57:14 +02:00
|
|
|
|
2025-03-11 14:16:42 -05:00
|
|
|
group.POST("/application-configuration/test-email", authMiddleware.Add(), acc.testEmailHandler)
|
|
|
|
|
group.POST("/application-configuration/sync-ldap", authMiddleware.Add(), acc.syncLdapHandler)
|
2024-08-17 21:57:14 +02:00
|
|
|
}
|
|
|
|
|
|
2024-08-23 17:04:19 +02:00
|
|
|
type AppConfigController struct {
|
2024-08-17 21:57:14 +02:00
|
|
|
appConfigService *service.AppConfigService
|
2024-11-21 18:24:01 +01:00
|
|
|
emailService *service.EmailService
|
2025-01-19 06:02:07 -06:00
|
|
|
ldapService *service.LdapService
|
2024-08-17 21:57:14 +02:00
|
|
|
}
|
|
|
|
|
|
2025-03-11 14:16:42 -05:00
|
|
|
// listAppConfigHandler godoc
|
|
|
|
|
// @Summary List public application configurations
|
|
|
|
|
// @Description Get all public application configurations
|
|
|
|
|
// @Tags Application Configuration
|
|
|
|
|
// @Accept json
|
|
|
|
|
// @Produce json
|
|
|
|
|
// @Success 200 {array} dto.PublicAppConfigVariableDto
|
|
|
|
|
// @Router /application-configuration [get]
|
2024-09-09 10:29:41 +02:00
|
|
|
func (acc *AppConfigController) listAppConfigHandler(c *gin.Context) {
|
2025-04-10 04:41:22 -07:00
|
|
|
configuration := acc.appConfigService.ListAppConfig(false)
|
2024-08-17 21:57:14 +02:00
|
|
|
|
2024-08-23 17:04:19 +02:00
|
|
|
var configVariablesDto []dto.PublicAppConfigVariableDto
|
|
|
|
|
if err := dto.MapStructList(configuration, &configVariablesDto); err != nil {
|
2025-03-27 16:48:36 +01:00
|
|
|
_ = c.Error(err)
|
2024-08-23 17:04:19 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-17 00:36:58 +02:00
|
|
|
// Manually add uiConfigDisabled which isn't in the database but defined with an environment variable
|
|
|
|
|
configVariablesDto = append(configVariablesDto, dto.PublicAppConfigVariableDto{
|
|
|
|
|
Key: "uiConfigDisabled",
|
|
|
|
|
Value: strconv.FormatBool(common.EnvConfig.UiConfigDisabled),
|
|
|
|
|
Type: "boolean",
|
|
|
|
|
})
|
|
|
|
|
|
2025-04-06 06:04:08 -07:00
|
|
|
c.JSON(http.StatusOK, configVariablesDto)
|
2024-08-17 21:57:14 +02:00
|
|
|
}
|
|
|
|
|
|
2025-03-11 14:16:42 -05:00
|
|
|
// listAllAppConfigHandler godoc
|
|
|
|
|
// @Summary List all application configurations
|
|
|
|
|
// @Description Get all application configurations including private ones
|
|
|
|
|
// @Tags Application Configuration
|
|
|
|
|
// @Accept json
|
|
|
|
|
// @Produce json
|
|
|
|
|
// @Success 200 {array} dto.AppConfigVariableDto
|
|
|
|
|
// @Router /application-configuration/all [get]
|
2024-09-09 10:29:41 +02:00
|
|
|
func (acc *AppConfigController) listAllAppConfigHandler(c *gin.Context) {
|
2025-04-10 04:41:22 -07:00
|
|
|
configuration := acc.appConfigService.ListAppConfig(true)
|
2024-08-17 21:57:14 +02:00
|
|
|
|
2024-08-23 17:04:19 +02:00
|
|
|
var configVariablesDto []dto.AppConfigVariableDto
|
|
|
|
|
if err := dto.MapStructList(configuration, &configVariablesDto); err != nil {
|
2025-03-27 16:48:36 +01:00
|
|
|
_ = c.Error(err)
|
2024-08-23 17:04:19 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-06 06:04:08 -07:00
|
|
|
c.JSON(http.StatusOK, configVariablesDto)
|
2024-08-17 21:57:14 +02:00
|
|
|
}
|
|
|
|
|
|
2025-03-11 14:16:42 -05:00
|
|
|
// updateAppConfigHandler godoc
|
|
|
|
|
// @Summary Update application configurations
|
|
|
|
|
// @Description Update application configuration settings
|
|
|
|
|
// @Tags Application Configuration
|
|
|
|
|
// @Accept json
|
|
|
|
|
// @Produce json
|
|
|
|
|
// @Param body body dto.AppConfigUpdateDto true "Application Configuration"
|
|
|
|
|
// @Success 200 {array} dto.AppConfigVariableDto
|
2025-03-23 14:26:07 -05:00
|
|
|
// @Router /api/application-configuration [put]
|
2024-09-09 10:29:41 +02:00
|
|
|
func (acc *AppConfigController) updateAppConfigHandler(c *gin.Context) {
|
2024-08-23 17:04:19 +02:00
|
|
|
var input dto.AppConfigUpdateDto
|
2025-07-13 18:15:57 +02:00
|
|
|
if err := dto.ShouldBindWithNormalizedJSON(c, &input); err != nil {
|
2025-03-27 16:48:36 +01:00
|
|
|
_ = c.Error(err)
|
2024-08-17 21:57:14 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-06 06:04:08 -07:00
|
|
|
savedConfigVariables, err := acc.appConfigService.UpdateAppConfig(c.Request.Context(), input)
|
2024-08-17 21:57:14 +02:00
|
|
|
if err != nil {
|
2025-03-27 16:48:36 +01:00
|
|
|
_ = c.Error(err)
|
2024-08-23 17:04:19 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var configVariablesDto []dto.AppConfigVariableDto
|
|
|
|
|
if err := dto.MapStructList(savedConfigVariables, &configVariablesDto); err != nil {
|
2025-03-27 16:48:36 +01:00
|
|
|
_ = c.Error(err)
|
2024-08-17 21:57:14 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-23 17:04:19 +02:00
|
|
|
c.JSON(http.StatusOK, configVariablesDto)
|
2024-08-17 21:57:14 +02:00
|
|
|
}
|
|
|
|
|
|
2025-03-11 14:16:42 -05:00
|
|
|
// syncLdapHandler godoc
|
|
|
|
|
// @Summary Synchronize LDAP
|
|
|
|
|
// @Description Manually trigger LDAP synchronization
|
|
|
|
|
// @Tags Application Configuration
|
|
|
|
|
// @Success 204 "No Content"
|
2025-03-23 14:26:07 -05:00
|
|
|
// @Router /api/application-configuration/sync-ldap [post]
|
2025-01-19 06:02:07 -06:00
|
|
|
func (acc *AppConfigController) syncLdapHandler(c *gin.Context) {
|
2025-04-06 06:04:08 -07:00
|
|
|
err := acc.ldapService.SyncAll(c.Request.Context())
|
2025-01-19 06:02:07 -06:00
|
|
|
if err != nil {
|
2025-03-27 16:48:36 +01:00
|
|
|
_ = c.Error(err)
|
2025-01-19 06:02:07 -06:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.Status(http.StatusNoContent)
|
|
|
|
|
}
|
2025-03-11 14:16:42 -05:00
|
|
|
|
|
|
|
|
// testEmailHandler godoc
|
|
|
|
|
// @Summary Send test email
|
|
|
|
|
// @Description Send a test email to verify email configuration
|
|
|
|
|
// @Tags Application Configuration
|
|
|
|
|
// @Success 204 "No Content"
|
2025-03-23 14:26:07 -05:00
|
|
|
// @Router /api/application-configuration/test-email [post]
|
2024-11-21 18:24:01 +01:00
|
|
|
func (acc *AppConfigController) testEmailHandler(c *gin.Context) {
|
2025-01-10 09:25:26 +01:00
|
|
|
userID := c.GetString("userID")
|
|
|
|
|
|
2025-04-06 06:04:08 -07:00
|
|
|
err := acc.emailService.SendTestEmail(c.Request.Context(), userID)
|
2024-11-21 18:24:01 +01:00
|
|
|
if err != nil {
|
2025-03-27 16:48:36 +01:00
|
|
|
_ = c.Error(err)
|
2024-11-21 18:24:01 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.Status(http.StatusNoContent)
|
|
|
|
|
}
|