Skip to content
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

Fix private repo permission problem #16142

Merged
merged 7 commits into from
Jun 14, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1613,6 +1613,7 @@ settings.transfer_form_title = Enter the repository name as confirmation:
settings.transfer_in_progress = There is currently an ongoing transfer. Please cancel it if you will like to transfer this repository to another user.
settings.transfer_notices_1 = - You will lose access to the repository if you transfer it to an individual user.
settings.transfer_notices_2 = - You will keep access to the repository if you transfer it to an organization that you (co-)own.
settings.transfer_notices_3 = - If the repository is private and is transferred to an individual user, this action makes sure that the user does have at least read permission (and changes permissions if necessary).
settings.transfer_owner = New Owner
settings.transfer_perform = Perform Transfer
settings.transfer_started = This repository has been marked for transfer and awaits confirmation from "%s"
Expand Down
14 changes: 14 additions & 0 deletions services/repository/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,20 @@ func StartRepositoryTransfer(doer, newOwner *models.User, repo *models.Repositor
}
}

// In case the new owner would not have sufficient access to the repo, give access rights for read
hasAccess, err := models.HasAccess(newOwner.ID, repo)
if err != nil {
return err
}
if !hasAccess {
if err := repo.AddCollaborator(newOwner); err != nil {
return err
}
if err := repo.ChangeCollaborationAccessMode(newOwner.ID, models.AccessModeRead); err != nil {
return err
}
}

// Make repo as pending for transfer
repo.Status = models.RepositoryPendingTransfer
if err := models.CreatePendingRepositoryTransfer(doer, newOwner, repo.ID, teams); err != nil {
Expand Down
21 changes: 21 additions & 0 deletions services/repository/transfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,24 @@ func TestTransferOwnership(t *testing.T) {

models.CheckConsistencyFor(t, &models.Repository{}, &models.User{}, &models.Team{})
}

func TestStartRepositoryTransferSetPermission(t *testing.T) {
assert.NoError(t, models.PrepareTestDatabase())

doer := models.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User)
recipient := models.AssertExistsAndLoadBean(t, &models.User{ID: 5}).(*models.User)
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository)
repo.Owner = models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)

hasAccess, err := models.HasAccess(recipient.ID, repo)
assert.NoError(t, err)
assert.False(t, hasAccess)

assert.NoError(t, StartRepositoryTransfer(doer, recipient, repo, nil))

hasAccess, err = models.HasAccess(recipient.ID, repo)
assert.NoError(t, err)
assert.True(t, hasAccess)

models.CheckConsistencyFor(t, &models.Repository{}, &models.User{}, &models.Team{})
}
3 changes: 2 additions & 1 deletion templates/repo/settings/options.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,8 @@
<div class="content">
<div class="ui warning message text left">
{{.i18n.Tr "repo.settings.transfer_notices_1"}} <br>
{{.i18n.Tr "repo.settings.transfer_notices_2"}}
{{.i18n.Tr "repo.settings.transfer_notices_2"}} <br>
{{.i18n.Tr "repo.settings.transfer_notices_3"}}
</div>
<form class="ui form" action="{{.Link}}" method="post">
{{.CsrfTokenHtml}}
Expand Down