aboutsummaryrefslogtreecommitdiff
path: root/modules/middleware
diff options
context:
space:
mode:
Diffstat (limited to 'modules/middleware')
-rw-r--r--modules/middleware/auth.go16
-rw-r--r--modules/middleware/context.go17
-rw-r--r--modules/middleware/repo.go2
3 files changed, 30 insertions, 5 deletions
diff --git a/modules/middleware/auth.go b/modules/middleware/auth.go
index d45a21e9..f211de32 100644
--- a/modules/middleware/auth.go
+++ b/modules/middleware/auth.go
@@ -15,12 +15,12 @@ func SignInRequire(redirect bool) martini.Handler {
return func(ctx *Context) {
if !ctx.IsSigned {
if redirect {
- ctx.Redirect("/")
+ ctx.Redirect("/user/login")
}
return
} else if !ctx.User.IsActive && base.Service.RegisterEmailConfirm {
ctx.Data["Title"] = "Activate Your Account"
- ctx.Render.HTML(200, "user/active", ctx.Data)
+ ctx.HTML(200, "user/active")
return
}
}
@@ -31,6 +31,18 @@ func SignOutRequire() martini.Handler {
return func(ctx *Context) {
if ctx.IsSigned {
ctx.Redirect("/")
+ return
+ }
+ }
+}
+
+// AdminRequire requires user signed in as administor.
+func AdminRequire() martini.Handler {
+ return func(ctx *Context) {
+ if !ctx.User.IsAdmin {
+ ctx.Error(403)
+ return
}
+ ctx.Data["PageIsAdmin"] = true
}
}
diff --git a/modules/middleware/context.go b/modules/middleware/context.go
index 6ac87de3..a25a3dbb 100644
--- a/modules/middleware/context.go
+++ b/modules/middleware/context.go
@@ -12,8 +12,11 @@ import (
"github.com/codegangsta/martini"
"github.com/martini-contrib/sessions"
+ "github.com/gogits/cache"
+
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/auth"
+ "github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/log"
)
@@ -25,6 +28,7 @@ type Context struct {
Req *http.Request
Res http.ResponseWriter
Session sessions.Session
+ Cache cache.Cache
User *models.User
IsSigned bool
@@ -61,24 +65,29 @@ func (ctx *Context) HasError() bool {
return hasErr.(bool)
}
+// HTML calls render.HTML underlying but reduce one argument.
+func (ctx *Context) HTML(status int, name string, htmlOpt ...HTMLOptions) {
+ ctx.Render.HTML(status, name, ctx.Data, htmlOpt...)
+}
+
// RenderWithErr used for page has form validation but need to prompt error to users.
func (ctx *Context) RenderWithErr(msg, tpl string, form auth.Form) {
ctx.Data["HasError"] = true
ctx.Data["ErrorMsg"] = msg
auth.AssignForm(form, ctx.Data)
- ctx.HTML(200, tpl, ctx.Data)
+ ctx.HTML(200, tpl)
}
// Handle handles and logs error by given status.
func (ctx *Context) Handle(status int, title string, err error) {
log.Error("%s: %v", title, err)
if martini.Dev == martini.Prod {
- ctx.HTML(500, "status/500", ctx.Data)
+ ctx.HTML(500, "status/500")
return
}
ctx.Data["ErrorMsg"] = err
- ctx.HTML(status, fmt.Sprintf("status/%d", status), ctx.Data)
+ ctx.HTML(status, fmt.Sprintf("status/%d", status))
}
// InitContext initializes a classic context for a request.
@@ -92,6 +101,7 @@ func InitContext() martini.Handler {
Req: r,
Res: res,
Session: session,
+ Cache: base.Cache,
Render: rd,
}
@@ -106,6 +116,7 @@ func InitContext() martini.Handler {
ctx.Data["SignedUser"] = user
ctx.Data["SignedUserId"] = user.Id
ctx.Data["SignedUserName"] = user.LowerName
+ ctx.Data["IsAdmin"] = ctx.User.IsAdmin
}
ctx.Data["PageStartTime"] = time.Now()
diff --git a/modules/middleware/repo.go b/modules/middleware/repo.go
index b25c9423..a9a90e3f 100644
--- a/modules/middleware/repo.go
+++ b/modules/middleware/repo.go
@@ -70,6 +70,7 @@ func RepoAssignment(redirect bool) martini.Handler {
}
ctx.Repo.Repository = repo
ctx.Repo.CloneLink.SSH = fmt.Sprintf("git@%s:%s/%s.git", base.Domain, user.LowerName, repo.LowerName)
+ ctx.Repo.CloneLink.HTTPS = fmt.Sprintf("https://%s/%s/%s.git", base.Domain, user.LowerName, repo.LowerName)
ctx.Data["IsRepositoryValid"] = true
ctx.Data["Repository"] = repo
@@ -78,5 +79,6 @@ func RepoAssignment(redirect bool) martini.Handler {
ctx.Data["CloneLink"] = ctx.Repo.CloneLink
ctx.Data["RepositoryLink"] = ctx.Data["Title"]
ctx.Data["IsRepositoryOwner"] = ctx.Repo.IsOwner
+ ctx.Data["IsRepositoryWatching"] = ctx.Repo.IsWatching
}
}