Having UserSession as a dependency of FavoriteUpdater forces any classes that is using it to also have a UserSession dependency
permissionTemplateService.applyDefaultPermissionTemplate(dbSession, organizationUuid, provisionedComponent.getKey());
if (Qualifiers.PROJECT.equals(provisionedComponent.qualifier())
&& permissionTemplateService.hasDefaultTemplateWithPermissionOnProjectCreator(dbSession, organizationUuid, provisionedComponent)) {
- favoriteUpdater.add(dbSession, provisionedComponent);
+ // TODO Set userId as null as this method will be removed in a next commit
+ favoriteUpdater.add(dbSession, provisionedComponent, null);
dbSession.commit();
}
.build();
ComponentDto project = componentService.create(dbSession, newProject);
if (permissionTemplateService.hasDefaultTemplateWithPermissionOnProjectCreator(dbSession, organizationUuid, project)) {
- favoriteUpdater.add(dbSession, project);
+ favoriteUpdater.add(dbSession, project, projectCreatorUserId);
dbSession.commit();
}
package org.sonar.server.favorite;
import java.util.List;
+import javax.annotation.Nullable;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.property.PropertyDto;
import org.sonar.db.property.PropertyQuery;
import org.sonar.server.exceptions.BadRequestException;
-import org.sonar.server.user.UserSession;
import static org.sonar.server.ws.WsUtils.checkRequest;
static final String PROP_FAVORITE_KEY = "favourite";
private final DbClient dbClient;
- private final UserSession userSession;
- public FavoriteUpdater(DbClient dbClient, UserSession userSession) {
+ public FavoriteUpdater(DbClient dbClient) {
this.dbClient = dbClient;
- this.userSession = userSession;
}
/**
- * Set favorite to the logged in user. If no user is logged, no action is done
+ * Set favorite to the logged in user. If no user, no action is done
*/
- public void add(DbSession dbSession, ComponentDto componentDto) {
- if (!userSession.isLoggedIn()) {
+ public void add(DbSession dbSession, ComponentDto componentDto, @Nullable Long userId) {
+ if (userId == null) {
return;
}
List<PropertyDto> existingFavoriteOnComponent = dbClient.propertiesDao().selectByQuery(PropertyQuery.builder()
.setKey(PROP_FAVORITE_KEY)
- .setUserId(userSession.getUserId())
+ .setUserId(userId.intValue())
.setComponentId(componentDto.getId())
.build(), dbSession);
checkRequest(existingFavoriteOnComponent.isEmpty(), "Component '%s' is already a favorite", componentDto.getKey());
dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto()
.setKey(PROP_FAVORITE_KEY)
.setResourceId(componentDto.getId())
- .setUserId(Long.valueOf(userSession.getUserId())));
+ .setUserId(userId));
}
/**
- * Remove a favorite to the logged in user.
+ * Remove a favorite to the user.
* @throws BadRequestException if the component is not a favorite
*/
- public void remove(DbSession dbSession, ComponentDto component) {
- if (!userSession.isLoggedIn()) {
+ public void remove(DbSession dbSession, ComponentDto component, @Nullable Long userId) {
+ if (userId == null) {
return;
}
int result = dbClient.propertiesDao().delete(dbSession, new PropertyDto()
.setKey(PROP_FAVORITE_KEY)
.setResourceId(component.getId())
- .setUserId(Long.valueOf(userSession.getUserId())));
+ .setUserId(userId));
checkRequest(result == 1, "Component '%s' is not a favorite", component.key());
}
}
userSession
.checkLoggedIn()
.checkComponentUuidPermission(UserRole.USER, componentDto.uuid());
- favoriteUpdater.add(dbSession, componentDto);
+ favoriteUpdater.add(dbSession, componentDto, userSession.isLoggedIn() ? userSession.getUserId().longValue() : null);
dbSession.commit();
}
};
ComponentDto component = componentFinder.getByKey(dbSession, request.mandatoryParam(PARAM_COMPONENT));
userSession
.checkLoggedIn();
- favoriteUpdater.remove(dbSession, component);
+ favoriteUpdater.remove(dbSession, component, userSession.isLoggedIn() ? userSession.getUserId().longValue() : null);
dbSession.commit();
}
};
}
private void handlePermissionTemplate(DbSession dbSession, ComponentDto componentDto, String organizationUuid) {
- permissionTemplateService.applyDefault(dbSession, organizationUuid, componentDto, userSession.isLoggedIn() ? userSession.getUserId().longValue() : null);
+ Long userId = userSession.isLoggedIn() ? userSession.getUserId().longValue() : null;
+ permissionTemplateService.applyDefault(dbSession, organizationUuid, componentDto, userId);
if (permissionTemplateService.hasDefaultTemplateWithPermissionOnProjectCreator(dbSession, organizationUuid, componentDto)) {
- favoriteUpdater.add(dbSession, componentDto);
+ favoriteUpdater.add(dbSession, componentDto, userId);
dbSession.commit();
}
}
assertThat(project.qualifier()).isEqualTo(qualifier);
assertThat(project.getId()).isEqualTo(result);
verify(permissionTemplateService).applyDefaultPermissionTemplate(any(DbSession.class), eq(defaultOrganizationUuid), eq(componentKey));
- verify(favoriteUpdater).add(any(DbSession.class), eq(project));
+ verify(favoriteUpdater).add(any(DbSession.class), eq(project), eq(null));
}
@Test(expected = BadRequestException.class)
import org.sonar.db.component.ComponentDto;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.server.component.ComponentService;
-import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.server.favorite.FavoriteUpdater;
import org.sonar.server.component.NewComponent;
import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.exceptions.NotFoundException;
+import org.sonar.server.favorite.FavoriteUpdater;
import org.sonar.server.permission.PermissionTemplateService;
import org.sonar.server.tester.UserSessionRule;
thrown.expect(NotFoundException.class);
thrown.expectMessage("Organization with key 'fop' does not exist");
- underTest.submit("fop", PROJECT_KEY, null, null, null /*method will fail before parameter is used*/);
+ underTest.submit("fop", PROJECT_KEY, null, null, null /* method will fail before parameter is used */);
}
@Test
mockSuccessfulPrepareSubmitCall();
ComponentDto createdProject = new ComponentDto().setId(23L).setUuid(PROJECT_UUID).setKey(PROJECT_KEY);
when(componentService.create(any(DbSession.class), any(NewComponent.class))).thenReturn(createdProject);
- when(permissionTemplateService.wouldUserHavePermissionWithDefaultTemplate(any(DbSession.class), eq(organization.getUuid()), anyLong(), eq(SCAN_EXECUTION), anyString(), eq(PROJECT_KEY), eq(Qualifiers.PROJECT)))
- .thenReturn(true);
+ when(permissionTemplateService.wouldUserHavePermissionWithDefaultTemplate(any(DbSession.class), eq(organization.getUuid()), anyLong(), eq(SCAN_EXECUTION), anyString(),
+ eq(PROJECT_KEY), eq(Qualifiers.PROJECT)))
+ .thenReturn(true);
when(permissionTemplateService.hasDefaultTemplateWithPermissionOnProjectCreator(any(DbSession.class), eq(organization.getUuid()), any(ComponentDto.class))).thenReturn(true);
underTest.submit(organization.getKey(), PROJECT_KEY, null, PROJECT_NAME, IOUtils.toInputStream("{binary}"));
verifyReportIsPersisted(TASK_UUID);
verify(permissionTemplateService).applyDefault(any(DbSession.class), eq(organization.getUuid()), eq(createdProject), anyLong());
- verify(favoriteUpdater).add(any(DbSession.class), eq(createdProject));
+ verify(favoriteUpdater).add(any(DbSession.class), eq(createdProject), eq(null));
verify(queue).submit(argThat(new TypeSafeMatcher<CeTaskSubmit>() {
@Override
protected boolean matchesSafely(CeTaskSubmit submit) {
mockSuccessfulPrepareSubmitCall();
ComponentDto createdProject = new ComponentDto().setId(23L).setUuid(PROJECT_UUID).setKey(PROJECT_KEY);
when(componentService.create(any(DbSession.class), any(NewComponent.class))).thenReturn(createdProject);
- when(permissionTemplateService.wouldUserHavePermissionWithDefaultTemplate(any(DbSession.class), eq(defaultOrganizationUuid), anyLong(), eq(SCAN_EXECUTION), anyString(), eq(PROJECT_KEY), eq(Qualifiers.PROJECT)))
- .thenReturn(true);
+ when(permissionTemplateService.wouldUserHavePermissionWithDefaultTemplate(any(DbSession.class), eq(defaultOrganizationUuid), anyLong(), eq(SCAN_EXECUTION), anyString(),
+ eq(PROJECT_KEY), eq(Qualifiers.PROJECT)))
+ .thenReturn(true);
when(permissionTemplateService.hasDefaultTemplateWithPermissionOnProjectCreator(any(DbSession.class), eq(defaultOrganizationUuid), any(ComponentDto.class))).thenReturn(false);
underTest.submit(defaultOrganizationKey, PROJECT_KEY, null, PROJECT_NAME, IOUtils.toInputStream("{binary}"));
mockSuccessfulPrepareSubmitCall();
when(componentService.create(any(DbSession.class), any(NewComponent.class))).thenReturn(new ComponentDto().setId(23L).setUuid(PROJECT_UUID).setKey(PROJECT_KEY));
- when(permissionTemplateService.wouldUserHavePermissionWithDefaultTemplate(any(DbSession.class), eq(defaultOrganizationUuid), anyLong(), eq(SCAN_EXECUTION), anyString(), eq(PROJECT_KEY), eq(Qualifiers.PROJECT)))
- .thenReturn(true);
+ when(permissionTemplateService.wouldUserHavePermissionWithDefaultTemplate(any(DbSession.class), eq(defaultOrganizationUuid), anyLong(), eq(SCAN_EXECUTION), anyString(),
+ eq(PROJECT_KEY), eq(Qualifiers.PROJECT)))
+ .thenReturn(true);
underTest.submit(defaultOrganizationKey, PROJECT_KEY, null, PROJECT_NAME, IOUtils.toInputStream("{binary}"));
import org.sonar.db.organization.OrganizationTesting;
import org.sonar.db.property.PropertyQuery;
import org.sonar.server.exceptions.BadRequestException;
-import org.sonar.server.tester.UserSessionRule;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.db.component.ComponentTesting.newProjectDto;
.setKey(COMPONENT_KEY);
private static final long USER_ID = 42L;
- @Rule
- public UserSessionRule userSession = UserSessionRule.standalone().login().setUserId((int) USER_ID);
@Rule
public ExpectedException expectedException = ExpectedException.none();
private DbClient dbClient = db.getDbClient();
private DbSession dbSession = db.getSession();
- private FavoriteUpdater underTest = new FavoriteUpdater(dbClient, userSession);
+ private FavoriteUpdater underTest = new FavoriteUpdater(dbClient);
@Test
public void put_favorite() {
assertNoFavorite();
- underTest.add(dbSession, COMPONENT);
+ underTest.add(dbSession, COMPONENT, USER_ID);
assertFavorite();
}
@Test
- public void do_nothing_when_not_logged_in() {
- userSession.anonymous();
-
- underTest.add(dbSession, COMPONENT);
+ public void do_nothing_when_no_user() {
+ underTest.add(dbSession, COMPONENT, null);
assertNoFavorite();
}
@Test
public void fail_when_adding_existing_favorite() {
- underTest.add(dbSession, COMPONENT);
+ underTest.add(dbSession, COMPONENT, USER_ID);
assertFavorite();
expectedException.expect(BadRequestException.class);
expectedException.expectMessage("Component 'K1' is already a favorite");
- underTest.add(dbSession, COMPONENT);
+ underTest.add(dbSession, COMPONENT, USER_ID);
}
private void assertFavorite() {
import org.sonar.db.property.PropertyDto;
import org.sonar.db.property.PropertyQuery;
import org.sonar.server.component.ComponentFinder;
-import org.sonar.server.favorite.FavoriteUpdater;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.exceptions.UnauthorizedException;
+import org.sonar.server.favorite.FavoriteUpdater;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.TestRequest;
import org.sonar.server.ws.TestResponse;
private DbClient dbClient = db.getDbClient();
private DbSession dbSession = db.getSession();
- private FavoriteUpdater favoriteUpdater = new FavoriteUpdater(dbClient, userSession);
+ private FavoriteUpdater favoriteUpdater = new FavoriteUpdater(dbClient);
private WsActionTester ws = new WsActionTester(new AddAction(userSession, dbClient, favoriteUpdater, new ComponentFinder(dbClient)));
@Test
public DbTester db = DbTester.create();
private DbClient dbClient = db.getDbClient();
- private FavoriteUpdater favoriteUpdater = new FavoriteUpdater(dbClient, userSession);
+ private FavoriteUpdater favoriteUpdater = new FavoriteUpdater(dbClient);
private WsActionTester ws = new WsActionTester(new RemoveAction(userSession, dbClient, favoriteUpdater, new ComponentFinder(dbClient)));
@Test
new ProjectMeasuresIndexer(system2, db.getDbClient(), es.client()),
new ComponentIndexer(db.getDbClient(), es.client())),
new PermissionTemplateService(db.getDbClient(), settings, new PermissionIndexer(db.getDbClient(), es.client()), userSession),
- new FavoriteUpdater(db.getDbClient(), userSession),
+ new FavoriteUpdater(db.getDbClient()),
defaultOrganizationProvider));
@Before