diff options
author | Joe Chen <jc@unknwon.io> | 2022-10-22 14:41:40 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-22 14:41:40 +0800 |
commit | c502dc6ed888a4cf2c8b36176585f4166536ab6d (patch) | |
tree | 71e3f758069dc2435eecb88ae786b2efaaa972f9 /internal/db/user.go | |
parent | 260e990be75885e1a560df449dacdeecafeffdcf (diff) |
refactor(db): move some methods from `user.go` to `users.go` (#7195)
Diffstat (limited to 'internal/db/user.go')
-rw-r--r-- | internal/db/user.go | 153 |
1 files changed, 7 insertions, 146 deletions
diff --git a/internal/db/user.go b/internal/db/user.go index 08b7e09e..33b69af1 100644 --- a/internal/db/user.go +++ b/internal/db/user.go @@ -27,7 +27,6 @@ import ( "xorm.io/xorm" "github.com/gogs/git-module" - api "github.com/gogs/go-gogs-client" "gogs.io/gogs/internal/avatar" "gogs.io/gogs/internal/conf" @@ -37,77 +36,13 @@ import ( "gogs.io/gogs/internal/tool" ) -// USER_AVATAR_URL_PREFIX is used to identify a URL is to access user avatar. -const USER_AVATAR_URL_PREFIX = "avatars" - -type UserType int - -const ( - UserIndividual UserType = iota // Historic reason to make it starts at 0. - UserOrganization -) - -// User represents the object of individual and member of organization. -type User struct { - ID int64 `gorm:"primaryKey"` - LowerName string `xorm:"UNIQUE NOT NULL" gorm:"unique;not null"` - Name string `xorm:"UNIQUE NOT NULL" gorm:"not null"` - 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"` - LoginSource int64 `xorm:"NOT NULL DEFAULT 0" gorm:"not null;default:0"` - LoginName string - Type UserType - OwnedOrgs []*User `xorm:"-" gorm:"-" json:"-"` - Orgs []*User `xorm:"-" gorm:"-" json:"-"` - Repos []*Repository `xorm:"-" gorm:"-" json:"-"` - Location string - Website string - Rands string `xorm:"VARCHAR(10)" gorm:"type:VARCHAR(10)"` - Salt string `xorm:"VARCHAR(10)" gorm:"type:VARCHAR(10)"` - - Created time.Time `xorm:"-" gorm:"-" json:"-"` - CreatedUnix int64 - Updated time.Time `xorm:"-" gorm:"-" json:"-"` - UpdatedUnix int64 - - // Remember visibility choice for convenience, true for private - LastRepoVisibility bool - // Maximum repository creation limit, -1 means use global default - MaxRepoCreation int `xorm:"NOT NULL DEFAULT -1" gorm:"not null;default:-1"` - - // Permissions - IsActive bool // Activate primary email - IsAdmin bool - AllowGitHook bool - AllowImportLocal bool // Allow migrate repository by local path - ProhibitLogin bool - - // Avatar - Avatar string `xorm:"VARCHAR(2048) NOT NULL" gorm:"type:VARCHAR(2048);not null"` - AvatarEmail string `xorm:"NOT NULL" gorm:"not null"` - UseCustomAvatar bool - - // Counters - NumFollowers int - NumFollowing int `xorm:"NOT NULL DEFAULT 0" gorm:"not null;default:0"` - NumStars int - NumRepos int - - // For organization - Description string - NumTeams int - NumMembers int - Teams []*Team `xorm:"-" gorm:"-" json:"-"` - Members []*User `xorm:"-" gorm:"-" json:"-"` -} - +// TODO(unknwon): Delete me once refactoring is done. func (u *User) BeforeInsert() { u.CreatedUnix = time.Now().Unix() u.UpdatedUnix = u.CreatedUnix } +// TODO(unknwon): Refactoring together with methods that do updates. func (u *User) BeforeUpdate() { if u.MaxRepoCreation < -1 { u.MaxRepoCreation = -1 @@ -115,6 +50,7 @@ func (u *User) BeforeUpdate() { u.UpdatedUnix = time.Now().Unix() } +// TODO(unknwon): Delete me once refactoring is done. func (u *User) AfterSet(colName string, _ xorm.Cell) { switch colName { case "created_unix": @@ -124,81 +60,6 @@ func (u *User) AfterSet(colName string, _ xorm.Cell) { } } -func (u *User) APIFormat() *api.User { - return &api.User{ - ID: u.ID, - UserName: u.Name, - Login: u.Name, - FullName: u.FullName, - Email: u.Email, - AvatarUrl: u.AvatarLink(), - } -} - -func (u *User) RepoCreationNum() int { - if u.MaxRepoCreation <= -1 { - return conf.Repository.MaxCreationLimit - } - return u.MaxRepoCreation -} - -func (u *User) CanCreateRepo() bool { - if u.MaxRepoCreation <= -1 { - if conf.Repository.MaxCreationLimit <= -1 { - return true - } - return u.NumRepos < conf.Repository.MaxCreationLimit - } - return u.NumRepos < u.MaxRepoCreation -} - -func (u *User) CanCreateOrganization() bool { - return !conf.Admin.DisableRegularOrgCreation || u.IsAdmin -} - -// CanEditGitHook returns true if user can edit Git hooks. -func (u *User) CanEditGitHook() bool { - return u.IsAdmin || u.AllowGitHook -} - -// CanImportLocal returns true if user can migrate repository by local path. -func (u *User) CanImportLocal() bool { - return conf.Repository.EnableLocalPathMigration && (u.IsAdmin || u.AllowImportLocal) -} - -// DashboardLink returns the user dashboard page link. -func (u *User) DashboardLink() string { - if u.IsOrganization() { - return conf.Server.Subpath + "/org/" + u.Name + "/dashboard/" - } - return conf.Server.Subpath + "/" -} - -// HomeLink returns the user or organization home page link. -func (u *User) HomeLink() string { - return conf.Server.Subpath + "/" + u.Name -} - -func (u *User) HTMLURL() string { - return conf.Server.ExternalURL + u.Name -} - -// GenerateEmailActivateCode generates an activate code based on user information and given e-mail. -func (u *User) GenerateEmailActivateCode(email string) string { - code := tool.CreateTimeLimitCode( - com.ToStr(u.ID)+email+u.LowerName+u.Passwd+u.Rands, - conf.Auth.ActivateCodeLives, nil) - - // Add tail hex username - code += hex.EncodeToString([]byte(u.LowerName)) - return code -} - -// GenerateActivateCode generates an activate code based on user information. -func (u *User) GenerateActivateCode() string { - return u.GenerateEmailActivateCode(u.Email) -} - // CustomAvatarPath returns user custom avatar file path. func (u *User) CustomAvatarPath() string { return filepath.Join(conf.Picture.AvatarUploadPath, com.ToStr(u.ID)) @@ -246,7 +107,7 @@ func (u *User) RelAvatarLink() string { if !com.IsExist(u.CustomAvatarPath()) { return defaultImgUrl } - return fmt.Sprintf("%s/%s/%d", conf.Server.Subpath, USER_AVATAR_URL_PREFIX, u.ID) + return fmt.Sprintf("%s/%s/%d", conf.Server.Subpath, conf.UsersAvatarURLPath, u.ID) case conf.Picture.DisableGravatar: if !com.IsExist(u.CustomAvatarPath()) { if err := u.GenerateRandomAvatar(); err != nil { @@ -254,7 +115,7 @@ func (u *User) RelAvatarLink() string { } } - return fmt.Sprintf("%s/%s/%d", conf.Server.Subpath, USER_AVATAR_URL_PREFIX, u.ID) + return fmt.Sprintf("%s/%s/%d", conf.Server.Subpath, conf.UsersAvatarURLPath, u.ID) } return tool.AvatarLink(u.AvatarEmail) } @@ -374,7 +235,7 @@ func (u *User) IsWriterOfRepo(repo *Repository) bool { // IsOrganization returns true if user is actually a organization. func (u *User) IsOrganization() bool { - return u.Type == UserOrganization + return u.Type == UserTypeOrganization } // IsUserOrgOwner returns true if user is in the owner team of given organization. @@ -434,7 +295,7 @@ func (u *User) GetOrganizations(showPrivate bool) error { } u.Orgs = make([]*User, 0, len(orgIDs)) - if err = x.Where("type = ?", UserOrganization).In("id", orgIDs).Find(&u.Orgs); err != nil { + if err = x.Where("type = ?", UserTypeOrganization).In("id", orgIDs).Find(&u.Orgs); err != nil { return err } return nil |