diff options
Diffstat (limited to 'internal/route')
-rw-r--r-- | internal/route/admin/users.go | 25 | ||||
-rw-r--r-- | internal/route/api/v1/admin/user.go | 40 | ||||
-rw-r--r-- | internal/route/install.go | 30 | ||||
-rw-r--r-- | internal/route/user/auth.go | 36 |
4 files changed, 72 insertions, 59 deletions
diff --git a/internal/route/admin/users.go b/internal/route/admin/users.go index ebdaf460..c1891fc4 100644 --- a/internal/route/admin/users.go +++ b/internal/route/admin/users.go @@ -5,6 +5,7 @@ package admin import ( + "strconv" "strings" "github.com/unknwon/com" @@ -77,22 +78,20 @@ func NewUserPost(c *context.Context, f form.AdminCrateUser) { return } - u := &db.User{ - Name: f.UserName, - Email: f.Email, - Password: f.Password, - IsActive: true, + createUserOpts := db.CreateUserOptions{ + Password: f.Password, + Activated: true, } - if len(f.LoginType) > 0 { fields := strings.Split(f.LoginType, "-") if len(fields) == 2 { - u.LoginSource = com.StrTo(fields[1]).MustInt64() - u.LoginName = f.LoginName + createUserOpts.LoginSource, _ = strconv.ParseInt(fields[1], 10, 64) + createUserOpts.LoginName = f.LoginName } } - if err := db.CreateUser(u); err != nil { + user, err := db.Users.Create(c.Req.Context(), f.UserName, f.Email, createUserOpts) + if err != nil { switch { case db.IsErrUserAlreadyExist(err): c.Data["Err_UserName"] = true @@ -108,15 +107,15 @@ func NewUserPost(c *context.Context, f form.AdminCrateUser) { } return } - log.Trace("Account created by admin (%s): %s", c.User.Name, u.Name) + log.Trace("Account %q created by admin %q", user.Name, c.User.Name) // Send email notification. if f.SendNotify && conf.Email.Enabled { - email.SendRegisterNotifyMail(c.Context, db.NewMailerUser(u)) + email.SendRegisterNotifyMail(c.Context, db.NewMailerUser(user)) } - c.Flash.Success(c.Tr("admin.users.new_success", u.Name)) - c.Redirect(conf.Server.Subpath + "/admin/users/" + com.ToStr(u.ID)) + c.Flash.Success(c.Tr("admin.users.new_success", user.Name)) + c.Redirect(conf.Server.Subpath + "/admin/users/" + strconv.FormatInt(user.ID, 10)) } func prepareUserInfo(c *context.Context) *db.User { diff --git a/internal/route/api/v1/admin/user.go b/internal/route/api/v1/admin/user.go index caca77d5..c7c207af 100644 --- a/internal/route/api/v1/admin/user.go +++ b/internal/route/api/v1/admin/user.go @@ -18,12 +18,12 @@ import ( "gogs.io/gogs/internal/userutil" ) -func parseLoginSource(c *context.APIContext, u *db.User, sourceID int64, loginName string) { +func parseLoginSource(c *context.APIContext, sourceID int64) { if sourceID == 0 { return } - source, err := db.LoginSources.GetByID(c.Req.Context(), sourceID) + _, err := db.LoginSources.GetByID(c.Req.Context(), sourceID) if err != nil { if db.IsErrLoginSourceNotExist(err) { c.ErrorStatus(http.StatusUnprocessableEntity, err) @@ -32,26 +32,27 @@ func parseLoginSource(c *context.APIContext, u *db.User, sourceID int64, loginNa } return } - - u.LoginSource = source.ID - u.LoginName = loginName } func CreateUser(c *context.APIContext, form api.CreateUserOption) { - u := &db.User{ - Name: form.Username, - FullName: form.FullName, - Email: form.Email, - Password: form.Password, - IsActive: true, - } - - parseLoginSource(c, u, form.SourceID, form.LoginName) + parseLoginSource(c, form.SourceID) if c.Written() { return } - if err := db.CreateUser(u); err != nil { + user, err := db.Users.Create( + c.Req.Context(), + form.Username, + form.Email, + db.CreateUserOptions{ + FullName: form.FullName, + Password: form.Password, + LoginSource: form.SourceID, + LoginName: form.LoginName, + Activated: true, + }, + ) + if err != nil { if db.IsErrUserAlreadyExist(err) || db.IsErrEmailAlreadyUsed(err) || db.IsErrNameNotAllowed(err) { @@ -61,14 +62,14 @@ func CreateUser(c *context.APIContext, form api.CreateUserOption) { } return } - log.Trace("Account created by admin %q: %s", c.User.Name, u.Name) + log.Trace("Account %q created by admin %q", user.Name, c.User.Name) // Send email notification. if form.SendNotify && conf.Email.Enabled { - email.SendRegisterNotifyMail(c.Context.Context, db.NewMailerUser(u)) + email.SendRegisterNotifyMail(c.Context.Context, db.NewMailerUser(user)) } - c.JSON(http.StatusCreated, u.APIFormat()) + c.JSON(http.StatusCreated, user.APIFormat()) } func EditUser(c *context.APIContext, form api.EditUserOption) { @@ -77,7 +78,7 @@ func EditUser(c *context.APIContext, form api.EditUserOption) { return } - parseLoginSource(c, u, form.SourceID, form.LoginName) + parseLoginSource(c, form.SourceID) if c.Written() { return } @@ -92,6 +93,7 @@ func EditUser(c *context.APIContext, form api.EditUserOption) { u.Password = userutil.EncodePassword(u.Password, u.Salt) } + u.LoginSource = form.SourceID u.LoginName = form.LoginName u.FullName = form.FullName u.Email = form.Email diff --git a/internal/route/install.go b/internal/route/install.go index 2e0e05b9..34a7cffc 100644 --- a/internal/route/install.go +++ b/internal/route/install.go @@ -387,27 +387,35 @@ func InstallPost(c *context.Context, f form.Install) { // Create admin account if len(f.AdminName) > 0 { - u := &db.User{ - Name: f.AdminName, - Email: f.AdminEmail, - Password: f.AdminPasswd, - IsAdmin: true, - IsActive: true, - } - if err := db.CreateUser(u); err != nil { + user, err := db.Users.Create( + c.Req.Context(), + f.AdminName, + f.AdminEmail, + db.CreateUserOptions{ + Password: f.AdminPasswd, + Activated: true, + Admin: true, + }, + ) + if err != nil { if !db.IsErrUserAlreadyExist(err) { conf.Security.InstallLock = false c.FormErr("AdminName", "AdminEmail") c.RenderWithErr(c.Tr("install.invalid_admin_setting", err), INSTALL, &f) return } + log.Info("Admin account already exist") - u, _ = db.GetUserByName(u.Name) + user, err = db.Users.GetByUsername(c.Req.Context(), f.AdminName) + if err != nil { + c.Error(err, "get user by name") + return + } } // Auto-login for admin - _ = c.Session.Set("uid", u.ID) - _ = c.Session.Set("uname", u.Name) + _ = c.Session.Set("uid", user.ID) + _ = c.Session.Set("uname", user.Name) } log.Info("First-time run install finished!") diff --git a/internal/route/user/auth.go b/internal/route/user/auth.go index 74c1a98f..17229a40 100644 --- a/internal/route/user/auth.go +++ b/internal/route/user/auth.go @@ -6,6 +6,7 @@ package user import ( "fmt" + "net/http" "net/url" "github.com/go-macaron/captcha" @@ -311,7 +312,7 @@ func SignUpPost(c *context.Context, cpt *captcha.Captcha, f form.Register) { c.Data["EnableCaptcha"] = conf.Auth.EnableRegistrationCaptcha if conf.Auth.DisableRegistration { - c.Status(403) + c.Status(http.StatusForbidden) return } @@ -332,13 +333,16 @@ func SignUpPost(c *context.Context, cpt *captcha.Captcha, f form.Register) { return } - u := &db.User{ - Name: f.UserName, - Email: f.Email, - Password: f.Password, - IsActive: !conf.Auth.RequireEmailConfirmation, - } - if err := db.CreateUser(u); err != nil { + user, err := db.Users.Create( + c.Req.Context(), + f.UserName, + f.Email, + db.CreateUserOptions{ + Password: f.Password, + Activated: !conf.Auth.RequireEmailConfirmation, + }, + ) + if err != nil { switch { case db.IsErrUserAlreadyExist(err): c.FormErr("UserName") @@ -354,27 +358,27 @@ func SignUpPost(c *context.Context, cpt *captcha.Captcha, f form.Register) { } return } - log.Trace("Account created: %s", u.Name) + log.Trace("Account created: %s", user.Name) // Auto-set admin for the only user. if db.CountUsers() == 1 { - u.IsAdmin = true - u.IsActive = true - if err := db.UpdateUser(u); err != nil { + user.IsAdmin = true + user.IsActive = true + if err := db.UpdateUser(user); err != nil { c.Error(err, "update user") return } } // Send confirmation email. - if conf.Auth.RequireEmailConfirmation && u.ID > 1 { - email.SendActivateAccountMail(c.Context, db.NewMailerUser(u)) + if conf.Auth.RequireEmailConfirmation && user.ID > 1 { + email.SendActivateAccountMail(c.Context, db.NewMailerUser(user)) c.Data["IsSendRegisterMail"] = true - c.Data["Email"] = u.Email + c.Data["Email"] = user.Email c.Data["Hours"] = conf.Auth.ActivateCodeLives / 60 c.Success(ACTIVATE) - if err := c.Cache.Put(userutil.MailResendCacheKey(u.ID), 1, 180); err != nil { + if err := c.Cache.Put(userutil.MailResendCacheKey(user.ID), 1, 180); err != nil { log.Error("Failed to put cache key 'mail resend': %v", err) } return |