From 7ca830677525e6fc9127660fde32c46bcf00ab59 Mon Sep 17 00:00:00 2001 From: slene Date: Sun, 16 Mar 2014 00:03:23 +0800 Subject: [PATCH] modify RepoAssignment --- modules/auth/repo.go | 62 ---------------------------- modules/middleware/context.go | 6 +++ modules/middleware/repo.go | 76 +++++++++++++++++++++++++++++++++++ routers/repo/single.go | 13 ++++-- web.go | 8 ++-- 5 files changed, 96 insertions(+), 69 deletions(-) create mode 100644 modules/middleware/repo.go diff --git a/modules/auth/repo.go b/modules/auth/repo.go index 5fe091d62f..ac1b6b699b 100644 --- a/modules/auth/repo.go +++ b/modules/auth/repo.go @@ -12,11 +12,8 @@ import ( "github.com/gogits/binding" - "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/log" - "github.com/martini-contrib/render" - "github.com/martini-contrib/sessions" ) type CreateRepoForm struct { @@ -61,62 +58,3 @@ type DeleteRepoForm struct { UserName string `form:"userName" binding:"Required"` RepoId int64 `form:"repoId" binding:"Required"` } - -func RepoAssignment(redirect bool) martini.Handler { - return func(params martini.Params, r render.Render, data base.TmplData, session sessions.Session) { - // assign false first - data["IsRepositoryValid"] = false - - var ( - user *models.User - err error - ) - // get repository owner - isOwner := (data["SignedUserName"] == params["username"]) - if !isOwner { - user, err = models.GetUserByName(params["username"]) - if err != nil { - if redirect { - r.Redirect("/") - return - } - //data["ErrorMsg"] = err - //log.Error("repo.Single: %v", err) - //r.HTML(200, "base/error", data) - return - } - } else { - user = SignedInUser(session) - } - if user == nil { - if redirect { - r.Redirect("/") - return - } - //data["ErrorMsg"] = "invliad user account for single repository" - //log.Error("repo.Single: %v", err) - //r.HTML(200, "base/error", data) - return - } - data["IsRepositoryOwner"] = isOwner - - // get repository - repo, err := models.GetRepositoryByName(user, params["reponame"]) - if err != nil { - if redirect { - r.Redirect("/") - return - } - //data["ErrorMsg"] = err - //log.Error("repo.Single: %v", err) - //r.HTML(200, "base/error", data) - return - } - - data["Repository"] = repo - data["Owner"] = user - data["Title"] = user.Name + "/" + repo.Name - data["RepositoryLink"] = data["Title"] - data["IsRepositoryValid"] = true - } -} 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"] + } +} diff --git a/routers/repo/single.go b/routers/repo/single.go index e933951050..bd5cfa1196 100644 --- a/routers/repo/single.go +++ b/routers/repo/single.go @@ -10,12 +10,14 @@ import ( ) func Single(ctx *middleware.Context, params martini.Params) { - if !ctx.Data["IsRepositoryValid"].(bool) { + if !ctx.Repo.IsValid { return } + if params["branchname"] == "" { params["branchname"] = "master" } + treename := params["_1"] files, err := models.GetReposFiles(params["username"], params["reponame"], params["branchname"], treename) @@ -46,11 +48,16 @@ func Single(ctx *middleware.Context, params martini.Params) { } func Setting(ctx *middleware.Context) { - if !ctx.Data["IsRepositoryValid"].(bool) { + if !ctx.Repo.IsValid { return } - ctx.Data["Title"] = ctx.Data["Title"].(string) + " - settings" + var title string + if t, ok := ctx.Data["Title"].(string); ok { + title = t + } + + ctx.Data["Title"] = title + " - settings" ctx.Data["IsRepoToolbarSetting"] = true ctx.Render.HTML(200, "repo/setting", ctx.Data) } diff --git a/web.go b/web.go index ad19a5da01..af430917f5 100644 --- a/web.go +++ b/web.go @@ -68,12 +68,12 @@ func runWeb(*cli.Context) { m.Any("/repo/delete", middleware.SignInRequire(true), binding.Bind(auth.DeleteRepoForm{}), repo.Delete) m.Any("/repo/list", middleware.SignInRequire(false), repo.List) - m.Get("/:username/:reponame/settings", middleware.SignInRequire(false), auth.RepoAssignment(true), repo.Setting) + m.Get("/:username/:reponame/settings", middleware.SignInRequire(false), middleware.RepoAssignment(true), repo.Setting) m.Get("/:username/:reponame/tree/:branchname/**", - middleware.SignInRequire(false), auth.RepoAssignment(true), repo.Single) + middleware.SignInRequire(false), middleware.RepoAssignment(true), repo.Single) m.Get("/:username/:reponame/tree/:branchname", - middleware.SignInRequire(false), auth.RepoAssignment(true), repo.Single) - m.Get("/:username/:reponame", middleware.SignInRequire(false), auth.RepoAssignment(true), repo.Single) + middleware.SignInRequire(false), middleware.RepoAssignment(true), repo.Single) + m.Get("/:username/:reponame", middleware.SignInRequire(false), middleware.RepoAssignment(true), repo.Single) //m.Get("/:username/:reponame", repo.Repo)