From: Ethan Koenig Date: Tue, 3 Oct 2017 06:29:26 +0000 (-0700) Subject: Use session when creating user (#2638) X-Git-Tag: v1.3.0-rc1~144 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=4b2c8ca533ee06bac72fed040a2e2412cc122954;p=gitea.git Use session when creating user (#2638) --- diff --git a/models/user.go b/models/user.go index bda80bfdf3..698d0207ec 100644 --- a/models/user.go +++ b/models/user.go @@ -591,19 +591,23 @@ func (u *User) IsMailable() bool { return u.IsActive } -// IsUserExist checks if given user name exist, -// the user name should be noncased unique. -// If uid is presented, then check will rule out that one, -// it is used when update a user name in settings page. -func IsUserExist(uid int64, name string) (bool, error) { +func isUserExist(e Engine, uid int64, name string) (bool, error) { if len(name) == 0 { return false, nil } - return x. + return e. Where("id!=?", uid). Get(&User{LowerName: strings.ToLower(name)}) } +// IsUserExist checks if given user name exist, +// the user name should be noncased unique. +// If uid is presented, then check will rule out that one, +// it is used when update a user name in settings page. +func IsUserExist(uid int64, name string) (bool, error) { + return isUserExist(x, uid, name) +} + // GetUserSalt returns a random user salt token. func GetUserSalt() (string, error) { return base.GetRandomString(10) @@ -659,7 +663,13 @@ func CreateUser(u *User) (err error) { return err } - isExist, err := IsUserExist(0, u.Name) + sess := x.NewSession() + defer sess.Close() + if err = sess.Begin(); err != nil { + return err + } + + isExist, err := isUserExist(sess, 0, u.Name) if err != nil { return err } else if isExist { @@ -667,16 +677,16 @@ func CreateUser(u *User) (err error) { } u.Email = strings.ToLower(u.Email) - has, err := x. + isExist, err = sess. Where("email=?", u.Email). Get(new(User)) if err != nil { return err - } else if has { + } else if isExist { return ErrEmailAlreadyUsed{u.Email} } - isExist, err = IsEmailUsed(u.Email) + isExist, err = isEmailUsed(sess, u.Email) if err != nil { return err } else if isExist { @@ -698,12 +708,6 @@ func CreateUser(u *User) (err error) { u.AllowCreateOrganization = setting.Service.DefaultAllowCreateOrganization u.MaxRepoCreation = -1 - sess := x.NewSession() - defer sess.Close() - if err = sess.Begin(); err != nil { - return err - } - if _, err = sess.Insert(u); err != nil { return err } else if err = os.MkdirAll(UserPath(u.Name), os.ModePerm); err != nil {