Skip to content

Commit 340bf89

Browse files
committed
...
1 parent 3f5c90a commit 340bf89

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

models/attachment.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ func (a *Attachment) IncreaseDownloadCount() error {
4242
return nil
4343
}
4444

45+
// IsNotAttached define is the attachement is linked to an issue or release
46+
func (a *Attachment) IsNotAttached() bool {
47+
return a.ReleaseID == 0 && a.IssueID == 0
48+
}
49+
4550
// APIFormat converts models.Attachment to api.Attachment
4651
func (a *Attachment) APIFormat() *api.Attachment {
4752
return &api.Attachment{

models/release.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ func createTag(gitRepo *git.Repository, rel *Release) error {
147147
}
148148
return nil
149149
}
150-
/* TODO set this at upload
151-
func addReleaseAttachments(releaseID int64, attachmentUUIDs []string) (err error) {
150+
151+
func linkReleaseAttachments(releaseID int64, attachmentUUIDs []string) (err error) {
152152
// Check attachments
153153
var attachments = make([]*Attachment, 0)
154154
for _, uuid := range attachmentUUIDs {
@@ -159,6 +159,10 @@ func addReleaseAttachments(releaseID int64, attachmentUUIDs []string) (err error
159159
}
160160
return fmt.Errorf("getAttachmentByUUID [%s]: %v", uuid, err)
161161
}
162+
if !attach.IsNotAttached(){
163+
log.Error("getAttachmentByUUID [%s]: skipping already linked attachement", uuid)
164+
continue
165+
}
162166
attachments = append(attachments, attach)
163167
}
164168

@@ -172,7 +176,6 @@ func addReleaseAttachments(releaseID int64, attachmentUUIDs []string) (err error
172176

173177
return
174178
}
175-
*/
176179

177180
// CreateRelease creates a new release of repository.
178181
func CreateRelease(gitRepo *git.Repository, rel *Release, attachmentUUIDs []string) error {
@@ -193,7 +196,7 @@ func CreateRelease(gitRepo *git.Repository, rel *Release, attachmentUUIDs []stri
193196
return err
194197
}
195198

196-
err = addReleaseAttachments(rel.ID, attachmentUUIDs)
199+
err = linkReleaseAttachments(rel.ID, attachmentUUIDs)
197200
if err != nil {
198201
return err
199202
}

routers/routes/routes.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ func RegisterRoutes(m *macaron.Macaron) {
478478
m.Get("/following", user.Following)
479479
})
480480

481+
//Keeping this path to have backward compat
481482
m.Get("/attachments/:uuid", func(ctx *context.Context) {
482483
attach, err := models.GetAttachmentByUUID(ctx.Params(":uuid"))
483484
if err != nil {
@@ -489,6 +490,38 @@ func RegisterRoutes(m *macaron.Macaron) {
489490
return
490491
}
491492

493+
//Attachement without issue or release attached should not be returned
494+
if attach.IsNotAttached() {
495+
ctx.Error(404)
496+
return
497+
}
498+
//Check issue access
499+
if attach.IssueID != 0 {
500+
iss, err = GetIssueByID(attach.IssueID)
501+
if err != nil {{
502+
ctx.ServerError("GetAttachmentByUUID.GetIssueByID", err)
503+
return
504+
}
505+
if !iss.Repo.CanRead(models.UnitTypeIssues){
506+
ctx.Error(403)
507+
return
508+
}
509+
}
510+
511+
//Check release access
512+
if attach.ReleaseID != 0 {
513+
rel, err = GetReleaseByID(attach.ReleaseID)
514+
if err != nil {{
515+
ctx.ServerError("GetAttachmentByUUID.GetReleaseByID", err)
516+
return
517+
}
518+
if !rel.Repo.CanRead(models.UnitTypeIssues){
519+
ctx.Error(403)
520+
return
521+
}
522+
}
523+
524+
//If we have matched a access release or issue
492525
fr, err := os.Open(attach.LocalPath())
493526
if err != nil {
494527
ctx.ServerError("Open", err)
@@ -675,6 +708,10 @@ func RegisterRoutes(m *macaron.Macaron) {
675708
m.Combo("/new").Get(context.RepoRef(), repo.NewIssue).
676709
Post(bindIgnErr(auth.CreateIssueForm{}), repo.NewIssuePost)
677710
}, context.RepoMustNotBeArchived(), reqRepoIssueReader)
711+
712+
//Should be able to create issue (a user that can create release can create issue)
713+
m.Post("/attachments", repo.UploadAttachment, context.RepoMustNotBeArchived(), reqRepoIssueReader)
714+
678715
// FIXME: should use different URLs but mostly same logic for comments of issue and pull reuqest.
679716
// So they can apply their own enable/disable logic on routers.
680717
m.Group("/issues", func() {
@@ -770,7 +807,6 @@ func RegisterRoutes(m *macaron.Macaron) {
770807
m.Get("/new", repo.NewRelease)
771808
m.Post("/new", bindIgnErr(auth.NewReleaseForm{}), repo.NewReleasePost)
772809
m.Post("/delete", repo.DeleteRelease)
773-
m.Post("/attachments", repo.UploadAttachment)
774810
}, reqSignIn, repo.MustBeNotEmpty, context.RepoMustNotBeArchived(), reqRepoReleaseWriter, context.RepoRef())
775811
m.Group("/releases", func() {
776812
m.Get("/edit/*", repo.EditRelease)

0 commit comments

Comments
 (0)