diff options
author | Unknwon <u@gogs.io> | 2015-09-06 10:08:14 -0400 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2015-09-06 10:08:14 -0400 |
commit | 80701d45bbe333f1bdef4673fae43df131b2222f (patch) | |
tree | 5aea48f87ac43997e3d9b60dacc2eb05877a408d /models | |
parent | aa67de910af6246a6dc9ad96c56f789e282e8403 (diff) | |
download | gitea-80701d45bbe333f1bdef4673fae43df131b2222f.tar.gz gitea-80701d45bbe333f1bdef4673fae43df131b2222f.zip |
#1193 Make organization emails non-mandatory
Diffstat (limited to 'models')
-rw-r--r-- | models/org.go | 51 | ||||
-rw-r--r-- | models/user.go | 96 |
2 files changed, 68 insertions, 79 deletions
diff --git a/models/org.go b/models/org.go index ac458c78d5..4076bf15e5 100644 --- a/models/org.go +++ b/models/org.go @@ -9,8 +9,6 @@ import ( "fmt" "os" "strings" - - "github.com/gogits/gogs/modules/base" ) var ( @@ -93,17 +91,6 @@ func (org *User) RemoveOrgRepo(repoID int64) error { return org.removeOrgRepo(x, repoID) } -// IsOrgEmailUsed returns true if the e-mail has been used in organization account. -func IsOrgEmailUsed(email string) (bool, error) { - if len(email) == 0 { - return false, nil - } - return x.Get(&User{ - Email: email, - Type: ORGANIZATION, - }) -} - // CreateOrganization creates record of a new organization. func CreateOrganization(org, owner *User) (err error) { if err = IsUsableName(org.Name); err != nil { @@ -117,18 +104,9 @@ func CreateOrganization(org, owner *User) (err error) { return ErrUserAlreadyExist{org.Name} } - isExist, err = IsOrgEmailUsed(org.Email) - if err != nil { - return err - } else if isExist { - return ErrEmailAlreadyUsed{org.Email} - } - org.LowerName = strings.ToLower(org.Name) org.FullName = org.Name - org.Avatar = base.EncodeMd5(org.Email) - org.AvatarEmail = org.Email - // No password for organization. + org.UseCustomAvatar = true org.NumTeams = 1 org.NumMembers = 1 @@ -141,6 +119,17 @@ func CreateOrganization(org, owner *User) (err error) { if _, err = sess.Insert(org); err != nil { return fmt.Errorf("insert organization: %v", err) } + org.GenerateRandomAvatar() + + // Add initial creator to organization and owner team. + if _, err = sess.Insert(&OrgUser{ + Uid: owner.Id, + OrgID: org.Id, + IsOwner: true, + NumTeams: 1, + }); err != nil { + return fmt.Errorf("insert org-user relation: %v", err) + } // Create default owner team. t := &Team{ @@ -154,23 +143,11 @@ func CreateOrganization(org, owner *User) (err error) { return fmt.Errorf("insert owner team: %v", err) } - // Add initial creator to organization and owner team. - ou := &OrgUser{ - Uid: owner.Id, - OrgID: org.Id, - IsOwner: true, - NumTeams: 1, - } - if _, err = sess.Insert(ou); err != nil { - return fmt.Errorf("insert org-user relation: %v", err) - } - - tu := &TeamUser{ + if _, err = sess.Insert(&TeamUser{ Uid: owner.Id, OrgID: org.Id, TeamID: t.ID, - } - if _, err = sess.Insert(tu); err != nil { + }); err != nil { return fmt.Errorf("insert team-user relation: %v", err) } diff --git a/models/user.go b/models/user.go index beea155ae2..fdd9429aa3 100644 --- a/models/user.go +++ b/models/user.go @@ -55,12 +55,12 @@ type User struct { Name string `xorm:"UNIQUE NOT NULL"` FullName string // Email is the primary email address (to be used for communication). - Email string `xorm:"UNIQUE(s) NOT NULL"` + Email string `xorm:"NOT NULL"` Passwd string `xorm:"NOT NULL"` LoginType LoginType LoginSource int64 `xorm:"NOT NULL DEFAULT 0"` LoginName string - Type UserType `xorm:"UNIQUE(s)"` + Type UserType Orgs []*User `xorm:"-"` Repos []*Repository `xorm:"-"` Location string @@ -132,42 +132,56 @@ func (u *User) HomeLink() string { return setting.AppSubUrl + "/" + u.Name } +// CustomAvatarPath returns user custom avatar file path. +func (u *User) CustomAvatarPath() string { + return filepath.Join(setting.AvatarUploadPath, com.ToStr(u.Id)) +} + +// GenerateRandomAvatar generates a random avatar for user. +func (u *User) GenerateRandomAvatar() error { + seed := u.Email + if len(seed) == 0 { + seed = u.Name + } + + img, err := avatar.RandomImage([]byte(seed)) + if err != nil { + return fmt.Errorf("RandomImage: %v", err) + } + if err = os.MkdirAll(path.Dir(u.CustomAvatarPath()), os.ModePerm); err != nil { + return fmt.Errorf("MkdirAll: %v", err) + } + fw, err := os.Create(u.CustomAvatarPath()) + if err != nil { + return fmt.Errorf("Create: %v", err) + } + defer fw.Close() + + if err = jpeg.Encode(fw, img, nil); err != nil { + return fmt.Errorf("Encode: %v", err) + } + + log.Info("New random avatar created: %d", u.Id) + return nil +} + func (u *User) RelAvatarLink() string { defaultImgUrl := "/img/avatar_default.jpg" if u.Id == -1 { return defaultImgUrl } - imgPath := path.Join(setting.AvatarUploadPath, com.ToStr(u.Id)) switch { case u.UseCustomAvatar: - if !com.IsExist(imgPath) { + if !com.IsExist(u.CustomAvatarPath()) { return defaultImgUrl } return "/avatars/" + com.ToStr(u.Id) case setting.DisableGravatar, setting.OfflineMode: - if !com.IsExist(imgPath) { - img, err := avatar.RandomImage([]byte(u.Email)) - if err != nil { - log.Error(3, "RandomImage: %v", err) - return defaultImgUrl + if !com.IsExist(u.CustomAvatarPath()) { + if err := u.GenerateRandomAvatar(); err != nil { + log.Error(3, "GenerateRandomAvatar: %v", err) } - if err = os.MkdirAll(path.Dir(imgPath), os.ModePerm); err != nil { - log.Error(3, "MkdirAll: %v", err) - return defaultImgUrl - } - fw, err := os.Create(imgPath) - if err != nil { - log.Error(3, "Create: %v", err) - return defaultImgUrl - } - defer fw.Close() - - if err = jpeg.Encode(fw, img, nil); err != nil { - log.Error(3, "Encode: %v", err) - return defaultImgUrl - } - log.Info("New random avatar created: %d", u.Id) } return "/avatars/" + com.ToStr(u.Id) @@ -208,11 +222,6 @@ func (u *User) ValidatePassword(passwd string) bool { return u.Passwd == newUser.Passwd } -// CustomAvatarPath returns user custom avatar file path. -func (u *User) CustomAvatarPath() string { - return filepath.Join(setting.AvatarUploadPath, com.ToStr(u.Id)) -} - // UploadAvatar saves custom avatar for user. // FIXME: split uploads to different subdirs in case we have massive users. func (u *User) UploadAvatar(data []byte) error { @@ -494,12 +503,20 @@ func ChangeUserName(u *User, newUserName string) (err error) { } func updateUser(e Engine, u *User) error { - u.Email = strings.ToLower(u.Email) - has, err := e.Where("id!=?", u.Id).And("type=?", u.Type).And("email=?", u.Email).Get(new(User)) - if err != nil { - return err - } else if has { - return ErrEmailAlreadyUsed{u.Email} + // Organization does not need e-mail. + if !u.IsOrganization() { + u.Email = strings.ToLower(u.Email) + has, err := e.Where("id!=?", u.Id).And("type=?", u.Type).And("email=?", u.Email).Get(new(User)) + if err != nil { + return err + } else if has { + return ErrEmailAlreadyUsed{u.Email} + } + + if len(u.AvatarEmail) == 0 { + u.AvatarEmail = u.Email + } + u.Avatar = avatar.HashEmail(u.AvatarEmail) } u.LowerName = strings.ToLower(u.Name) @@ -514,13 +531,8 @@ func updateUser(e Engine, u *User) error { u.Description = u.Description[:255] } - if u.AvatarEmail == "" { - u.AvatarEmail = u.Email - } - u.Avatar = avatar.HashEmail(u.AvatarEmail) - u.FullName = base.Sanitizer.Sanitize(u.FullName) - _, err = e.Id(u.Id).AllCols().Update(u) + _, err := e.Id(u.Id).AllCols().Update(u) return err } |