summaryrefslogtreecommitdiffstats
path: root/models/user.go
diff options
context:
space:
mode:
authorEthan Koenig <ethantkoenig@gmail.com>2017-10-02 23:29:26 -0700
committerLauris BH <lauris@nix.lv>2017-10-03 09:29:26 +0300
commit4b2c8ca533ee06bac72fed040a2e2412cc122954 (patch)
tree73de5bced2fac1dfc3264065b0f2147b98a16dcf /models/user.go
parentb3cfa5a77ecca54f873d9c477f052770206b3f55 (diff)
downloadgitea-4b2c8ca533ee06bac72fed040a2e2412cc122954.tar.gz
gitea-4b2c8ca533ee06bac72fed040a2e2412cc122954.zip
Use session when creating user (#2638)
Diffstat (limited to 'models/user.go')
-rw-r--r--models/user.go36
1 files changed, 20 insertions, 16 deletions
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 {