You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
1.7 KiB

  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package middleware
  5. import (
  6. "github.com/codegangsta/martini"
  7. "github.com/gogits/gogs/models"
  8. )
  9. func RepoAssignment(redirect bool) martini.Handler {
  10. return func(ctx *Context, params martini.Params) {
  11. // assign false first
  12. ctx.Data["IsRepositoryValid"] = false
  13. var (
  14. user *models.User
  15. err error
  16. )
  17. // get repository owner
  18. ctx.Repo.IsOwner = ctx.IsSigned && ctx.User.LowerName == params["username"]
  19. ctx.Data["IsRepositoryOwner"] = ctx.Repo.IsOwner
  20. if !ctx.Repo.IsOwner {
  21. user, err = models.GetUserByName(params["username"])
  22. if err != nil {
  23. if redirect {
  24. ctx.Render.Redirect("/")
  25. return
  26. }
  27. //data["ErrorMsg"] = err
  28. //log.Error("repo.Single: %v", err)
  29. //r.HTML(200, "base/error", data)
  30. return
  31. }
  32. } else {
  33. user = ctx.User
  34. }
  35. if user == nil {
  36. if redirect {
  37. ctx.Render.Redirect("/")
  38. return
  39. }
  40. //data["ErrorMsg"] = "invliad user account for single repository"
  41. //log.Error("repo.Single: %v", err)
  42. //r.HTML(200, "base/error", data)
  43. return
  44. }
  45. ctx.Repo.Owner = user
  46. // get repository
  47. repo, err := models.GetRepositoryByName(user, params["reponame"])
  48. if err != nil {
  49. if redirect {
  50. ctx.Render.Redirect("/")
  51. return
  52. }
  53. //data["ErrorMsg"] = err
  54. //log.Error("repo.Single: %v", err)
  55. //r.HTML(200, "base/error", data)
  56. return
  57. }
  58. ctx.Repo.IsValid = true
  59. ctx.Repo.Repository = repo
  60. ctx.Data["IsRepositoryValid"] = true
  61. ctx.Data["Repository"] = repo
  62. ctx.Data["Owner"] = user
  63. ctx.Data["Title"] = user.Name + "/" + repo.Name
  64. ctx.Data["RepositoryLink"] = ctx.Data["Title"]
  65. }
  66. }