summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorUnknwon <u@gogs.io>2016-07-23 18:58:18 +0800
committerUnknwon <u@gogs.io>2016-07-23 18:58:18 +0800
commit69f53087611b3b3fc558f6656bf68ad8603f9776 (patch)
tree0ea0648c073adaa0bd390a96b194ac90c95dc98d /models
parent4b5e09e4d6061e12a2a6728e5f7cfdf2f2550e58 (diff)
downloadgitea-69f53087611b3b3fc558f6656bf68ad8603f9776.tar.gz
gitea-69f53087611b3b3fc558f6656bf68ad8603f9776.zip
#2903 use different reversed words and patterns for repository and user
Diffstat (limited to 'models')
-rw-r--r--models/org.go2
-rw-r--r--models/repo.go42
-rw-r--r--models/user.go39
3 files changed, 49 insertions, 34 deletions
diff --git a/models/org.go b/models/org.go
index d2cb188935..9cb862dec9 100644
--- a/models/org.go
+++ b/models/org.go
@@ -94,7 +94,7 @@ func (org *User) RemoveOrgRepo(repoID int64) error {
// CreateOrganization creates record of a new organization.
func CreateOrganization(org, owner *User) (err error) {
- if err = IsUsableName(org.Name); err != nil {
+ if err = IsUsableUsername(org.Name); err != nil {
return err
}
diff --git a/models/repo.go b/models/repo.go
index e1073419a3..d5ca030d9f 100644
--- a/models/repo.go
+++ b/models/repo.go
@@ -19,7 +19,6 @@ import (
"strings"
"sync"
"time"
- "unicode/utf8"
"github.com/Unknwon/cae/zip"
"github.com/Unknwon/com"
@@ -517,34 +516,6 @@ func (repo *Repository) CloneLink() (cl *CloneLink) {
return repo.cloneLink(false)
}
-var (
- reservedNames = []string{"debug", "raw", "install", "api", "avatar", "user", "org", "help", "stars", "issues", "pulls", "commits", "repo", "template", "admin", "new", ".", ".."}
- reservedPatterns = []string{"*.git", "*.keys", "*.wiki"}
-)
-
-// IsUsableName checks if name is reserved or pattern of name is not allowed.
-func IsUsableName(name string) error {
- name = strings.TrimSpace(strings.ToLower(name))
- if utf8.RuneCountInString(name) == 0 {
- return ErrNameEmpty
- }
-
- for i := range reservedNames {
- if name == reservedNames[i] {
- return ErrNameReserved{name}
- }
- }
-
- for _, pat := range reservedPatterns {
- if pat[0] == '*' && strings.HasSuffix(name, pat[1:]) ||
- (pat[len(pat)-1] == '*' && strings.HasPrefix(name, pat[:len(pat)-1])) {
- return ErrNamePatternNotAllowed{pat}
- }
- }
-
- return nil
-}
-
// Mirror represents a mirror information of repository.
type Mirror struct {
ID int64 `xorm:"pk autoincr"`
@@ -940,8 +911,17 @@ func initRepository(e Engine, repoPath string, u *User, repo *Repository, opts C
return nil
}
+var (
+ reservedRepoNames = []string{".", ".."}
+ reservedRepoPatterns = []string{"*.git", "*.wiki"}
+)
+
+func IsUsableRepoName(name string) error {
+ return isUsableName(reservedRepoNames, reservedRepoPatterns, name)
+}
+
func createRepository(e *xorm.Session, u *User, repo *Repository) (err error) {
- if err = IsUsableName(repo.Name); err != nil {
+ if err = IsUsableRepoName(repo.Name); err != nil {
return err
}
@@ -1209,7 +1189,7 @@ func TransferOwnership(u *User, newOwnerName string, repo *Repository) error {
func ChangeRepositoryName(u *User, oldRepoName, newRepoName string) (err error) {
oldRepoName = strings.ToLower(oldRepoName)
newRepoName = strings.ToLower(newRepoName)
- if err = IsUsableName(newRepoName); err != nil {
+ if err = IsUsableRepoName(newRepoName); err != nil {
return err
}
diff --git a/models/user.go b/models/user.go
index e8e79f247c..a7c877140c 100644
--- a/models/user.go
+++ b/models/user.go
@@ -18,6 +18,7 @@ import (
"path/filepath"
"strings"
"time"
+ "unicode/utf8"
"github.com/Unknwon/com"
"github.com/go-xorm/xorm"
@@ -468,9 +469,43 @@ func NewFakeUser() *User {
}
}
+var (
+ reversedUsernames = []string{"debug", "raw", "install", "api", "avatar", "user", "org", "help", "stars", "issues", "pulls", "commits", "repo", "template", "admin", "new", ".", ".."}
+ reversedUserPatterns = []string{"*.keys"}
+)
+
+// isUsableName checks if name is reserved or pattern of name is not allowed
+// based on given reversed names and patterns.
+// Names are exact match, patterns can be prefix or suffix match with placeholder '*'.
+func isUsableName(names, patterns []string, name string) error {
+ name = strings.TrimSpace(strings.ToLower(name))
+ if utf8.RuneCountInString(name) == 0 {
+ return ErrNameEmpty
+ }
+
+ for i := range names {
+ if name == names[i] {
+ return ErrNameReserved{name}
+ }
+ }
+
+ for _, pat := range patterns {
+ if pat[0] == '*' && strings.HasSuffix(name, pat[1:]) ||
+ (pat[len(pat)-1] == '*' && strings.HasPrefix(name, pat[:len(pat)-1])) {
+ return ErrNamePatternNotAllowed{pat}
+ }
+ }
+
+ return nil
+}
+
+func IsUsableUsername(name string) error {
+ return isUsableName(reversedUsernames, reversedUserPatterns, name)
+}
+
// CreateUser creates record of a new user.
func CreateUser(u *User) (err error) {
- if err = IsUsableName(u.Name); err != nil {
+ if err = IsUsableUsername(u.Name); err != nil {
return err
}
@@ -583,7 +618,7 @@ func VerifyActiveEmailCode(code, email string) *EmailAddress {
// ChangeUserName changes all corresponding setting from old user name to new one.
func ChangeUserName(u *User, newUserName string) (err error) {
- if err = IsUsableName(newUserName); err != nil {
+ if err = IsUsableUsername(newUserName); err != nil {
return err
}