Add Repository/user name filter

This commit is contained in:
Unknown 2014-03-20 11:41:24 -04:00
parent 1a0d7c54a2
commit 3b387336bf
7 changed files with 51 additions and 5 deletions

View File

@ -5,7 +5,7 @@ Gogs(Go Git Service) is a GitHub-like clone in the Go Programming Language.
Since we choose to use pure Go implementation of Git manipulation, Gogs certainly supports **ALL platforms** that Go supports, including Linux, Max OS X, and Windows with **ZERO** dependency. Since we choose to use pure Go implementation of Git manipulation, Gogs certainly supports **ALL platforms** that Go supports, including Linux, Max OS X, and Windows with **ZERO** dependency.
##### Current version: 0.1.1 Alpha ##### Current version: 0.1.4 Alpha
## Purpose ## Purpose

View File

@ -20,7 +20,7 @@ import (
// Test that go1.1 tag above is included in builds. main.go refers to this definition. // Test that go1.1 tag above is included in builds. main.go refers to this definition.
const go11tag = true const go11tag = true
const APP_VER = "0.1.2.0320.1" const APP_VER = "0.1.3.0320.1"
func init() { func init() {
base.AppVer = APP_VER base.AppVer = APP_VER

View File

@ -12,6 +12,7 @@ import (
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"regexp"
"strings" "strings"
"sync" "sync"
"time" "time"
@ -82,6 +83,7 @@ var (
ErrRepoAlreadyExist = errors.New("Repository already exist") ErrRepoAlreadyExist = errors.New("Repository already exist")
ErrRepoNotExist = errors.New("Repository does not exist") ErrRepoNotExist = errors.New("Repository does not exist")
ErrRepoFileNotExist = errors.New("Target Repo file does not exist") ErrRepoFileNotExist = errors.New("Target Repo file does not exist")
ErrRepoNameIllegal = errors.New("Repository name contains illegal characters")
) )
func init() { func init() {
@ -104,6 +106,15 @@ func init() {
os.Exit(2) os.Exit(2)
} }
} }
// Initialize illegal patterns.
for i := range illegalPatterns[1:] {
pattern := ""
for j := range illegalPatterns[i+1] {
pattern += "[" + string(illegalPatterns[i+1][j]-32) + string(illegalPatterns[i+1][j]) + "]"
}
illegalPatterns[i+1] = pattern
}
} }
// IsRepositoryExist returns true if the repository with given name under user has already existed. // IsRepositoryExist returns true if the repository with given name under user has already existed.
@ -120,8 +131,28 @@ func IsRepositoryExist(user *User, repoName string) (bool, error) {
return s.IsDir(), nil return s.IsDir(), nil
} }
var (
// Define as all lower case!!
illegalPatterns = []string{"[.][Gg][Ii][Tt]", "user", "help", "stars", "issues", "pulls", "commits", "admin", "repo", "template"}
)
// IsLegalName returns false if name contains illegal characters.
func IsLegalName(repoName string) bool {
for _, pattern := range illegalPatterns {
has, _ := regexp.MatchString(pattern, repoName)
if has {
return false
}
}
return true
}
// CreateRepository creates a repository for given user or orgnaziation. // CreateRepository creates a repository for given user or orgnaziation.
func CreateRepository(user *User, repoName, desc, repoLang, license string, private bool, initReadme bool) (*Repository, error) { func CreateRepository(user *User, repoName, desc, repoLang, license string, private bool, initReadme bool) (*Repository, error) {
if !IsLegalName(repoName) {
return nil, ErrRepoNameIllegal
}
isExist, err := IsRepositoryExist(user, repoName) isExist, err := IsRepositoryExist(user, repoName)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -79,6 +79,7 @@ var (
ErrUserAlreadyExist = errors.New("User already exist") ErrUserAlreadyExist = errors.New("User already exist")
ErrUserNotExist = errors.New("User does not exist") ErrUserNotExist = errors.New("User does not exist")
ErrEmailAlreadyUsed = errors.New("E-mail already used") ErrEmailAlreadyUsed = errors.New("E-mail already used")
ErrUserNameIllegal = errors.New("User name contains illegal characters")
) )
// IsUserExist checks if given user name exist, // IsUserExist checks if given user name exist,
@ -108,6 +109,10 @@ func GetUserSalt() string {
// RegisterUser creates record of a new user. // RegisterUser creates record of a new user.
func RegisterUser(user *User) (*User, error) { func RegisterUser(user *User) (*User, error) {
if !IsLegalName(user.Name) {
return nil, ErrUserNameIllegal
}
isExist, err := IsUserExist(user.Name) isExist, err := IsUserExist(user.Name)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -31,6 +31,9 @@ func Create(ctx *middleware.Context, form auth.CreateRepoForm) {
} else if err == models.ErrRepoAlreadyExist { } else if err == models.ErrRepoAlreadyExist {
ctx.RenderWithErr("Repository name has already been used", "repo/create", &form) ctx.RenderWithErr("Repository name has already been used", "repo/create", &form)
return return
} else if err == models.ErrRepoNameIllegal {
ctx.RenderWithErr(models.ErrRepoNameIllegal.Error(), "repo/create", &form)
return
} }
ctx.Handle(200, "repo.Create", err) ctx.Handle(200, "repo.Create", err)
} }

View File

@ -217,6 +217,11 @@ func Setting(ctx *middleware.Context, params martini.Params) {
title = t title = t
} }
if len(params["branchname"]) == 0 {
params["branchname"] = "master"
}
ctx.Data["Branchname"] = params["branchname"]
ctx.Data["Title"] = title + " - settings" ctx.Data["Title"] = title + " - settings"
ctx.HTML(200, "repo/setting") ctx.HTML(200, "repo/setting")
} }

View File

@ -139,11 +139,13 @@ func SignUp(ctx *middleware.Context, form auth.RegisterForm) {
var err error var err error
if u, err = models.RegisterUser(u); err != nil { if u, err = models.RegisterUser(u); err != nil {
switch err.Error() { switch err {
case models.ErrUserAlreadyExist.Error(): case models.ErrUserAlreadyExist:
ctx.RenderWithErr("Username has been already taken", "user/signup", &form) ctx.RenderWithErr("Username has been already taken", "user/signup", &form)
case models.ErrEmailAlreadyUsed.Error(): case models.ErrEmailAlreadyUsed:
ctx.RenderWithErr("E-mail address has been already used", "user/signup", &form) ctx.RenderWithErr("E-mail address has been already used", "user/signup", &form)
case models.ErrUserNameIllegal:
ctx.RenderWithErr(models.ErrRepoNameIllegal.Error(), "user/signup", &form)
default: default:
ctx.Handle(200, "user.SignUp", err) ctx.Handle(200, "user.SignUp", err)
} }