1Panel/core/middleware/password_expired.go
2024-07-25 18:13:57 +08:00

53 lines
1.5 KiB
Go

package middleware
import (
"strconv"
"strings"
"time"
"github.com/1Panel-dev/1Panel/core/app/api/v1/helper"
"github.com/1Panel-dev/1Panel/core/app/repo"
"github.com/1Panel-dev/1Panel/core/constant"
"github.com/1Panel-dev/1Panel/core/utils/common"
"github.com/gin-gonic/gin"
)
func PasswordExpired() gin.HandlerFunc {
return func(c *gin.Context) {
if strings.HasPrefix(c.Request.URL.Path, "/api/v2/core/auth") ||
c.Request.URL.Path == "/api/v2/core/settings/expired/handle" ||
c.Request.URL.Path == "/api/v2/core/settings/search" {
c.Next()
return
}
settingRepo := repo.NewISettingRepo()
setting, err := settingRepo.Get(settingRepo.WithByKey("ExpirationDays"))
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypePasswordExpired, err)
return
}
expiredDays, _ := strconv.Atoi(setting.Value)
if expiredDays == 0 {
c.Next()
return
}
extime, err := settingRepo.Get(settingRepo.WithByKey("ExpirationTime"))
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypePasswordExpired, err)
return
}
loc, _ := time.LoadLocation(common.LoadTimeZone())
expiredTime, err := time.ParseInLocation(constant.DateTimeLayout, extime.Value, loc)
if err != nil {
helper.ErrorWithDetail(c, constant.CodePasswordExpired, constant.ErrTypePasswordExpired, err)
return
}
if time.Now().After(expiredTime) {
helper.ErrorWithDetail(c, constant.CodePasswordExpired, constant.ErrTypePasswordExpired, err)
return
}
c.Next()
}
}