import org.sonar.api.ServerComponent;
import org.sonar.api.security.DefaultGroups;
import org.sonar.api.web.UserRole;
-import org.sonar.core.component.ComponentDto;
+import org.sonar.core.component.AuthorizedComponentDto;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.core.permission.PermissionFacade;
import org.sonar.core.persistence.DbSession;
import org.sonar.core.resource.ResourceDao;
import org.sonar.core.resource.ResourceDto;
-import org.sonar.core.resource.ResourceQuery;
import org.sonar.core.user.GroupDto;
import org.sonar.core.user.UserDao;
import org.sonar.core.user.UserDto;
DbSession session = dbClient.openSession(false);
try {
- ComponentDto component = dbClient.componentDao().getByKey(session, componentKey);
+ AuthorizedComponentDto component = dbClient.componentDao().getAuthorizedComponentByKey(componentKey, session);
ResourceDto provisioned = resourceDao.selectProvisionedProject(session, componentKey);
if (provisioned == null) {
checkProjectAdminPermission(componentKey);
}
for (String componentKey : query.getSelectedComponents()) {
- ComponentDto component = (ComponentDto) resourceDao.findByKey(componentKey);
+ AuthorizedComponentDto component = dbClient.componentDao().getAuthorizedComponentByKey(componentKey, session);
if (component == null) {
throw new IllegalStateException("Unable to find component with key " + componentKey);
}
if (componentKey == null) {
return null;
} else {
- ResourceDto resourceDto = resourceDao.getResource(ResourceQuery.create().setKey(componentKey), session);
- badRequestIfNullResult(resourceDto, OBJECT_TYPE_COMPONENT, componentKey);
- return resourceDto.getId();
+ AuthorizedComponentDto component = dbClient.componentDao().getAuthorizedComponentByKey(componentKey, session);
+ return component.getId();
}
}
}
private void synchronizePermissions() {
- // The synchronisation cannot use an existing session, otherwise it's failing with the error : org.apache.ibatis.executor.ExecutorException: Executor was closed
+ // The synchronisation cannot use an existing session, otherwise it's failing with the error :
+ // org.apache.ibatis.executor.ExecutorException: Executor was closed
DbSession session = dbClient.openSession(false);
try {
dbClient.issueAuthorizationDao().synchronizeAfter(session, index.get(IssueAuthorizationIndex.class).getLastSynchronization());
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.security.DefaultGroups;
import org.sonar.api.web.UserRole;
+import org.sonar.core.component.AuthorizedComponentDto;
import org.sonar.core.component.ComponentDto;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.core.permission.PermissionFacade;
import org.sonar.core.persistence.DbSession;
import org.sonar.core.resource.ResourceDao;
import org.sonar.core.resource.ResourceDto;
-import org.sonar.core.resource.ResourceQuery;
import org.sonar.core.user.GroupDto;
import org.sonar.core.user.UserDao;
import org.sonar.core.user.UserDto;
@Test
public void add_component_user_permission() throws Exception {
- when(resourceDao.getResource(any(ResourceQuery.class), eq(session))).thenReturn(
- new ResourceDto().setId(10L).setKey("org.sample.Sample"));
+ when(componentDao.getAuthorizedComponentByKey("org.sample.Sample", session)).thenReturn(new AuthorizedComponentDto().setId(10L).setKey("org.sample.Sample"));
params = buildPermissionChangeParams("user", null, "org.sample.Sample", "user");
setUpComponentUserPermissions("user", 10L, "codeviewer");
@Test
public void remove_component_user_permission() throws Exception {
- when(resourceDao.getResource(any(ResourceQuery.class), eq(session))).thenReturn(
- new ResourceDto().setId(10L).setKey("org.sample.Sample"));
-
+ when(componentDao.getAuthorizedComponentByKey("org.sample.Sample", session)).thenReturn(new AuthorizedComponentDto().setId(10L).setKey("org.sample.Sample"));
params = buildPermissionChangeParams("user", null, "org.sample.Sample", "codeviewer");
setUpComponentUserPermissions("user", 10L, "codeviewer");
MockUserSession.set().setLogin("admin").addProjectPermissions(UserRole.ADMIN, "org.sample.Sample");
@Test
public void add_component_group_permission() throws Exception {
- when(resourceDao.getResource(any(ResourceQuery.class), eq(session))).thenReturn(
- new ResourceDto().setId(10L).setKey("org.sample.Sample"));
+ when(componentDao.getAuthorizedComponentByKey("org.sample.Sample", session)).thenReturn(new AuthorizedComponentDto().setId(10L).setKey("org.sample.Sample"));
params = buildPermissionChangeParams(null, "group", "org.sample.Sample", "user");
setUpGlobalGroupPermissions("group", "codeviewer");
@Test
public void add_component_permission_to_anyone_group() throws Exception {
- when(resourceDao.getResource(any(ResourceQuery.class), eq(session))).thenReturn(
- new ResourceDto().setId(10L).setKey("org.sample.Sample"));
+ when(componentDao.getAuthorizedComponentByKey("org.sample.Sample", session)).thenReturn(new AuthorizedComponentDto().setId(10L).setKey("org.sample.Sample"));
params = buildPermissionChangeParams(null, DefaultGroups.ANYONE, "org.sample.Sample", "user");
MockUserSession.set().setLogin("admin").addProjectPermissions(UserRole.ADMIN, "org.sample.Sample");
@Test
public void remove_component_group_permission() throws Exception {
- when(resourceDao.getResource(any(ResourceQuery.class), eq(session))).thenReturn(
- new ResourceDto().setId(10L).setKey("org.sample.Sample"));
+ when(componentDao.getAuthorizedComponentByKey("org.sample.Sample", session)).thenReturn(new AuthorizedComponentDto().setId(10L).setKey("org.sample.Sample"));
params = buildPermissionChangeParams(null, "group", "org.sample.Sample", "codeviewer");
setUpComponentGroupPermissions("group", 10L, "codeviewer");
@Test
public void remove_component_permission_from_anyone_group() throws Exception {
- when(resourceDao.getResource(any(ResourceQuery.class), eq(session))).thenReturn(
- new ResourceDto().setId(10L).setKey("org.sample.Sample"));
+ when(componentDao.getAuthorizedComponentByKey("org.sample.Sample", session)).thenReturn(new AuthorizedComponentDto().setId(10L).setKey("org.sample.Sample"));
params = buildPermissionChangeParams(null, DefaultGroups.ANYONE, "org.sample.Sample", "codeviewer");
setUpComponentGroupPermissions(DefaultGroups.ANYONE, 10L, "codeviewer");
@Test
public void skip_redundant_add_component_user_permission_change() throws Exception {
- when(resourceDao.getResource(any(ResourceQuery.class), eq(session))).thenReturn(
- new ResourceDto().setId(10L).setKey("org.sample.Sample"));
+ when(componentDao.getAuthorizedComponentByKey("org.sample.Sample", session)).thenReturn(new AuthorizedComponentDto().setId(10L).setKey("org.sample.Sample"));
params = buildPermissionChangeParams("user", null, "org.sample.Sample", "codeviewer");
setUpComponentUserPermissions("user", 10L, "codeviewer");
@Test
public void skip_redundant_add_component_group_permission_change() throws Exception {
- when(resourceDao.getResource(any(ResourceQuery.class), eq(session))).thenReturn(
- new ResourceDto().setId(10L).setKey("org.sample.Sample"));
+ when(componentDao.getAuthorizedComponentByKey("org.sample.Sample", session)).thenReturn(new AuthorizedComponentDto().setId(10L).setKey("org.sample.Sample"));
params = buildPermissionChangeParams(null, "group", "org.sample.Sample", "codeviewer");
setUpComponentGroupPermissions("group", 10L, "codeviewer");
}
}
- @Test
- public void fail_when_component_is_not_found() throws Exception {
- try {
- params = buildPermissionChangeParams(null, "group", "unknown", "user");
- service.addPermission(params);
- } catch (Exception e) {
- assertThat(e).isInstanceOf(BadRequestException.class).hasMessage("Component unknown does not exist");
- }
- }
-
@Test
public void fail_on_insufficient_global_rights() throws Exception {
try {
@Test
public void fail_on_insufficient_project_rights() throws Exception {
try {
- when(resourceDao.getResource(any(ResourceQuery.class), eq(session))).thenReturn(
- new ResourceDto().setId(10L).setKey("org.sample.Sample"));
+ when(componentDao.getAuthorizedComponentByKey("org.sample.Sample", session)).thenReturn(new AuthorizedComponentDto().setId(10L).setKey("org.sample.Sample"));
params = buildPermissionChangeParams(null, DefaultGroups.ANYONE, "org.sample.Sample", "user");
MockUserSession.set().setLogin("admin").addProjectPermissions(UserRole.ADMIN);
public void apply_permission_template_on_many_projects() throws Exception {
ComponentDto c1 = mock(ComponentDto.class);
when(c1.getId()).thenReturn(1L);
- when(resourceDao.findByKey("org.sample.Sample1")).thenReturn(c1);
+ when(componentDao.getAuthorizedComponentByKey("org.sample.Sample1", session)).thenReturn(c1);
ComponentDto c2 = mock(ComponentDto.class);
when(c2.getId()).thenReturn(2L);
- when(resourceDao.findByKey("org.sample.Sample2")).thenReturn(c2);
+ when(componentDao.getAuthorizedComponentByKey("org.sample.Sample2", session)).thenReturn(c2);
ComponentDto c3 = mock(ComponentDto.class);
when(c3.getId()).thenReturn(3L);
- when(resourceDao.findByKey("org.sample.Sample3")).thenReturn(c3);
+ when(componentDao.getAuthorizedComponentByKey("org.sample.Sample3", session)).thenReturn(c3);
params = Maps.newHashMap();
params.put("template_key", "my_template_key");
params.put("components", "org.sample.Sample1,org.sample.Sample2,org.sample.Sample3");
ComponentDto c1 = mock(ComponentDto.class);
when(c1.getId()).thenReturn(1L);
- when(resourceDao.findByKey("org.sample.Sample1")).thenReturn(c1);
+ when(componentDao.getAuthorizedComponentByKey("org.sample.Sample1", session)).thenReturn(c1);
ComponentDto c2 = mock(ComponentDto.class);
when(c2.getId()).thenReturn(2L);
- when(resourceDao.findByKey("org.sample.Sample2")).thenReturn(c2);
+ when(componentDao.getAuthorizedComponentByKey("org.sample.Sample2", session)).thenReturn(c2);
ComponentDto c3 = mock(ComponentDto.class);
when(c3.getId()).thenReturn(3L);
- when(resourceDao.findByKey("org.sample.Sample3")).thenReturn(c3);
+ when(componentDao.getAuthorizedComponentByKey("org.sample.Sample3", session)).thenReturn(c3);
params = Maps.newHashMap();
params.put("template_key", "my_template_key");
params.put("components", "org.sample.Sample1,org.sample.Sample2,org.sample.Sample3");
ComponentDto c = mock(ComponentDto.class);
when(c.getId()).thenReturn(1L);
- when(resourceDao.findByKey("org.sample.Sample")).thenReturn(c);
+ when(componentDao.getAuthorizedComponentByKey("org.sample.Sample", session)).thenReturn(c);
service.applyPermissionTemplate(params);
when(mockComponent.qualifier()).thenReturn(qualifier);
MockUserSession.set().setLogin("admin").addProjectPermissions(UserRole.ADMIN, componentKey);
- when(componentDao.getByKey(session, componentKey)).thenReturn(mockComponent);
+ when(componentDao.getAuthorizedComponentByKey(componentKey, session)).thenReturn(mockComponent);
service.applyDefaultPermissionTemplate(componentKey);
verify(permissionFacade).grantDefaultRoles(session, componentId, qualifier);
when(mockComponent.getId()).thenReturn(componentId);
when(mockComponent.qualifier()).thenReturn(qualifier);
- when(componentDao.getByKey(session, componentKey)).thenReturn(mockComponent);
+ when(componentDao.getAuthorizedComponentByKey(componentKey, session)).thenReturn(mockComponent);
when(resourceDao.selectProvisionedProject(session, componentKey)).thenReturn(mock(ResourceDto.class));
service.applyDefaultPermissionTemplate(componentKey);
when(mockComponent.getId()).thenReturn(componentId);
when(mockComponent.qualifier()).thenReturn(qualifier);
- when(componentDao.getByKey(session, componentKey)).thenReturn(mockComponent);
+ when(componentDao.getAuthorizedComponentByKey(componentKey, session)).thenReturn(mockComponent);
when(resourceDao.selectProvisionedProject(session, componentKey)).thenReturn(mock(ResourceDto.class));
service.applyDefaultPermissionTemplate(componentKey);