Skip to content
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

[BugFix] use default avatar for ghost user (fix 500 error) #9536

Merged
merged 10 commits into from
Dec 29, 2019
1 change: 1 addition & 0 deletions integrations/links_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func TestRedirectsNoLogin(t *testing.T) {
"/user2/repo1/src/master": "/user2/repo1/src/branch/master",
"/user2/repo1/src/master/file.txt": "/user2/repo1/src/branch/master/file.txt",
"/user2/repo1/src/master/directory/file.txt": "/user2/repo1/src/branch/master/directory/file.txt",
"/user/avatar/Ghost/-1": "/img/avatar_default.png",
}
for link, redirectLink := range redirects {
req := NewRequest(t, "GET", link)
Expand Down
20 changes: 13 additions & 7 deletions routers/user/avatar.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package user

import (
"strconv"
"strings"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
Expand All @@ -23,14 +24,19 @@ func Avatar(ctx *context.Context) {

log.Debug("Asked avatar for user %v and size %v", userName, size)

user, err := models.GetUserByName(userName)
if err != nil {
if models.IsErrUserNotExist(err) {
ctx.ServerError("Requested avatar for invalid user", err)
} else {
ctx.ServerError("Retrieving user by name", err)
var user *models.User
if strings.ToLower(userName) != "ghost" {
user, err = models.GetUserByName(userName)
if err != nil {
if models.IsErrUserNotExist(err) {
ctx.ServerError("Requested avatar for invalid user", err)
} else {
ctx.ServerError("Retrieving user by name", err)
}
return
}
return
} else {
user = models.NewGhostUser()
}

ctx.Redirect(user.RealSizedAvatarLink(size))
Expand Down