*/
package org.sonar.server.permission.ws;
+import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
+import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createTemplateParameters;
+import static org.sonar.server.permission.ws.WsTemplateRef.newTemplateRef;
+import static org.sonar.server.ws.WsUtils.checkRequest;
+import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
+import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
+
import java.util.Set;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.server.user.UserSession;
import org.sonarqube.ws.client.permission.DeleteTemplateWsRequest;
-import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
-import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createTemplateParameters;
-import static org.sonar.server.permission.ws.WsTemplateRef.newTemplateRef;
-import static org.sonar.server.ws.WsUtils.checkRequest;
-import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
-import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
-
public class DeleteTemplateAction implements PermissionsWsAction {
private final DbClient dbClient;
private final UserSession userSession;
*/
package org.sonar.server.permission.ws.template;
+import static com.google.common.primitives.Longs.asList;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.mockito.internal.util.collections.Sets.newSet;
+import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
+import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
+import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
+
import java.util.Collections;
+import java.util.Date;
import javax.annotation.Nullable;
import org.junit.Before;
import org.junit.Rule;
import org.sonar.db.DbTester;
import org.sonar.db.component.ResourceTypesRule;
import org.sonar.db.permission.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto;
import org.sonar.db.user.GroupDto;
import org.sonar.db.user.GroupTesting;
import org.sonar.db.user.UserDto;
import org.sonar.server.ws.TestResponse;
import org.sonar.server.ws.WsActionTester;
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-import static org.mockito.internal.util.collections.Sets.newSet;
-import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
-import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
-import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
-
public class DeleteTemplateActionTest {
static final String TEMPLATE_UUID = "permission-template-uuid";
newRequest(null);
}
+ @Test
+ public void delete_perm_tpl_characteristic_when_delete_template() throws Exception {
+ dbClient.permissionTemplateCharacteristicDao().insert(dbSession, new PermissionTemplateCharacteristicDto()
+ .setPermission(UserRole.USER)
+ .setTemplateId(permissionTemplate.getId())
+ .setWithProjectCreator(true)
+ .setCreatedAt(new Date().getTime())
+ .setUpdatedAt(new Date().getTime()));
+ dbSession.commit();
+ assertThat(dbClient.permissionTemplateCharacteristicDao().selectByTemplateIds(dbSession, asList(permissionTemplate.getId()))).hasSize(1);
+
+ newRequest(TEMPLATE_UUID);
+
+ assertThat(dbClient.permissionTemplateCharacteristicDao().selectByTemplateIds(dbSession, asList(permissionTemplate.getId()))).isEmpty();
+ }
+
private PermissionTemplateDto insertTemplateAndAssociatedPermissions(PermissionTemplateDto template) {
dbClient.permissionTemplateDao().insert(dbSession, template);
UserDto user = dbClient.userDao().insert(dbSession, UserTesting.newUserDto().setActive(true));
*/
package org.sonar.db.permission;
+import static com.google.common.collect.Maps.newHashMap;
+import static java.lang.String.format;
+import static org.sonar.db.DatabaseUtils.executeLargeInputsWithoutOutput;
+
import com.google.common.annotations.VisibleForTesting;
import java.util.Date;
import java.util.HashMap;
import org.sonar.db.Dao;
import org.sonar.db.DbSession;
import org.sonar.db.MyBatis;
-
-import static com.google.common.collect.Maps.newHashMap;
-import static java.lang.String.format;
-import static org.sonar.db.DatabaseUtils.executeLargeInputsWithoutOutput;
+import org.sonar.db.permission.template.PermissionTemplateCharacteristicMapper;
public class PermissionTemplateDao implements Dao {
return mapper(dbSession).countAll(upperCasedNameQuery);
}
- public int countAll(DbSession session) {
- return mapper(session).countAll(null);
- }
-
private static String toUppercaseSqlQuery(String nameMatch) {
String wildcard = "%";
return format("%s%s%s", wildcard, nameMatch.toUpperCase(Locale.ENGLISH), wildcard);
PermissionTemplateMapper mapper = mapper(session);
mapper.deleteUserPermissions(templateId);
mapper.deleteGroupPermissions(templateId);
+ session.getMapper(PermissionTemplateCharacteristicMapper.class).deleteByTemplateId(templateId);
mapper.delete(templateId);
}
package org.sonar.db.permission.template;
+import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.Collections.emptyList;
+import static java.util.Objects.requireNonNull;
+
import java.util.List;
import java.util.Optional;
import org.sonar.db.Dao;
import org.sonar.db.DbSession;
-import static com.google.common.base.Preconditions.checkArgument;
-import static java.util.Collections.emptyList;
-import static java.util.Objects.requireNonNull;
-
public class PermissionTemplateCharacteristicDao implements Dao {
public List<PermissionTemplateCharacteristicDto> selectByTemplateIds(DbSession dbSession, List<Long> templateIds) {
return templateIds.isEmpty() ? emptyList() : mapper(dbSession).selectByTemplateIds(templateIds);
void insert(PermissionTemplateCharacteristicDto templatePermissionDto);
void update(PermissionTemplateCharacteristicDto templatePermissionDto);
+
+ void deleteByTemplateId(long id);
}
updated_at=#{updatedAt, jdbcType=BIGINT}
where id=#{id}
</update>
+
+ <delete id="deleteByTemplateId" parameterType="long">
+ DELETE FROM perm_tpl_characteristics
+ WHERE template_id = #{permissionTemplateId}
+ </delete>
</mapper>
*/
package org.sonar.db.permission;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.sonar.api.web.UserRole.ADMIN;
+import static org.sonar.api.web.UserRole.CODEVIEWER;
+import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
+import static org.sonar.api.web.UserRole.USER;
+import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
+import static org.sonar.db.user.GroupTesting.newGroupDto;
+import static org.sonar.db.user.UserTesting.newUserDto;
+
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import org.sonar.db.user.GroupDto;
import org.sonar.db.user.UserDto;
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-import static org.sonar.api.web.UserRole.ADMIN;
-import static org.sonar.api.web.UserRole.CODEVIEWER;
-import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
-import static org.sonar.api.web.UserRole.USER;
-import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
-import static org.sonar.db.user.GroupTesting.newGroupDto;
-import static org.sonar.db.user.UserTesting.newUserDto;
-
public class PermissionTemplateDaoTest {
session.commit();
checkTemplateTables("deletePermissionTemplate-result.xml");
+ db.assertDbUnitTable(getClass(), "deletePermissionTemplate-result.xml", "perm_tpl_characteristics");
}
@Test
package org.sonar.db.permission.template;
+import static com.google.common.collect.Lists.newArrayList;
+import static com.google.common.primitives.Longs.asList;
+import static java.util.Collections.emptyList;
+import static org.assertj.core.api.Assertions.assertThat;
+
import java.util.List;
import java.util.Optional;
import org.junit.Rule;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
-import static com.google.common.collect.Lists.newArrayList;
-import static java.util.Collections.emptyList;
-import static org.assertj.core.api.Assertions.assertThat;
-
public class PermissionTemplateCharacteristicDaoTest {
@Rule
public ExpectedException expectedException = ExpectedException.none();
.setCreatedAt(123_456_789L)
.setUpdatedAt(2_000_000_000L));
}
+
+ @Test
+ public void delete_by_permission_template_id() throws Exception {
+ underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
+ .setPermission(UserRole.USER)
+ .setTemplateId(1L)
+ .setWithProjectCreator(true)
+ .setCreatedAt(123_456_789L)
+ .setUpdatedAt(2_000_000_000L));
+ underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
+ .setPermission(UserRole.USER)
+ .setTemplateId(2L)
+ .setWithProjectCreator(true)
+ .setCreatedAt(123_456_789L)
+ .setUpdatedAt(2_000_000_000L));
+
+ assertThat(underTest.selectByTemplateIds(dbSession, asList(1L))).hasSize(1);
+ assertThat(underTest.selectByTemplateIds(dbSession, asList(1L, 2L))).hasSize(2);
+
+ dbSession.getMapper(PermissionTemplateCharacteristicMapper.class).deleteByTemplateId(1L);
+
+ assertThat(underTest.selectByTemplateIds(dbSession, asList(1L))).hasSize(0);
+ assertThat(underTest.selectByTemplateIds(dbSession, asList(1L, 2L))).hasSize(1);
+ }
}
<permission_templates id="2" name="other template" kee="my_template_20130102_030405" description="other description"
created_at="[null]" updated_at="[null]"/>
- <perm_templates_users/>
+ <perm_templates_users id="4" template_id="2" user_id="2" permission_reference="user_permission2"/>
- <perm_templates_groups/>
+ <perm_templates_groups id="4" template_id="2" group_id="[null]" permission_reference="group_permission2"/>
+
+ <perm_tpl_characteristics id="2" template_id="2" permission_key="user" with_project_creator="[true]" created_at="1000000" updated_at="1000000"/>
</dataset>
<perm_templates_users id="1" template_id="1" user_id="1" permission_reference="user_permission1"/>
<perm_templates_users id="2" template_id="1" user_id="2" permission_reference="user_permission1"/>
<perm_templates_users id="3" template_id="1" user_id="2" permission_reference="user_permission2"/>
+ <perm_templates_users id="4" template_id="2" user_id="2" permission_reference="user_permission2"/>
<perm_templates_groups id="1" template_id="1" group_id="1" permission_reference="group_permission1"/>
<perm_templates_groups id="2" template_id="1" group_id="2" permission_reference="group_permission1"/>
<perm_templates_groups id="3" template_id="1" group_id="[null]" permission_reference="group_permission2"/>
+ <perm_templates_groups id="4" template_id="2" group_id="[null]" permission_reference="group_permission2"/>
+
+ <perm_tpl_characteristics id="1" template_id="1" permission_key="user" with_project_creator="[true]" created_at="1000000" updated_at="1000000"/>
+ <perm_tpl_characteristics id="2" template_id="2" permission_key="user" with_project_creator="[true]" created_at="1000000" updated_at="1000000"/>
+
</dataset>