Skip to content

Commit 085b563

Browse files
authored
Support "." char as user name for User/Orgs in RSS/ATOM/GPG/KEYS path ... (#23874) (#23878)
Backport #23874
1 parent 5066b20 commit 085b563

File tree

7 files changed

+107
-15
lines changed

7 files changed

+107
-15
lines changed

models/fixtures/action.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,12 @@
6464
repo_id: 1700 # dangling intentional
6565
is_private: false
6666
created_unix: 1603011541
67+
68+
- id: 9
69+
user_id: 34
70+
op_type: 12 # close issue
71+
act_user_id: 34
72+
repo_id: 1 # public
73+
is_private: false
74+
created_unix: 1680454039
75+
content: '4|' # issueId 5

models/fixtures/user.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,3 +1220,41 @@
12201220
repo_admin_change_team_access: false
12211221
theme: ""
12221222
keep_activity_private: false
1223+
1224+
-
1225+
id: 34
1226+
lower_name: the_34-user.with.all.allowedchars
1227+
name: the_34-user.with.all.allowedChars
1228+
full_name: the_1-user.with.all.allowedChars
1229+
description: 'some [commonmark](https://commonmark.org/)!'
1230+
1231+
keep_email_private: false
1232+
email_notifications_preference: enabled
1233+
passwd: ZogKvWdyEx:password
1234+
passwd_hash_algo: dummy
1235+
must_change_password: false
1236+
login_source: 0
1237+
login_name: the_34-user.with.all.allowedchars
1238+
type: 0
1239+
salt: ZogKvWdyEx
1240+
max_repo_creation: -1
1241+
is_active: true
1242+
is_admin: false
1243+
is_restricted: false
1244+
allow_git_hook: false
1245+
allow_import_local: false
1246+
allow_create_organization: false
1247+
prohibit_login: false
1248+
avatar: avatar34
1249+
avatar_email: [email protected]
1250+
use_custom_avatar: true
1251+
num_followers: 0
1252+
num_following: 0
1253+
num_stars: 0
1254+
num_repos: 0
1255+
num_teams: 0
1256+
num_members: 0
1257+
visibility: 0
1258+
repo_admin_change_team_access: false
1259+
theme: ""
1260+
keep_activity_private: false

models/user/user_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,13 @@ func TestSearchUsers(t *testing.T) {
9999
}
100100

101101
testUserSuccess(&user_model.SearchUserOptions{OrderBy: "id ASC", ListOptions: db.ListOptions{Page: 1}},
102-
[]int64{1, 2, 4, 5, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 20, 21, 24, 27, 28, 29, 30, 32})
102+
[]int64{1, 2, 4, 5, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 20, 21, 24, 27, 28, 29, 30, 32, 34})
103103

104104
testUserSuccess(&user_model.SearchUserOptions{ListOptions: db.ListOptions{Page: 1}, IsActive: util.OptionalBoolFalse},
105105
[]int64{9})
106106

107107
testUserSuccess(&user_model.SearchUserOptions{OrderBy: "id ASC", ListOptions: db.ListOptions{Page: 1}, IsActive: util.OptionalBoolTrue},
108-
[]int64{1, 2, 4, 5, 8, 10, 11, 12, 13, 14, 15, 16, 18, 20, 21, 24, 27, 28, 29, 30, 32})
108+
[]int64{1, 2, 4, 5, 8, 10, 11, 12, 13, 14, 15, 16, 18, 20, 21, 24, 27, 28, 29, 30, 32, 34})
109109

110110
testUserSuccess(&user_model.SearchUserOptions{Keyword: "user1", OrderBy: "id ASC", ListOptions: db.ListOptions{Page: 1}, IsActive: util.OptionalBoolTrue},
111111
[]int64{1, 10, 11, 12, 13, 14, 15, 16, 18})

routers/web/web.go

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/http"
99
"os"
1010
"path"
11+
"strings"
1112

