Skip to content

Commit ec48618

Browse files
Fix bug preventing transfer to private organization (#12497) (#12501)
* Fix bug preventing transfer to private organization The code assessing whether a private organization was visible to a user before allowing transfer was incorrect due to testing membership the wrong way round This PR fixes this issue and renames the function performing the test to be clearer. Further looking at the API for transfer repository - no testing was performed to ensure that the acting user could actually see the new owning organization. Signed-off-by: Andrew Thornton <[email protected]> * change IsUserPartOfOrg everywhere Co-authored-by: zeripath <[email protected]>
1 parent f0dd071 commit ec48618

File tree

5 files changed

+17
-8
lines changed

5 files changed

+17
-8
lines changed

models/org.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ func hasOrgVisible(e Engine, org *User, user *User) bool {
435435
return true
436436
}
437437

438-
if (org.Visibility == structs.VisibleTypePrivate || user.IsRestricted) && !org.isUserPartOfOrg(e, user.ID) {
438+
if (org.Visibility == structs.VisibleTypePrivate || user.IsRestricted) && !org.hasMemberWithUserID(e, user.ID) {
439439
return false
440440
}
441441
return true

models/user.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -609,12 +609,12 @@ func (u *User) IsUserOrgOwner(orgID int64) bool {
609609
return isOwner
610610
}
611611

612-
// IsUserPartOfOrg returns true if user with userID is part of the u organisation.
613-
func (u *User) IsUserPartOfOrg(userID int64) bool {
614-
return u.isUserPartOfOrg(x, userID)
612+
// HasMemberWithUserID returns true if user with userID is part of the u organisation.
613+
func (u *User) HasMemberWithUserID(userID int64) bool {
614+
return u.hasMemberWithUserID(x, userID)
615615
}
616616

617-
func (u *User) isUserPartOfOrg(e Engine, userID int64) bool {
617+
func (u *User) hasMemberWithUserID(e Engine, userID int64) bool {
618618
isMember, err := isOrganizationMember(e, u.ID, userID)
619619
if err != nil {
620620
log.Error("IsOrganizationMember: %v", err)

routers/api/v1/repo/transfer.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"code.gitea.io/gitea/modules/context"
1313
"code.gitea.io/gitea/modules/convert"
1414
"code.gitea.io/gitea/modules/log"
15+
"code.gitea.io/gitea/modules/structs"
1516
api "code.gitea.io/gitea/modules/structs"
1617
repo_service "code.gitea.io/gitea/services/repository"
1718
)
@@ -53,13 +54,21 @@ func Transfer(ctx *context.APIContext, opts api.TransferRepoOption) {
5354
newOwner, err := models.GetUserByName(opts.NewOwner)
5455
if err != nil {
5556
if models.IsErrUserNotExist(err) {
56-
ctx.Error(http.StatusNotFound, "GetUserByName", err)
57+
ctx.Error(http.StatusNotFound, "", "The new owner does not exist or cannot be found")
5758
return
5859
}
5960
ctx.InternalServerError(err)
6061
return
6162
}
6263

64+
if newOwner.Type == models.UserTypeOrganization {
65+
if !ctx.User.IsAdmin && newOwner.Visibility == structs.VisibleTypePrivate && !newOwner.HasMemberWithUserID(ctx.User.ID) {
66+
// The user shouldn't know about this organization
67+
ctx.Error(http.StatusNotFound, "", "The new owner does not exist or cannot be found")
68+
return
69+
}
70+
}
71+
6372
var teams []*models.Team
6473
if opts.TeamIDs != nil {
6574
if !newOwner.IsOrganization() {

routers/repo/setting.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) {
381381
}
382382

383383
if newOwner.Type == models.UserTypeOrganization {
384-
if !ctx.User.IsAdmin && newOwner.Visibility == structs.VisibleTypePrivate && !ctx.User.IsUserPartOfOrg(newOwner.ID) {
384+
if !ctx.User.IsAdmin && newOwner.Visibility == structs.VisibleTypePrivate && !newOwner.HasMemberWithUserID(ctx.User.ID) {
385385
// The user shouldn't know about this organization
386386
ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_owner_name"), tplSettingsOptions, nil)
387387
return

templates/user/profile.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
<li>
5454
<ul class="user-orgs">
5555
{{range .Orgs}}
56-
{{if (or .Visibility.IsPublic (and ($.SignedUser) (or .Visibility.IsLimited (and (.IsUserPartOfOrg $.SignedUserID) .Visibility.IsPrivate) ($.IsAdmin))))}}
56+
{{if (or .Visibility.IsPublic (and ($.SignedUser) (or .Visibility.IsLimited (and (.HasMemberWithUserID $.SignedUserID) .Visibility.IsPrivate) ($.IsAdmin))))}}
5757
<li>
5858
<a href="{{.HomeLink}}"><img class="ui image poping up" src="{{.RelAvatarLink}}" data-content="{{.Name}}" data-position="top center" data-variation="tiny inverted"></a>
5959
</li>

0 commit comments

Comments
 (0)