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