diff options
Diffstat (limited to 'modules/middleware')
-rw-r--r-- | modules/middleware/auth.go | 8 | ||||
-rw-r--r-- | modules/middleware/context.go | 13 | ||||
-rw-r--r-- | modules/middleware/org.go | 8 | ||||
-rw-r--r-- | modules/middleware/repo.go | 20 |
4 files changed, 45 insertions, 4 deletions
diff --git a/modules/middleware/auth.go b/modules/middleware/auth.go index b2aaae10..2a02d276 100644 --- a/modules/middleware/auth.go +++ b/modules/middleware/auth.go @@ -10,6 +10,7 @@ import ( "github.com/Unknwon/macaron" "github.com/macaron-contrib/csrf" + "github.com/gogits/gogs/modules/auth" "github.com/gogits/gogs/modules/setting" ) @@ -49,6 +50,12 @@ func Toggle(options *ToggleOptions) macaron.Handler { if options.SignInRequire { if !ctx.IsSigned { + // Restrict API calls with error message. + if auth.IsAPIPath(ctx.Req.URL.Path) { + ctx.HandleAPI(403, "Only signed in user is allowed to call APIs.") + return + } + ctx.SetCookie("redirect_to", url.QueryEscape(setting.AppSubUrl+ctx.Req.RequestURI), 0, setting.AppSubUrl) ctx.Redirect(setting.AppSubUrl + "/user/login") return @@ -69,6 +76,7 @@ func Toggle(options *ToggleOptions) macaron.Handler { } } +// Contexter middleware already checks token for user sign in process. func ApiReqToken() macaron.Handler { return func(ctx *Context) { if !ctx.IsSigned { diff --git a/modules/middleware/context.go b/modules/middleware/context.go index b580de50..2995d578 100644 --- a/modules/middleware/context.go +++ b/modules/middleware/context.go @@ -106,6 +106,12 @@ func (ctx *Context) HasError() bool { return hasErr.(bool) } +// HasValue returns true if value of given name exists. +func (ctx *Context) HasValue(name string) bool { + _, ok := ctx.Data[name] + return ok +} + // HTML calls Context.HTML and converts template name to string. func (ctx *Context) HTML(status int, name base.TplName) { ctx.Context.HTML(status, string(name)) @@ -139,6 +145,13 @@ func (ctx *Context) Handle(status int, title string, err error) { ctx.HTML(status, base.TplName(fmt.Sprintf("status/%d", status))) } +func (ctx *Context) HandleText(status int, title string) { + if (status/100 == 4) || (status/100 == 5) { + log.Error(4, "%s", title) + } + ctx.RenderData(status, []byte(title)) +} + func (ctx *Context) HandleAPI(status int, obj interface{}) { var message string if err, ok := obj.(error); ok { diff --git a/modules/middleware/org.go b/modules/middleware/org.go index 0e544fe4..065e1b1e 100644 --- a/modules/middleware/org.go +++ b/modules/middleware/org.go @@ -34,7 +34,7 @@ func OrgAssignment(redirect bool, args ...bool) macaron.Handler { var err error ctx.Org.Organization, err = models.GetUserByName(orgName) if err != nil { - if err == models.ErrUserNotExist { + if models.IsErrUserNotExist(err) { ctx.Handle(404, "GetUserByName", err) } else if redirect { log.Error(4, "GetUserByName", err) @@ -47,6 +47,12 @@ func OrgAssignment(redirect bool, args ...bool) macaron.Handler { org := ctx.Org.Organization ctx.Data["Org"] = org + // Force redirection when username is actually a user. + if !org.IsOrganization() { + ctx.Redirect("/" + org.Name) + return + } + if ctx.IsSigned { ctx.Org.IsOwner = org.IsOwnedBy(ctx.User.Id) if ctx.Org.IsOwner { diff --git a/modules/middleware/repo.go b/modules/middleware/repo.go index a200d6d6..12164632 100644 --- a/modules/middleware/repo.go +++ b/modules/middleware/repo.go @@ -10,6 +10,8 @@ import ( "strings" "github.com/Unknwon/macaron" + "github.com/mcuadros/go-version" + "github.com/mssola/user_agent" "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/base" @@ -18,6 +20,11 @@ import ( "github.com/gogits/gogs/modules/setting" ) +const ( + FIREFOX_COPY_SUPPORT = "41.0" + CHROME_COPY_SUPPORT = "43.0.2356" +) + func ApiRepoAssignment() macaron.Handler { return func(ctx *Context) { userName := ctx.Params(":username") @@ -34,7 +41,7 @@ func ApiRepoAssignment() macaron.Handler { } else { u, err = models.GetUserByName(userName) if err != nil { - if err == models.ErrUserNotExist { + if models.IsErrUserNotExist(err) { ctx.Error(404) } else { ctx.JSON(500, &base.ApiJsonErr{"GetUserByName: " + err.Error(), base.DOC_URL}) @@ -210,7 +217,7 @@ func RepoAssignment(redirect bool, args ...bool) macaron.Handler { } else { u, err = models.GetUserByName(userName) if err != nil { - if err == models.ErrUserNotExist { + if models.IsErrUserNotExist(err) { ctx.Handle(404, "GetUserByName", err) } else { ctx.Handle(500, "GetUserByName", err) @@ -345,10 +352,17 @@ func RepoAssignment(redirect bool, args ...bool) macaron.Handler { ctx.Data["BranchName"] = ctx.Repo.BranchName ctx.Data["CommitId"] = ctx.Repo.CommitId + + userAgent := ctx.Req.Header.Get("User-Agent") + ua := user_agent.New(userAgent) + browserName, browserVer := ua.Browser() + + ctx.Data["BrowserSupportsCopy"] = (browserName == "Chrome" && version.Compare(browserVer, CHROME_COPY_SUPPORT, ">=")) || + (browserName == "Firefox" && version.Compare(browserVer, FIREFOX_COPY_SUPPORT, ">=")) } } -func RequireAdmin() macaron.Handler { +func RequireRepoAdmin() macaron.Handler { return func(ctx *Context) { if ctx.Repo.AccessMode < models.ACCESS_MODE_ADMIN { if !ctx.IsSigned { |