mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-05-31 11:52:10 +00:00
Support private repo
This commit is contained in:
parent
d6dac160df
commit
33aa4f7438
13 changed files with 87 additions and 31 deletions
|
@ -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))
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue