modify RepoAssignment

release/v1.15
slene 2014-03-16 00:03:23 +08:00
parent f047df6e2b
commit 7ca8306775
5 changed files with 96 additions and 69 deletions

View File

@ -12,11 +12,8 @@ import (
"github.com/gogits/binding" "github.com/gogits/binding"
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/log"
"github.com/martini-contrib/render"
"github.com/martini-contrib/sessions"
) )
type CreateRepoForm struct { type CreateRepoForm struct {
@ -61,62 +58,3 @@ type DeleteRepoForm struct {
UserName string `form:"userName" binding:"Required"` UserName string `form:"userName" binding:"Required"`
RepoId int64 `form:"repoId" 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
}
}

View File

@ -29,6 +29,12 @@ type Context struct {
Render render.Render Render render.Render
User *models.User User *models.User
IsSigned bool IsSigned bool
Repo struct {
IsValid bool
IsOwner bool
Repository *models.Repository
}
} }
// Query querys form parameter. // Query querys form parameter.

View File

@ -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"]
}
}

View File

@ -10,12 +10,14 @@ import (
) )
func Single(ctx *middleware.Context, params martini.Params) { func Single(ctx *middleware.Context, params martini.Params) {
if !ctx.Data["IsRepositoryValid"].(bool) { if !ctx.Repo.IsValid {
return return
} }
if params["branchname"] == "" { if params["branchname"] == "" {
params["branchname"] = "master" params["branchname"] = "master"
} }
treename := params["_1"] treename := params["_1"]
files, err := models.GetReposFiles(params["username"], params["reponame"], files, err := models.GetReposFiles(params["username"], params["reponame"],
params["branchname"], treename) params["branchname"], treename)
@ -46,11 +48,16 @@ func Single(ctx *middleware.Context, params martini.Params) {
} }
func Setting(ctx *middleware.Context) { func Setting(ctx *middleware.Context) {
if !ctx.Data["IsRepositoryValid"].(bool) { if !ctx.Repo.IsValid {
return 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.Data["IsRepoToolbarSetting"] = true
ctx.Render.HTML(200, "repo/setting", ctx.Data) ctx.Render.HTML(200, "repo/setting", ctx.Data)
} }

8
web.go
View File

@ -68,12 +68,12 @@ func runWeb(*cli.Context) {
m.Any("/repo/delete", middleware.SignInRequire(true), binding.Bind(auth.DeleteRepoForm{}), repo.Delete) m.Any("/repo/delete", middleware.SignInRequire(true), binding.Bind(auth.DeleteRepoForm{}), repo.Delete)
m.Any("/repo/list", middleware.SignInRequire(false), repo.List) 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/**", 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", 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", middleware.SignInRequire(false), auth.RepoAssignment(true), repo.Single) m.Get("/:username/:reponame", middleware.SignInRequire(false), middleware.RepoAssignment(true), repo.Single)
//m.Get("/:username/:reponame", repo.Repo) //m.Get("/:username/:reponame", repo.Repo)