1213
"code.gitea.io/gitea/models/perm"
1314
"code.gitea.io/gitea/models/unit"
@@ -651,16 +652,47 @@ func RegisterRoutes(m *web.Route) {
651652
})
652653
http.ServeFile(ctx.Resp, ctx.Req, path.Join(setting.StaticRootPath, "public/img/favicon.png"))
653654
})
654-
m.Group("/{username}", func() {
655-
m.Get(".png", user.AvatarByUserName)
656-
m.Get(".keys", user.ShowSSHKeys)
657-
m.Get(".gpg", user.ShowGPGKeys)
658-
m.Get(".rss", feedEnabled, feed.ShowUserFeedRSS)
659-
m.Get(".atom", feedEnabled, feed.ShowUserFeedAtom)
660-
m.Get("", user.Profile)
661-
}, func(ctx *context.Context) {
662-
ctx.Data["EnableFeed"] = setting.EnableFeed
663-
}, context_service.UserAssignmentWeb())
655+
m.Get("/{username}", func(ctx *context.Context) {
656+
// WORKAROUND to support usernames with "." in it
657+
// https://github.com/go-chi/chi/issues/781
658+
username := ctx.Params("username")
659+
reloadParam := func(suffix string) (success bool) {
660+
ctx.SetParams("username", strings.TrimSuffix(username, suffix))
661+
context_service.UserAssignmentWeb()(ctx)
662+
return !ctx.Written()
663+
}
664+
switch {
665+
case strings.HasSuffix(username, ".png"):
666+
if reloadParam(".png") {
667+
user.AvatarByUserName(ctx)
668+
}
669+
case strings.HasSuffix(username, ".keys"):
670+
if reloadParam(".keys") {
671+
user.ShowSSHKeys(ctx)
672+
}
673+
case strings.HasSuffix(username, ".gpg"):
674+
if reloadParam(".gpg") {
675+
user.ShowGPGKeys(ctx)
676+
}
677+
case strings.HasSuffix(username, ".rss"):
678+
feedEnabled(ctx)
679+
if !ctx.Written() && reloadParam(".rss") {
680+
context_service.UserAssignmentWeb()(ctx)
681+
feed.ShowUserFeedRSS(ctx)
682+
}
683+
case strings.HasSuffix(username, ".atom"):
684+
feedEnabled(ctx)
685+
if !ctx.Written() && reloadParam(".atom") {
686+
feed.ShowUserFeedAtom(ctx)
687+
}
688+
default:
689+
context_service.UserAssignmentWeb()(ctx)
690+
if !ctx.Written() {
691+
ctx.Data["EnableFeed"] = setting.EnableFeed
692+
user.Profile(ctx)
693+
}
694+
}
695+
})
664696
m.Get("/attachments/{uuid}", repo.GetAttachment)
665697
}, ignSignIn)
666698

tests/integration/api_nodeinfo_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func TestNodeinfo(t *testing.T) {
3333
DecodeJSON(t, resp, &nodeinfo)
3434
assert.True(t, nodeinfo.OpenRegistrations)
3535
assert.Equal(t, "gitea", nodeinfo.Software.Name)
36-
assert.Equal(t, 24, nodeinfo.Usage.Users.Total)
36+
assert.Equal(t, 25, nodeinfo.Usage.Users.Total)
3737
assert.Equal(t, 18, nodeinfo.Usage.LocalPosts)
3838
assert.Equal(t, 2, nodeinfo.Usage.LocalComments)
3939
})

tests/integration/setting_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestSettingShowUserEmailExplore(t *testing.T) {
2525
htmlDoc := NewHTMLParser(t, resp.Body)
2626
assert.Contains(t,
2727
htmlDoc.doc.Find(".ui.user.list").Text(),
28-
"user4@example.com",
28+
"user34@example.com",
2929
)
3030

3131
setting.UI.ShowUserEmail = false
@@ -35,7 +35,7 @@ func TestSettingShowUserEmailExplore(t *testing.T) {
3535
htmlDoc = NewHTMLParser(t, resp.Body)
3636
assert.NotContains(t,
3737
htmlDoc.doc.Find(".ui.user.list").Text(),
38-
"user4@example.com",
38+
"user34@example.com",
3939
)
4040

4141
setting.UI.ShowUserEmail = showUserEmail

tests/integration/user_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,19 @@ func testExportUserGPGKeys(t *testing.T, user, expected string) {
241241
assert.Equal(t, expected, resp.Body.String())
242242
}
243243

244+
func TestGetUserRss(t *testing.T) {
245+
user34 := "the_34-user.with.all.allowedChars"
246+
req := NewRequestf(t, "GET", "/%s.rss", user34)
247+
resp := MakeRequest(t, req, http.StatusOK)
248+
if assert.EqualValues(t, "application/rss+xml;charset=utf-8", resp.Header().Get("Content-Type")) {
249+
rssDoc := NewHTMLParser(t, resp.Body).Find("channel")
250+
title, _ := rssDoc.ChildrenFiltered("title").Html()
251+
assert.EqualValues(t, "Feed of "the_1-user.with.all.allowedChars"", title)
252+
description, _ := rssDoc.ChildrenFiltered("description").Html()
253+
assert.EqualValues(t, "<p>some <a href="https://commonmark.org/" rel="nofollow">commonmark</a>!</p>\n", description)
254+
}
255+
}
256+
244257
func TestListStopWatches(t *testing.T) {
245258
defer tests.PrepareTestEnv(t)()
246259

0 commit comments

Comments
 (0)