Use session when creating user (#2638)

tokarchuk/v1.17
Ethan Koenig 7 years ago committed by Lauris BH
parent b3cfa5a77e
commit 4b2c8ca533
  1. 36
      models/user.go

@ -591,19 +591,23 @@ func (u *User) IsMailable() bool {
return u.IsActive return u.IsActive
} }
// IsUserExist checks if given user name exist, func isUserExist(e Engine, uid int64, name string) (bool, error) {
// 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) {
if len(name) == 0 { if len(name) == 0 {
return false, nil return false, nil
} }
return x. return e.
Where("id!=?", uid). Where("id!=?", uid).
Get(&User{LowerName: strings.ToLower(name)}) 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. // GetUserSalt returns a random user salt token.
func GetUserSalt() (string, error) { func GetUserSalt() (string, error) {
return base.GetRandomString(10) return base.GetRandomString(10)
@ -659,7 +663,13 @@ func CreateUser(u *User) (err error) {
return err 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 { if err != nil {
return err return err
} else if isExist { } else if isExist {
@ -667,16 +677,16 @@ func CreateUser(u *User) (err error) {
} }
u.Email = strings.ToLower(u.Email) u.Email = strings.ToLower(u.Email)
has, err := x. isExist, err = sess.
Where("email=?", u.Email). Where("email=?", u.Email).
Get(new(User)) Get(new(User))
if err != nil { if err != nil {
return err return err
} else if has { } else if isExist {
return ErrEmailAlreadyUsed{u.Email} return ErrEmailAlreadyUsed{u.Email}
} }
isExist, err = IsEmailUsed(u.Email) isExist, err = isEmailUsed(sess, u.Email)
if err != nil { if err != nil {
return err return err
} else if isExist { } else if isExist {
@ -698,12 +708,6 @@ func CreateUser(u *User) (err error) {
u.AllowCreateOrganization = setting.Service.DefaultAllowCreateOrganization u.AllowCreateOrganization = setting.Service.DefaultAllowCreateOrganization
u.MaxRepoCreation = -1 u.MaxRepoCreation = -1
sess := x.NewSession()
defer sess.Close()
if err = sess.Begin(); err != nil {
return err
}
if _, err = sess.Insert(u); err != nil { if _, err = sess.Insert(u); err != nil {
return err return err
} else if err = os.MkdirAll(UserPath(u.Name), os.ModePerm); err != nil { } else if err = os.MkdirAll(UserPath(u.Name), os.ModePerm); err != nil {

Loading…
Cancel
Save