Skip to content

Commit 0d63034

Browse files
authored
Merge branch 'main' into lunny/fix_update_user_visibility
2 parents df080cf + a5a3d9b commit 0d63034

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+635
-295
lines changed

cmd/admin_auth_oauth.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ func oauthCLIFlags() []cli.Flag {
8787
Value: nil,
8888
Usage: "Scopes to request when to authenticate against this OAuth2 source",
8989
},
90+
&cli.StringFlag{
91+
Name: "ssh-public-key-claim-name",
92+
Usage: "Claim name that provides SSH public keys",
93+
},
94+
&cli.StringFlag{
95+
Name: "full-name-claim-name",
96+
Usage: "Claim name that provides user's full name",
97+
},
9098
&cli.StringFlag{
9199
Name: "required-claim-name",
92100
Value: "",
@@ -177,6 +185,8 @@ func parseOAuth2Config(c *cli.Command) *oauth2.Source {
177185
RestrictedGroup: c.String("restricted-group"),
178186
GroupTeamMap: c.String("group-team-map"),
179187
GroupTeamMapRemoval: c.Bool("group-team-map-removal"),
188+
SSHPublicKeyClaimName: c.String("ssh-public-key-claim-name"),
189+
FullNameClaimName: c.String("full-name-claim-name"),
180190
}
181191
}
182192

@@ -268,6 +278,12 @@ func (a *authService) runUpdateOauth(ctx context.Context, c *cli.Command) error
268278
if c.IsSet("group-team-map-removal") {
269279
oAuth2Config.GroupTeamMapRemoval = c.Bool("group-team-map-removal")
270280
}
281+
if c.IsSet("ssh-public-key-claim-name") {
282+
oAuth2Config.SSHPublicKeyClaimName = c.String("ssh-public-key-claim-name")
283+
}
284+
if c.IsSet("full-name-claim-name") {
285+
oAuth2Config.FullNameClaimName = c.String("full-name-claim-name")
286+
}
271287

272288
// update custom URL mapping
273289
customURLMapping := &oauth2.CustomURLMapping{}

