*/
package org.sonar.server.organization.ws;
+import java.util.Date;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.sonar.api.config.Settings;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
+import org.sonar.api.utils.System2;
+import org.sonar.api.web.UserRole;
import org.sonar.core.config.CorePropertyDefinitions;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.core.util.UuidFactory;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
+import org.sonar.db.organization.DefaultTemplates;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.permission.GroupPermissionDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonar.db.user.GroupDto;
import org.sonar.db.user.UserGroupDto;
import org.sonar.server.user.UserSession;
private static final String ACTION = "create";
private static final String OWNERS_GROUP_NAME = "Owners";
private static final String OWNERS_GROUP_DESCRIPTION_PATTERN = "Owners of organization %s";
+ private static final String PERM_TEMPLATE_DESCRIPTION_PATTERN = "Default permission template of organization %s";
private final Settings settings;
private final UserSession userSession;
private final DbClient dbClient;
private final UuidFactory uuidFactory;
private final OrganizationsWsSupport wsSupport;
+ private final System2 system2;
- public CreateAction(Settings settings, UserSession userSession, DbClient dbClient, UuidFactory uuidFactory, OrganizationsWsSupport wsSupport) {
+ public CreateAction(Settings settings, UserSession userSession, DbClient dbClient, UuidFactory uuidFactory,
+ OrganizationsWsSupport wsSupport, System2 system2) {
this.settings = settings;
this.userSession = userSession;
this.dbClient = dbClient;
this.uuidFactory = uuidFactory;
this.wsSupport = wsSupport;
+ this.system2 = system2;
}
@Override
try (DbSession dbSession = dbClient.openSession(false)) {
checkKeyIsNotUsed(dbSession, key, requestKey, name);
- OrganizationDto dto = createOrganizationDto(request, name, key);
- dbClient.organizationDao().insert(dbSession, dto);
- GroupDto group = createOwnersGroup(dbSession, dto);
+ OrganizationDto organization = createOrganizationDto(dbSession, request, name, key);
+ GroupDto group = createOwnersGroup(dbSession, organization);
+ createDefaultTemplate(dbSession, organization, group);
addCurrentUserToGroup(dbSession, group);
+
dbSession.commit();
- writeResponse(request, response, dto);
+ writeResponse(request, response, organization);
}
}
+ private OrganizationDto createOrganizationDto(DbSession dbSession, Request request, String name, String key) {
+ OrganizationDto res = new OrganizationDto()
+ .setUuid(uuidFactory.create())
+ .setName(name)
+ .setKey(key)
+ .setDescription(request.param(PARAM_DESCRIPTION))
+ .setUrl(request.param(PARAM_URL))
+ .setAvatarUrl(request.param(PARAM_AVATAR_URL));
+ dbClient.organizationDao().insert(dbSession, res);
+ return res;
+ }
+
+ private void createDefaultTemplate(DbSession dbSession, OrganizationDto organizationDto, GroupDto group) {
+ Date now = new Date(system2.now());
+ PermissionTemplateDto permissionTemplateDto = dbClient.permissionTemplateDao().insert(
+ dbSession,
+ new PermissionTemplateDto()
+ .setOrganizationUuid(organizationDto.getUuid())
+ .setUuid(uuidFactory.create())
+ .setName("Default template")
+ .setDescription(format(PERM_TEMPLATE_DESCRIPTION_PATTERN, organizationDto.getName()))
+ .setCreatedAt(now)
+ .setUpdatedAt(now));
+
+ insertGroupPermission(dbSession, permissionTemplateDto, UserRole.ADMIN, group);
+ insertGroupPermission(dbSession, permissionTemplateDto, UserRole.ISSUE_ADMIN, group);
+ insertGroupPermission(dbSession, permissionTemplateDto, UserRole.USER, null);
+ insertGroupPermission(dbSession, permissionTemplateDto, UserRole.CODEVIEWER, null);
+
+ dbClient.organizationDao().setDefaultTemplates(
+ dbSession,
+ organizationDto.getUuid(),
+ new DefaultTemplates().setProjectUuid(permissionTemplateDto.getUuid()));
+ }
+
+ private void insertGroupPermission(DbSession dbSession, PermissionTemplateDto template, String permission, @Nullable GroupDto group) {
+ dbClient.permissionTemplateDao().insertGroupPermission(dbSession, template.getId(), group == null ? null : group.getId(), permission);
+ }
+
/**
* Owners group has an hard coded name, a description based on the organization's name and has all global permissions.
*/
return dbClient.organizationDao().selectByKey(dbSession, key).isPresent();
}
- private OrganizationDto createOrganizationDto(Request request, String name, String key) {
- return new OrganizationDto()
- .setUuid(uuidFactory.create())
- .setName(name)
- .setKey(key)
- .setDescription(request.param(PARAM_DESCRIPTION))
- .setUrl(request.param(PARAM_URL))
- .setAvatarUrl(request.param(PARAM_AVATAR_URL));
- }
-
private void writeResponse(Request request, Response response, OrganizationDto dto) {
writeProtobuf(
CreateWsResponse.newBuilder().setOrganization(wsSupport.toOrganization(dto)).build(),
import org.sonar.api.config.Settings;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.utils.System2;
+import org.sonar.api.utils.internal.AlwaysIncreasingSystem2;
+import org.sonar.api.web.UserRole;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.core.util.UuidFactory;
import org.sonar.core.util.Uuids;
+import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
+import org.sonar.db.organization.DefaultTemplates;
import org.sonar.db.organization.OrganizationDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateGroupDto;
import org.sonar.db.user.GroupDto;
import org.sonar.db.user.UserDto;
import org.sonar.db.user.UserMembershipDto;
import org.sonarqube.ws.Organizations.Organization;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.tuple;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.sonar.core.config.CorePropertyDefinitions.ORGANIZATIONS_ANYONE_CAN_CREATE;
@Rule
public ExpectedException expectedException = ExpectedException.none();
+ private DbClient dbClient = dbTester.getDbClient();
+ private DbSession dbSession = dbTester.getSession();
+
private Settings settings = new MapSettings()
.setProperty(ORGANIZATIONS_ANYONE_CAN_CREATE, false);
private UuidFactory uuidFactory = mock(UuidFactory.class);
- private CreateAction underTest = new CreateAction(settings, userSession, dbTester.getDbClient(), uuidFactory, new OrganizationsWsSupport());
+ private CreateAction underTest = new CreateAction(settings, userSession, dbClient, uuidFactory, new OrganizationsWsSupport(), new AlwaysIncreasingSystem2());
private WsActionTester wsTester = new WsActionTester(underTest);
@Test
executeRequest("orgFoo");
DbSession dbSession = dbTester.getSession();
- OrganizationDto organization = dbTester.getDbClient().organizationDao().selectByKey(dbSession, "orgfoo").get();
- Optional<GroupDto> groupDtoOptional = dbTester.getDbClient().groupDao().selectByName(dbSession, organization.getUuid(), "Owners");
+ OrganizationDto organization = dbClient.organizationDao().selectByKey(dbSession, "orgfoo").get();
+ Optional<GroupDto> groupDtoOptional = dbClient.groupDao().selectByName(dbSession, organization.getUuid(), "Owners");
assertThat(groupDtoOptional).isNotEmpty();
GroupDto groupDto = groupDtoOptional.get();
assertThat(groupDto.getDescription()).isEqualTo("Owners of organization orgFoo");
- assertThat(dbTester.getDbClient().groupPermissionDao().selectGlobalPermissionsOfGroup(dbSession, groupDto.getOrganizationUuid(), groupDto.getId()))
+ assertThat(dbClient.groupPermissionDao().selectGlobalPermissionsOfGroup(dbSession, groupDto.getOrganizationUuid(), groupDto.getId()))
.containsOnly(GlobalPermissions.ALL.toArray(new String[GlobalPermissions.ALL.size()]));
- List<UserMembershipDto> members = dbTester.getDbClient().groupMembershipDao().selectMembers(
+ List<UserMembershipDto> members = dbClient.groupMembershipDao().selectMembers(
dbSession,
UserMembershipQuery.builder().groupId(groupDto.getId()).membership(UserMembershipQuery.IN).build(), 0, Integer.MAX_VALUE);
assertThat(members)
.containsOnly(user.getLogin());
}
+ @Test
+ public void request_creates_default_template_for_owner_group_and_anyone() {
+ mockForSuccessfulInsert(SOME_UUID, SOME_DATE);
+ UserDto user = dbTester.users().makeRoot(dbTester.users().insertUser());
+ userSession.login(user).setRoot();
+
+ executeRequest("orgFoo");
+
+ OrganizationDto organization = dbClient.organizationDao().selectByKey(dbSession, "orgfoo").get();
+ GroupDto ownersGroup = dbClient.groupDao().selectByName(dbSession, organization.getUuid(), "Owners").get();
+ PermissionTemplateDto defaultTemplate = dbClient.permissionTemplateDao().selectByName(dbSession, organization.getUuid(), "default template");
+ assertThat(defaultTemplate.getName()).isEqualTo("Default template");
+ assertThat(defaultTemplate.getDescription()).isEqualTo("Default permission template of organization orgFoo");
+ DefaultTemplates defaultTemplates = dbClient.organizationDao().getDefaultTemplates(dbSession, organization.getUuid()).get();
+ assertThat(defaultTemplates.getProjectUuid()).isEqualTo(defaultTemplate.getUuid());
+ assertThat(defaultTemplates.getViewUuid()).isNull();
+ assertThat(dbClient.permissionTemplateDao().selectGroupPermissionsByTemplateId(dbSession, defaultTemplate.getId()))
+ .extracting(PermissionTemplateGroupDto::getGroupId, PermissionTemplateGroupDto::getPermission)
+ .containsOnly(
+ tuple(ownersGroup.getId(), UserRole.ADMIN), tuple(ownersGroup.getId(), UserRole.ISSUE_ADMIN),
+ tuple(0L, UserRole.USER), tuple(0L, UserRole.CODEVIEWER));
+
+ }
+
private void makeUserRoot() {
userSession.login().setRoot();
}
assertThat(organization.getAvatar()).isEqualTo(avatar);
}
- OrganizationDto dto = dbTester.getDbClient().organizationDao().selectByUuid(dbTester.getSession(), id).get();
+ OrganizationDto dto = dbClient.organizationDao().selectByUuid(dbTester.getSession(), id).get();
assertThat(dto.getUuid()).isEqualTo(id);
assertThat(dto.getKey()).isEqualTo(key);
assertThat(dto.getName()).isEqualTo(name);
}
private void insertOrganization(String key) {
- dbTester.getDbClient().organizationDao().insert(dbTester.getSession(), new OrganizationDto()
+ dbClient.organizationDao().insert(dbTester.getSession(), new OrganizationDto()
.setUuid(key + "_uuid")
.setKey(key)
.setName(key + "_name")