2022-09-28 09:01:17 +02:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2023-06-19 17:24:02 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2022-09-28 09:01:17 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// AddUser creates a new user record and sets the password in a single transaction.
|
|
|
|
func AddUser(frm form.User) error {
|
|
|
|
user := NewUser().SetFormValues(frm)
|
|
|
|
|
2023-06-19 17:24:02 +02:00
|
|
|
if len([]rune(frm.Password)) < PasswordLength {
|
2022-11-22 22:14:34 +01:00
|
|
|
return fmt.Errorf("password must have at least %d characters", PasswordLength)
|
2023-06-19 17:24:02 +02:00
|
|
|
} else if len(frm.Password) > txt.ClipPassword {
|
|
|
|
return fmt.Errorf("password must have less than %d characters", txt.ClipPassword)
|
2022-09-28 09:01:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := user.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return Db().Transaction(func(tx *gorm.DB) error {
|
|
|
|
if err := tx.Create(user).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-13 19:46:00 +02:00
|
|
|
pw := NewPassword(user.UserUID, frm.Password, false)
|
2022-09-28 09:01:17 +02:00
|
|
|
|
|
|
|
if err := tx.Create(&pw).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-08 23:30:39 +01:00
|
|
|
log.Infof("successfully added user %s", clean.LogQuote(user.Username()))
|
2022-09-28 09:01:17 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|