Skip to content

Commit a1a6b8e

Browse files
authored
Merge branch 'main' into any-assignee
2 parents 9576848 + 5407382 commit a1a6b8e

File tree

23 files changed

+135
-79
lines changed

23 files changed

+135
-79
lines changed

cmd/admin_auth_ldap.go

+49
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,34 @@ var (
127127
&cli.UintFlag{
128128
Name: "page-size",
129129
Usage: "Search page size.",
130+
},
131+
&cli.BoolFlag{
132+
Name: "enable-groups",
133+
Usage: "Enable LDAP groups",
134+
},
135+
&cli.StringFlag{
136+
Name: "group-search-base-dn",
137+
Usage: "The LDAP base DN at which group accounts will be searched for",
138+
},
139+
&cli.StringFlag{
140+
Name: "group-member-attribute",
141+
Usage: "Group attribute containing list of users",
142+
},
143+
&cli.StringFlag{
144+
Name: "group-user-attribute",
145+
Usage: "User attribute listed in group",
146+
},
147+
&cli.StringFlag{
148+
Name: "group-filter",
149+
Usage: "Verify group membership in LDAP",
150+
},
151+
&cli.StringFlag{
152+
Name: "group-team-map",
153+
Usage: "Map LDAP groups to Organization teams",
154+
},
155+
&cli.BoolFlag{
156+
Name: "group-team-map-removal",
157+
Usage: "Remove users from synchronized teams if user does not belong to corresponding LDAP group",
130158
})
131159

132160
ldapSimpleAuthCLIFlags = append(commonLdapCLIFlags,
@@ -273,6 +301,27 @@ func parseLdapConfig(c *cli.Context, config *ldap.Source) error {
273301
if c.IsSet("skip-local-2fa") {
274302
config.SkipLocalTwoFA = c.Bool("skip-local-2fa")
275303
}
304+
if c.IsSet("enable-groups") {
305+
config.GroupsEnabled = c.Bool("enable-groups")
306+
}
307+
if c.IsSet("group-search-base-dn") {
308+
config.GroupDN = c.String("group-search-base-dn")
309+
}
310+
if c.IsSet("group-member-attribute") {
311+
config.GroupMemberUID = c.String("group-member-attribute")
312+
}
313+
if c.IsSet("group-user-attribute") {
314+
config.UserUID = c.String("group-user-attribute")
315+
}
316+
if c.IsSet("group-filter") {
317+
config.GroupFilter = c.String("group-filter")
318+
}
319+
if c.IsSet("group-team-map") {
320+
config.GroupTeamMap = c.String("group-team-map")
321+
}
322+
if c.IsSet("group-team-map-removal") {
323+
config.GroupTeamMapRemoval = c.Bool("group-team-map-removal")
324+
}
276325
return nil
277326
}
278327

cmd/admin_auth_ldap_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ func TestAddLdapBindDn(t *testing.T) {
5151
"--attributes-in-bind",
5252
"--synchronize-users",
5353
"--page-size", "99",
54+
"--enable-groups",
55+
"--group-search-base-dn", "ou=group,dc=full-domain-bind,dc=org",
56+
"--group-member-attribute", "memberUid",
57+
"--group-user-attribute", "uid",
58+
"--group-filter", "(|(cn=gitea_users)(cn=admins))",
59+
"--group-team-map", `{"cn=my-group,cn=groups,dc=example,dc=org": {"MyGiteaOrganization": ["MyGiteaTeam1", "MyGiteaTeam2"]}}`,
60+
"--group-team-map-removal",
5461
},
5562
source: &auth.Source{
5663
Type: auth.LDAP,
@@ -78,6 +85,13 @@ func TestAddLdapBindDn(t *testing.T) {
7885
AdminFilter: "(memberOf=cn=admin-group,ou=example,dc=full-domain-bind,dc=org)",
7986
RestrictedFilter: "(memberOf=cn=restricted-group,ou=example,dc=full-domain-bind,dc=org)",
8087
Enabled: true,
88+
GroupsEnabled: true,
89+
GroupDN: "ou=group,dc=full-domain-bind,dc=org",
90+
GroupMemberUID: "memberUid",
91+
UserUID: "uid",
92+
GroupFilter: "(|(cn=gitea_users)(cn=admins))",
93+
GroupTeamMap: `{"cn=my-group,cn=groups,dc=example,dc=org": {"MyGiteaOrganization": ["MyGiteaTeam1", "MyGiteaTeam2"]}}`,
94+
GroupTeamMapRemoval: true,
8195
},
8296
},
8397
},
@@ -510,6 +524,13 @@ func TestUpdateLdapBindDn(t *testing.T) {
510524
"--bind-password", "secret-bind-full",
511525
"--synchronize-users",
512526
"--page-size", "99",
527+
"--enable-groups",
528+
"--group-search-base-dn", "ou=group,dc=full-domain-bind,dc=org",
529+
"--group-member-attribute", "memberUid",
530+
"--group-user-attribute", "uid",
531+
"--group-filter", "(|(cn=gitea_users)(cn=admins))",
532+
"--group-team-map", `{"cn=my-group,cn=groups,dc=example,dc=org": {"MyGiteaOrganization": ["MyGiteaTeam1", "MyGiteaTeam2"]}}`,
533+
"--group-team-map-removal",
513534
},
514535
id: 23,
515536
existingAuthSource: &auth.Source{
@@ -545,6 +566,13 @@ func TestUpdateLdapBindDn(t *testing.T) {
545566
AdminFilter: "(memberOf=cn=admin-group,ou=example,dc=full-domain-bind,dc=org)",
546567
RestrictedFilter: "(memberOf=cn=restricted-group,ou=example,dc=full-domain-bind,dc=org)",
547568
Enabled: true,
569+
GroupsEnabled: true,
570+
GroupDN: "ou=group,dc=full-domain-bind,dc=org",
571+
GroupMemberUID: "memberUid",
572+
UserUID: "uid",
573+
GroupFilter: "(|(cn=gitea_users)(cn=admins))",
574+
GroupTeamMap: `{"cn=my-group,cn=groups,dc=example,dc=org": {"MyGiteaOrganization": ["MyGiteaTeam1", "MyGiteaTeam2"]}}`,
575+
GroupTeamMapRemoval: true,
548576
},
549577
},
550578
},