cmd/admin_auth_oauth_test.go

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ func TestAddOauth(t *testing.T) {
8888
"--restricted-group", "restricted",
8989
"--group-team-map", `{"group1": [1,2]}`,
9090
"--group-team-map-removal=true",
91+
"--ssh-public-key-claim-name", "attr_ssh_pub_key",
92+
"--full-name-claim-name", "attr_full_name",
9193
},
9294
source: &auth_model.Source{
9395
Type: auth_model.OAuth2,
@@ -104,15 +106,17 @@ func TestAddOauth(t *testing.T) {
104106
EmailURL: "https://example.com/email",
105107
Tenant: "some_tenant",
106108
},
107-
IconURL: "https://example.com/icon",
108-
Scopes: []string{"scope1", "scope2"},
109-
RequiredClaimName: "claim_name",
110-
RequiredClaimValue: "claim_value",
111-
GroupClaimName: "group_name",
112-
AdminGroup: "admin",
113-
RestrictedGroup: "restricted",
114-
GroupTeamMap: `{"group1": [1,2]}`,
115-
GroupTeamMapRemoval: true,
109+
IconURL: "https://example.com/icon",
110+
Scopes: []string{"scope1", "scope2"},
111+
RequiredClaimName: "claim_name",
112+
RequiredClaimValue: "claim_value",
113+
GroupClaimName: "group_name",
114+
AdminGroup: "admin",
115+
RestrictedGroup: "restricted",
116+
GroupTeamMap: `{"group1": [1,2]}`,
117+
GroupTeamMapRemoval: true,
118+
SSHPublicKeyClaimName: "attr_ssh_pub_key",
119+
FullNameClaimName: "attr_full_name",
116120
},
117121
TwoFactorPolicy: "skip",
118122
},
@@ -223,15 +227,17 @@ func TestUpdateOauth(t *testing.T) {
223227
EmailURL: "https://old.example.com/email",
224228
Tenant: "old_tenant",
225229
},
226-
IconURL: "https://old.example.com/icon",
227-
Scopes: []string{"old_scope1", "old_scope2"},
228-
RequiredClaimName: "old_claim_name",
229-
RequiredClaimValue: "old_claim_value",
230-
GroupClaimName: "old_group_name",
231-
AdminGroup: "old_admin",
232-
RestrictedGroup: "old_restricted",
233-
GroupTeamMap: `{"old_group1": [1,2]}`,
234-
GroupTeamMapRemoval: true,
230+
IconURL: "https://old.example.com/icon",
231+
Scopes: []string{"old_scope1", "old_scope2"},
232+
RequiredClaimName: "old_claim_name",
233+
RequiredClaimValue: "old_claim_value",
234+
GroupClaimName: "old_group_name",
235+
AdminGroup: "old_admin",
236+
RestrictedGroup: "old_restricted",
237+
GroupTeamMap: `{"old_group1": [1,2]}`,
238+
GroupTeamMapRemoval: true,
239+
SSHPublicKeyClaimName: "old_ssh_pub_key",
240+
FullNameClaimName: "old_full_name",
235241
},
236242
TwoFactorPolicy: "",
237243
},
@@ -257,6 +263,8 @@ func TestUpdateOauth(t *testing.T) {
257263
"--restricted-group", "restricted",
258264
"--group-team-map", `{"group1": [1,2]}`,
259265
"--group-team-map-removal=false",
266+
"--ssh-public-key-claim-name", "new_ssh_pub_key",
267+
"--full-name-claim-name", "new_full_name",
260268
},
261269
authSource: &auth_model.Source{
262270
ID: 1,
@@ -274,15 +282,17 @@ func TestUpdateOauth(t *testing.T) {
274282
EmailURL: "https://example.com/email",
275283
Tenant: "new_tenant",
276284
},
277-
IconURL: "https://example.com/icon",
278-
Scopes: []string{"scope1", "scope2"},
279-
RequiredClaimName: "claim_name",
280-
RequiredClaimValue: "claim_value",
281-
GroupClaimName: "group_name",
282-
AdminGroup: "admin",
283-
RestrictedGroup: "restricted",
284-
GroupTeamMap: `{"group1": [1,2]}`,
285-
GroupTeamMapRemoval: false,
285+
IconURL: "https://example.com/icon",
286+
Scopes: []string{"scope1", "scope2"},
287+
RequiredClaimName: "claim_name",
288+
RequiredClaimValue: "claim_value",
289+
GroupClaimName: "group_name",
290+
AdminGroup: "admin",
291+
RestrictedGroup: "restricted",
292+
GroupTeamMap: `{"group1": [1,2]}`,
293+
GroupTeamMapRemoval: false,
294+
SSHPublicKeyClaimName: "new_ssh_pub_key",
295+
FullNameClaimName: "new_full_name",
286296
},
287297
TwoFactorPolicy: "skip",
288298
},

models/asymkey/ssh_key.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,13 +355,13 @@ func AddPublicKeysBySource(ctx context.Context, usr *user_model.User, s *auth.So
355355
return sshKeysNeedUpdate
356356
}
357357

