diff options
Diffstat (limited to 'internal/db/user.go')
-rw-r--r-- | internal/db/user.go | 161 |
1 files changed, 8 insertions, 153 deletions
diff --git a/internal/db/user.go b/internal/db/user.go index e15c9410..922a340a 100644 --- a/internal/db/user.go +++ b/internal/db/user.go @@ -6,11 +6,9 @@ package db import ( "context" - "encoding/hex" "fmt" _ "image/jpeg" "os" - "path/filepath" "strings" "time" @@ -23,6 +21,7 @@ import ( "gogs.io/gogs/internal/conf" "gogs.io/gogs/internal/db/errors" "gogs.io/gogs/internal/errutil" + "gogs.io/gogs/internal/repoutil" "gogs.io/gogs/internal/tool" "gogs.io/gogs/internal/userutil" ) @@ -58,77 +57,6 @@ func (u *User) getOrganizationCount(e Engine) (int64, error) { return e.Where("uid=?", u.ID).Count(new(OrgUser)) } -func countUsers(e Engine) int64 { - count, _ := e.Where("type=0").Count(new(User)) - return count -} - -// CountUsers returns number of users. -func CountUsers() int64 { - return countUsers(x) -} - -// Users returns number of users in given page. -func ListUsers(page, pageSize int) ([]*User, error) { - users := make([]*User, 0, pageSize) - return users, x.Limit(pageSize, (page-1)*pageSize).Where("type=0").Asc("id").Find(&users) -} - -// parseUserFromCode returns user by username encoded in code. -// It returns nil if code or username is invalid. -func parseUserFromCode(code string) (user *User) { - if len(code) <= tool.TIME_LIMIT_CODE_LENGTH { - return nil - } - - // Use tail hex username to query user - hexStr := code[tool.TIME_LIMIT_CODE_LENGTH:] - if b, err := hex.DecodeString(hexStr); err == nil { - if user, err = GetUserByName(string(b)); user != nil { - return user - } else if !IsErrUserNotExist(err) { - log.Error("Failed to get user by name %q: %v", string(b), err) - } - } - - return nil -} - -// verify active code when active account -func VerifyUserActiveCode(code string) (user *User) { - minutes := conf.Auth.ActivateCodeLives - - 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.Password + user.Rands - - if tool.VerifyTimeLimitCode(data, minutes, prefix) { - return user - } - } - return nil -} - -// verify active code when active account -func VerifyActiveEmailCode(code, email string) *EmailAddress { - minutes := conf.Auth.ActivateCodeLives - - 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.Password + user.Rands - - if tool.VerifyTimeLimitCode(data, minutes, prefix) { - emailAddress := &EmailAddress{Email: email} - if has, _ := x.Get(emailAddress); has { - return emailAddress - } - } - } - return nil -} - // ChangeUserName changes all corresponding setting from old user name to new one. func ChangeUserName(u *User, newUserName string) (err error) { if err = isUsernameAllowed(newUserName); err != nil { @@ -155,8 +83,8 @@ func ChangeUserName(u *User, newUserName string) (err error) { } // Rename or create user base directory - baseDir := UserPath(u.Name) - newBaseDir := UserPath(newUserName) + baseDir := repoutil.UserPath(u.Name) + newBaseDir := repoutil.UserPath(newUserName) if com.IsExist(baseDir) { return os.Rename(baseDir, newBaseDir) } @@ -270,7 +198,7 @@ func deleteUser(e *xorm.Session, u *User) error { &Follow{FollowID: u.ID}, &Action{UserID: u.ID}, &IssueUser{UID: u.ID}, - &EmailAddress{UID: u.ID}, + &EmailAddress{UserID: u.ID}, ); err != nil { return fmt.Errorf("deleteBeans: %v", err) } @@ -303,7 +231,7 @@ func deleteUser(e *xorm.Session, u *User) error { // Note: There are something just cannot be roll back, // so just keep error logs of those operations. - _ = os.RemoveAll(UserPath(u.Name)) + _ = os.RemoveAll(repoutil.UserPath(u.Name)) _ = os.Remove(userutil.CustomAvatarPath(u.ID)) return nil @@ -351,13 +279,6 @@ func DeleteInactivateUsers() (err error) { return err } -// UserPath returns the path absolute path of user repositories. -// -// Deprecated: Use repoutil.UserPath instead. -func UserPath(username string) string { - return filepath.Join(conf.Repository.Root, strings.ToLower(username)) -} - func GetUserByKeyID(keyID int64) (*User, error) { user := new(User) has, err := x.SQL("SELECT a.* FROM `user` AS a, public_key AS b WHERE a.id = b.owner_id AND b.id=?", keyID).Get(user) @@ -380,12 +301,6 @@ func getUserByID(e Engine, id int64) (*User, error) { return u, nil } -// GetUserByID returns the user object by given ID if exists. -// Deprecated: Use Users.GetByID instead. -func GetUserByID(id int64) (*User, error) { - return getUserByID(x, id) -} - // GetAssigneeByID returns the user with read access of repository by given ID. func GetAssigneeByID(repo *Repository, userID int64) (*User, error) { ctx := context.TODO() @@ -400,27 +315,11 @@ func GetAssigneeByID(repo *Repository, userID int64) (*User, error) { return Users.GetByID(ctx, userID) } -// GetUserByName returns a user by given name. -// Deprecated: Use Users.GetByUsername instead. -func GetUserByName(name string) (*User, error) { - if name == "" { - return nil, ErrUserNotExist{args: map[string]interface{}{"name": name}} - } - u := &User{LowerName: strings.ToLower(name)} - has, err := x.Get(u) - if err != nil { - return nil, err - } else if !has { - return nil, ErrUserNotExist{args: map[string]interface{}{"name": name}} - } - return u, nil -} - // GetUserEmailsByNames returns a list of e-mails corresponds to names. func GetUserEmailsByNames(names []string) []string { mails := make([]string, 0, len(names)) for _, name := range names { - u, err := GetUserByName(name) + u, err := Users.GetByUsername(context.TODO(), name) if err != nil { continue } @@ -431,19 +330,6 @@ func GetUserEmailsByNames(names []string) []string { return mails } -// GetUserIDsByNames returns a slice of ids corresponds to names. -func GetUserIDsByNames(names []string) []int64 { - ids := make([]int64, 0, len(names)) - for _, name := range names { - u, err := GetUserByName(name) - if err != nil { - continue - } - ids = append(ids, u.ID) - } - return ids -} - // UserCommit represents a commit with validation of user. type UserCommit struct { User *User @@ -452,7 +338,7 @@ type UserCommit struct { // ValidateCommitWithEmail checks if author's e-mail of commit is corresponding to a user. func ValidateCommitWithEmail(c *git.Commit) *User { - u, err := GetUserByEmail(c.Author.Email) + u, err := Users.GetByEmail(context.TODO(), c.Author.Email) if err != nil { return nil } @@ -466,7 +352,7 @@ func ValidateCommitsWithEmails(oldCommits []*git.Commit) []*UserCommit { for i := range oldCommits { var u *User if v, ok := emails[oldCommits[i].Author.Email]; !ok { - u, _ = GetUserByEmail(oldCommits[i].Author.Email) + u, _ = Users.GetByEmail(context.TODO(), oldCommits[i].Author.Email) emails[oldCommits[i].Author.Email] = u } else { u = v @@ -480,37 +366,6 @@ func ValidateCommitsWithEmails(oldCommits []*git.Commit) []*UserCommit { return newCommits } -// GetUserByEmail returns the user object by given e-mail if exists. -// Deprecated: Use Users.GetByEmail instead. -func GetUserByEmail(email string) (*User, error) { - if email == "" { - return nil, ErrUserNotExist{args: map[string]interface{}{"email": email}} - } - - email = strings.ToLower(email) - // First try to find the user by primary email - user := &User{Email: email} - has, err := x.Get(user) - if err != nil { - return nil, err - } - if has { - return user, nil - } - - // Otherwise, check in alternative list for activated email addresses - emailAddress := &EmailAddress{Email: email, IsActivated: true} - has, err = x.Get(emailAddress) - if err != nil { - return nil, err - } - if has { - return GetUserByID(emailAddress.UID) - } - - return nil, ErrUserNotExist{args: map[string]interface{}{"email": email}} -} - type SearchUserOptions struct { Keyword string Type UserType |