models/repo/repo.go

+16-4
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,24 @@ func init() {
215215
db.RegisterModel(new(Repository))
216216
}
217217

218-
func (repo *Repository) GetName() string {
219-
return repo.Name
218+
func RelativePath(ownerName, repoName string) string {
219+
return strings.ToLower(ownerName) + "/" + strings.ToLower(repoName) + ".git"
220220
}
221221

222-
func (repo *Repository) GetOwnerName() string {
223-
return repo.OwnerName
222+
// RelativePath should be an unix style path like username/reponame.git
223+
func (repo *Repository) RelativePath() string {
224+
return RelativePath(repo.OwnerName, repo.Name)
225+
}
226+
227+
type StorageRepo string
228+
229+
// RelativePath should be an unix style path like username/reponame.git
230+
func (sr StorageRepo) RelativePath() string {
231+
return string(sr)
232+
}
233+
234+
func (repo *Repository) WikiStorageRepo() StorageRepo {
235+
return StorageRepo(strings.ToLower(repo.OwnerName) + "/" + strings.ToLower(repo.Name) + ".wiki.git")
224236
}
225237

226238
// SanitizedOriginalURL returns a sanitized OriginalURL

modules/gitrepo/branch.go

-12
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,12 @@ func GetDefaultBranch(ctx context.Context, repo Repository) (string, error) {
4444
return git.GetDefaultBranch(ctx, repoPath(repo))
4545
}
4646

47-
func GetWikiDefaultBranch(ctx context.Context, repo Repository) (string, error) {
48-
return git.GetDefaultBranch(ctx, wikiPath(repo))
49-
}
50-
5147
// IsReferenceExist returns true if given reference exists in the repository.
5248
func IsReferenceExist(ctx context.Context, repo Repository, name string) bool {
5349
return git.IsReferenceExist(ctx, repoPath(repo), name)
5450
}
5551

56-
func IsWikiReferenceExist(ctx context.Context, repo Repository, name string) bool {
57-
return git.IsReferenceExist(ctx, wikiPath(repo), name)
58-
}
59-
6052
// IsBranchExist returns true if given branch exists in the repository.
6153
func IsBranchExist(ctx context.Context, repo Repository, name string) bool {
6254
return IsReferenceExist(ctx, repo, git.BranchPrefix+name)
6355
}
64-
65-
func IsWikiBranchExist(ctx context.Context, repo Repository, name string) bool {
66-
return IsWikiReferenceExist(ctx, repo, git.BranchPrefix+name)
67-
}

modules/gitrepo/gitrepo.go

+7-19
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,29 @@ import (
88
"fmt"
99
"io"
1010
"path/filepath"
11-
"strings"
1211

1312
"code.gitea.io/gitea/modules/git"
1413
"code.gitea.io/gitea/modules/reqctx"
1514
"code.gitea.io/gitea/modules/setting"
1615
"code.gitea.io/gitea/modules/util"
1716
)
1817

18+
// Repository represents a git repository which stored in a disk
1919
type Repository interface {
20-
GetName() string
21-
GetOwnerName() string
22-
}
23-
24-
func absPath(owner, name string) string {
25-
return filepath.Join(setting.RepoRootPath, strings.ToLower(owner), strings.ToLower(name)+".git")
20+
RelativePath() string // We don't assume how the directory structure of the repository is, so we only need the relative path
2621
}
2722

23+
// RelativePath should be an unix style path like username/reponame.git
24+
// This method should change it according to the current OS.
2825
func repoPath(repo Repository) string {
29-
return absPath(repo.GetOwnerName(), repo.GetName())
30-
}
31-
32-
func wikiPath(repo Repository) string {
33-
return filepath.Join(setting.RepoRootPath, strings.ToLower(repo.GetOwnerName()), strings.ToLower(repo.GetName())+".wiki.git")
26+
return filepath.Join(setting.RepoRootPath, filepath.FromSlash(repo.RelativePath()))
3427
}
3528

3629
// OpenRepository opens the repository at the given relative path with the provided context.
3730
func OpenRepository(ctx context.Context, repo Repository) (*git.Repository, error) {
3831
return git.OpenRepository(ctx, repoPath(repo))
3932
}
4033

41-
func OpenWikiRepository(ctx context.Context, repo Repository) (*git.Repository, error) {
42-
return git.OpenRepository(ctx, wikiPath(repo))
43-
}
44-
4534
// contextKey is a value for use with context.WithValue.
4635
type contextKey struct {
4736
repoPath string
@@ -86,9 +75,8 @@ func DeleteRepository(ctx context.Context, repo Repository) error {
8675
}
8776

8877
// RenameRepository renames a repository's name on disk
89-
func RenameRepository(ctx context.Context, repo Repository, newName string) error {
90-
newRepoPath := absPath(repo.GetOwnerName(), newName)
91-
if err := util.Rename(repoPath(repo), newRepoPath); err != nil {
78+
func RenameRepository(ctx context.Context, repo, newRepo Repository) error {
79+
if err := util.Rename(repoPath(repo), repoPath(newRepo)); err != nil {
9280
return fmt.Errorf("rename repository directory: %w", err)
9381
}
9482
return nil

modules/gitrepo/hooks.go

+4-14
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,11 @@ done
106106
return hookNames, hookTpls, giteaHookTpls
107107
}
108108

109-
// CreateDelegateHooksForRepo creates all the hooks scripts for the repo
110-
func CreateDelegateHooksForRepo(_ context.Context, repo Repository) (err error) {
109+
// CreateDelegateHooks creates all the hooks scripts for the repo
110+
func CreateDelegateHooks(_ context.Context, repo Repository) (err error) {
111111
return createDelegateHooks(filepath.Join(repoPath(repo), "hooks"))
112112
}
113113

114-
// CreateDelegateHooksForWiki creates all the hooks scripts for the wiki repo
115-
func CreateDelegateHooksForWiki(_ context.Context, repo Repository) (err error) {
116-
return createDelegateHooks(filepath.Join(wikiPath(repo), "hooks"))
117-
}
118-
119114
func createDelegateHooks(hookDir string) (err error) {
120115
hookNames, hookTpls, giteaHookTpls := getHookTemplates()
121116

@@ -178,16 +173,11 @@ func ensureExecutable(filename string) error {
178173
return os.Chmod(filename, mode)
179174
}
180175

181-
// CheckDelegateHooksForRepo checks the hooks scripts for the repo
182-
func CheckDelegateHooksForRepo(_ context.Context, repo Repository) ([]string, error) {
176+
// CheckDelegateHooks checks the hooks scripts for the repo
177+
func CheckDelegateHooks(_ context.Context, repo Repository) ([]string, error) {
183178
return checkDelegateHooks(filepath.Join(repoPath(repo), "hooks"))
184179
}
185180

186-
// CheckDelegateHooksForWiki checks the hooks scripts for the repo
187-
func CheckDelegateHooksForWiki(_ context.Context, repo Repository) ([]string, error) {
188-
return checkDelegateHooks(filepath.Join(wikiPath(repo), "hooks"))
189-
}
190-
191181
func checkDelegateHooks(hookDir string) ([]string, error) {
192182
hookNames, hookTpls, giteaHookTpls := getHookTemplates()
193183

modules/repository/init.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func CheckInitRepository(ctx context.Context, repo *repo_model.Repository) (err
138138
// Init git bare new repository.
139139
if err = git.InitRepository(ctx, repo.RepoPath(), true, repo.ObjectFormatName); err != nil {
140140
return fmt.Errorf("git.InitRepository: %w", err)
141-
} else if err = gitrepo.CreateDelegateHooksForRepo(ctx, repo); err != nil {
141+
} else if err = gitrepo.CreateDelegateHooks(ctx, repo); err != nil {
142142
return fmt.Errorf("createDelegateHooks: %w", err)
143143
}
144144
return nil

routers/api/v1/repo/wiki.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ func findEntryForFile(commit *git.Commit, target string) (*git.TreeEntry, error)
476476
// findWikiRepoCommit opens the wiki repo and returns the latest commit, writing to context on error.
477477
// The caller is responsible for closing the returned repo again
478478
func findWikiRepoCommit(ctx *context.APIContext) (*git.Repository, *git.Commit) {
479-
wikiRepo, err := gitrepo.OpenWikiRepository(ctx, ctx.Repo.Repository)
479+
wikiRepo, err := gitrepo.OpenRepository(ctx, ctx.Repo.Repository.WikiStorageRepo())
480480
if err != nil {
481481
if git.IsErrNotExist(err) || err.Error() == "no such file or directory" {
482482
ctx.APIErrorNotFound(err)

routers/web/repo/commit.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ func Diff(ctx *context.Context) {
284284
)
285285

286286
if ctx.Data["PageIsWiki"] != nil {
287-
gitRepo, err = gitrepo.OpenWikiRepository(ctx, ctx.Repo.Repository)
287+
gitRepo, err = gitrepo.OpenRepository(ctx, ctx.Repo.Repository.WikiStorageRepo())
288288
if err != nil {
289289
ctx.ServerError("Repo.GitRepo.GetCommit", err)
290290
return
@@ -417,7 +417,7 @@ func Diff(ctx *context.Context) {
417417
func RawDiff(ctx *context.Context) {
418418
var gitRepo *git.Repository
419419
if ctx.Data["PageIsWiki"] != nil {
420-
wikiRepo, err := gitrepo.OpenWikiRepository(ctx, ctx.Repo.Repository)
420+
wikiRepo, err := gitrepo.OpenRepository(ctx, ctx.Repo.Repository.WikiStorageRepo())
421421
if err != nil {
422422
ctx.ServerError("OpenRepository", err)
423423
return

routers/web/repo/compare.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ func ExcerptBlob(ctx *context.Context) {
885885
gitRepo := ctx.Repo.GitRepo
886886
if ctx.Data["PageIsWiki"] == true {
887887
var err error
888-
gitRepo, err = gitrepo.OpenWikiRepository(ctx, ctx.Repo.Repository)
888+
gitRepo, err = gitrepo.OpenRepository(ctx, ctx.Repo.Repository.WikiStorageRepo())
889889
if err != nil {
890890
ctx.ServerError("OpenRepository", err)
891891
return

routers/web/repo/wiki.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func findEntryForFile(commit *git.Commit, target string) (*git.TreeEntry, error)
9696
}
9797

9898
func findWikiRepoCommit(ctx *context.Context) (*git.Repository, *git.Commit, error) {
99-
wikiGitRepo, errGitRepo := gitrepo.OpenWikiRepository(ctx, ctx.Repo.Repository)
99+
wikiGitRepo, errGitRepo := gitrepo.OpenRepository(ctx, ctx.Repo.Repository.WikiStorageRepo())
100100
if errGitRepo != nil {
101101
ctx.ServerError("OpenRepository", errGitRepo)
102102
return nil, nil, errGitRepo
@@ -105,7 +105,7 @@ func findWikiRepoCommit(ctx *context.Context) (*git.Repository, *git.Commit, err
105105
commit, errCommit := wikiGitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultWikiBranch)
106106
if git.IsErrNotExist(errCommit) {
107107
// if the default branch recorded in database is out of sync, then re-sync it
108-
gitRepoDefaultBranch, errBranch := gitrepo.GetWikiDefaultBranch(ctx, ctx.Repo.Repository)
108+
gitRepoDefaultBranch, errBranch := gitrepo.GetDefaultBranch(ctx, ctx.Repo.Repository.WikiStorageRepo())
109109
if errBranch != nil {
110110
return wikiGitRepo, nil, errBranch
111111
}

routers/web/repo/wiki_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const (
2929
)
3030

3131
func wikiEntry(t *testing.T, repo *repo_model.Repository, wikiName wiki_service.WebPath) *git.TreeEntry {
32-
wikiRepo, err := gitrepo.OpenWikiRepository(git.DefaultContext, repo)
32+
wikiRepo, err := gitrepo.OpenRepository(git.DefaultContext, repo.WikiStorageRepo())
3333
assert.NoError(t, err)
3434
defer wikiRepo.Close()
3535
commit, err := wikiRepo.GetBranchCommit("master")

services/asymkey/sign.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ Loop:
204204
return false, "", nil, &ErrWontSign{twofa}
205205
}
206206
case parentSigned:
207-
gitRepo, err := gitrepo.OpenWikiRepository(ctx, repo)
207+
gitRepo, err := gitrepo.OpenRepository(ctx, repo.WikiStorageRepo())
208208
if err != nil {
209209
return false, "", nil, err
210210
}

services/doctor/misc.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ func checkScriptType(ctx context.Context, logger log.Logger, autofix bool) error
4949

5050
func checkHooks(ctx context.Context, logger log.Logger, autofix bool) error {
5151
if err := iterateRepositories(ctx, func(repo *repo_model.Repository) error {
52-
results, err := gitrepo.CheckDelegateHooksForRepo(ctx, repo)
52+
results, err := gitrepo.CheckDelegateHooks(ctx, repo)
5353
if err != nil {
5454
logger.Critical("Unable to check delegate hooks for repo %-v. ERROR: %v", repo, err)
5555
return fmt.Errorf("Unable to check delegate hooks for repo %-v. ERROR: %w", repo, err)
5656
}
5757
if len(results) > 0 && autofix {
5858
logger.Warn("Regenerated hooks for %s", repo.FullName())
59-
if err := gitrepo.CreateDelegateHooksForRepo(ctx, repo); err != nil {
59+
if err := gitrepo.CreateDelegateHooks(ctx, repo); err != nil {
6060
logger.Critical("Unable to recreate delegate hooks for %-v. ERROR: %v", repo, err)
6161
return fmt.Errorf("Unable to recreate delegate hooks for %-v. ERROR: %w", repo, err)
6262
}

services/mirror/mirror_push.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func runPushSync(ctx context.Context, m *repo_model.PushMirror) error {
143143

144144
var gitRepo *git.Repository
145145
if isWiki {
146-
gitRepo, err = gitrepo.OpenWikiRepository(ctx, repo)
146+
gitRepo, err = gitrepo.OpenRepository(ctx, repo.WikiStorageRepo())
147147
} else {
148148
gitRepo, err = gitrepo.OpenRepository(ctx, repo)
149149
}

services/repository/adopt.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func adoptRepository(ctx context.Context, repo *repo_model.Repository, defaultBr
115115
return fmt.Errorf("adoptRepository: path does not already exist: %s", repo.FullName())
116116
}
117117

118-
if err := gitrepo.CreateDelegateHooksForRepo(ctx, repo); err != nil {
118+
if err := gitrepo.CreateDelegateHooks(ctx, repo); err != nil {
119119
return fmt.Errorf("createDelegateHooks: %w", err)
120120
}
121121

services/repository/fork.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func ForkRepository(ctx context.Context, doer, owner *user_model.User, opts Fork
170170
return fmt.Errorf("git update-server-info: %w", err)
171171
}
172172

173-
if err = gitrepo.CreateDelegateHooksForRepo(ctx, repo); err != nil {
173+
if err = gitrepo.CreateDelegateHooks(ctx, repo); err != nil {
174174
return fmt.Errorf("createDelegateHooks: %w", err)
175175
}
176176

0 commit comments

Comments
 (0)