diff options
author | Unknwon <u@gogs.io> | 2015-11-03 18:40:52 -0500 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2015-11-03 18:40:52 -0500 |
commit | 6f0a41b8b28ba33382ab8d655c0d015324be7647 (patch) | |
tree | d30191b529354c42aaf8ae4066d73b402c0b4bb2 /routers | |
parent | 25ec20d5251511ebd0b9e6b963e189b860c39704 (diff) | |
download | gitea-6f0a41b8b28ba33382ab8d655c0d015324be7647.tar.gz gitea-6f0a41b8b28ba33382ab8d655c0d015324be7647.zip |
#1511 Allow local import only for admin users
Diffstat (limited to 'routers')
-rw-r--r-- | routers/admin/users.go | 1 | ||||
-rw-r--r-- | routers/api/v1/repo.go | 33 | ||||
-rw-r--r-- | routers/repo/repo.go | 35 |
3 files changed, 33 insertions, 36 deletions
diff --git a/routers/admin/users.go b/routers/admin/users.go index ac0f538171..d5358553ce 100644 --- a/routers/admin/users.go +++ b/routers/admin/users.go @@ -213,6 +213,7 @@ func EditUserPost(ctx *middleware.Context, form auth.AdminEditUserForm) { u.IsActive = form.Active u.IsAdmin = form.Admin u.AllowGitHook = form.AllowGitHook + u.AllowImportLocal = form.AllowImportLocal if err := models.UpdateUser(u); err != nil { if models.IsErrEmailAlreadyUsed(err) { diff --git a/routers/api/v1/repo.go b/routers/api/v1/repo.go index 5a00267778..2341e28878 100644 --- a/routers/api/v1/repo.go +++ b/routers/api/v1/repo.go @@ -5,9 +5,7 @@ package v1 import ( - "net/url" "path" - "strings" "github.com/Unknwon/com" @@ -218,22 +216,23 @@ func MigrateRepo(ctx *middleware.Context, form auth.MigrateRepoForm) { } } - // Remote address can be HTTP/HTTPS/Git URL or local path. - remoteAddr := form.CloneAddr - if strings.HasPrefix(form.CloneAddr, "http://") || - strings.HasPrefix(form.CloneAddr, "https://") || - strings.HasPrefix(form.CloneAddr, "git://") { - u, err := url.Parse(form.CloneAddr) - if err != nil { - ctx.APIError(422, "", err) - return - } - if len(form.AuthUsername) > 0 || len(form.AuthPassword) > 0 { - u.User = url.UserPassword(form.AuthUsername, form.AuthPassword) + remoteAddr, err := form.ParseRemoteAddr(ctx.User) + if err != nil { + if models.IsErrInvalidCloneAddr(err) { + addrErr := err.(models.ErrInvalidCloneAddr) + switch { + case addrErr.IsURLError: + ctx.APIError(422, "", err) + case addrErr.IsPermissionDenied: + ctx.APIError(422, "", "You are not allowed to import local repositories.") + case addrErr.IsInvalidPath: + ctx.APIError(422, "", "Invalid local path, it does not exist or not a directory.") + default: + ctx.APIError(500, "ParseRemoteAddr", "Unknown error type (ErrInvalidCloneAddr): "+err.Error()) + } + } else { + ctx.APIError(500, "ParseRemoteAddr", err) } - remoteAddr = u.String() - } else if !com.IsDir(remoteAddr) { - ctx.APIError(422, "", "Invalid local path, it does not exist or not a directory.") return } diff --git a/routers/repo/repo.go b/routers/repo/repo.go index f7e7e32093..dab9dba1f5 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -6,7 +6,6 @@ package repo import ( "fmt" - "net/url" "os" "path" "strings" @@ -164,26 +163,24 @@ func MigratePost(ctx *middleware.Context, form auth.MigrateRepoForm) { return } - // Remote address can be HTTP/HTTPS/Git URL or local path. - // Note: remember to change api/v1/repo.go: MigrateRepo - // FIXME: merge these two functions with better error handling - remoteAddr := form.CloneAddr - if strings.HasPrefix(form.CloneAddr, "http://") || - strings.HasPrefix(form.CloneAddr, "https://") || - strings.HasPrefix(form.CloneAddr, "git://") { - u, err := url.Parse(form.CloneAddr) - if err != nil { + remoteAddr, err := form.ParseRemoteAddr(ctx.User) + if err != nil { + if models.IsErrInvalidCloneAddr(err) { ctx.Data["Err_CloneAddr"] = true - ctx.RenderWithErr(ctx.Tr("form.url_error"), MIGRATE, &form) - return - } - if len(form.AuthUsername) > 0 || len(form.AuthPassword) > 0 { - u.User = url.UserPassword(form.AuthUsername, form.AuthPassword) + addrErr := err.(models.ErrInvalidCloneAddr) + switch { + case addrErr.IsURLError: + ctx.RenderWithErr(ctx.Tr("form.url_error"), MIGRATE, &form) + case addrErr.IsPermissionDenied: + ctx.RenderWithErr(ctx.Tr("repo.migrate.permission_denied"), MIGRATE, &form) + case addrErr.IsInvalidPath: + ctx.RenderWithErr(ctx.Tr("repo.migrate.invalid_local_path"), MIGRATE, &form) + default: + ctx.Handle(500, "Unknown error", err) + } + } else { + ctx.Handle(500, "ParseRemoteAddr", err) } - remoteAddr = u.String() - } else if !com.IsDir(remoteAddr) { - ctx.Data["Err_CloneAddr"] = true - ctx.RenderWithErr(ctx.Tr("repo.migrate.invalid_local_path"), MIGRATE, &form) return } |