Skip to content

Commit 59b10e6

Browse files
adelowolafriks
authored andcommitted
An inactive user shouldn't be able to be added as a collaborator (#4535)
* an inactive user shouldn't be able to be a collaborator * use translated error message * add active user check when adding a new collaborator via the api * fix translation text * added collaborator test * improvee testcases
1 parent c7a6ee5 commit 59b10e6

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

options/locale/locale_en-US.ini

+2-1
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,8 @@ settings.transfer_succeed = The repository has been transferred.
10251025
settings.confirm_delete = Delete Repository
10261026
settings.add_collaborator = Add Collaborator
10271027
settings.add_collaborator_success = The collaborator has been added.
1028-
settings.add_collaborator_duplicate =The collaborator is already added to this repository.
1028+
settings.add_collaborator_inactive_user = Can not add an inactive user as a collaborator.
1029+
settings.add_collaborator_duplicate = The collaborator is already added to this repository.
10291030
settings.delete_collaborator = Remove
10301031
settings.collaborator_deletion = Remove Collaborator
10311032
settings.collaborator_deletion_desc = Removing a collaborator will revoke their access to this repository. Continue?

routers/api/v1/repo/collaborators.go

+7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
package repo
66

77
import (
8+
"errors"
9+
810
"code.gitea.io/gitea/models"
911
"code.gitea.io/gitea/modules/context"
1012

@@ -145,6 +147,11 @@ func AddCollaborator(ctx *context.APIContext, form api.AddCollaboratorOption) {
145147
return
146148
}
147149

150+
if !collaborator.IsActive {
151+
ctx.Error(500, "InactiveCollaborator", errors.New("collaborator's account is inactive"))
152+
return
153+
}
154+
148155
if err := ctx.Repo.Repository.AddCollaborator(collaborator); err != nil {
149156
ctx.Error(500, "AddCollaborator", err)
150157
return

routers/repo/setting.go

+6
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,12 @@ func CollaborationPost(ctx *context.Context) {
381381
return
382382
}
383383

384+
if !u.IsActive {
385+
ctx.Flash.Error(ctx.Tr("repo.settings.add_collaborator_inactive_user"))
386+
ctx.Redirect(setting.AppSubURL + ctx.Req.URL.Path)
387+
return
388+
}
389+
384390
// Organization is not allowed to be added as a collaborator.
385391
if u.IsOrganization() {
386392
ctx.Flash.Error(ctx.Tr("repo.settings.org_not_allowed_to_be_collaborator"))

routers/repo/settings_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,30 @@ func TestCollaborationPost(t *testing.T) {
9797
assert.True(t, exists)
9898
}
9999

100+
func TestCollaborationPost_InactiveUser(t *testing.T) {
101+
102+
models.PrepareTestEnv(t)
103+
ctx := test.MockContext(t, "user2/repo1/issues/labels")
104+
test.LoadUser(t, ctx, 2)
105+
test.LoadUser(t, ctx, 9)
106+
test.LoadRepo(t, ctx, 1)
107+
108+
ctx.Req.Form.Set("collaborator", "user9")
109+
110+
repo := &context.Repository{
111+
Owner: &models.User{
112+
LowerName: "user2",
113+
},
114+
}
115+
116+
ctx.Repo = repo
117+
118+
CollaborationPost(ctx)
119+
120+
assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
121+
assert.NotEmpty(t, ctx.Flash.ErrorMsg)
122+
}
123+
100124
func TestCollaborationPost_AddCollaboratorTwice(t *testing.T) {
101125

102126
models.PrepareTestEnv(t)

0 commit comments

Comments
 (0)