import org.sonar.api.server.ServerSide;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.db.component.ResourceDto;
+import org.sonar.db.component.ComponentDto;
import org.sonar.db.user.GroupDto;
import org.sonar.db.user.GroupRoleDto;
import org.sonar.db.user.UserRoleDto;
* <p/>
* Should be removed when batch will no more create permission, and be replaced by a new PermissionService in module server (probably be a merge with InternalPermissionService)
* <p/>
- * WARNING, this class is called by Views to apply default permission template on new views
+ * WARNING, this class is called by Deveveloper Cockpit to apply default permission template on new developers
*/
@ServerSide
public class PermissionRepository {
dbClient.roleDao().deleteUserRole(userRoleDto, session);
}
+ /**
+ * @param updateProjectAuthorizationDate is false when doing bulk action in order to not update the same project multiple times for nothing
+ */
private void insertGroupPermission(@Nullable Long resourceId, @Nullable Long groupId, String permission, boolean updateProjectAuthorizationDate, DbSession session) {
GroupRoleDto groupRole = new GroupRoleDto()
.setRole(permission)
.setGroupId(groupId)
.setResourceId(resourceId);
- updateProjectAuthorizationDate(session, resourceId);
+ if (updateProjectAuthorizationDate) {
+ updateProjectAuthorizationDate(session, resourceId);
+ }
dbClient.roleDao().insertGroupRole(session, groupRole);
}
}
}
- public void grantDefaultRoles(DbSession session, long componentId, String qualifier) {
- ResourceDto resource = dbClient.resourceDao().selectResource(componentId, session);
- String applicablePermissionTemplateKey = getApplicablePermissionTemplateKey(session, resource.getKey(), qualifier);
- applyPermissionTemplate(session, applicablePermissionTemplateKey, componentId);
+ /**
+ * Warning, this method is also used by the Developer Cockpit plugin
+ */
+ public void applyDefaultPermissionTemplate(DbSession session, long componentId) {
+ ComponentDto component = dbClient.componentDao().selectOrFailById(session, componentId);
+ applyDefaultPermissionTemplate(session, component);
+ }
+
+ public void applyDefaultPermissionTemplate(DbSession session, ComponentDto componentDto) {
+ String applicablePermissionTemplateKey = getApplicablePermissionTemplateKey(session, componentDto.getKey(), componentDto.qualifier());
+ applyPermissionTemplate(session, applicablePermissionTemplateKey, componentDto.getId());
}
/**
import org.sonar.api.config.Settings;
import org.sonar.api.utils.System2;
import org.sonar.api.web.UserRole;
+import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.user.RoleDao;
import org.sonar.test.DbTests;
@Category(DbTests.class)
public class PermissionRepositoryTest {
+ static final String DEFAULT_TEMPLATE = "default_20130101_010203";
+ static final long PROJECT_ID = 123L;
+ static final long NOW = 123456789L;
+
@Rule
public ExpectedException throwable = ExpectedException.none();
@Rule
public DbTester dbTester = DbTester.create(system2);
+ DbSession session = dbTester.getSession();
- PermissionRepository underTest;
+ Settings settings = new Settings();
+ PermissionRepository underTest = new PermissionRepository(dbTester.getDbClient(), settings);
@Before
public void setUp() {
- when(system2.now()).thenReturn(123456789L);
-
- Settings settings = new Settings();
- underTest = new PermissionRepository(dbTester.getDbClient(), settings);
+ when(system2.now()).thenReturn(NOW);
}
@Test
- public void should_apply_permission_template() {
+ public void apply_permission_template() {
dbTester.prepareDbUnit(getClass(), "should_apply_permission_template.xml");
RoleDao roleDao = dbTester.getDbClient().roleDao();
- assertThat(roleDao.selectGroupPermissions(dbTester.getSession(), "sonar-administrators", 123L)).isEmpty();
- assertThat(roleDao.selectGroupPermissions(dbTester.getSession(), "sonar-users", 123L)).isEmpty();
- assertThat(roleDao.selectGroupPermissions(dbTester.getSession(), "Anyone", 123L)).isEmpty();
- assertThat(roleDao.selectUserPermissions(dbTester.getSession(), "marius", 123L)).isEmpty();
+ assertThat(roleDao.selectGroupPermissions(session, "sonar-administrators", PROJECT_ID)).isEmpty();
+ assertThat(roleDao.selectGroupPermissions(session, "sonar-users", PROJECT_ID)).isEmpty();
+ assertThat(roleDao.selectGroupPermissions(session, "Anyone", PROJECT_ID)).isEmpty();
+ assertThat(roleDao.selectUserPermissions(session, "marius", PROJECT_ID)).isEmpty();
+
+ underTest.applyPermissionTemplate(session, "default_20130101_010203", PROJECT_ID);
+
+ assertThat(roleDao.selectGroupPermissions(session, "sonar-administrators", PROJECT_ID)).containsOnly("admin", "issueadmin");
+ assertThat(roleDao.selectGroupPermissions(session, "sonar-users", PROJECT_ID)).containsOnly("user", "codeviewer");
+ assertThat(roleDao.selectGroupPermissions(session, "Anyone", PROJECT_ID)).containsOnly("user", "codeviewer");
+
+ assertThat(roleDao.selectUserPermissions(session, "marius", PROJECT_ID)).containsOnly("admin");
+
+ checkAuthorizationUpdatedAtIsUpdated();
+ }
+
+ @Test
+ public void apply_default_permission_template_from_component_id() {
+ dbTester.prepareDbUnit(getClass(), "apply_default_permission_template.xml");
+ settings.setProperty("sonar.permission.template.default", DEFAULT_TEMPLATE);
- underTest.applyPermissionTemplate(dbTester.getSession(), "default_20130101_010203", 123L);
+ underTest.applyDefaultPermissionTemplate(session, PROJECT_ID);
+ session.commit();
- assertThat(roleDao.selectGroupPermissions(dbTester.getSession(), "sonar-administrators", 123L)).containsOnly("admin", "issueadmin");
- assertThat(roleDao.selectGroupPermissions(dbTester.getSession(), "sonar-users", 123L)).containsOnly("user", "codeviewer");
- assertThat(roleDao.selectGroupPermissions(dbTester.getSession(), "Anyone", 123L)).containsOnly("user", "codeviewer");
+ dbTester.assertDbUnitTable(getClass(), "apply_default_permission_template-result.xml", "user_roles", "user_id", "resource_id", "role");
+ }
+
+ @Test
+ public void apply_default_permission_template_from_component() {
+ dbTester.prepareDbUnit(getClass(), "apply_default_permission_template.xml");
+ settings.setProperty("sonar.permission.template.default", DEFAULT_TEMPLATE);
- assertThat(roleDao.selectUserPermissions(dbTester.getSession(), "marius", 123L)).containsOnly("admin");
+ underTest.applyDefaultPermissionTemplate(session, dbTester.getDbClient().componentDao().selectOrFailByKey(session, "org.struts:struts"));
+ session.commit();
- assertThat(dbTester.getDbClient().resourceDao().selectResource(123L, dbTester.getSession()).getAuthorizationUpdatedAt()).isEqualTo(123456789L);
+ dbTester.assertDbUnitTable(getClass(), "apply_default_permission_template-result.xml", "user_roles", "user_id", "resource_id", "role");
}
@Test
public void should_add_user_permission() {
dbTester.prepareDbUnit(getClass(), "should_add_user_permission.xml");
- underTest.insertUserPermission(123L, 200L, UserRole.ADMIN, dbTester.getSession());
- dbTester.getSession().commit();
+ underTest.insertUserPermission(PROJECT_ID, 200L, UserRole.ADMIN, session);
+ session.commit();
dbTester.assertDbUnitTable(getClass(), "should_add_user_permission-result.xml", "user_roles", "user_id", "resource_id", "role");
dbTester.assertDbUnitTable(getClass(), "should_add_user_permission-result.xml", "projects", "authorization_updated_at");
+
+ checkAuthorizationUpdatedAtIsUpdated();
}
@Test
public void should_delete_user_permission() {
dbTester.prepareDbUnit(getClass(), "should_delete_user_permission.xml");
- underTest.deleteUserPermission(123L, 200L, UserRole.ADMIN, dbTester.getSession());
- dbTester.getSession().commit();
+ underTest.deleteUserPermission(PROJECT_ID, 200L, UserRole.ADMIN, session);
+ session.commit();
dbTester.assertDbUnitTable(getClass(), "should_delete_user_permission-result.xml", "user_roles", "user_id", "resource_id", "role");
dbTester.assertDbUnitTable(getClass(), "should_delete_user_permission-result.xml", "projects", "authorization_updated_at");
+ checkAuthorizationUpdatedAtIsUpdated();
}
@Test
public void should_insert_group_permission() {
dbTester.prepareDbUnit(getClass(), "should_insert_group_permission.xml");
- underTest.insertGroupPermission(123L, 100L, UserRole.USER, dbTester.getSession());
- dbTester.getSession().commit();
+ underTest.insertGroupPermission(PROJECT_ID, 100L, UserRole.USER, session);
+ session.commit();
dbTester.assertDbUnitTable(getClass(), "should_insert_group_permission-result.xml", "group_roles", "group_id", "resource_id", "role");
dbTester.assertDbUnitTable(getClass(), "should_insert_group_permission-result.xml", "projects", "authorization_updated_at");
+ checkAuthorizationUpdatedAtIsUpdated();
}
@Test
public void should_insert_group_name_permission() {
dbTester.prepareDbUnit(getClass(), "should_insert_group_permission.xml");
- underTest.insertGroupPermission(123L, "devs", UserRole.USER, dbTester.getSession());
- dbTester.getSession().commit();
+ underTest.insertGroupPermission(PROJECT_ID, "devs", UserRole.USER, session);
+ session.commit();
dbTester.assertDbUnitTable(getClass(), "should_insert_group_permission-result.xml", "group_roles", "group_id", "resource_id", "role");
dbTester.assertDbUnitTable(getClass(), "should_insert_group_permission-result.xml", "projects", "authorization_updated_at");
public void should_insert_anyone_group_permission() {
dbTester.prepareDbUnit(getClass(), "should_insert_anyone_group_permission.xml");
- underTest.insertGroupPermission(123L, "Anyone", UserRole.USER, dbTester.getSession());
- dbTester.getSession().commit();
+ underTest.insertGroupPermission(PROJECT_ID, "Anyone", UserRole.USER, session);
+ session.commit();
dbTester.assertDbUnitTable(getClass(), "should_insert_anyone_group_permission-result.xml", "group_roles", "group_id", "resource_id", "role");
dbTester.assertDbUnitTable(getClass(), "should_insert_anyone_group_permission-result.xml", "projects", "authorization_updated_at");
public void should_delete_group_permission() {
dbTester.prepareDbUnit(getClass(), "should_delete_group_permission.xml");
- underTest.deleteGroupPermission(123L, 100L, UserRole.USER, dbTester.getSession());
- dbTester.getSession().commit();
+ underTest.deleteGroupPermission(PROJECT_ID, 100L, UserRole.USER, session);
+ session.commit();
dbTester.assertDbUnitTable(getClass(), "should_delete_group_permission-result.xml", "group_roles", "group_id", "resource_id", "role");
dbTester.assertDbUnitTable(getClass(), "should_delete_group_permission-result.xml", "projects", "authorization_updated_at");
+ checkAuthorizationUpdatedAtIsUpdated();
}
@Test
public void should_delete_group_name_permission() {
dbTester.prepareDbUnit(getClass(), "should_delete_group_permission.xml");
- underTest.deleteGroupPermission(123L, "devs", UserRole.USER, dbTester.getSession());
- dbTester.getSession().commit();
+ underTest.deleteGroupPermission(PROJECT_ID, "devs", UserRole.USER, session);
+ session.commit();
dbTester.assertDbUnitTable(getClass(), "should_delete_group_permission-result.xml", "group_roles", "group_id", "resource_id", "role");
dbTester.assertDbUnitTable(getClass(), "should_delete_group_permission-result.xml", "projects", "authorization_updated_at");
}
+
+ private void checkAuthorizationUpdatedAtIsUpdated() {
+ assertThat(dbTester.getDbClient().resourceDao().selectResource(PROJECT_ID, session).getAuthorizationUpdatedAt()).isEqualTo(NOW);
+ }
+
+ private void checkAuthorizationUpdatedAtIsNotUpdated() {
+ assertThat(dbTester.getDbClient().resourceDao().selectResource(PROJECT_ID, session).getAuthorizationUpdatedAt()).isNull();
+ }
+
}