repoAssignment extracts information from URL parameters to retrieve the repository, and makes sure the context user has at least the read access to the repository.
()
| 22 | // repoAssignment extracts information from URL parameters to retrieve the repository, |
| 23 | // and makes sure the context user has at least the read access to the repository. |
| 24 | func repoAssignment() macaron.Handler { |
| 25 | return func(c *context.APIContext) { |
| 26 | username := c.Params(":username") |
| 27 | reponame := c.Params(":reponame") |
| 28 | |
| 29 | var err error |
| 30 | var owner *database.User |
| 31 | |
| 32 | // Check if the context user is the repository owner. |
| 33 | if c.IsLogged && c.User.LowerName == strings.ToLower(username) { |
| 34 | owner = c.User |
| 35 | } else { |
| 36 | owner, err = database.Handle.Users().GetByUsername(c.Req.Context(), username) |
| 37 | if err != nil { |
| 38 | c.NotFoundOrError(err, "get user by name") |
| 39 | return |
| 40 | } |
| 41 | } |
| 42 | c.Repo.Owner = owner |
| 43 | |
| 44 | repo, err := database.Handle.Repositories().GetByName(c.Req.Context(), owner.ID, reponame) |
| 45 | if err != nil { |
| 46 | c.NotFoundOrError(err, "get repository by name") |
| 47 | return |
| 48 | } else if err = repo.GetOwner(); err != nil { |
| 49 | c.Error(err, "get owner") |
| 50 | return |
| 51 | } |
| 52 | |
| 53 | if c.IsTokenAuth && c.User.IsAdmin { |
| 54 | c.Repo.AccessMode = database.AccessModeOwner |
| 55 | } else { |
| 56 | c.Repo.AccessMode = database.Handle.Permissions().AccessMode(c.Req.Context(), c.UserID(), repo.ID, |
| 57 | database.AccessModeOptions{ |
| 58 | OwnerID: repo.OwnerID, |
| 59 | Private: repo.IsPrivate, |
| 60 | }, |
| 61 | ) |
| 62 | } |
| 63 | |
| 64 | if !c.Repo.HasAccess() { |
| 65 | c.NotFound() |
| 66 | return |
| 67 | } |
| 68 | |
| 69 | c.Repo.Repository = repo |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // orgAssignment extracts information from URL parameters to retrieve the organization or team. |
| 74 | func orgAssignment(args ...bool) macaron.Handler { |
no test coverage detected