Skip to content

Commit 566e8ec

Browse files
jonasfranzlunny
authored andcommitted
Fork permission bug fixes (#2534)
* Hotfix for "Add time manually" (#2211 (comment)) Signed-off-by: Jonas Franz <[email protected]> * Checking if Code unit is enabled before creating a fork. Signed-off-by: Jonas Franz <[email protected]> * Adding a discrete function for RepoIDAssignment Signed-off-by: Jonas Franz <[email protected]> * Improved Documentation
1 parent 6c6533f commit 566e8ec

File tree

3 files changed

+73
-3
lines changed

3 files changed

+73
-3
lines changed

models/repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ func (repo *Repository) UpdateSize() error {
638638

639639
// CanBeForked returns true if repository meets the requirements of being forked.
640640
func (repo *Repository) CanBeForked() bool {
641-
return !repo.IsBare
641+
return !repo.IsBare && repo.UnitEnabled(UnitTypeCode)
642642
}
643643

644644
// CanEnablePulls returns true if repository meets the requirements of accepting pulls.

modules/context/repo.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,74 @@ func RedirectToRepo(ctx *Context, redirectRepoID int64) {
171171
)
172172
ctx.Redirect(redirectPath)
173173
}
174+
// RepoIDAssignment returns an macaron handler which assigns the repo to the context.
175+
func RepoIDAssignment() macaron.Handler {
176+
return func(ctx *Context) {
177+
var (
178+
err error
179+
)
180+
181+
repoID := ctx.ParamsInt64(":repoid")
182+
183+
// Get repository.
184+
repo, err := models.GetRepositoryByID(repoID)
185+
if err != nil {
186+
if models.IsErrRepoNotExist(err) {
187+
ctx.Handle(404, "GetRepositoryByID", nil)
188+
} else {
189+
ctx.Handle(500, "GetRepositoryByID", err)
190+
}
191+
return
192+
}
193+
194+
if err = repo.GetOwner(); err != nil {
195+
ctx.Handle(500, "GetOwner", err)
196+
return
197+
}
198+
199+
// Admin has super access.
200+
if ctx.IsSigned && ctx.User.IsAdmin {
201+
ctx.Repo.AccessMode = models.AccessModeOwner
202+
} else {
203+
var userID int64
204+
if ctx.User != nil {
205+
userID = ctx.User.ID
206+
}
207+
mode, err := models.AccessLevel(userID, repo)
208+
if err != nil {
209+
ctx.Handle(500, "AccessLevel", err)
210+
return
211+
}
212+
ctx.Repo.AccessMode = mode
213+
}
214+
215+
// Check access.
216+
if ctx.Repo.AccessMode == models.AccessModeNone {
217+
if ctx.Query("go-get") == "1" {
218+
earlyResponseForGoGetMeta(ctx)
219+
return
220+
}
221+
ctx.Handle(404, "no access right", err)
222+
return
223+
}
224+
ctx.Data["HasAccess"] = true
225+
226+
if repo.IsMirror {
227+
ctx.Repo.Mirror, err = models.GetMirrorByRepoID(repo.ID)
228+
if err != nil {
229+
ctx.Handle(500, "GetMirror", err)
230+
return
231+
}
232+
ctx.Data["MirrorEnablePrune"] = ctx.Repo.Mirror.EnablePrune
233+
ctx.Data["MirrorInterval"] = ctx.Repo.Mirror.Interval
234+
ctx.Data["Mirror"] = ctx.Repo.Mirror
235+
}
236+
237+
ctx.Repo.Repository = repo
238+
ctx.Data["RepoName"] = ctx.Repo.Repository.Name
239+
ctx.Data["IsBareRepo"] = ctx.Repo.Repository.IsBare
240+
}
241+
}
174242

175243
// RepoAssignment returns a macaron to handle repository assignment
176244
func RepoAssignment() macaron.Handler {

routers/routes/routes.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,8 +419,10 @@ func RegisterRoutes(m *macaron.Macaron) {
419419
m.Post("/create", bindIgnErr(auth.CreateRepoForm{}), repo.CreatePost)
420420
m.Get("/migrate", repo.Migrate)
421421
m.Post("/migrate", bindIgnErr(auth.MigrateRepoForm{}), repo.MigratePost)
422-
m.Combo("/fork/:repoid").Get(repo.Fork).
423-
Post(bindIgnErr(auth.CreateRepoForm{}), repo.ForkPost)
422+
m.Group("/fork", func() {
423+
m.Combo("/:repoid").Get(repo.Fork).
424+
Post(bindIgnErr(auth.CreateRepoForm{}), repo.ForkPost)
425+
}, context.RepoIDAssignment(), context.UnitTypes(), context.LoadRepoUnits(), context.CheckUnit(models.UnitTypeCode))
424426
}, reqSignIn)
425427

426428
m.Group("/:username/:reponame", func() {

0 commit comments

Comments
 (0)