358-
// SynchronizePublicKeys updates a users public keys. Returns true if there are changes.
358+
// SynchronizePublicKeys updates a user's public keys. Returns true if there are changes.
359359
func SynchronizePublicKeys(ctx context.Context, usr *user_model.User, s *auth.Source, sshPublicKeys []string) bool {
360360
var sshKeysNeedUpdate bool
361361

362362
log.Trace("synchronizePublicKeys[%s]: Handling Public SSH Key synchronization for user %s", s.Name, usr.Name)
363363

364-
// Get Public Keys from DB with current LDAP source
364+
// Get Public Keys from DB with the current auth source
365365
var giteaKeys []string
366366
keys, err := db.Find[PublicKey](ctx, FindPublicKeyOptions{
367367
OwnerID: usr.ID,

models/auth/oauth2.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -612,8 +612,8 @@ func (err ErrOAuthApplicationNotFound) Unwrap() error {
612612
return util.ErrNotExist
613613
}
614614

615-
// GetActiveOAuth2SourceByName returns a OAuth2 AuthSource based on the given name
616-
func GetActiveOAuth2SourceByName(ctx context.Context, name string) (*Source, error) {
615+
// GetActiveOAuth2SourceByAuthName returns a OAuth2 AuthSource based on the given name
616+
func GetActiveOAuth2SourceByAuthName(ctx context.Context, name string) (*Source, error) {
617617
authSource := new(Source)
618618
has, err := db.GetEngine(ctx).Where("name = ? and type = ? and is_active = ?", name, OAuth2, true).Get(authSource)
619619
if err != nil {

models/auth/source.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ func UpdateSource(ctx context.Context, source *Source) error {
334334

335335
err = registerableSource.RegisterSource()
336336
if err != nil {
337-
// restore original values since we cannot update the provider it self
337+
// restore original values since we cannot update the provider itself
338338
if _, err := db.GetEngine(ctx).ID(source.ID).AllCols().Update(originalSource); err != nil {
339339
log.Error("UpdateSource: Error while wrapOpenIDConnectInitializeError: %v", err)
340340
}

models/git/branch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ type RecentlyPushedNewBranch struct {
472472
// if opts.CommitAfterUnix is 0, we will find the branches that were committed to in the last 2 hours
473473
// if opts.ListOptions is not set, we will only display top 2 latest branches.
474474
// Protected branches will be skipped since they are unlikely to be used to create new PRs.
475-
func FindRecentlyPushedNewBranches(ctx context.Context, doer *user_model.User, opts *FindRecentlyPushedNewBranchesOptions) ([]*RecentlyPushedNewBranch, error) {
475+
func FindRecentlyPushedNewBranches(ctx context.Context, doer *user_model.User, opts FindRecentlyPushedNewBranchesOptions) ([]*RecentlyPushedNewBranch, error) {
476476
if doer == nil {
477477
return []*RecentlyPushedNewBranch{}, nil
478478
}

models/repo/repo.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,13 @@ func (repo *Repository) AllowsPulls(ctx context.Context) bool {
652652
}
653653

654654
// CanEnableEditor returns true if repository meets the requirements of web editor.
655+
// FIXME: most CanEnableEditor calls should be replaced with CanContentChange
656+
// And all other like CanCreateBranch / CanEnablePulls should also be updated
655657
func (repo *Repository) CanEnableEditor() bool {
658+
return repo.CanContentChange()
659+
}
660+
661+
func (repo *Repository) CanContentChange() bool {
656662
return !repo.IsMirror && !repo.IsArchived
657663
}
658664

modules/git/commit_submodule_file.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@ package git
66

77
import (
88
"context"
9+
"strings"
910

1011
giturl "code.gitea.io/gitea/modules/git/url"
1112
)
1213

1314
// CommitSubmoduleFile represents a file with submodule type.
1415
type CommitSubmoduleFile struct {
15-
refURL string
16-
parsedURL *giturl.RepositoryURL
17-
parsed bool
18-
refID string
19-
repoLink string
16+
refURL string
17+
refID string
18+
19+
parsed bool
20+
targetRepoLink string
2021
}
2122

2223
// NewCommitSubmoduleFile create a new submodule file
@@ -35,20 +36,27 @@ func (sf *CommitSubmoduleFile) SubmoduleWebLink(ctx context.Context, optCommitID
3536
}
3637
if !sf.parsed {
3738
sf.parsed = true
38-
parsedURL, err := giturl.ParseRepositoryURL(ctx, sf.refURL)
39-
if err != nil {
40-
return nil
39+
if strings.HasPrefix(sf.refURL, "../") {
40+
// FIXME: when handling relative path, this logic is not right. It needs to:
41+
// 1. Remember the submodule's full path and its commit's repo home link
42+
// 2. Resolve the relative path: targetRepoLink = path.Join(repoHomeLink, path.Dir(submoduleFullPath), refURL)
43+
// Not an easy task and need to refactor related code a lot.
44+
sf.targetRepoLink = sf.refURL
45+
} else {
46+
parsedURL, err := giturl.ParseRepositoryURL(ctx, sf.refURL)
47+
if err != nil {
48+
return nil
49+
}
50+
sf.targetRepoLink = giturl.MakeRepositoryWebLink(parsedURL)
4151
}
42-
sf.parsedURL = parsedURL
43-
sf.repoLink = giturl.MakeRepositoryWebLink(sf.parsedURL)
4452
}
4553
var commitLink string
4654
if len(optCommitID) == 2 {
47-
commitLink = sf.repoLink + "/compare/" + optCommitID[0] + "..." + optCommitID[1]
55+
commitLink = sf.targetRepoLink + "/compare/" + optCommitID[0] + "..." + optCommitID[1]
4856
} else if len(optCommitID) == 1 {
49-
commitLink = sf.repoLink + "/tree/" + optCommitID[0]
57+
commitLink = sf.targetRepoLink + "/tree/" + optCommitID[0]
5058
} else {
51-
commitLink = sf.repoLink + "/tree/" + sf.refID
59+
commitLink = sf.targetRepoLink + "/tree/" + sf.refID
5260
}
53-
return &SubmoduleWebLink{RepoWebLink: sf.repoLink, CommitWebLink: commitLink}
61+
return &SubmoduleWebLink{RepoWebLink: sf.targetRepoLink, CommitWebLink: commitLink}
5462
}

modules/git/commit_submodule_file_test.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,29 @@ import (
1010
)
1111

1212
func TestCommitSubmoduleLink(t *testing.T) {
13-
sf := NewCommitSubmoduleFile("[email protected]:user/repo.git", "aaaa")
13+
wl := (*CommitSubmoduleFile)(nil).SubmoduleWebLink(t.Context())
14+
assert.Nil(t, wl)
1415

15-
wl := sf.SubmoduleWebLink(t.Context())
16-
assert.Equal(t, "https://github.com/user/repo", wl.RepoWebLink)
17-
assert.Equal(t, "https://github.com/user/repo/tree/aaaa", wl.CommitWebLink)
16+
t.Run("GitHubRepo", func(t *testing.T) {
17+
sf := NewCommitSubmoduleFile("[email protected]:user/repo.git", "aaaa")
1818

19-
wl = sf.SubmoduleWebLink(t.Context(), "1111")
20-
assert.Equal(t, "https://github.com/user/repo", wl.RepoWebLink)
21-
assert.Equal(t, "https://github.com/user/repo/tree/1111", wl.CommitWebLink)
19+
wl := sf.SubmoduleWebLink(t.Context())
20+
assert.Equal(t, "https://github.com/user/repo", wl.RepoWebLink)
21+
assert.Equal(t, "https://github.com/user/repo/tree/aaaa", wl.CommitWebLink)
2222

23-
wl = sf.SubmoduleWebLink(t.Context(), "1111", "2222")
24-
assert.Equal(t, "https://github.com/user/repo", wl.RepoWebLink)
25-
assert.Equal(t, "https://github.com/user/repo/compare/1111...2222", wl.CommitWebLink)
23+
wl = sf.SubmoduleWebLink(t.Context(), "1111")
24+
assert.Equal(t, "https://github.com/user/repo", wl.RepoWebLink)
25+
assert.Equal(t, "https://github.com/user/repo/tree/1111", wl.CommitWebLink)
2626

27-
wl = (*CommitSubmoduleFile)(nil).SubmoduleWebLink(t.Context())
28-
assert.Nil(t, wl)
27+
wl = sf.SubmoduleWebLink(t.Context(), "1111", "2222")
28+
assert.Equal(t, "https://github.com/user/repo", wl.RepoWebLink)
29+
assert.Equal(t, "https://github.com/user/repo/compare/1111...2222", wl.CommitWebLink)
30+
})
31+
32+
t.Run("RelativePath", func(t *testing.T) {
33+
sf := NewCommitSubmoduleFile("../../user/repo", "aaaa")
34+
wl := sf.SubmoduleWebLink(t.Context())
35+
assert.Equal(t, "../../user/repo", wl.RepoWebLink)
36+
assert.Equal(t, "../../user/repo/tree/aaaa", wl.CommitWebLink)
37+
})
2938
}

modules/setting/oauth2.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"code.gitea.io/gitea/modules/log"
1313
)
1414

15-
// OAuth2UsernameType is enum describing the way gitea 'name' should be generated from oauth2 data
15+
// OAuth2UsernameType is enum describing the way gitea generates its 'username' from oauth2 data
1616
type OAuth2UsernameType string
1717

1818
const (

0 commit comments

Comments
 (0)