From 7cbd84d5b3d4af36e4afcf7af9374bc765f6bb9c Mon Sep 17 00:00:00 2001
From: Joe Chen <jc@unknwon.io>
Date: Sat, 22 Oct 2022 14:56:25 +0800
Subject: [PATCH] refactor(db): rename `User.Passwd` to `User.Password` (#7196)

---
 internal/cmd/admin.go               |  2 +-
 internal/context/auth.go            |  2 +-
 internal/db/issue_mail.go           |  2 +-
 internal/db/user.go                 | 12 ++++++------
 internal/db/users.go                |  4 ++--
 internal/route/admin/users.go       |  4 ++--
 internal/route/api/v1/admin/user.go |  4 ++--
 internal/route/install.go           |  2 +-
 internal/route/user/auth.go         |  8 ++++----
 internal/route/user/setting.go      |  2 +-
 10 files changed, 21 insertions(+), 21 deletions(-)

diff --git a/internal/cmd/admin.go b/internal/cmd/admin.go
index 5b2dd290f..798202337 100644
--- a/internal/cmd/admin.go
+++ b/internal/cmd/admin.go
@@ -154,7 +154,7 @@ func runCreateUser(c *cli.Context) error {
 	if err := db.CreateUser(&db.User{
 		Name:     c.String("name"),
 		Email:    c.String("email"),
-		Passwd:   c.String("password"),
+		Password: c.String("password"),
 		IsActive: true,
 		IsAdmin:  c.Bool("admin"),
 	}); err != nil {
diff --git a/internal/context/auth.go b/internal/context/auth.go
index cba687069..9cb378631 100644
--- a/internal/context/auth.go
+++ b/internal/context/auth.go
@@ -185,7 +185,7 @@ func authenticatedUser(ctx *macaron.Context, sess session.Store) (_ *db.User, is
 						u := &db.User{
 							Name:     webAuthUser,
 							Email:    gouuid.NewV4().String() + "@localhost",
-							Passwd:   webAuthUser,
+							Password: webAuthUser,
 							IsActive: true,
 						}
 						if err = db.CreateUser(u); err != nil {
diff --git a/internal/db/issue_mail.go b/internal/db/issue_mail.go
index 91d89a415..e1dbb6714 100644
--- a/internal/db/issue_mail.go
+++ b/internal/db/issue_mail.go
@@ -42,7 +42,7 @@ func (this mailerUser) GenerateEmailActivateCode(email string) string {
 		this.user.ID,
 		email,
 		this.user.Name,
-		this.user.Passwd,
+		this.user.Password,
 		this.user.Rands,
 	)
 }
diff --git a/internal/db/user.go b/internal/db/user.go
index 33b69af16..11e4c4a6a 100644
--- a/internal/db/user.go
+++ b/internal/db/user.go
@@ -168,15 +168,15 @@ func (u *User) NewGitSig() *git.Signature {
 
 // EncodePassword encodes password to safe format.
 func (u *User) EncodePassword() {
-	newPasswd := pbkdf2.Key([]byte(u.Passwd), []byte(u.Salt), 10000, 50, sha256.New)
-	u.Passwd = fmt.Sprintf("%x", newPasswd)
+	newPasswd := pbkdf2.Key([]byte(u.Password), []byte(u.Salt), 10000, 50, sha256.New)
+	u.Password = fmt.Sprintf("%x", newPasswd)
 }
 
 // ValidatePassword checks if given password matches the one belongs to the user.
 func (u *User) ValidatePassword(passwd string) bool {
-	newUser := &User{Passwd: passwd, Salt: u.Salt}
+	newUser := &User{Password: passwd, Salt: u.Salt}
 	newUser.EncodePassword()
-	return subtle.ConstantTimeCompare([]byte(u.Passwd), []byte(newUser.Passwd)) == 1
+	return subtle.ConstantTimeCompare([]byte(u.Password), []byte(newUser.Password)) == 1
 }
 
 // UploadAvatar saves custom avatar for user.
@@ -499,7 +499,7 @@ func VerifyUserActiveCode(code string) (user *User) {
 	if user = parseUserFromCode(code); user != nil {
 		// time limit code
 		prefix := code[:tool.TIME_LIMIT_CODE_LENGTH]
-		data := com.ToStr(user.ID) + user.Email + user.LowerName + user.Passwd + user.Rands
+		data := com.ToStr(user.ID) + user.Email + user.LowerName + user.Password + user.Rands
 
 		if tool.VerifyTimeLimitCode(data, minutes, prefix) {
 			return user
@@ -515,7 +515,7 @@ func VerifyActiveEmailCode(code, email string) *EmailAddress {
 	if user := parseUserFromCode(code); user != nil {
 		// time limit code
 		prefix := code[:tool.TIME_LIMIT_CODE_LENGTH]
-		data := com.ToStr(user.ID) + email + user.LowerName + user.Passwd + user.Rands
+		data := com.ToStr(user.ID) + email + user.LowerName + user.Password + user.Rands
 
 		if tool.VerifyTimeLimitCode(data, minutes, prefix) {
 			emailAddress := &EmailAddress{Email: email}
diff --git a/internal/db/users.go b/internal/db/users.go
index 94bc25f5f..1ae500644 100644
--- a/internal/db/users.go
+++ b/internal/db/users.go
@@ -230,7 +230,7 @@ func (db *users) Create(ctx context.Context, username, email string, opts Create
 		Name:            username,
 		FullName:        opts.FullName,
 		Email:           email,
-		Passwd:          opts.Password,
+		Password:        opts.Password,
 		LoginSource:     opts.LoginSource,
 		LoginName:       opts.LoginName,
 		Location:        opts.Location,
@@ -355,7 +355,7 @@ type User struct {
 	FullName  string
 	// Email is the primary email address (to be used for communication)
 	Email       string `xorm:"NOT NULL" gorm:"not null"`
-	Passwd      string `xorm:"NOT NULL" gorm:"not null"`
+	Password    string `xorm:"passwd NOT NULL" gorm:"column:passwd;not null"`
 	LoginSource int64  `xorm:"NOT NULL DEFAULT 0" gorm:"not null;default:0"`
 	LoginName   string
 	Type        UserType
diff --git a/internal/route/admin/users.go b/internal/route/admin/users.go
index ef890e7db..9b273b36e 100644
--- a/internal/route/admin/users.go
+++ b/internal/route/admin/users.go
@@ -79,7 +79,7 @@ func NewUserPost(c *context.Context, f form.AdminCrateUser) {
 	u := &db.User{
 		Name:     f.UserName,
 		Email:    f.Email,
-		Passwd:   f.Password,
+		Password: f.Password,
 		IsActive: true,
 	}
 
@@ -186,7 +186,7 @@ func EditUserPost(c *context.Context, f form.AdminEditUser) {
 	}
 
 	if len(f.Password) > 0 {
-		u.Passwd = f.Password
+		u.Password = f.Password
 		var err error
 		if u.Salt, err = db.GetUserSalt(); err != nil {
 			c.Error(err, "get user salt")
diff --git a/internal/route/api/v1/admin/user.go b/internal/route/api/v1/admin/user.go
index 543220dbd..7fad5ed14 100644
--- a/internal/route/api/v1/admin/user.go
+++ b/internal/route/api/v1/admin/user.go
@@ -41,7 +41,7 @@ func CreateUser(c *context.APIContext, form api.CreateUserOption) {
 		Name:     form.Username,
 		FullName: form.FullName,
 		Email:    form.Email,
-		Passwd:   form.Password,
+		Password: form.Password,
 		IsActive: true,
 	}
 
@@ -82,7 +82,7 @@ func EditUser(c *context.APIContext, form api.EditUserOption) {
 	}
 
 	if len(form.Password) > 0 {
-		u.Passwd = form.Password
+		u.Password = form.Password
 		var err error
 		if u.Salt, err = db.GetUserSalt(); err != nil {
 			c.Error(err, "get user salt")
diff --git a/internal/route/install.go b/internal/route/install.go
index 4a5296558..2e0e05b90 100644
--- a/internal/route/install.go
+++ b/internal/route/install.go
@@ -390,7 +390,7 @@ func InstallPost(c *context.Context, f form.Install) {
 		u := &db.User{
 			Name:     f.AdminName,
 			Email:    f.AdminEmail,
-			Passwd:   f.AdminPasswd,
+			Password: f.AdminPasswd,
 			IsAdmin:  true,
 			IsActive: true,
 		}
diff --git a/internal/route/user/auth.go b/internal/route/user/auth.go
index 38751cb6c..213ff050d 100644
--- a/internal/route/user/auth.go
+++ b/internal/route/user/auth.go
@@ -60,7 +60,7 @@ func AutoLogin(c *context.Context) (bool, error) {
 		return false, nil
 	}
 
-	if val, ok := c.GetSuperSecureCookie(u.Rands+u.Passwd, conf.Security.CookieRememberName); !ok || val != u.Name {
+	if val, ok := c.GetSuperSecureCookie(u.Rands+u.Password, conf.Security.CookieRememberName); !ok || val != u.Name {
 		return false, nil
 	}
 
@@ -122,7 +122,7 @@ func afterLogin(c *context.Context, u *db.User, remember bool) {
 	if remember {
 		days := 86400 * conf.Security.LoginRememberDays
 		c.SetCookie(conf.Security.CookieUsername, u.Name, days, conf.Server.Subpath, "", conf.Security.CookieSecure, true)
-		c.SetSuperSecureCookie(u.Rands+u.Passwd, conf.Security.CookieRememberName, u.Name, days, conf.Server.Subpath, "", conf.Security.CookieSecure, true)
+		c.SetSuperSecureCookie(u.Rands+u.Password, conf.Security.CookieRememberName, u.Name, days, conf.Server.Subpath, "", conf.Security.CookieSecure, true)
 	}
 
 	_ = c.Session.Set("uid", u.ID)
@@ -334,7 +334,7 @@ func SignUpPost(c *context.Context, cpt *captcha.Captcha, f form.Register) {
 	u := &db.User{
 		Name:     f.UserName,
 		Email:    f.Email,
-		Passwd:   f.Password,
+		Password: f.Password,
 		IsActive: !conf.Auth.RequireEmailConfirmation,
 	}
 	if err := db.CreateUser(u); err != nil {
@@ -544,7 +544,7 @@ func ResetPasswdPost(c *context.Context) {
 			return
 		}
 
-		u.Passwd = passwd
+		u.Password = passwd
 		var err error
 		if u.Rands, err = db.GetUserSalt(); err != nil {
 			c.Error(err, "get user salt")
diff --git a/internal/route/user/setting.go b/internal/route/user/setting.go
index 66e3ace29..8c0e87c12 100644
--- a/internal/route/user/setting.go
+++ b/internal/route/user/setting.go
@@ -202,7 +202,7 @@ func SettingsPasswordPost(c *context.Context, f form.ChangePassword) {
 	} else if f.Password != f.Retype {
 		c.Flash.Error(c.Tr("form.password_not_match"))
 	} else {
-		c.User.Passwd = f.Password
+		c.User.Password = f.Password
 		var err error
 		if c.User.Salt, err = db.GetUserSalt(); err != nil {
 			c.Errorf(err, "get user salt")