2022-09-15 10:44:43 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-09-16 16:00:49 +08:00
|
|
|
"strconv"
|
2022-09-09 17:17:02 +08:00
|
|
|
"time"
|
2022-09-15 10:44:43 +08:00
|
|
|
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/encrypt"
|
2022-09-08 18:47:15 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
2022-09-15 10:44:43 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type SettingService struct{}
|
|
|
|
|
|
|
|
type ISettingService interface {
|
|
|
|
GetSettingInfo() (*dto.SettingInfo, error)
|
2022-12-07 17:28:14 +08:00
|
|
|
GetDaemonjson() (string, error)
|
2022-09-08 18:47:15 +08:00
|
|
|
Update(c *gin.Context, key, value string) error
|
|
|
|
UpdatePassword(c *gin.Context, old, new string) error
|
2022-09-29 16:15:59 +08:00
|
|
|
HandlePasswordExpired(c *gin.Context, old, new string) error
|
2022-09-15 10:44:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewISettingService() ISettingService {
|
|
|
|
return &SettingService{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *SettingService) GetSettingInfo() (*dto.SettingInfo, error) {
|
2022-09-08 18:47:15 +08:00
|
|
|
setting, err := settingRepo.GetList()
|
2022-09-15 10:44:43 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, constant.ErrRecordNotFound
|
|
|
|
}
|
|
|
|
settingMap := make(map[string]string)
|
|
|
|
for _, set := range setting {
|
|
|
|
settingMap[set.Key] = set.Value
|
|
|
|
}
|
|
|
|
var info dto.SettingInfo
|
|
|
|
arr, err := json.Marshal(settingMap)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-16 16:00:49 +08:00
|
|
|
_ = json.Unmarshal(arr, &info)
|
|
|
|
info.MonitorStoreDays, _ = strconv.Atoi(settingMap["MonitorStoreDays"])
|
|
|
|
info.ServerPort, _ = strconv.Atoi(settingMap["ServerPort"])
|
|
|
|
info.SessionTimeout, _ = strconv.Atoi(settingMap["SessionTimeout"])
|
2022-11-16 18:27:22 +08:00
|
|
|
info.ExpirationDays, _ = strconv.Atoi(settingMap["ExpirationDays"])
|
2022-09-13 18:45:03 +08:00
|
|
|
info.LocalTime = time.Now().Format("2006-01-02 15:04:05")
|
2022-09-15 10:44:43 +08:00
|
|
|
return &info, err
|
|
|
|
}
|
|
|
|
|
2022-12-07 17:28:14 +08:00
|
|
|
func (u *SettingService) GetDaemonjson() (string, error) {
|
|
|
|
setting, err := settingRepo.Get(settingRepo.WithByKey("DaemonJsonPath"))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return setting.Value, nil
|
|
|
|
}
|
|
|
|
|
2022-09-08 18:47:15 +08:00
|
|
|
func (u *SettingService) Update(c *gin.Context, key, value string) error {
|
2022-09-29 16:15:59 +08:00
|
|
|
if key == "ExpirationDays" {
|
|
|
|
timeout, _ := strconv.Atoi(value)
|
2022-12-02 10:23:35 +08:00
|
|
|
if err := settingRepo.Update("ExpirationTime", time.Now().AddDate(0, 0, timeout).Format("2006-01-02 15:04:05")); err != nil {
|
2022-09-29 16:15:59 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-09-08 18:47:15 +08:00
|
|
|
if err := settingRepo.Update(key, value); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-29 16:15:59 +08:00
|
|
|
return nil
|
2022-09-15 10:44:43 +08:00
|
|
|
}
|
2022-09-08 18:47:15 +08:00
|
|
|
|
2022-09-29 16:15:59 +08:00
|
|
|
func (u *SettingService) HandlePasswordExpired(c *gin.Context, old, new string) error {
|
2022-09-08 18:47:15 +08:00
|
|
|
setting, err := settingRepo.Get(settingRepo.WithByKey("Password"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
passwordFromDB, err := encrypt.StringDecrypt(setting.Value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if passwordFromDB == old {
|
|
|
|
newPassword, err := encrypt.StringEncrypt(new)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := settingRepo.Update("Password", newPassword); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-29 16:15:59 +08:00
|
|
|
|
|
|
|
expiredSetting, err := settingRepo.Get(settingRepo.WithByKey("ExpirationDays"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
timeout, _ := strconv.Atoi(expiredSetting.Value)
|
2022-12-02 10:23:35 +08:00
|
|
|
if err := settingRepo.Update("ExpirationTime", time.Now().AddDate(0, 0, timeout).Format("2006-01-02 15:04:05")); err != nil {
|
2022-09-29 16:15:59 +08:00
|
|
|
return err
|
2022-09-08 18:47:15 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return constant.ErrInitialPassword
|
|
|
|
}
|
2022-09-29 16:15:59 +08:00
|
|
|
|
|
|
|
func (u *SettingService) UpdatePassword(c *gin.Context, old, new string) error {
|
|
|
|
if err := u.HandlePasswordExpired(c, old, new); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sID, _ := c.Cookie(constant.SessionName)
|
|
|
|
if sID != "" {
|
|
|
|
c.SetCookie(constant.SessionName, sID, -1, "", "", false, false)
|
|
|
|
err := global.SESSION.Delete(sID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|