From 30b428bf0eabb9f6cd73894d776502e1b7e351b7 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Sat, 15 Aug 2015 02:48:05 +0800 Subject: #1419: 500 when visit a issue with issue/comments of deleted user --- cmd/web.go | 2 +- models/issue.go | 2 +- models/user.go | 21 ++++++++++++++++++++- routers/repo/issue.go | 4 ++-- templates/repo/issue/view_content.tmpl | 6 ++++-- 5 files changed, 28 insertions(+), 7 deletions(-) diff --git a/cmd/web.go b/cmd/web.go index a091c5c69d..c441e91966 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -453,12 +453,12 @@ func runWeb(ctx *cli.Context) { m.Combo("/new").Get(repo.NewIssue). Post(bindIgnErr(auth.CreateIssueForm{}), repo.NewIssuePost) + m.Combo("/:index/comments").Post(bindIgnErr(auth.CreateCommentForm{}), repo.NewComment) m.Group("/:index", func() { m.Post("", bindIgnErr(auth.CreateIssueForm{}), repo.UpdateIssue) m.Post("/label", repo.UpdateIssueLabel) m.Post("/milestone", repo.UpdateIssueMilestone) m.Post("/assignee", repo.UpdateIssueAssignee) - m.Combo("/comments").Post(bindIgnErr(auth.CreateCommentForm{}), repo.NewComment) }, reqRepoAdmin) }) m.Group("/labels", func() { diff --git a/models/issue.go b/models/issue.go index 712dd6bb2d..4b7da8ce6c 100644 --- a/models/issue.go +++ b/models/issue.go @@ -1364,7 +1364,7 @@ func (c *Comment) AfterSet(colName string, _ xorm.Cell) { if err != nil { if IsErrUserNotExist(err) { c.PosterID = -1 - c.Poster = &User{Name: "someone"} + c.Poster = NewFakeUser() } else { log.Error(3, "GetUserByID[%d]: %v", c.ID, err) } diff --git a/models/user.go b/models/user.go index f2151a7080..ca49a5b8f6 100644 --- a/models/user.go +++ b/models/user.go @@ -119,6 +119,10 @@ func (u *User) HomeLink() string { // AvatarLink returns user gravatar link. func (u *User) AvatarLink() string { defaultImgUrl := setting.AppSubUrl + "/img/avatar_default.jpg" + if u.Id == -1 { + return defaultImgUrl + } + imgPath := path.Join(setting.AvatarUploadPath, com.ToStr(u.Id)) switch { case u.UseCustomAvatar: @@ -321,6 +325,15 @@ func GetUserSalt() string { return base.GetRandomString(10) } +// NewFakeUser creates and returns a fake user for someone has deleted his/her account. +func NewFakeUser() *User { + return &User{ + Id: -1, + Name: "Someone", + LowerName: "someone", + } +} + // CreateUser creates record of a new user. func CreateUser(u *User) (err error) { if err = IsUsableName(u.Name); err != nil { @@ -546,6 +559,7 @@ func DeleteUser(u *User) error { &Collaboration{UserID: u.Id}, &EmailAddress{Uid: u.Id}, &Watch{UserID: u.Id}, + &IssueUser{UID: u.Id}, ); err != nil { return err } @@ -563,11 +577,16 @@ func DeleteUser(u *User) error { return err } for _, key := range keys { - if err = DeletePublicKey(key); err != nil { + if err = deletePublicKey(sess, key); err != nil { return err } } + // Clear assignee. + if _, err = sess.Exec("UPDATE `issue` SET assignee_id=0 WHERE assignee_id=?", u.Id); err != nil { + return err + } + if _, err = sess.Delete(u); err != nil { return err } diff --git a/routers/repo/issue.go b/routers/repo/issue.go index d992cecc53..dc86dedff0 100644 --- a/routers/repo/issue.go +++ b/routers/repo/issue.go @@ -508,7 +508,7 @@ func ViewIssue(ctx *middleware.Context) { } ctx.Data["Issue"] = issue - // ctx.Data["IsIssueOwner"] = ctx.Repo.IsOwner() || (ctx.IsSigned && issue.PosterID == ctx.User.Id) + ctx.Data["IsIssueOwner"] = ctx.Repo.IsAdmin() || (ctx.IsSigned && issue.IsPoster(ctx.User.Id)) ctx.HTML(200, ISSUE_VIEW) } @@ -685,7 +685,7 @@ func NewComment(ctx *middleware.Context, form auth.CreateCommentForm) { } // Check if issue owner/poster changes the status of issue. - if (ctx.Repo.IsOwner() || issue.IsPoster(ctx.User.Id)) && + if (ctx.Repo.IsOwner() || (ctx.IsSigned && issue.IsPoster(ctx.User.Id))) && (form.Status == "reopen" || form.Status == "close") { issue.Repo = ctx.Repo.Repository if err = issue.ChangeStatus(ctx.User, form.Status == "close"); err != nil { diff --git a/templates/repo/issue/view_content.tmpl b/templates/repo/issue/view_content.tmpl index d368027591..fc84d101bc 100644 --- a/templates/repo/issue/view_content.tmpl +++ b/templates/repo/issue/view_content.tmpl @@ -56,12 +56,12 @@ {{if eq .Type 0}}
- +
- {{.Poster.Name}} {{$.i18n.Tr "repo.issues.commented_at" .HashTag $createdStr | Safe}} + {{.Poster.Name}} {{$.i18n.Tr "repo.issues.commented_at" .HashTag $createdStr | Safe}}
{{if gt .ShowTag 0}}
@@ -125,6 +125,7 @@ {{.CsrfTokenHtml}}
+ {{if .IsIssueOwner}} {{if .Issue.IsClosed}}
{{.i18n.Tr "repo.issues.reopen_issue"}} @@ -134,6 +135,7 @@ {{.i18n.Tr "repo.issues.close_issue"}}
{{end}} + {{end}} -- cgit v1.2.3