Skip to content

Commit 6f4ba68

Browse files
ethantkoeniglunny
authored andcommitted
Repo permission bug fixes (#513)
1 parent ac51caa commit 6f4ba68

File tree

3 files changed

+7
-35
lines changed

3 files changed

+7
-35
lines changed

modules/context/api.go

-30
Original file line numberDiff line numberDiff line change
@@ -75,36 +75,6 @@ func APIContexter() macaron.Handler {
7575
}
7676
}
7777

78-
// ExtractOwnerAndRepo returns a handler that populates the `Repo.Owner` and
79-
// `Repo.Repository` fields of an APIContext
80-
func ExtractOwnerAndRepo() macaron.Handler {
81-
return func(ctx *APIContext) {
82-
owner, err := models.GetUserByName(ctx.Params(":username"))
83-
if err != nil {
84-
if models.IsErrUserNotExist(err) {
85-
ctx.Error(422, "", err)
86-
} else {
87-
ctx.Error(500, "GetUserByName", err)
88-
}
89-
return
90-
}
91-
92-
repo, err := models.GetRepositoryByName(owner.ID, ctx.Params(":reponame"))
93-
if err != nil {
94-
if models.IsErrRepoNotExist(err) {
95-
ctx.Status(404)
96-
} else {
97-
ctx.Error(500, "GetRepositoryByName", err)
98-
}
99-
return
100-
}
101-
ctx.Repo.Owner = owner
102-
ctx.Data["Owner"] = owner
103-
ctx.Repo.Repository = repo
104-
ctx.Data["Repository"] = repo
105-
}
106-
}
107-
10878
// ReferencesGitRepo injects the GitRepo into the Context
10979
func ReferencesGitRepo() macaron.Handler {
11080
return func(ctx *APIContext) {

routers/api/v1/api.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ func RegisterRoutes(m *macaron.Macaron) {
239239
m.Get("", user.IsStarring)
240240
m.Put("", user.Star)
241241
m.Delete("", user.Unstar)
242-
}, context.ExtractOwnerAndRepo())
242+
}, repoAssignment())
243243
})
244244

245245
m.Get("/subscriptions", user.GetMyWatchedRepos)
@@ -258,11 +258,9 @@ func RegisterRoutes(m *macaron.Macaron) {
258258

259259
m.Group("/repos", func() {
260260
m.Post("/migrate", bind(auth.MigrateRepoForm{}), repo.Migrate)
261-
m.Combo("/:username/:reponame", context.ExtractOwnerAndRepo()).
262-
Get(repo.Get).
263-
Delete(repo.Delete)
264261

265262
m.Group("/:username/:reponame", func() {
263+
m.Combo("").Get(repo.Get).Delete(repo.Delete)
266264
m.Group("/hooks", func() {
267265
m.Combo("").Get(repo.ListHooks).
268266
Post(bind(api.CreateHookOption{}), repo.CreateHook)
@@ -330,7 +328,7 @@ func RegisterRoutes(m *macaron.Macaron) {
330328
m.Get("", user.IsWatching)
331329
m.Put("", user.Watch)
332330
m.Delete("", user.Unwatch)
333-
}, context.ExtractOwnerAndRepo())
331+
})
334332
m.Get("/editorconfig/:filename", context.RepoRef(), repo.GetEditorconfig)
335333
m.Group("/pulls", func() {
336334
m.Combo("").Get(bind(api.ListPullRequestsOptions{}), repo.ListPullRequests).Post(reqRepoWriter(), bind(api.CreatePullRequestOption{}), repo.CreatePullRequest)

routers/api/v1/repo/repo.go

+4
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,10 @@ func GetByID(ctx *context.APIContext) {
275275
// Delete one repository
276276
// see https://github.com/gogits/go-gogs-client/wiki/Repositories#delete
277277
func Delete(ctx *context.APIContext) {
278+
if !ctx.Repo.IsAdmin() {
279+
ctx.Error(403, "", "Must have admin rights")
280+
return
281+
}
278282
owner := ctx.Repo.Owner
279283
repo := ctx.Repo.Repository
280284

0 commit comments

Comments
 (0)