Skip to content

Commit e3d3360

Browse files
Allow pipeline group admins to create or update scms. (#2760)
1 parent 6ebcde1 commit e3d3360

File tree

5 files changed

+59
-19
lines changed

5 files changed

+59
-19
lines changed

server/src/com/thoughtworks/go/config/update/CreateSCMConfigCommand.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,4 @@ public void update(CruiseConfig modifiedConfig) throws Exception {
3838
scms.add(globalScmConfig);
3939
modifiedConfig.setSCMs(scms);
4040
}
41-
42-
@Override
43-
public boolean canContinue(CruiseConfig cruiseConfig) {
44-
if (!(goConfigService.isUserAdmin(currentUser)) || goConfigService.isGroupAdministrator(currentUser.getUsername())) {
45-
result.unauthorized(LocalizedMessage.string("UNAUTHORIZED_TO_EDIT"), HealthStateType.unauthorised());
46-
return false;
47-
}
48-
return true;
49-
}
50-
5141
}

server/src/com/thoughtworks/go/config/update/SCMConfigCommand.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,17 @@ public SCM getPreprocessedEntityConfig() {
6868
public void clearErrors() {
6969
BasicCruiseConfig.clearErrors(globalScmConfig);
7070
}
71+
72+
@Override
73+
public boolean canContinue(CruiseConfig cruiseConfig) {
74+
return isAuthorized();
75+
}
76+
77+
private boolean isAuthorized() {
78+
if (!(goConfigService.isUserAdmin(currentUser) || goConfigService.isGroupAdministrator(currentUser.getUsername()))) {
79+
result.unauthorized(LocalizedMessage.string("UNAUTHORIZED_TO_EDIT"), HealthStateType.unauthorised());
80+
return false;
81+
}
82+
return true;
83+
}
7184
}

server/src/com/thoughtworks/go/config/update/UpdateSCMConfigCommand.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void update(CruiseConfig modifiedConfig) throws Exception {
5050

5151
@Override
5252
public boolean canContinue(CruiseConfig cruiseConfig) {
53-
return isUserAuthorized() && isRequestFresh(cruiseConfig);
53+
return super.canContinue(cruiseConfig) && isRequestFresh(cruiseConfig);
5454
}
5555

5656
private boolean isRequestFresh(CruiseConfig cruiseConfig) {
@@ -63,14 +63,6 @@ private boolean isRequestFresh(CruiseConfig cruiseConfig) {
6363
return freshRequest;
6464
}
6565

66-
private boolean isUserAuthorized() {
67-
if (!(goConfigService.isUserAdmin(currentUser)) || goConfigService.isGroupAdministrator(currentUser.getUsername())) {
68-
result.unauthorized(LocalizedMessage.string("UNAUTHORIZED_TO_EDIT"), HealthStateType.unauthorised());
69-
return false;
70-
}
71-
return true;
72-
}
73-
7466
private SCM findSCM(CruiseConfig modifiedConfig) {
7567
SCMs scms = modifiedConfig.getSCMs();
7668
SCM existingSCM = scms.find(globalScmConfig.getSCMId());

server/test/unit/com/thoughtworks/go/config/update/CreateSCMConfigCommandTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,24 @@ public void shouldNotContinueWithConfigSaveIfUserIsUnauthorized() {
7979
assertThat(command.canContinue(cruiseConfig), is(false));
8080
}
8181

82+
@Test
83+
public void shouldContinueWithConfigSaveIfUserIsAdmin() {
84+
when(goConfigService.isUserAdmin(currentUser)).thenReturn(true);
85+
when(goConfigService.isGroupAdministrator(currentUser.getUsername())).thenReturn(false);
86+
87+
CreateSCMConfigCommand command = new CreateSCMConfigCommand(scm, pluggableScmService, result, currentUser, goConfigService);
88+
89+
assertThat(command.canContinue(cruiseConfig), is(true));
90+
}
91+
92+
@Test
93+
public void shouldContinueWithConfigSaveIfUserIsGroupAdmin() {
94+
when(goConfigService.isUserAdmin(currentUser)).thenReturn(false);
95+
when(goConfigService.isGroupAdministrator(currentUser.getUsername())).thenReturn(true);
96+
97+
CreateSCMConfigCommand command = new CreateSCMConfigCommand(scm, pluggableScmService, result, currentUser, goConfigService);
98+
99+
assertThat(command.canContinue(cruiseConfig), is(true));
100+
}
101+
82102
}

server/test/unit/com/thoughtworks/go/config/update/UpdateSCMConfigCommandTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import static org.hamcrest.core.Is.is;
3939
import static org.hamcrest.core.StringContains.containsString;
4040
import static org.junit.Assert.*;
41+
import static org.mockito.Matchers.any;
4142
import static org.mockito.Mockito.when;
4243
import static org.mockito.MockitoAnnotations.initMocks;
4344

@@ -107,6 +108,30 @@ public void shouldNotContinueWithConfigSaveIfUserIsUnauthorized() {
107108
assertThat(result.toString(), containsString("UNAUTHORIZED_TO_EDIT"));
108109
}
109110

111+
@Test
112+
public void shouldContinueWithConfigSaveIfUserIsAdmin() {
113+
when(goConfigService.isUserAdmin(currentUser)).thenReturn(true);
114+
when(goConfigService.isGroupAdministrator(currentUser.getUsername())).thenReturn(false);
115+
when(entityHashingService.md5ForEntity(any(SCM.class))).thenReturn("md5");
116+
117+
SCM updatedScm = new SCM("id", new PluginConfiguration("plugin-id", "1"), new Configuration(new ConfigurationProperty(new ConfigurationKey("key1"),new ConfigurationValue("value1"))));
118+
UpdateSCMConfigCommand command = new UpdateSCMConfigCommand(updatedScm, pluggableScmService, goConfigService, currentUser, result, "md5", entityHashingService);
119+
120+
assertThat(command.canContinue(cruiseConfig), is(true));
121+
}
122+
123+
@Test
124+
public void shouldContinueWithConfigSaveIfUserIsGroupAdmin() {
125+
when(goConfigService.isUserAdmin(currentUser)).thenReturn(false);
126+
when(goConfigService.isGroupAdministrator(currentUser.getUsername())).thenReturn(true);
127+
when(entityHashingService.md5ForEntity(any(SCM.class))).thenReturn("md5");
128+
129+
SCM updatedScm = new SCM("id", new PluginConfiguration("plugin-id", "1"), new Configuration(new ConfigurationProperty(new ConfigurationKey("key1"),new ConfigurationValue("value1"))));
130+
UpdateSCMConfigCommand command = new UpdateSCMConfigCommand(updatedScm, pluggableScmService, goConfigService, currentUser, result, "md5", entityHashingService);
131+
132+
assertThat(command.canContinue(cruiseConfig), is(true));
133+
}
134+
110135
@Test
111136
public void shouldNotContinueWithConfigSaveIfRequestIsNotFresh() {
112137
when(goConfigService.isUserAdmin(currentUser)).thenReturn(true);

0 commit comments

Comments
 (0)