Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restrict creating organisations by user #193

Merged
merged 12 commits into from
Dec 31, 2016
2 changes: 1 addition & 1 deletion cmd/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func runWeb(ctx *cli.Context) error {

m.Group("/users", func() {
m.Get("", admin.Users)
m.Combo("/new").Get(admin.NewUser).Post(bindIgnErr(auth.AdminCrateUserForm{}), admin.NewUserPost)
m.Combo("/new").Get(admin.NewUser).Post(bindIgnErr(auth.AdminCreateUserForm{}), admin.NewUserPost)
m.Combo("/:userid").Get(admin.EditUser).Post(bindIgnErr(auth.AdminEditUserForm{}), admin.EditUserPost)
m.Post("/:userid/delete", admin.DeleteUser)
})
Expand Down
2 changes: 2 additions & 0 deletions conf/locale/locale_de-DE.ini
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,7 @@ team_permission_desc=Welche Berechtigungsstufe soll das Team haben?

form.name_reserved=Organisationsname '%s' ist bereits vergeben.
form.name_pattern_not_allowed=Organisationsnamen der Form '%s' sind nicht erlaubt.
form.create_org_not_allowed=Dieser Benutzer darf keine Organisation erstellen.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're supposed to only modify the english localization, whereas other languages are delegated to an external service. Please @lunny, @bkcsoft, @tboerger correct me if I'm wrong

Copy link
Contributor Author

@Schwobaland Schwobaland Nov 18, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't find the translation service for gitea. There is only Crowdin for gogs and an empty project for gitea.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to use an external service again but it's not enabled yet


settings=Einstellungen
settings.options=Optionen
Expand Down Expand Up @@ -963,6 +964,7 @@ users.prohibit_login=Diesem Konto ist es nicht gestattet sich anzumelden
users.is_admin=Dieses Konto hat Administratorrechte
users.allow_git_hook=Dieses Konto ist berechtigt Git-Hooks zu erstellen
users.allow_import_local=Dieses Konto ist berechtigt, lokale Repositories zu importieren
users.allow_create_organization=Dieses Konto ist berechtigt, Organisationen zu erstellen
users.update_profile=Konto aktualisieren
users.delete_account=Dieses Konto löschen
users.still_own_repo=Dieses Konto besitzt noch Repositories. Sie müssen diese zuerst löschen oder übertragen.
Expand Down
2 changes: 2 additions & 0 deletions conf/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,7 @@ team_permission_desc = What permission level should this team have?

form.name_reserved = Organization name '%s' is reserved.
form.name_pattern_not_allowed = Organization name pattern '%s' is not allowed.
form.create_org_not_allowed = This user is not allowed to create an organization.

settings = Settings
settings.options = Options
Expand Down Expand Up @@ -968,6 +969,7 @@ users.prohibit_login = This account is prohibited to login
users.is_admin = This account has administrator permissions
users.allow_git_hook = This account has permissions to create Git hooks
users.allow_import_local = This account has permissions to import local repositories
users.allow_create_organization = This account has permissions to create Organizations
users.update_profile = Update Account Profile
users.delete_account = Delete This Account
users.still_own_repo = This account still has ownership over at least one repository, you have to delete or transfer them first.
Expand Down
12 changes: 12 additions & 0 deletions models/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ func (err ErrUserHasOrgs) Error() string {
return fmt.Sprintf("user still has membership of organizations [uid: %d]", err.UID)
}

type ErrUserNotAllowedCreateOrg struct {
}

func IsErrUserNotAllowedCreateOrg(err error) bool {
_, ok := err.(ErrUserNotAllowedCreateOrg)
return ok
}

func (err ErrUserNotAllowedCreateOrg) Error() string {
return fmt.Sprintf("user is not allowed to create organizations")
}

type ErrReachLimitOfRepo struct {
Limit int
}
Expand Down
6 changes: 4 additions & 2 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/Unknwon/com"
"github.com/go-xorm/xorm"
gouuid "github.com/satori/go.uuid"
"gopkg.in/ini.v1"
ini "gopkg.in/ini.v1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this isn't needed for your patch please avoid the change

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As long as it's done in a separate commit it's OK. It's an goimports correction.


"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/log"
Expand Down Expand Up @@ -72,8 +72,10 @@ var migrations = []Migration{

// v13 -> v14:v0.9.87
NewMigration("set comment updated with created", setCommentUpdatedWithCreated),

// v14
NewMigration("create user column diff view style", createUserColumnDiffViewStyle),
// v15
NewMigration("create user colum allow create organization", createAllowCreateOrganizationColumn),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"... column ..."?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed typo

}

// Migrate database to current version
Expand Down
19 changes: 19 additions & 0 deletions models/migrations/v15.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2016 Gitea. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package migrations

import "github.com/go-xorm/xorm"

type UserV15 struct {
AllowCreateOrganization bool
}

func (*UserV15) TableName() string {
return "user"
}

func createAllowCreateOrganizationColumn(x *xorm.Engine) error {
return x.Sync2(new(UserV15))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the default value set for existing users upon migration ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the default have to be true

}
4 changes: 4 additions & 0 deletions models/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ func (org *User) RemoveOrgRepo(repoID int64) error {

// CreateOrganization creates record of a new organization.
func CreateOrganization(org, owner *User) (err error) {
if !owner.CanCreateOrganization() {
return ErrUserNotAllowedCreateOrg{}
}

if err = IsUsableUsername(org.Name); err != nil {
return err
}
Expand Down
16 changes: 11 additions & 5 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,12 @@ type User struct {
MaxRepoCreation int `xorm:"NOT NULL DEFAULT -1"`

// Permissions
IsActive bool // Activate primary email
IsAdmin bool
AllowGitHook bool
AllowImportLocal bool // Allow migrate repository by local path
ProhibitLogin bool
IsActive bool // Activate primary email
IsAdmin bool
AllowGitHook bool
AllowImportLocal bool // Allow migrate repository by local path
AllowCreateOrganization bool
ProhibitLogin bool

// Avatar
Avatar string `xorm:"VARCHAR(2048) NOT NULL"`
Expand Down Expand Up @@ -185,6 +186,11 @@ func (u *User) CanCreateRepo() bool {
return u.NumRepos < u.MaxRepoCreation
}

// CanCreateOrg returns true if user can create organisation.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CanCreateOrganization?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed typo

func (u *User) CanCreateOrganization() bool {
return u.IsAdmin || u.AllowCreateOrganization
}

// CanEditGitHook returns true if user can edit Git hooks.
func (u *User) CanEditGitHook() bool {
return u.IsAdmin || u.AllowGitHook
Expand Down
33 changes: 17 additions & 16 deletions modules/auth/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
package auth

import (
"gopkg.in/macaron.v1"
macaron "gopkg.in/macaron.v1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this isn't needed for your patch please avoid the change


"github.com/go-macaron/binding"
)

type AdminCrateUserForm struct {
type AdminCreateUserForm struct {
LoginType string `binding:"Required"`
LoginName string
UserName string `binding:"Required;AlphaDashDot;MaxSize(35)"`
Expand All @@ -19,24 +19,25 @@ type AdminCrateUserForm struct {
SendNotify bool
}

func (f *AdminCrateUserForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
func (f *AdminCreateUserForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
return validate(errs, ctx.Data, f, ctx.Locale)
}

type AdminEditUserForm struct {
LoginType string `binding:"Required"`
LoginName string
FullName string `binding:"MaxSize(100)"`
Email string `binding:"Required;Email;MaxSize(254)"`
Password string `binding:"MaxSize(255)"`
Website string `binding:"MaxSize(50)"`
Location string `binding:"MaxSize(50)"`
MaxRepoCreation int
Active bool
Admin bool
AllowGitHook bool
AllowImportLocal bool
ProhibitLogin bool
LoginType string `binding:"Required"`
LoginName string
FullName string `binding:"MaxSize(100)"`
Email string `binding:"Required;Email;MaxSize(254)"`
Password string `binding:"MaxSize(255)"`
Website string `binding:"MaxSize(50)"`
Location string `binding:"MaxSize(50)"`
MaxRepoCreation int
Active bool
Admin bool
AllowGitHook bool
AllowImportLocal bool
AllowCreateOrganization bool
ProhibitLogin bool
}

func (f *AdminEditUserForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
Expand Down
Loading