You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
2.8 KiB

// Copyright 2014 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package middleware
import (
11 years ago
"fmt"
"net/http"
11 years ago
"time"
"github.com/codegangsta/martini"
"github.com/martini-contrib/sessions"
11 years ago
"github.com/gogits/cache"
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/auth"
11 years ago
"github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/log"
)
11 years ago
// Context represents context of a request.
type Context struct {
11 years ago
*Render
c martini.Context
p martini.Params
Req *http.Request
Res http.ResponseWriter
Session sessions.Session
11 years ago
Cache cache.Cache
User *models.User
IsSigned bool
Repo struct {
IsValid bool
IsOwner bool
IsWatching bool
Repository *models.Repository
Owner *models.User
11 years ago
CloneLink struct {
SSH string
HTTPS string
Git string
}
}
}
11 years ago
// Query querys form parameter.
func (ctx *Context) Query(name string) string {
ctx.Req.ParseForm()
return ctx.Req.Form.Get(name)
}
// func (ctx *Context) Param(name string) string {
// return ctx.p[name]
// }
11 years ago
// HasError returns true if error occurs in form validation.
func (ctx *Context) HasError() bool {
hasErr, ok := ctx.Data["HasError"]
if !ok {
return false
}
return hasErr.(bool)
}
11 years ago
// 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...)
}
11 years ago
// 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)
11 years ago
ctx.HTML(200, tpl)
11 years ago
}
11 years ago
// Handle handles and logs error by given status.
func (ctx *Context) Handle(status int, title string, err error) {
log.Error("%s: %v", title, err)
11 years ago
if martini.Dev == martini.Prod {
11 years ago
ctx.HTML(500, "status/500")
11 years ago
return
}
ctx.Data["ErrorMsg"] = err
11 years ago
ctx.HTML(status, fmt.Sprintf("status/%d", status))
}
11 years ago
// InitContext initializes a classic context for a request.
func InitContext() martini.Handler {
return func(res http.ResponseWriter, r *http.Request, c martini.Context,
11 years ago
session sessions.Session, rd *Render) {
ctx := &Context{
c: c,
// p: p,
11 years ago
Req: r,
Res: res,
Session: session,
11 years ago
Cache: base.Cache,
11 years ago
Render: rd,
}
// Get user from session if logined.
user := auth.SignedInUser(session)
ctx.User = user
11 years ago
ctx.IsSigned = user != nil
11 years ago
ctx.Data["IsSigned"] = ctx.IsSigned
11 years ago
if user != nil {
11 years ago
ctx.Data["SignedUser"] = user
ctx.Data["SignedUserId"] = user.Id
ctx.Data["SignedUserName"] = user.LowerName
11 years ago
ctx.Data["IsAdmin"] = ctx.User.IsAdmin
11 years ago
}
11 years ago
ctx.Data["PageStartTime"] = time.Now()
c.Map(ctx)
c.Next()
}
}