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"
|
|
|
|
|
"github.com/pocket-id/pocket-id/backend/internal/utils"
|
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
|
|
|
|
|
|
|
|
group.GET("/application-configuration/logo", acc.getLogoHandler)
|
|
|
|
|
group.GET("/application-configuration/background-image", acc.getBackgroundImageHandler)
|
|
|
|
|
group.GET("/application-configuration/favicon", acc.getFaviconHandler)
|
2025-03-11 14:16:42 -05:00
|
|
|
group.PUT("/application-configuration/logo", authMiddleware.Add(), acc.updateLogoHandler)
|
|
|
|
|
group.PUT("/application-configuration/favicon", authMiddleware.Add(), acc.updateFaviconHandler)
|
|
|
|
|
group.PUT("/application-configuration/background-image", authMiddleware.Add(), acc.updateBackgroundImageHandler)
|
2024-11-21 18:24:01 +01: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
|
|
|
|
|
// @Failure 500 {object} object "{"error": "error message"}"
|
|
|
|
|
// @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-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
|
|
|
|
|
// @Security BearerAuth
|
|
|
|
|
// @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
|
|
|
|
|
// @Security BearerAuth
|
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
|
2024-08-17 21:57:14 +02:00
|
|
|
if err := c.ShouldBindJSON(&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
|
|
|
// getLogoHandler godoc
|
|
|
|
|
// @Summary Get logo image
|
|
|
|
|
// @Description Get the logo image for the application
|
|
|
|
|
// @Tags Application Configuration
|
|
|
|
|
// @Param light query boolean false "Light mode logo (true) or dark mode logo (false)"
|
|
|
|
|
// @Produce image/png
|
|
|
|
|
// @Produce image/jpeg
|
|
|
|
|
// @Produce image/svg+xml
|
|
|
|
|
// @Success 200 {file} binary "Logo image"
|
2025-03-23 14:26:07 -05:00
|
|
|
// @Router /api/application-configuration/logo [get]
|
2024-08-23 17:04:19 +02:00
|
|
|
func (acc *AppConfigController) getLogoHandler(c *gin.Context) {
|
2025-04-10 04:41:22 -07:00
|
|
|
dbConfig := acc.appConfigService.GetDbConfig()
|
2024-10-03 11:27:31 +02:00
|
|
|
|
2025-04-10 04:41:22 -07:00
|
|
|
lightLogo, _ := strconv.ParseBool(c.DefaultQuery("light", "true"))
|
2024-10-03 11:27:31 +02:00
|
|
|
|
2025-04-10 04:41:22 -07:00
|
|
|
var imageName, imageType string
|
2024-10-03 11:27:31 +02:00
|
|
|
if lightLogo {
|
|
|
|
|
imageName = "logoLight"
|
2025-04-10 04:41:22 -07:00
|
|
|
imageType = dbConfig.LogoLightImageType.Value
|
2024-10-03 11:27:31 +02:00
|
|
|
} else {
|
|
|
|
|
imageName = "logoDark"
|
2025-04-10 04:41:22 -07:00
|
|
|
imageType = dbConfig.LogoDarkImageType.Value
|
2024-10-03 11:27:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
acc.getImage(c, imageName, imageType)
|
2024-08-17 21:57:14 +02:00
|
|
|
}
|
|
|
|
|
|
2025-03-11 14:16:42 -05:00
|
|
|
// getFaviconHandler godoc
|
|
|
|
|
// @Summary Get favicon
|
|
|
|
|
// @Description Get the favicon for the application
|
|
|
|
|
// @Tags Application Configuration
|
|
|
|
|
// @Produce image/x-icon
|
|
|
|
|
// @Success 200 {file} binary "Favicon image"
|
|
|
|
|
// @Failure 404 {object} object "{"error": "File not found"}"
|
2025-03-23 14:26:07 -05:00
|
|
|
// @Router /api/application-configuration/favicon [get]
|
2024-08-23 17:04:19 +02:00
|
|
|
func (acc *AppConfigController) getFaviconHandler(c *gin.Context) {
|
2024-08-17 21:57:14 +02:00
|
|
|
acc.getImage(c, "favicon", "ico")
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-11 14:16:42 -05:00
|
|
|
// getBackgroundImageHandler godoc
|
|
|
|
|
// @Summary Get background image
|
|
|
|
|
// @Description Get the background image for the application
|
|
|
|
|
// @Tags Application Configuration
|
|
|
|
|
// @Produce image/png
|
|
|
|
|
// @Produce image/jpeg
|
|
|
|
|
// @Success 200 {file} binary "Background image"
|
|
|
|
|
// @Failure 404 {object} object "{"error": "File not found"}"
|
2025-03-23 14:26:07 -05:00
|
|
|
// @Router /api/application-configuration/background-image [get]
|
2024-08-23 17:04:19 +02:00
|
|
|
func (acc *AppConfigController) getBackgroundImageHandler(c *gin.Context) {
|
2025-04-10 04:41:22 -07:00
|
|
|
imageType := acc.appConfigService.GetDbConfig().BackgroundImageType.Value
|
2024-08-17 21:57:14 +02:00
|
|
|
acc.getImage(c, "background", imageType)
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-11 14:16:42 -05:00
|
|
|
// updateLogoHandler godoc
|
|
|
|
|
// @Summary Update logo
|
|
|
|
|
// @Description Update the application logo
|
|
|
|
|
// @Tags Application Configuration
|
|
|
|
|
// @Accept multipart/form-data
|
|
|
|
|
// @Param light query boolean false "Light mode logo (true) or dark mode logo (false)"
|
|
|
|
|
// @Param file formData file true "Logo image file"
|
|
|
|
|
// @Success 204 "No Content"
|
|
|
|
|
// @Security BearerAuth
|
2025-03-23 14:26:07 -05:00
|
|
|
// @Router /api/application-configuration/logo [put]
|
2024-08-23 17:04:19 +02:00
|
|
|
func (acc *AppConfigController) updateLogoHandler(c *gin.Context) {
|
2025-04-10 04:41:22 -07:00
|
|
|
dbConfig := acc.appConfigService.GetDbConfig()
|
2024-10-03 11:27:31 +02:00
|
|
|
|
2025-04-10 04:41:22 -07:00
|
|
|
lightLogo, _ := strconv.ParseBool(c.DefaultQuery("light", "true"))
|
2024-10-03 11:27:31 +02:00
|
|
|
|
2025-04-10 04:41:22 -07:00
|
|
|
var imageName, imageType string
|
2024-10-03 11:27:31 +02:00
|
|
|
if lightLogo {
|
|
|
|
|
imageName = "logoLight"
|
2025-04-10 04:41:22 -07:00
|
|
|
imageType = dbConfig.LogoLightImageType.Value
|
2024-10-03 11:27:31 +02:00
|
|
|
} else {
|
|
|
|
|
imageName = "logoDark"
|
2025-04-10 04:41:22 -07:00
|
|
|
imageType = dbConfig.LogoDarkImageType.Value
|
2024-10-03 11:27:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
acc.updateImage(c, imageName, imageType)
|
2024-08-17 21:57:14 +02:00
|
|
|
}
|
|
|
|
|
|
2025-03-11 14:16:42 -05:00
|
|
|
// updateFaviconHandler godoc
|
|
|
|
|
// @Summary Update favicon
|
|
|
|
|
// @Description Update the application favicon
|
|
|
|
|
// @Tags Application Configuration
|
|
|
|
|
// @Accept multipart/form-data
|
|
|
|
|
// @Param file formData file true "Favicon file (.ico)"
|
|
|
|
|
// @Success 204 "No Content"
|
|
|
|
|
// @Security BearerAuth
|
2025-03-23 14:26:07 -05:00
|
|
|
// @Router /api/application-configuration/favicon [put]
|
2024-08-23 17:04:19 +02:00
|
|
|
func (acc *AppConfigController) updateFaviconHandler(c *gin.Context) {
|
2024-08-17 21:57:14 +02:00
|
|
|
file, err := c.FormFile("file")
|
|
|
|
|
if err != nil {
|
2025-03-27 16:48:36 +01:00
|
|
|
_ = c.Error(err)
|
2024-08-17 21:57:14 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fileType := utils.GetFileExtension(file.Filename)
|
|
|
|
|
if fileType != "ico" {
|
2025-03-27 16:48:36 +01:00
|
|
|
_ = c.Error(&common.WrongFileTypeError{ExpectedFileType: ".ico"})
|
2024-08-17 21:57:14 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
acc.updateImage(c, "favicon", "ico")
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-11 14:16:42 -05:00
|
|
|
// updateBackgroundImageHandler godoc
|
|
|
|
|
// @Summary Update background image
|
|
|
|
|
// @Description Update the application background image
|
|
|
|
|
// @Tags Application Configuration
|
|
|
|
|
// @Accept multipart/form-data
|
|
|
|
|
// @Param file formData file true "Background image file"
|
|
|
|
|
// @Success 204 "No Content"
|
|
|
|
|
// @Security BearerAuth
|
2025-03-23 14:26:07 -05:00
|
|
|
// @Router /api/application-configuration/background-image [put]
|
2024-08-23 17:04:19 +02:00
|
|
|
func (acc *AppConfigController) updateBackgroundImageHandler(c *gin.Context) {
|
2025-04-10 04:41:22 -07:00
|
|
|
imageType := acc.appConfigService.GetDbConfig().BackgroundImageType.Value
|
2024-08-17 21:57:14 +02:00
|
|
|
acc.updateImage(c, "background", imageType)
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-11 14:16:42 -05:00
|
|
|
// getImage is a helper function to serve image files
|
2024-08-23 17:04:19 +02:00
|
|
|
func (acc *AppConfigController) getImage(c *gin.Context, name string, imageType string) {
|
2025-04-06 06:04:08 -07:00
|
|
|
imagePath := common.EnvConfig.UploadPath + "/application-images/" + name + "." + imageType
|
2024-08-17 21:57:14 +02:00
|
|
|
mimeType := utils.GetImageMimeType(imageType)
|
|
|
|
|
|
|
|
|
|
c.Header("Content-Type", mimeType)
|
|
|
|
|
c.File(imagePath)
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-11 14:16:42 -05:00
|
|
|
// updateImage is a helper function to update image files
|
2024-08-23 17:04:19 +02:00
|
|
|
func (acc *AppConfigController) updateImage(c *gin.Context, imageName string, oldImageType string) {
|
2024-08-17 21:57:14 +02:00
|
|
|
file, err := c.FormFile("file")
|
|
|
|
|
if 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
|
|
|
err = acc.appConfigService.UpdateImage(c.Request.Context(), file, imageName, oldImageType)
|
2024-08-17 21:57:14 +02:00
|
|
|
if err != nil {
|
2025-03-27 16:48:36 +01:00
|
|
|
_ = c.Error(err)
|
2024-08-17 21:57:14 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.Status(http.StatusNoContent)
|
|
|
|
|
}
|
2024-11-21 18:24:01 +01: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"
|
|
|
|
|
// @Security BearerAuth
|
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"
|
|
|
|
|
// @Security BearerAuth
|
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)
|
|
|
|
|
}
|