1Panel/backend/middleware/password_expired.go

46 lines
1.3 KiB
Go
Raw Normal View History

package middleware
import (
2022-11-21 15:20:04 +08:00
"strconv"
2022-12-02 10:23:35 +08:00
"time"
2022-11-21 15:20:04 +08:00
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper"
2022-11-21 15:20:04 +08:00
"github.com/1Panel-dev/1Panel/backend/app/repo"
"github.com/1Panel-dev/1Panel/backend/constant"
2023-05-04 12:00:37 +08:00
"github.com/1Panel-dev/1Panel/backend/utils/common"
"github.com/gin-gonic/gin"
)
func PasswordExpired() gin.HandlerFunc {
return func(c *gin.Context) {
2022-11-21 15:20:04 +08:00
settingRepo := repo.NewISettingRepo()
setting, err := settingRepo.Get(settingRepo.WithByKey("ExpirationDays"))
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypePasswordExpired, err)
2022-12-02 10:23:35 +08:00
return
2022-11-21 15:20:04 +08:00
}
expiredDays, _ := strconv.Atoi(setting.Value)
if expiredDays == 0 {
c.Next()
2022-11-24 23:56:48 +08:00
return
2022-11-21 15:20:04 +08:00
}
2022-12-02 10:23:35 +08:00
extime, err := settingRepo.Get(settingRepo.WithByKey("ExpirationTime"))
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypePasswordExpired, err)
2022-12-02 10:23:35 +08:00
return
}
2023-05-04 12:00:37 +08:00
loc, _ := time.LoadLocation(common.LoadTimeZone())
2024-06-28 14:04:08 +08:00
expiredTime, err := time.ParseInLocation(constant.DateTimeLayout, extime.Value, loc)
2022-12-02 10:23:35 +08:00
if err != nil {
helper.ErrorWithDetail(c, constant.CodePasswordExpired, constant.ErrTypePasswordExpired, err)
2022-12-02 10:23:35 +08:00
return
}
if time.Now().After(expiredTime) {
helper.ErrorWithDetail(c, constant.CodePasswordExpired, constant.ErrTypePasswordExpired, err)
return
}
c.Next()
}
}