2020-04-05 19:37:21 +07:00
|
|
|
package validator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2021-08-11 08:04:35 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
|
|
|
"github.com/authelia/authelia/v4/internal/utils"
|
2020-04-05 19:37:21 +07:00
|
|
|
)
|
|
|
|
|
2020-04-21 04:03:38 +07:00
|
|
|
// ValidateRegulation validates and update regulator configuration.
|
2020-04-05 19:37:21 +07:00
|
|
|
func ValidateRegulation(configuration *schema.RegulationConfiguration, validator *schema.StructValidator) {
|
|
|
|
if configuration.FindTime == "" {
|
2022-01-31 12:25:15 +07:00
|
|
|
configuration.FindTime = schema.DefaultRegulationConfiguration.FindTime // 2 min.
|
2020-04-05 19:37:21 +07:00
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2020-04-05 19:37:21 +07:00
|
|
|
if configuration.BanTime == "" {
|
2022-01-31 12:25:15 +07:00
|
|
|
configuration.BanTime = schema.DefaultRegulationConfiguration.BanTime // 5 min.
|
2020-04-05 19:37:21 +07:00
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2020-04-05 19:37:21 +07:00
|
|
|
findTime, err := utils.ParseDurationString(configuration.FindTime)
|
|
|
|
if err != nil {
|
2020-04-09 08:05:17 +07:00
|
|
|
validator.Push(fmt.Errorf("Error occurred parsing regulation find_time string: %s", err))
|
2020-04-05 19:37:21 +07:00
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2020-04-05 19:37:21 +07:00
|
|
|
banTime, err := utils.ParseDurationString(configuration.BanTime)
|
|
|
|
if err != nil {
|
2020-04-09 08:05:17 +07:00
|
|
|
validator.Push(fmt.Errorf("Error occurred parsing regulation ban_time string: %s", err))
|
2020-04-05 19:37:21 +07:00
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2020-04-05 19:37:21 +07:00
|
|
|
if findTime > banTime {
|
2020-04-09 08:05:17 +07:00
|
|
|
validator.Push(fmt.Errorf("find_time cannot be greater than ban_time"))
|
2020-04-05 19:37:21 +07:00
|
|
|
}
|
|
|
|
}
|