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.
267 lines
7.1 KiB
267 lines
7.1 KiB
11 years ago
|
// 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.
|
||
|
|
||
9 years ago
|
package context
|
||
11 years ago
|
|
||
|
import (
|
||
11 years ago
|
"fmt"
|
||
11 years ago
|
"html/template"
|
||
11 years ago
|
"io"
|
||
11 years ago
|
"net/http"
|
||
11 years ago
|
"strings"
|
||
11 years ago
|
"time"
|
||
11 years ago
|
|
||
9 years ago
|
"github.com/go-macaron/cache"
|
||
|
"github.com/go-macaron/csrf"
|
||
|
"github.com/go-macaron/i18n"
|
||
|
"github.com/go-macaron/session"
|
||
|
"gopkg.in/macaron.v1"
|
||
11 years ago
|
|
||
9 years ago
|
"github.com/gogits/git-module"
|
||
9 years ago
|
|
||
11 years ago
|
"github.com/gogits/gogs/models"
|
||
|
"github.com/gogits/gogs/modules/auth"
|
||
11 years ago
|
"github.com/gogits/gogs/modules/base"
|
||
11 years ago
|
"github.com/gogits/gogs/modules/log"
|
||
11 years ago
|
"github.com/gogits/gogs/modules/setting"
|
||
11 years ago
|
)
|
||
|
|
||
9 years ago
|
type PullRequest struct {
|
||
9 years ago
|
BaseRepo *models.Repository
|
||
|
Allowed bool
|
||
|
SameRepo bool
|
||
|
HeadInfo string // [<user>:]<branch>
|
||
|
}
|
||
|
|
||
9 years ago
|
type Repository struct {
|
||
9 years ago
|
AccessMode models.AccessMode
|
||
|
IsWatching bool
|
||
9 years ago
|
IsViewBranch bool
|
||
|
IsViewTag bool
|
||
|
IsViewCommit bool
|
||
9 years ago
|
Repository *models.Repository
|
||
|
Owner *models.User
|
||
|
Commit *git.Commit
|
||
|
Tag *git.Tag
|
||
|
GitRepo *git.Repository
|
||
|
BranchName string
|
||
|
TagName string
|
||
|
TreeName string
|
||
9 years ago
|
CommitID string
|
||
9 years ago
|
RepoLink string
|
||
|
CloneLink models.CloneLink
|
||
9 years ago
|
CommitsCount int64
|
||
9 years ago
|
Mirror *models.Mirror
|
||
9 years ago
|
|
||
9 years ago
|
PullRequest *PullRequest
|
||
|
}
|
||
|
|
||
|
// IsOwner returns true if current user is the owner of repository.
|
||
|
func (r *Repository) IsOwner() bool {
|
||
|
return r.AccessMode >= models.ACCESS_MODE_OWNER
|
||
|
}
|
||
|
|
||
|
// IsAdmin returns true if current user has admin or higher access of repository.
|
||
|
func (r *Repository) IsAdmin() bool {
|
||
|
return r.AccessMode >= models.ACCESS_MODE_ADMIN
|
||
|
}
|
||
|
|
||
|
// IsWriter returns true if current user has write or higher access of repository.
|
||
|
func (r *Repository) IsWriter() bool {
|
||
|
return r.AccessMode >= models.ACCESS_MODE_WRITE
|
||
|
}
|
||
|
|
||
|
// HasAccess returns true if the current user has at least read access for this repository
|
||
|
func (r *Repository) HasAccess() bool {
|
||
|
return r.AccessMode >= models.ACCESS_MODE_READ
|
||
9 years ago
|
}
|
||
|
|
||
11 years ago
|
// Context represents context of a request.
|
||
11 years ago
|
type Context struct {
|
||
10 years ago
|
*macaron.Context
|
||
10 years ago
|
Cache cache.Cache
|
||
|
csrf csrf.CSRF
|
||
10 years ago
|
Flash *session.Flash
|
||
|
Session session.Store
|
||
|
|
||
10 years ago
|
User *models.User
|
||
|
IsSigned bool
|
||
|
IsBasicAuth bool
|
||
11 years ago
|
|
||
9 years ago
|
Repo *Repository
|
||
10 years ago
|
|
||
|
Org struct {
|
||
|
IsOwner bool
|
||
|
IsMember bool
|
||
9 years ago
|
IsTeamMember bool // Is member of team.
|
||
9 years ago
|
IsTeamAdmin bool // In owner team or team that has admin permission level.
|
||
10 years ago
|
Organization *models.User
|
||
10 years ago
|
OrgLink string
|
||
10 years ago
|
|
||
|
Team *models.Team
|
||
10 years ago
|
}
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
// HasError returns true if error occurs in form validation.
|
||
|
func (ctx *Context) HasApiError() bool {
|
||
|
hasErr, ok := ctx.Data["HasError"]
|
||
|
if !ok {
|
||
|
return false
|
||
|
}
|
||
|
return hasErr.(bool)
|
||
|
}
|
||
|
|
||
|
func (ctx *Context) GetErrMsg() string {
|
||
|
return ctx.Data["ErrorMsg"].(string)
|
||
|
}
|
||
|
|
||
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
|
||
|
}
|
||
11 years ago
|
ctx.Flash.ErrorMsg = ctx.Data["ErrorMsg"].(string)
|
||
|
ctx.Data["Flash"] = ctx.Flash
|
||
11 years ago
|
return hasErr.(bool)
|
||
|
}
|
||
|
|
||
9 years ago
|
// HasValue returns true if value of given name exists.
|
||
|
func (ctx *Context) HasValue(name string) bool {
|
||
|
_, ok := ctx.Data[name]
|
||
|
return ok
|
||
|
}
|
||
|
|
||
10 years ago
|
// HTML calls Context.HTML and converts template name to string.
|
||
10 years ago
|
func (ctx *Context) HTML(status int, name base.TplName) {
|
||
9 years ago
|
log.Debug("Template: %s", name)
|
||
10 years ago
|
ctx.Context.HTML(status, string(name))
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
// RenderWithErr used for page has form validation but need to prompt error to users.
|
||
10 years ago
|
func (ctx *Context) RenderWithErr(msg string, tpl base.TplName, form interface{}) {
|
||
11 years ago
|
if form != nil {
|
||
|
auth.AssignForm(form, ctx.Data)
|
||
|
}
|
||
11 years ago
|
ctx.Flash.ErrorMsg = msg
|
||
|
ctx.Data["Flash"] = ctx.Flash
|
||
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) {
|
||
11 years ago
|
if err != nil {
|
||
10 years ago
|
log.Error(4, "%s: %v", title, err)
|
||
|
if macaron.Env != macaron.PROD {
|
||
11 years ago
|
ctx.Data["ErrorMsg"] = err
|
||
|
}
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
switch status {
|
||
|
case 404:
|
||
|
ctx.Data["Title"] = "Page Not Found"
|
||
|
case 500:
|
||
|
ctx.Data["Title"] = "Internal Server Error"
|
||
|
}
|
||
11 years ago
|
ctx.HTML(status, base.TplName(fmt.Sprintf("status/%d", status)))
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
func (ctx *Context) HandleText(status int, title string) {
|
||
9 years ago
|
if (status/100 == 4) || (status/100 == 5) {
|
||
10 years ago
|
log.Error(4, "%s", title)
|
||
|
}
|
||
9 years ago
|
ctx.PlainText(status, []byte(title))
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
// APIError logs error with title if status is 500.
|
||
|
func (ctx *Context) APIError(status int, title string, obj interface{}) {
|
||
10 years ago
|
var message string
|
||
|
if err, ok := obj.(error); ok {
|
||
|
message = err.Error()
|
||
|
} else {
|
||
|
message = obj.(string)
|
||
|
}
|
||
9 years ago
|
|
||
|
if status == 500 {
|
||
|
log.Error(4, "%s: %s", title, message)
|
||
|
}
|
||
|
|
||
10 years ago
|
ctx.JSON(status, map[string]string{
|
||
|
"message": message,
|
||
9 years ago
|
"url": base.DOC_URL,
|
||
10 years ago
|
})
|
||
|
}
|
||
|
|
||
11 years ago
|
func (ctx *Context) ServeContent(name string, r io.ReadSeeker, params ...interface{}) {
|
||
|
modtime := time.Now()
|
||
|
for _, p := range params {
|
||
|
switch v := p.(type) {
|
||
|
case time.Time:
|
||
|
modtime = v
|
||
|
}
|
||
|
}
|
||
10 years ago
|
ctx.Resp.Header().Set("Content-Description", "File Transfer")
|
||
|
ctx.Resp.Header().Set("Content-Type", "application/octet-stream")
|
||
|
ctx.Resp.Header().Set("Content-Disposition", "attachment; filename="+name)
|
||
|
ctx.Resp.Header().Set("Content-Transfer-Encoding", "binary")
|
||
|
ctx.Resp.Header().Set("Expires", "0")
|
||
|
ctx.Resp.Header().Set("Cache-Control", "must-revalidate")
|
||
|
ctx.Resp.Header().Set("Pragma", "public")
|
||
10 years ago
|
http.ServeContent(ctx.Resp, ctx.Req.Request, name, modtime, r)
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
// Contexter initializes a classic context for a request.
|
||
|
func Contexter() macaron.Handler {
|
||
10 years ago
|
return func(c *macaron.Context, l i18n.Locale, cache cache.Cache, sess session.Store, f *session.Flash, x csrf.CSRF) {
|
||
11 years ago
|
ctx := &Context{
|
||
10 years ago
|
Context: c,
|
||
10 years ago
|
Cache: cache,
|
||
|
csrf: x,
|
||
10 years ago
|
Flash: f,
|
||
|
Session: sess,
|
||
9 years ago
|
Repo: &Repository{
|
||
|
PullRequest: &PullRequest{},
|
||
9 years ago
|
},
|
||
11 years ago
|
}
|
||
10 years ago
|
// Compute current URL for real-time change language.
|
||
9 years ago
|
ctx.Data["Link"] = setting.AppSubUrl + strings.TrimSuffix(ctx.Req.URL.Path, "/")
|
||
11 years ago
|
|
||
10 years ago
|
ctx.Data["PageStartTime"] = time.Now()
|
||
11 years ago
|
|
||
11 years ago
|
// Get user from session if logined.
|
||
9 years ago
|
ctx.User, ctx.IsBasicAuth = auth.SignedInUser(ctx.Context, ctx.Session)
|
||
10 years ago
|
|
||
10 years ago
|
if ctx.User != nil {
|
||
|
ctx.IsSigned = true
|
||
|
ctx.Data["IsSigned"] = ctx.IsSigned
|
||
|
ctx.Data["SignedUser"] = ctx.User
|
||
9 years ago
|
ctx.Data["SignedUserID"] = ctx.User.Id
|
||
10 years ago
|
ctx.Data["SignedUserName"] = ctx.User.Name
|
||
11 years ago
|
ctx.Data["IsAdmin"] = ctx.User.IsAdmin
|
||
10 years ago
|
} else {
|
||
9 years ago
|
ctx.Data["SignedUserID"] = 0
|
||
10 years ago
|
ctx.Data["SignedUserName"] = ""
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
10 years ago
|
// If request sends files, parse them here otherwise the Query() can't be parsed and the CsrfToken will be invalid.
|
||
10 years ago
|
if ctx.Req.Method == "POST" && strings.Contains(ctx.Req.Header.Get("Content-Type"), "multipart/form-data") {
|
||
|
if err := ctx.Req.ParseMultipartForm(setting.AttachmentMaxSize << 20); err != nil && !strings.Contains(err.Error(), "EOF") { // 32MB max size
|
||
|
ctx.Handle(500, "ParseMultipartForm", err)
|
||
10 years ago
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
ctx.Data["CsrfToken"] = x.GetToken()
|
||
|
ctx.Data["CsrfTokenHtml"] = template.HTML(`<input type="hidden" name="_csrf" value="` + x.GetToken() + `">`)
|
||
9 years ago
|
log.Debug("Session ID: %s", sess.ID())
|
||
|
log.Debug("CSRF Token: %v", ctx.Data["CsrfToken"])
|
||
11 years ago
|
|
||
10 years ago
|
ctx.Data["ShowRegistrationButton"] = setting.Service.ShowRegistrationButton
|
||
10 years ago
|
ctx.Data["ShowFooterBranding"] = setting.ShowFooterBranding
|
||
9 years ago
|
ctx.Data["ShowFooterVersion"] = setting.ShowFooterVersion
|
||
10 years ago
|
|
||
11 years ago
|
c.Map(ctx)
|
||
|
}
|
||
|
}
|