]> source.dussan.org Git - gitea.git/commitdiff
Fix private repo permission problem (#16142)
authorNils Dralle <nils.91@googlemail.com>
Mon, 14 Jun 2021 18:30:35 +0000 (20:30 +0200)
committerGitHub <noreply@github.com>
Mon, 14 Jun 2021 18:30:35 +0000 (14:30 -0400)
* Change user access permission

* Add string 'transfer_notices_3'

* Add 3rd transfer note to transfer dialog

* Add test

Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: techknowlogick <techknowlogick@gitea.io>
options/locale/locale_en-US.ini
services/repository/transfer.go
services/repository/transfer_test.go
templates/repo/settings/options.tmpl

index c6d8d1f6128006a4bbea1eaed78858d9a38935ce..cc678e1a73f54d420064540ad7851d690694ee38 100644 (file)
@@ -1625,6 +1625,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"
index ec769190bdb49ae117df908b8c265d33400c1c71..bb323c1c0a23bd007d48b921d9221a3f9671f7bc 100644 (file)
@@ -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 {
index 052b8c9954285bb18a310ad8fd823044ca0d128b..c92844674c6d47ef71054141620382e7e5cc65fe 100644 (file)
@@ -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{})
+}
index 163a1a66d2a8d3edd8de6b96ae97b52f51006a69..eb76a3b7200643a97ea4dc03a9b3278cabf658e6 100644 (file)
                <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}}