-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Fix org admin can not access org project unit #25691
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
Closed
Closed
Changes from 20 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
4593393
fix logic
yp05327 6ea4173
add migration
yp05327 c36b931
fix lint
yp05327 82dc31f
fix lint
yp05327 74129a3
fix sql
yp05327 a1e5db3
rename to v267
yp05327 738a4ae
Merge branch 'main' into fix-25670
yp05327 169ae50
rename to v268
yp05327 273327f
Merge branch 'main' into fix-25670
yp05327 4de3b01
fix lint
yp05327 b4b72e7
rename to v270
yp05327 31058b1
Merge branch 'main' into fix-25670
yp05327 0bd77f1
rename to v271
yp05327 e3ffe02
Merge branch 'main' into fix-25670
yp05327 c5d335a
rename to v283
yp05327 d956ce8
Merge branch 'main' into fix-25670
yp05327 c60d25e
Update models/migrations/v1_22/v283.go
yp05327 9a9c144
fix
yp05327 44f0666
rename migration
yp05327 7e3b48f
Merge branch 'main' into fix-25670
yp05327 eb8a311
avoid a too big transaction
yp05327 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// Copyright 2023 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package v1_22 //nolint | ||
|
||
import ( | ||
"code.gitea.io/gitea/modules/container" | ||
|
||
"xorm.io/xorm" | ||
) | ||
|
||
func FixMissingAdminTeamUnitRecords(x *xorm.Engine) error { | ||
type UnitType int | ||
type AccessMode int | ||
|
||
type Team struct { | ||
ID int64 `xorm:"pk autoincr"` | ||
OrgID int64 `xorm:"INDEX"` | ||
AccessMode AccessMode `xorm:"'authorize'"` | ||
} | ||
|
||
type TeamUnit struct { | ||
ID int64 `xorm:"pk autoincr"` | ||
OrgID int64 `xorm:"INDEX"` | ||
TeamID int64 `xorm:"UNIQUE(s)"` | ||
Type UnitType `xorm:"UNIQUE(s)"` | ||
AccessMode AccessMode | ||
} | ||
|
||
const ( | ||
// AccessModeRead read access | ||
AccessModeRead = 1 | ||
// AccessModeAdmin admin access | ||
AccessModeAdmin = 3 | ||
|
||
// Unit Type | ||
TypeInvalid UnitType = iota // 0 invalid | ||
TypeCode // 1 code | ||
TypeIssues // 2 issues | ||
TypePullRequests // 3 PRs | ||
TypeReleases // 4 Releases | ||
TypeWiki // 5 Wiki | ||
TypeExternalWiki // 6 ExternalWiki | ||
TypeExternalTracker // 7 ExternalTracker | ||
TypeProjects // 8 Kanban board | ||
TypePackages // 9 Packages | ||
TypeActions // 10 Actions | ||
) | ||
|
||
AllRepoUnitTypes := []UnitType{ | ||
TypeCode, | ||
TypeIssues, | ||
TypePullRequests, | ||
TypeReleases, | ||
TypeWiki, | ||
TypeExternalWiki, | ||
TypeExternalTracker, | ||
TypeProjects, | ||
TypePackages, | ||
TypeActions, | ||
} | ||
|
||
sess := x.NewSession() | ||
defer sess.Close() | ||
|
||
if err := sess.Begin(); err != nil { | ||
return err | ||
} | ||
|
||
// find all admin teams | ||
teams := make([]*Team, 0) | ||
err := sess.Where("team.authorize = ?", AccessModeAdmin).Find(&teams) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, team := range teams { | ||
// find all existing records | ||
teamunits := make([]*TeamUnit, 0, len(AllRepoUnitTypes)) | ||
err := sess.Where("`team_unit`.team_id = ?", team.ID).Find(&teamunits) | ||
if err != nil { | ||
return err | ||
} | ||
existingUnitTypes := make(container.Set[UnitType], 0) | ||
for _, tu := range teamunits { | ||
if tu.Type > 0 { | ||
existingUnitTypes.Add(tu.Type) | ||
} | ||
} | ||
|
||
// insert or update records | ||
for _, u := range AllRepoUnitTypes { | ||
newTeamUnit := &TeamUnit{ | ||
OrgID: team.OrgID, | ||
TeamID: team.ID, | ||
Type: u, | ||
} | ||
// external unit should be read | ||
if u == TypeExternalWiki || u == TypeExternalTracker { | ||
newTeamUnit.AccessMode = AccessModeRead | ||
} else { | ||
newTeamUnit.AccessMode = AccessModeAdmin | ||
} | ||
|
||
if existingUnitTypes.Contains(u) { | ||
sess.Cols("access_mode").Update(newTeamUnit) | ||
} else { | ||
sess.Insert(newTeamUnit) | ||
} | ||
} | ||
} | ||
|
||
return sess.Commit() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to detect whether there are still records haven't been committed. Some database will fail if submit a zero change commit. |
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may consume much memory for a big instance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.