2020-06-06 00:36:39 +00:00
|
|
|
package users
|
|
|
|
|
2020-08-10 03:10:37 +00:00
|
|
|
import (
|
|
|
|
"github.com/statping/statping/types/errors"
|
|
|
|
"github.com/statping/statping/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (u *User) Validate() error {
|
|
|
|
if u.Username == "" {
|
|
|
|
return errors.New("username is empty")
|
|
|
|
} else if u.Password == "" {
|
|
|
|
return errors.New("password is empty")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-06-06 00:36:39 +00:00
|
|
|
|
2020-09-01 19:47:32 +00:00
|
|
|
func (u *User) BeforeDelete() error {
|
|
|
|
if utils.Params.GetBool("DEMO_MODE") {
|
|
|
|
if u.Username == "admin" {
|
|
|
|
return errors.New("cannot delete admin in DEMO_MODE")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-06 00:36:39 +00:00
|
|
|
func (u *User) BeforeCreate() error {
|
2020-08-10 03:10:37 +00:00
|
|
|
if err := u.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-06 00:36:39 +00:00
|
|
|
u.Password = utils.HashPassword(u.Password)
|
|
|
|
u.ApiKey = utils.NewSHA256Hash()
|
|
|
|
return nil
|
|
|
|
}
|
2020-08-10 03:10:37 +00:00
|
|
|
|
|
|
|
func (u *User) BeforeUpdate() error {
|
|
|
|
return u.Validate()
|
|
|
|
}
|