summaryrefslogtreecommitdiffstats
path: root/modules/middleware
diff options
context:
space:
mode:
authorslene <vslene@gmail.com>2014-03-16 00:03:23 +0800
committerslene <vslene@gmail.com>2014-03-16 00:03:23 +0800
commit7ca830677525e6fc9127660fde32c46bcf00ab59 (patch)
tree3e4234b6f59a64783c173ff43dec239a263f97f7 /modules/middleware
parentf047df6e2b69ecb41c3fe4bef746145916aa4063 (diff)
downloadgitea-7ca830677525e6fc9127660fde32c46bcf00ab59.tar.gz
gitea-7ca830677525e6fc9127660fde32c46bcf00ab59.zip
modify RepoAssignment
Diffstat (limited to 'modules/middleware')
-rw-r--r--modules/middleware/context.go6
-rw-r--r--modules/middleware/repo.go76
2 files changed, 82 insertions, 0 deletions
diff --git a/modules/middleware/context.go b/modules/middleware/context.go
index d3fe7bbfbe..42ef1320cb 100644
--- a/modules/middleware/context.go
+++ b/modules/middleware/context.go
@@ -29,6 +29,12 @@ type Context struct {
Render render.Render
User *models.User
IsSigned bool
+
+ Repo struct {
+ IsValid bool
+ IsOwner bool
+ Repository *models.Repository
+ }
}
// Query querys form parameter.
diff --git a/modules/middleware/repo.go b/modules/middleware/repo.go
new file mode 100644
index 0000000000..fcf1f07b98
--- /dev/null
+++ b/modules/middleware/repo.go
@@ -0,0 +1,76 @@
+// 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 (
+ "github.com/codegangsta/martini"
+
+ "github.com/gogits/gogs/models"
+)
+
+func RepoAssignment(redirect bool) martini.Handler {
+ return func(ctx *Context, params martini.Params) {
+ // assign false first
+ ctx.Data["IsRepositoryValid"] = false
+
+ var (
+ user *models.User
+ err error
+ )
+
+ // get repository owner
+ ctx.Repo.IsOwner = ctx.IsSigned && ctx.User.LowerName == params["username"]
+ ctx.Data["IsRepositoryOwner"] = ctx.Repo.IsOwner
+
+ if !ctx.Repo.IsOwner {
+ user, err = models.GetUserByName(params["username"])
+ if err != nil {
+ if redirect {
+ ctx.Render.Redirect("/")
+ return
+ }
+ //data["ErrorMsg"] = err
+ //log.Error("repo.Single: %v", err)
+ //r.HTML(200, "base/error", data)
+ return
+ }
+ } else {
+ user = ctx.User
+ }
+
+ if user == nil {
+ if redirect {
+ ctx.Render.Redirect("/")
+ return
+ }
+ //data["ErrorMsg"] = "invliad user account for single repository"
+ //log.Error("repo.Single: %v", err)
+ //r.HTML(200, "base/error", data)
+ return
+ }
+
+ // get repository
+ repo, err := models.GetRepositoryByName(user, params["reponame"])
+ if err != nil {
+ if redirect {
+ ctx.Render.Redirect("/")
+ return
+ }
+ //data["ErrorMsg"] = err
+ //log.Error("repo.Single: %v", err)
+ //r.HTML(200, "base/error", data)
+ return
+ }
+
+ ctx.Repo.IsValid = true
+ ctx.Repo.Repository = repo
+
+ ctx.Data["IsRepositoryValid"] = true
+ ctx.Data["Repository"] = repo
+ ctx.Data["Owner"] = user
+ ctx.Data["Title"] = user.Name + "/" + repo.Name
+ ctx.Data["RepositoryLink"] = ctx.Data["Title"]
+ }
+}