#1575 Limit repo creation
This commit is contained in:
parent
c6083c335e
commit
2a0bb1fa90
15 changed files with 73 additions and 14 deletions
|
@ -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)
|
||||
}
|
||||
|
||||
// __ __.__ __ .__
|
||||
// / \ / \__| | _|__|
|
||||
// \ \/\/ / | |/ / |
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue