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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// AddUser creates a new user record and sets the password in a single transaction.
|
|
|
|
func AddUser(frm form.User) error {
|
|
|
|
user := NewUser().SetFormValues(frm)
|
|
|
|
|
2022-11-22 22:14:34 +01:00
|
|
|
if len(frm.Password) < PasswordLength {
|
|
|
|
return fmt.Errorf("password must have at least %d characters", PasswordLength)
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
pw := NewPassword(user.UserUID, frm.Password)
|
|
|
|
|
|
|
|
if err := tx.Create(&pw).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("successfully added user %s", clean.LogQuote(user.Name()))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|