]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8710 create org's default perm template in api/organizations/create
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Wed, 25 Jan 2017 17:07:06 +0000 (18:07 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 27 Jan 2017 15:55:16 +0000 (16:55 +0100)
server/sonar-server/src/main/java/org/sonar/server/organization/ws/CreateAction.java
server/sonar-server/src/test/java/org/sonar/server/organization/ws/CreateActionTest.java

index b94dc7ab60b59f92689b3cac99ccd2b9dd5912d4..5afea4c8300ed5cdfa0f5b4d997ca96f87bb9943 100644 (file)
  */
 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;
@@ -53,19 +58,23 @@ public class CreateAction implements OrganizationsAction {
   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
@@ -108,16 +117,56 @@ public class CreateAction implements OrganizationsAction {
     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.
    */
@@ -173,16 +222,6 @@ public class CreateAction implements OrganizationsAction {
     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(),
index be8eb8c27f5a437f9e133ca7c774ff10b1903245..142983cad979f8205486190966dfed4563c20683 100644 (file)
@@ -32,12 +32,18 @@ import org.sonar.api.config.MapSettings;
 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;
@@ -52,6 +58,7 @@ import org.sonarqube.ws.Organizations.CreateWsResponse;
 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;
@@ -72,10 +79,13 @@ public class CreateActionTest {
   @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
@@ -434,14 +444,14 @@ public class CreateActionTest {
     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)
@@ -449,6 +459,30 @@ public class CreateActionTest {
       .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();
   }
@@ -522,7 +556,7 @@ public class CreateActionTest {
       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);
@@ -534,7 +568,7 @@ public class CreateActionTest {
   }
 
   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")