diff options
author | Unknown <joe2010xtmf@163.com> | 2014-04-11 21:47:39 -0400 |
---|---|---|
committer | Unknown <joe2010xtmf@163.com> | 2014-04-11 21:47:39 -0400 |
commit | 33aa4f74380ab117673a1cc30bead3a7f2b3cb4b (patch) | |
tree | ac6c7c1ebebb86260545386c7ce923593e4d5046 /modules | |
parent | d6dac160dfcac068b31bda9316ddc3d4919e3288 (diff) | |
download | gitea-33aa4f74380ab117673a1cc30bead3a7f2b3cb4b.tar.gz gitea-33aa4f74380ab117673a1cc30bead3a7f2b3cb4b.zip |
Support private repo
Diffstat (limited to 'modules')
-rw-r--r-- | modules/auth/repo.go | 2 | ||||
-rw-r--r-- | modules/middleware/context.go | 7 | ||||
-rw-r--r-- | modules/middleware/repo.go | 48 |
3 files changed, 50 insertions, 7 deletions
diff --git a/modules/auth/repo.go b/modules/auth/repo.go index eddd647528..1c740bed19 100644 --- a/modules/auth/repo.go +++ b/modules/auth/repo.go @@ -18,7 +18,7 @@ import ( type CreateRepoForm struct { RepoName string `form:"repo" binding:"Required;AlphaDash"` - Visibility string `form:"visibility"` + Private string `form:"private"` Description string `form:"desc" binding:"MaxSize(100)"` Language string `form:"language"` License string `form:"license"` diff --git a/modules/middleware/context.go b/modules/middleware/context.go index e7f962c3ed..edbf1f3ce8 100644 --- a/modules/middleware/context.go +++ b/modules/middleware/context.go @@ -49,6 +49,7 @@ type Context struct { IsBranch bool IsTag bool IsCommit bool + HasAccess bool Repository *models.Repository Owner *models.User Commit *git.Commit @@ -102,12 +103,10 @@ func (ctx *Context) RenderWithErr(msg, tpl string, form auth.Form) { // Handle handles and logs error by given status. func (ctx *Context) Handle(status int, title string, err error) { log.Error("%s: %v", title, err) - if martini.Dev == martini.Prod { - ctx.HTML(200, "status/500") - return + if martini.Dev != martini.Prod { + ctx.Data["ErrorMsg"] = err } - ctx.Data["ErrorMsg"] = err ctx.HTML(status, fmt.Sprintf("status/%d", status)) } diff --git a/modules/middleware/repo.go b/modules/middleware/repo.go index 258fc9d2ab..75d9f999c6 100644 --- a/modules/middleware/repo.go +++ b/modules/middleware/repo.go @@ -67,12 +67,14 @@ func RepoAssignment(redirect bool, args ...bool) martini.Handler { ctx.Handle(200, "RepoAssignment", errors.New("invliad user account for single repository")) return } + ctx.Repo.Owner = user // get repository repo, err := models.GetRepositoryByName(user.Id, repoName) if err != nil { if err == models.ErrRepoNotExist { ctx.Handle(404, "RepoAssignment", err) + return } else if redirect { ctx.Redirect("/") return @@ -80,6 +82,26 @@ func RepoAssignment(redirect bool, args ...bool) martini.Handler { ctx.Handle(500, "RepoAssignment", err) return } + + // Check access. + if repo.IsPrivate { + if ctx.User == nil { + ctx.Handle(404, "RepoAssignment(HasAccess)", nil) + return + } + + hasAccess, err := models.HasAccess(ctx.User.Name, ctx.Repo.Owner.Name+"/"+repo.Name, models.AU_READABLE) + if err != nil { + ctx.Handle(500, "RepoAssignment(HasAccess)", err) + return + } else if !hasAccess { + ctx.Handle(404, "RepoAssignment(HasAccess)", nil) + return + } + } + ctx.Repo.HasAccess = true + ctx.Data["HasAccess"] = true + repo.NumOpenIssues = repo.NumIssues - repo.NumClosedIssues ctx.Repo.Repository = repo @@ -91,8 +113,6 @@ func RepoAssignment(redirect bool, args ...bool) martini.Handler { return } ctx.Repo.GitRepo = gitRepo - - ctx.Repo.Owner = user ctx.Repo.RepoLink = "/" + user.Name + "/" + repo.Name ctx.Data["Title"] = user.Name + "/" + repo.Name @@ -170,3 +190,27 @@ func RepoAssignment(redirect bool, args ...bool) martini.Handler { ctx.Data["IsRepositoryWatching"] = ctx.Repo.IsWatching } } + +func WriteAccess() martini.Handler { + return func(ctx *Context) { + if ctx.Repo.Repository.IsPrivate { + ctx.Repo.HasAccess = false + ctx.Data["HasAccess"] = false + if ctx.User == nil { + ctx.Handle(404, "WriteAccess", nil) + return + } + + hasAccess, err := models.HasAccess(ctx.User.Name, ctx.Repo.Owner.Name+"/"+ctx.Repo.Repository.Name, models.AU_WRITABLE) + if err != nil { + ctx.Handle(500, "WriteAccess(HasAccess)", err) + return + } else if !hasAccess { + ctx.Handle(404, "WriteAccess(HasAccess)", nil) + return + } + } + ctx.Repo.HasAccess = true + ctx.Data["HasAccess"] = true + } +} |