Skip to content

Restricting access to fork functioanlity to users with Code access #2534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 18, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ func (repo *Repository) UpdateSize() error {

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

// CanEnablePulls returns true if repository meets the requirements of accepting pulls.
Expand Down
92 changes: 55 additions & 37 deletions modules/context/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,54 +177,72 @@ func RepoAssignment() macaron.Handler {
return func(ctx *Context) {
var (
owner *models.User
repo *models.Repository
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these changes to RepoAssignment() necessary? It seems to me that adding the context.LoadRepoUnits() and context.CheckUnit(models.UnitTypeCode) handlers should be sufficient (along with the changes to repo.CanBeForked())

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it needs load repo because context.LoadRepoUnits() needs ctx.Reop to context but fork's route only has reopid no repopath.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, you're right.

But since we only need to load ctx.Repo, maybe it would be better to add a separate handler for loading :repoid, instead of making RepoAssignment() more complicated by adding a big if/else?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ethantkoenig I agree with you to make a new middleware to do that but not change ReopAssignment

err error
)
if repoid := ctx.ParamsInt64(":repoid"); repoid == 0 {
userName := ctx.Params(":username")
repoName := ctx.Params(":reponame")

userName := ctx.Params(":username")
repoName := ctx.Params(":reponame")

// Check if the user is the same as the repository owner
if ctx.IsSigned && ctx.User.LowerName == strings.ToLower(userName) {
owner = ctx.User
} else {
owner, err = models.GetUserByName(userName)
// Check if the user is the same as the repository owner
if ctx.IsSigned && ctx.User.LowerName == strings.ToLower(userName) {
owner = ctx.User
} else {
owner, err = models.GetUserByName(userName)
if err != nil {
if models.IsErrUserNotExist(err) {
if ctx.Query("go-get") == "1" {
earlyResponseForGoGetMeta(ctx)
return
}
ctx.Handle(404, "GetUserByName", nil)
} else {
ctx.Handle(500, "GetUserByName", err)
}
return
}
}
// Get repository.
repo, err = models.GetRepositoryByName(owner.ID, repoName)
if err != nil {
if models.IsErrUserNotExist(err) {
if ctx.Query("go-get") == "1" {
earlyResponseForGoGetMeta(ctx)
return
if models.IsErrRepoNotExist(err) {
redirectRepoID, err := models.LookupRepoRedirect(owner.ID, repoName)
if err == nil {
RedirectToRepo(ctx, redirectRepoID)
} else if models.IsErrRepoRedirectNotExist(err) {
if ctx.Query("go-get") == "1" {
earlyResponseForGoGetMeta(ctx)
return
}
ctx.Handle(404, "GetRepositoryByName", nil)
} else {
ctx.Handle(500, "LookupRepoRedirect", err)
}
ctx.Handle(404, "GetUserByName", nil)
} else {
ctx.Handle(500, "GetUserByName", err)
ctx.Handle(500, "GetRepositoryByName", err)
}
return
}
}
ctx.Repo.Owner = owner
ctx.Data["Username"] = ctx.Repo.Owner.Name

// Get repository.
repo, err := models.GetRepositoryByName(owner.ID, repoName)
if err != nil {
if models.IsErrRepoNotExist(err) {
redirectRepoID, err := models.LookupRepoRedirect(owner.ID, repoName)
if err == nil {
RedirectToRepo(ctx, redirectRepoID)
} else if models.IsErrRepoRedirectNotExist(err) {
if ctx.Query("go-get") == "1" {
earlyResponseForGoGetMeta(ctx)
return
}
ctx.Handle(404, "GetRepositoryByName", nil)
} else {
repo, err = models.GetRepositoryByID(repoid)
if err != nil {
if models.IsErrRepoNotExist(err) {
ctx.Handle(404, "GetRepositoryByID", nil)
} else {
ctx.Handle(500, "LookupRepoRedirect", err)
ctx.Handle(500, "GetRepositoryByID", err)
}
} else {
ctx.Handle(500, "GetRepositoryByName", err)
return
}
return
if err = repo.GetOwner(); err != nil {
ctx.Handle(500, "GetOwner", err)
return
}
owner = repo.Owner
}

ctx.Repo.Owner = owner
ctx.Data["Username"] = ctx.Repo.Owner.Name

repo.Owner = owner

// Admin has super access.
Expand Down Expand Up @@ -269,9 +287,9 @@ func RepoAssignment() macaron.Handler {
ctx.Data["RepoName"] = ctx.Repo.Repository.Name
ctx.Data["IsBareRepo"] = ctx.Repo.Repository.IsBare

gitRepo, err := git.OpenRepository(models.RepoPath(userName, repoName))
gitRepo, err := git.OpenRepository(repo.RepoPath())
if err != nil {
ctx.Handle(500, "RepoAssignment Invalid repo "+models.RepoPath(userName, repoName), err)
ctx.Handle(500, "RepoAssignment Invalid repo", err)
return
}
ctx.Repo.GitRepo = gitRepo
Expand Down
6 changes: 4 additions & 2 deletions routers/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,10 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Post("/create", bindIgnErr(auth.CreateRepoForm{}), repo.CreatePost)
m.Get("/migrate", repo.Migrate)
m.Post("/migrate", bindIgnErr(auth.MigrateRepoForm{}), repo.MigratePost)
m.Combo("/fork/:repoid").Get(repo.Fork).
Post(bindIgnErr(auth.CreateRepoForm{}), repo.ForkPost)
m.Group("/fork", func() {
m.Combo("/:repoid").Get(repo.Fork).
Post(bindIgnErr(auth.CreateRepoForm{}), repo.ForkPost)
}, context.RepoAssignment(), context.UnitTypes(), context.LoadRepoUnits(), context.CheckUnit(models.UnitTypeCode))
}, reqSignIn)

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