aboutsummaryrefslogtreecommitdiff
path: root/models/user.go
diff options
context:
space:
mode:
Diffstat (limited to 'models/user.go')
-rw-r--r--models/user.go191
1 files changed, 137 insertions, 54 deletions
diff --git a/models/user.go b/models/user.go
index b98e81ba..96881ea3 100644
--- a/models/user.go
+++ b/models/user.go
@@ -14,9 +14,10 @@ import (
"strings"
"time"
- "github.com/gogits/git"
+ "github.com/Unknwon/com"
"github.com/gogits/gogs/modules/base"
+ "github.com/gogits/gogs/modules/git"
"github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/setting"
)
@@ -44,30 +45,31 @@ var (
// User represents the object of individual and member of organization.
type User struct {
Id int64
- LowerName string `xorm:"unique not null"`
- Name string `xorm:"unique not null"`
+ LowerName string `xorm:"UNIQUE NOT NULL"`
+ Name string `xorm:"UNIQUE NOT NULL"`
FullName string
- Email string `xorm:"unique not null"`
- Passwd string `xorm:"not null"`
+ Email string `xorm:"UNIQUE NOT NULL"`
+ Passwd string `xorm:"NOT NULL"`
LoginType LoginType
- LoginSource int64 `xorm:"not null default 0"`
+ LoginSource int64 `xorm:"NOT NULL DEFAULT 0"`
LoginName string
Type UserType
- Orgs []*User `xorm:"-"`
+ Orgs []*User `xorm:"-"`
+ Repos []*Repository `xorm:"-"`
NumFollowers int
NumFollowings int
NumStars int
NumRepos int
- Avatar string `xorm:"varchar(2048) not null"`
- AvatarEmail string `xorm:"not null"`
+ Avatar string `xorm:"VARCHAR(2048) NOT NULL"`
+ AvatarEmail string `xorm:"NOT NULL"`
Location string
Website string
IsActive bool
IsAdmin bool
Rands string `xorm:"VARCHAR(10)"`
Salt string `xorm:"VARCHAR(10)"`
- Created time.Time `xorm:"created"`
- Updated time.Time `xorm:"updated"`
+ Created time.Time `xorm:"CREATED"`
+ Updated time.Time `xorm:"UPDATED"`
// For organization.
Description string
@@ -77,6 +79,14 @@ type User struct {
Members []*User `xorm:"-"`
}
+// DashboardLink returns the user dashboard page link.
+func (u *User) DashboardLink() string {
+ if u.IsOrganization() {
+ return "/org/" + u.Name + "/dashboard/"
+ }
+ return "/"
+}
+
// HomeLink returns the user home page link.
func (u *User) HomeLink() string {
return "/user/" + u.Name
@@ -107,16 +117,39 @@ func (u *User) EncodePasswd() {
u.Passwd = fmt.Sprintf("%x", newPasswd)
}
+// ValidtePassword checks if given password matches the one belongs to the user.
+func (u *User) ValidtePassword(passwd string) bool {
+ newUser := &User{Passwd: passwd, Salt: u.Salt}
+ newUser.EncodePasswd()
+ return u.Passwd == newUser.Passwd
+}
+
// IsOrganization returns true if user is actually a organization.
func (u *User) IsOrganization() bool {
return u.Type == ORGANIZATION
}
+// IsUserOrgOwner returns true if user is in the owner team of given organization.
+func (u *User) IsUserOrgOwner(orgId int64) bool {
+ return IsOrganizationOwner(orgId, u.Id)
+}
+
+// IsPublicMember returns true if user public his/her membership in give organization.
+func (u *User) IsPublicMember(orgId int64) bool {
+ return IsPublicMembership(orgId, u.Id)
+}
+
// GetOrganizationCount returns count of membership of organization of user.
func (u *User) GetOrganizationCount() (int64, error) {
return x.Where("uid=?", u.Id).Count(new(OrgUser))
}
+// GetRepositories returns all repositories that user owns, including private repositories.
+func (u *User) GetRepositories() (err error) {
+ u.Repos, err = GetRepositories(u.Id, true)
+ return err
+}
+
// GetOrganizations returns all organizations that user belongs to.
func (u *User) GetOrganizations() error {
ous, err := GetOrgUsersByUserId(u.Id)
@@ -157,23 +190,23 @@ func GetUserSalt() string {
}
// CreateUser creates record of a new user.
-func CreateUser(u *User) (*User, error) {
+func CreateUser(u *User) error {
if !IsLegalName(u.Name) {
- return nil, ErrUserNameIllegal
+ return ErrUserNameIllegal
}
isExist, err := IsUserExist(u.Name)
if err != nil {
- return nil, err
+ return err
} else if isExist {
- return nil, ErrUserAlreadyExist
+ return ErrUserAlreadyExist
}
isExist, err = IsEmailUsed(u.Email)
if err != nil {
- return nil, err
+ return err
} else if isExist {
- return nil, ErrEmailAlreadyUsed
+ return ErrEmailAlreadyUsed
}
u.LowerName = strings.ToLower(u.Name)
@@ -186,21 +219,17 @@ func CreateUser(u *User) (*User, error) {
sess := x.NewSession()
defer sess.Close()
if err = sess.Begin(); err != nil {
- return nil, err
+ return err
}
if _, err = sess.Insert(u); err != nil {
sess.Rollback()
- return nil, err
- }
-
- if err = os.MkdirAll(UserPath(u.Name), os.ModePerm); err != nil {
+ return err
+ } else if err = os.MkdirAll(UserPath(u.Name), os.ModePerm); err != nil {
sess.Rollback()
- return nil, err
- }
-
- if err = sess.Commit(); err != nil {
- return nil, err
+ return err
+ } else if err = sess.Commit(); err != nil {
+ return err
}
// Auto-set admin for user whose ID is 1.
@@ -209,12 +238,18 @@ func CreateUser(u *User) (*User, error) {
u.IsActive = true
_, err = x.Id(u.Id).UseBool().Update(u)
}
- return u, err
+ return err
+}
+
+// CountUsers returns number of users.
+func CountUsers() int64 {
+ count, _ := x.Where("type=0").Count(new(User))
+ return count
}
// GetUsers returns given number of user objects with offset.
-func GetUsers(num, offset int) ([]User, error) {
- users := make([]User, 0, num)
+func GetUsers(num, offset int) ([]*User, error) {
+ users := make([]*User, 0, num)
err := x.Limit(num, offset).Where("type=0").Asc("id").Find(&users)
return users, err
}
@@ -231,7 +266,7 @@ func getVerifyUser(code string) (user *User) {
if user, err = GetUserByName(string(b)); user != nil {
return user
}
- log.Error("user.getVerifyUser: %v", err)
+ log.Error(4, "user.getVerifyUser: %v", err)
}
return nil
@@ -244,7 +279,7 @@ func VerifyUserActiveCode(code string) (user *User) {
if user = getVerifyUser(code); user != nil {
// time limit code
prefix := code[:base.TimeLimitCodeLength]
- data := base.ToStr(user.Id) + user.Email + user.LowerName + user.Passwd + user.Rands
+ data := com.ToStr(user.Id) + user.Email + user.LowerName + user.Passwd + user.Rands
if base.VerifyTimeLimitCode(data, minutes, prefix) {
return user
@@ -254,12 +289,16 @@ func VerifyUserActiveCode(code string) (user *User) {
}
// ChangeUserName changes all corresponding setting from old user name to new one.
-func ChangeUserName(user *User, newUserName string) (err error) {
+func ChangeUserName(u *User, newUserName string) (err error) {
+ if !IsLegalName(newUserName) {
+ return ErrUserNameIllegal
+ }
+
newUserName = strings.ToLower(newUserName)
// Update accesses of user.
accesses := make([]Access, 0, 10)
- if err = x.Find(&accesses, &Access{UserName: user.LowerName}); err != nil {
+ if err = x.Find(&accesses, &Access{UserName: u.LowerName}); err != nil {
return err
}
@@ -271,36 +310,38 @@ func ChangeUserName(user *User, newUserName string) (err error) {
for i := range accesses {
accesses[i].UserName = newUserName
- if strings.HasPrefix(accesses[i].RepoName, user.LowerName+"/") {
- accesses[i].RepoName = strings.Replace(accesses[i].RepoName, user.LowerName, newUserName, 1)
+ if strings.HasPrefix(accesses[i].RepoName, u.LowerName+"/") {
+ accesses[i].RepoName = strings.Replace(accesses[i].RepoName, u.LowerName, newUserName, 1)
}
if err = UpdateAccessWithSession(sess, &accesses[i]); err != nil {
return err
}
}
- repos, err := GetRepositories(user.Id, true)
+ repos, err := GetRepositories(u.Id, true)
if err != nil {
return err
}
for i := range repos {
accesses = make([]Access, 0, 10)
// Update accesses of user repository.
- if err = x.Find(&accesses, &Access{RepoName: user.LowerName + "/" + repos[i].LowerName}); err != nil {
+ if err = x.Find(&accesses, &Access{RepoName: u.LowerName + "/" + repos[i].LowerName}); err != nil {
return err
}
for j := range accesses {
- accesses[j].UserName = newUserName
- accesses[j].RepoName = newUserName + "/" + repos[i].LowerName
- if err = UpdateAccessWithSession(sess, &accesses[j]); err != nil {
- return err
+ // if the access is not the user's access (already updated above)
+ if accesses[j].UserName != u.LowerName {
+ accesses[j].RepoName = newUserName + "/" + repos[i].LowerName
+ if err = UpdateAccessWithSession(sess, &accesses[j]); err != nil {
+ return err
+ }
}
}
}
// Change user directory name.
- if err = os.Rename(UserPath(user.LowerName), UserPath(newUserName)); err != nil {
+ if err = os.Rename(UserPath(u.LowerName), UserPath(newUserName)); err != nil {
sess.Rollback()
return err
}
@@ -309,7 +350,7 @@ func ChangeUserName(user *User, newUserName string) (err error) {
}
// UpdateUser updates user's information.
-func UpdateUser(u *User) (err error) {
+func UpdateUser(u *User) error {
u.LowerName = strings.ToLower(u.Name)
if len(u.Location) > 255 {
@@ -322,7 +363,7 @@ func UpdateUser(u *User) (err error) {
u.Description = u.Description[:255]
}
- _, err = x.Id(u.Id).AllCols().Update(u)
+ _, err := x.Id(u.Id).AllCols().Update(u)
return err
}
@@ -332,7 +373,7 @@ func DeleteUser(u *User) error {
// Check ownership of repository.
count, err := GetRepositoryCount(u)
if err != nil {
- return errors.New("modesl.GetRepositories(GetRepositoryCount): " + err.Error())
+ return errors.New("GetRepositoryCount: " + err.Error())
} else if count > 0 {
return ErrUserOwnRepos
}
@@ -480,21 +521,21 @@ func GetUserByEmail(email string) (*User, error) {
}
// SearchUserByName returns given number of users whose name contains keyword.
-func SearchUserByName(key string, limit int) (us []*User, err error) {
+func SearchUserByName(opt SearchOption) (us []*User, err error) {
// Prevent SQL inject.
- key = strings.TrimSpace(key)
- if len(key) == 0 {
+ opt.Keyword = strings.TrimSpace(opt.Keyword)
+ if len(opt.Keyword) == 0 {
return us, nil
}
- key = strings.Split(key, " ")[0]
- if len(key) == 0 {
+ opt.Keyword = strings.Split(opt.Keyword, " ")[0]
+ if len(opt.Keyword) == 0 {
return us, nil
}
- key = strings.ToLower(key)
+ opt.Keyword = strings.ToLower(opt.Keyword)
- us = make([]*User, 0, limit)
- err = x.Limit(limit).Where("lower_name like '%" + key + "%'").Find(&us)
+ us = make([]*User, 0, opt.Limit)
+ err = x.Limit(opt.Limit).Where("type=0").And("lower_name like '%" + opt.Keyword + "%'").Find(&us)
return us, err
}
@@ -554,3 +595,45 @@ func UnFollowUser(userId int64, unFollowId int64) (err error) {
}
return session.Commit()
}
+
+func UpdateMentions(userNames []string, issueId int64) error {
+ users := make([]*User, 0, len(userNames))
+
+ if err := x.Where("name IN (?)", strings.Join(userNames, "\",\"")).OrderBy("name ASC").Find(&users); err != nil {
+ return err
+ }
+
+ ids := make([]int64, 0, len(userNames))
+
+ for _, user := range users {
+ ids = append(ids, user.Id)
+
+ if user.Type == INDIVIDUAL {
+ continue
+ }
+
+ if user.NumMembers == 0 {
+ continue
+ }
+
+ tempIds := make([]int64, 0, user.NumMembers)
+
+ orgUsers, err := GetOrgUsersByOrgId(user.Id)
+
+ if err != nil {
+ return err
+ }
+
+ for _, orgUser := range orgUsers {
+ tempIds = append(tempIds, orgUser.Id)
+ }
+
+ ids = append(ids, tempIds...)
+ }
+
+ if err := UpdateIssueUserPairsByMentions(ids, issueId); err != nil {
+ return err
+ }
+
+ return nil
+}