-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
auth_user_add.go
42 lines (31 loc) Β· 1023 Bytes
/
auth_user_add.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package entity
import (
"fmt"
"github.com/jinzhu/gorm"
"github.com/photoprism/photoprism/internal/form"
"github.com/photoprism/photoprism/pkg/clean"
"github.com/photoprism/photoprism/pkg/txt"
)
// AddUser creates a new user record and sets the password in a single transaction.
func AddUser(frm form.User) error {
user := NewUser().SetFormValues(frm)
if len([]rune(frm.Password)) < PasswordLength {
return fmt.Errorf("password must have at least %d characters", PasswordLength)
} else if len(frm.Password) > txt.ClipPassword {
return fmt.Errorf("password must have less than %d characters", txt.ClipPassword)
}
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, false)
if err := tx.Create(&pw).Error; err != nil {
return err
}
log.Infof("successfully added user %s", clean.LogQuote(user.Username()))
return nil
})
}