diff options
author | Unknwon <u@gogs.io> | 2015-12-10 12:37:53 -0500 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2015-12-10 12:37:53 -0500 |
commit | 2a0bb1fa906a4d135ed88de4f705c3ab7214e60c (patch) | |
tree | 37d207458c999c5789fd6ca3a2c62d6700ed81b7 /models | |
parent | c6083c335e53a860fd4c500a86dcdda49a16515e (diff) | |
download | gitea-2a0bb1fa906a4d135ed88de4f705c3ab7214e60c.tar.gz gitea-2a0bb1fa906a4d135ed88de4f705c3ab7214e60c.zip |
#1575 Limit repo creation
Diffstat (limited to 'models')
-rw-r--r-- | models/error.go | 13 | ||||
-rw-r--r-- | models/repo.go | 4 | ||||
-rw-r--r-- | models/user.go | 22 |
3 files changed, 39 insertions, 0 deletions
diff --git a/models/error.go b/models/error.go index 54d32d1546..83a24e7f4e 100644 --- a/models/error.go +++ b/models/error.go @@ -107,6 +107,19 @@ func (err ErrUserHasOrgs) Error() string { return fmt.Sprintf("user still has membership of organizations [uid: %d]", err.UID) } +type ErrReachLimitOfRepo struct { + Limit int +} + +func IsErrReachLimitOfRepo(err error) bool { + _, ok := err.(ErrReachLimitOfRepo) + return ok +} + +func (err ErrReachLimitOfRepo) Error() string { + return fmt.Sprintf("user has reached maximum limit of repositories [limit: %d]", err.Limit) +} + // __ __.__ __ .__ // / \ / \__| | _|__| // \ \/\/ / | |/ / | diff --git a/models/repo.go b/models/repo.go index 307f86cb25..fafc3b9c90 100644 --- a/models/repo.go +++ b/models/repo.go @@ -900,6 +900,10 @@ func createRepository(e *xorm.Session, u *User, repo *Repository) (err error) { // CreateRepository creates a repository for given user or organization. func CreateRepository(u *User, opts CreateRepoOptions) (_ *Repository, err error) { + if !u.CanCreateRepo() { + return nil, ErrReachLimitOfRepo{u.MaxRepoCreation} + } + repo := &Repository{ OwnerID: u.Id, Owner: u, diff --git a/models/user.go b/models/user.go index f19e33039b..3ff525c589 100644 --- a/models/user.go +++ b/models/user.go @@ -75,6 +75,8 @@ type User struct { // Remember visibility choice for convenience, true for private LastRepoVisibility bool + // Maximum repository creation limit, 0 means use gloabl default + MaxRepoCreation int `xorm:"NOT NULL"` // Permissions. IsActive bool @@ -101,6 +103,12 @@ type User struct { Members []*User `xorm:"-"` } +func (u *User) BeforeUpdate() { + if u.MaxRepoCreation < 0 { + u.MaxRepoCreation = 0 + } +} + func (u *User) AfterSet(colName string, _ xorm.Cell) { switch colName { case "full_name": @@ -116,6 +124,20 @@ func (u *User) HasForkedRepo(repoID int64) bool { return has } +func (u *User) RepoCreationNum() int { + if u.MaxRepoCreation == 0 { + return setting.Repository.MaxCreationLimit + } + return u.MaxRepoCreation +} + +func (u *User) CanCreateRepo() bool { + if u.MaxRepoCreation == 0 { + return u.NumRepos < setting.Repository.MaxCreationLimit + } + return u.NumRepos < u.MaxRepoCreation +} + // CanEditGitHook returns true if user can edit Git hooks. func (u *User) CanEditGitHook() bool { return u.IsAdmin || u.AllowGitHook |