Skip to content

Commit

Permalink
Fix integer type of user and group ids
Browse files Browse the repository at this point in the history
User IDs are INTEGER in database. The java classes are fixed
to replace long by int.
  • Loading branch information
Simon Brandhof committed Feb 16, 2017
1 parent e6dd781 commit 33acda7
Show file tree
Hide file tree
Showing 112 changed files with 422 additions and 419 deletions.
Expand Up @@ -49,10 +49,10 @@ public void execute(Context context) throws SQLException {
massUpdate.update("update groups set organization_uuid=?, updated_at=? where id=?");
massUpdate.rowPluralName("groups");
massUpdate.execute((row, update) -> {
long groupId = row.getLong(1);
int groupId = row.getInt(1);
update.setString(1, organizationUuid);
update.setDate(2, new Date(system2.now()));
update.setLong(3, groupId);
update.setInt(3, groupId);
return true;
});
}
Expand Down
Expand Up @@ -49,10 +49,10 @@ public void execute(Context context) throws SQLException {
massUpdate.update("update permission_templates set organization_uuid=?, updated_at=? where id=?");
massUpdate.rowPluralName("permission_templates");
massUpdate.execute((row, update) -> {
long groupId = row.getLong(1);
int groupId = row.getInt(1);
update.setString(1, organizationUuid);
update.setDate(2, new Date(system2.now()));
update.setLong(3, groupId);
update.setInt(3, groupId);
return true;
});
}
Expand Down
Expand Up @@ -235,8 +235,9 @@ private static Property2 asInternalProperty(DbTester dbTester, DbSession dbSessi
" user_id as \"userId\", resource_id as \"resourceId\", is_empty as \"isEmpty\", text_value as \"textValue\", clob_value as \"clobValue\", created_at as \"createdAt\"" +
" from properties2" +
" where prop_key='" + key + "'");
Long userId = (Long) row.get("userId");
return new Property2(
(Long) row.get("userId"),
userId == null ? null : userId.intValue(),
(Long) row.get("resourceId"),
isEmpty(row),
(String) row.get("textValue"),
Expand Down Expand Up @@ -266,7 +267,7 @@ public Property2Assert hasNoUserId() {
return this;
}

public Property2Assert hasUserId(long expected) {
public Property2Assert hasUserId(int expected) {
isNotNull();

if (!Objects.equals(actual.getUserId(), expected)) {
Expand Down Expand Up @@ -357,14 +358,14 @@ public Property2Assert hasCreatedAt(long expected) {
}

private static final class Property2 {
private final Long userId;
private final Integer userId;
private final Long resourceId;
private final Boolean empty;
private final String textValue;
private final String clobValue;
private final Long createdAt;

private Property2(@Nullable Long userId, @Nullable Long resourceId,
private Property2(@Nullable Integer userId, @Nullable Long resourceId,
@Nullable Boolean empty, @Nullable String textValue, @Nullable String clobValue,
@Nullable Long createdAt) {
this.userId = userId;
Expand All @@ -375,7 +376,7 @@ private Property2(@Nullable Long userId, @Nullable Long resourceId,
this.createdAt = createdAt;
}

public Long getUserId() {
public Integer getUserId() {
return userId;
}

Expand Down
Expand Up @@ -66,7 +66,7 @@ public void execute_sets_inactive_user_with_no_permission_has_not_root() throws

@Test
public void execute_sets_active_user_with_admin_role_has_root() throws SQLException {
long userId = insertUser("foo", true);
int userId = insertUser("foo", true);
insertRole(userId, ROLE_ADMIN);

underTest.execute();
Expand All @@ -76,7 +76,7 @@ public void execute_sets_active_user_with_admin_role_has_root() throws SQLExcept

@Test
public void execute_sets_inactive_user_with_admin_role_has_not_root() throws SQLException {
long userId = insertUser("bar", false);
int userId = insertUser("bar", false);
insertRole(userId, ROLE_ADMIN);

underTest.execute();
Expand All @@ -86,8 +86,8 @@ public void execute_sets_inactive_user_with_admin_role_has_not_root() throws SQL

@Test
public void execute_sets_active_user_in_group_with_admin_role_has_root() throws SQLException {
long userId = insertUser("doo", true);
long groupId = insertGroup("admin group");
int userId = insertUser("doo", true);
int groupId = insertGroup("admin group");
insertGroupRole(groupId, ROLE_ADMIN);
addUserToGroup(userId, groupId);

Expand All @@ -98,8 +98,8 @@ public void execute_sets_active_user_in_group_with_admin_role_has_root() throws

@Test
public void execute_sets_inactive_user_in_group_with_admin_role_has_not_root() throws SQLException {
long userId = insertUser("bar", false);
long groupId = insertGroup("admin group");
int userId = insertUser("bar", false);
int groupId = insertGroup("admin group");
insertGroupRole(groupId, ROLE_ADMIN);
addUserToGroup(userId, groupId);

Expand All @@ -110,10 +110,10 @@ public void execute_sets_inactive_user_in_group_with_admin_role_has_not_root() t

@Test
public void migration_is_reentrant() throws SQLException {
long adminGroupId = insertGroup("admin group");
int adminGroupId = insertGroup("admin group");
insertGroupRole(adminGroupId, ROLE_ADMIN);
long groupId = insertGroup("other group");
long[] userIds = {
int groupId = insertGroup("other group");
int[] userIds = {
insertUser("inactive_direct_admin", false),
insertUser("active_direct_admin", true),
insertUser("inactive_group_admin", false),
Expand Down Expand Up @@ -158,29 +158,31 @@ private void verifyUsers() {
verifyUser("no_perm_user", false);
}

private void insertRole(long userId, String role) {
private void insertRole(int userId, String role) {
dbTester.executeInsert("user_roles", "user_id", userId, "role", role);
dbTester.commit();
}

private long insertUser(String login, boolean active) {
private int insertUser(String login, boolean active) {
dbTester.executeInsert(USERS_TABLE, "login", login, "active", active);
dbTester.commit();
return (Long) dbTester.selectFirst("select id as \"id\" from users where login = '" + login + "'").get("id");
Long userId = (Long) dbTester.selectFirst("select id as \"id\" from users where login = '" + login + "'").get("id");
return userId.intValue();
}

private long insertGroup(String groupName) {
private int insertGroup(String groupName) {
dbTester.executeInsert("groups", "name", groupName);
dbTester.commit();
return (Long) dbTester.selectFirst("select id as \"id\" from groups where name = '" + groupName + "'").get("id");
Long groupId = (Long) dbTester.selectFirst("select id as \"id\" from groups where name = '" + groupName + "'").get("id");
return groupId.intValue();
}

private void insertGroupRole(long groupId, String role) {
private void insertGroupRole(int groupId, String role) {
dbTester.executeInsert("group_roles", "group_id", groupId, "role", role);
dbTester.commit();
}

private void addUserToGroup(long userId, long groupId) {
private void addUserToGroup(int userId, int groupId) {
dbTester.executeInsert("groups_users", "user_id", userId, "group_id", groupId);
dbTester.commit();
}
Expand Down
Expand Up @@ -69,7 +69,7 @@ public ComponentUpdater(DbClient dbClient, I18n i18n, System2 system2,
* - Add component to favorite if the component has the 'Project Creators' permission
* - Index component if es indexes
*/
public ComponentDto create(DbSession dbSession, NewComponent newComponent, @Nullable Long userId) {
public ComponentDto create(DbSession dbSession, NewComponent newComponent, @Nullable Integer userId) {
checkKeyFormat(newComponent.qualifier(), newComponent.key());
ComponentDto componentDto = createRootComponent(dbSession, newComponent);
removeDuplicatedProjects(dbSession, componentDto.getKey());
Expand Down Expand Up @@ -119,7 +119,7 @@ private void removeDuplicatedProjects(DbSession session, String projectKey) {
}
}

private void handlePermissionTemplate(DbSession dbSession, ComponentDto componentDto, String organizationUuid, @Nullable Long userId) {
private void handlePermissionTemplate(DbSession dbSession, ComponentDto componentDto, String organizationUuid, @Nullable Integer userId) {
permissionTemplateService.applyDefault(dbSession, organizationUuid, componentDto, userId);
if (componentDto.qualifier().equals(PROJECT)
&& permissionTemplateService.hasDefaultTemplateWithPermissionOnProjectCreator(dbSession, organizationUuid, componentDto)) {
Expand Down
Expand Up @@ -48,7 +48,7 @@ public DefaultRubyComponentService(UserSession userSession, DbClient dbClient, C

// Used in GOV
/**
* @deprecated Use {@link ComponentUpdater#create(DbSession, NewComponent, Long)} instead
* @deprecated Use {@link ComponentUpdater#create(DbSession, NewComponent, Integer)} instead
*/
@Deprecated
public Long createComponent(String key, String name, String qualifier) {
Expand All @@ -62,7 +62,7 @@ public Long createComponent(String key, String name, String qualifier) {
return componentUpdater.create(
dbSession,
newComponent,
userSession.isLoggedIn() ? userSession.getUserId().longValue() : null).getId();
userSession.isLoggedIn() ? userSession.getUserId() : null).getId();
}
}

Expand Down
Expand Up @@ -109,10 +109,9 @@ private static void ensureOrganizationIsConsistent(Optional<ComponentDto> projec
private ComponentDto createProject(DbSession dbSession, String organizationUuid, String projectKey, @Nullable String projectBranch, @Nullable String projectName) {
userSession.checkOrganizationPermission(organizationUuid, PROVISIONING);
Integer userId = userSession.getUserId();
Long projectCreatorUserId = userId == null ? null : userId.longValue();

boolean wouldCurrentUserHaveScanPermission = permissionTemplateService.wouldUserHavePermissionWithDefaultTemplate(
dbSession, organizationUuid, projectCreatorUserId, SCAN_EXECUTION, projectBranch, projectKey, Qualifiers.PROJECT);
dbSession, organizationUuid, userId, SCAN_EXECUTION, projectBranch, projectKey, Qualifiers.PROJECT);
if (!wouldCurrentUserHaveScanPermission) {
throw insufficientPrivilegesException();
}
Expand All @@ -124,7 +123,7 @@ private ComponentDto createProject(DbSession dbSession, String organizationUuid,
.setBranch(projectBranch)
.setQualifier(Qualifiers.PROJECT)
.build();
return componentUpdater.create(dbSession, newProject, projectCreatorUserId);
return componentUpdater.create(dbSession, newProject, userId);
}

private CeTask submitReport(DbSession dbSession, InputStream reportInput, ComponentDto project) {
Expand Down
Expand Up @@ -42,14 +42,14 @@ public FavoriteUpdater(DbClient dbClient) {
/**
* Set favorite to the logged in user. If no user, no action is done
*/
public void add(DbSession dbSession, ComponentDto componentDto, @Nullable Long userId) {
public void add(DbSession dbSession, ComponentDto componentDto, @Nullable Integer userId) {
if (userId == null) {
return;
}

List<PropertyDto> existingFavoriteOnComponent = dbClient.propertiesDao().selectByQuery(PropertyQuery.builder()
.setKey(PROP_FAVORITE_KEY)
.setUserId(userId.intValue())
.setUserId(userId)
.setComponentId(componentDto.getId())
.build(), dbSession);
checkRequest(existingFavoriteOnComponent.isEmpty(), "Component '%s' is already a favorite", componentDto.getKey());
Expand All @@ -63,7 +63,7 @@ public void add(DbSession dbSession, ComponentDto componentDto, @Nullable Long u
* Remove a favorite to the user.
* @throws BadRequestException if the component is not a favorite
*/
public void remove(DbSession dbSession, ComponentDto component, @Nullable Long userId) {
public void remove(DbSession dbSession, ComponentDto component, @Nullable Integer userId) {
if (userId == null) {
return;
}
Expand Down
Expand Up @@ -75,7 +75,7 @@ private Consumer<Request> addFavorite() {
userSession
.checkLoggedIn()
.checkComponentPermission(UserRole.USER, componentDto);
favoriteUpdater.add(dbSession, componentDto, userSession.isLoggedIn() ? userSession.getUserId().longValue() : null);
favoriteUpdater.add(dbSession, componentDto, userSession.isLoggedIn() ? userSession.getUserId() : null);
dbSession.commit();
}
};
Expand Down
Expand Up @@ -73,7 +73,7 @@ private Consumer<Request> removeFavorite() {
ComponentDto component = componentFinder.getByKey(dbSession, request.mandatoryParam(PARAM_COMPONENT));
userSession
.checkLoggedIn();
favoriteUpdater.remove(dbSession, component, userSession.isLoggedIn() ? userSession.getUserId().longValue() : null);
favoriteUpdater.remove(dbSession, component, userSession.isLoggedIn() ? userSession.getUserId() : null);
dbSession.commit();
}
};
Expand Down
Expand Up @@ -70,7 +70,7 @@ public void add(DbSession dbSession, String channel, String dispatcher, @Nullabl

dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto()
.setKey(key)
.setUserId(Long.valueOf(userSession.getUserId()))
.setUserId(userSession.getUserId())
.setValue(PROP_NOTIFICATION_VALUE)
.setResourceId(projectId));
}
Expand Down Expand Up @@ -100,7 +100,7 @@ public void remove(DbSession dbSession, String channel, String dispatcher, @Null

dbClient.propertiesDao().delete(dbSession, new PropertyDto()
.setKey(key)
.setUserId(Long.valueOf(userSession.getUserId()))
.setUserId(userSession.getUserId())
.setValue(PROP_NOTIFICATION_VALUE)
.setResourceId(projectId));
}
Expand Down
Expand Up @@ -62,7 +62,7 @@ public interface OrganizationCreation {
* @throws KeyConflictException if an organization with the specified key already exists
* @throws IllegalArgumentException if any field of {@code newOrganization} is invalid according to {@link OrganizationValidation}
*/
OrganizationDto create(DbSession dbSession, long createUserId, NewOrganization newOrganization) throws KeyConflictException;
OrganizationDto create(DbSession dbSession, int createUserId, NewOrganization newOrganization) throws KeyConflictException;

/**
* Create a new guarded organization which details are based on the login of the specified User.
Expand Down
Expand Up @@ -62,7 +62,7 @@ public OrganizationCreationImpl(DbClient dbClient, System2 system2, UuidFactory
}

@Override
public OrganizationDto create(DbSession dbSession, long creatorUserId, NewOrganization newOrganization) throws KeyConflictException {
public OrganizationDto create(DbSession dbSession, int creatorUserId, NewOrganization newOrganization) throws KeyConflictException {
validate(newOrganization);
String key = newOrganization.getKey();
if (organizationKeyIsUsed(dbSession, key)) {
Expand Down Expand Up @@ -201,7 +201,7 @@ private void addPermissionToGroup(DbSession dbSession, GroupDto group, String pe
.setRole(permission));
}

private void addCurrentUserToGroup(DbSession dbSession, GroupDto group, long createUserId) {
private void addCurrentUserToGroup(DbSession dbSession, GroupDto group, int createUserId) {
dbClient.userGroupDao().insert(
dbSession,
new UserGroupDto().setGroupId(group.getId()).setUserId(createUserId));
Expand Down
Expand Up @@ -103,7 +103,7 @@ public void handle(Request request, Response response) throws Exception {
organizationFlags.checkEnabled(dbSession);
OrganizationDto organization = organizationCreation.create(
dbSession,
userSession.getUserId().longValue(),
userSession.getUserId(),
newOrganizationBuilder()
.setName(name)
.setKey(key)
Expand Down
Expand Up @@ -70,7 +70,7 @@ public PermissionTemplateService(DbClient dbClient, PermissionIndexer permission
}

public boolean wouldUserHavePermissionWithDefaultTemplate(DbSession dbSession,
String organizationUuid, @Nullable Long userId, String globalPermission, @Nullable String branch, String projectKey,
String organizationUuid, @Nullable Integer userId, String globalPermission, @Nullable String branch, String projectKey,
String qualifier) {
if (userSession.hasOrganizationPermission(organizationUuid, globalPermission)) {
return true;
Expand Down Expand Up @@ -109,7 +109,7 @@ public void apply(DbSession dbSession, PermissionTemplateDto template, Collectio
* can be provisioned (so has no permissions yet).
* @param projectCreatorUserId id of the user who creates the project, only if project is provisioned. He will
*/
public void applyDefault(DbSession dbSession, String organizationUuid, ComponentDto component, @Nullable Long projectCreatorUserId) {
public void applyDefault(DbSession dbSession, String organizationUuid, ComponentDto component, @Nullable Integer projectCreatorUserId) {
PermissionTemplateDto template = findTemplate(dbSession, organizationUuid, component);
checkArgument(template != null, "Cannot retrieve default permission template");
copyPermissions(dbSession, template, component, projectCreatorUserId);
Expand All @@ -131,7 +131,7 @@ private void indexProjectPermissions(DbSession dbSession, List<String> projectOr
permissionIndexer.indexProjectsByUuids(dbSession, projectOrViewUuids);
}

private void copyPermissions(DbSession dbSession, PermissionTemplateDto template, ComponentDto project, @Nullable Long projectCreatorUserId) {
private void copyPermissions(DbSession dbSession, PermissionTemplateDto template, ComponentDto project, @Nullable Integer projectCreatorUserId) {
dbClient.resourceDao().updateAuthorizationDate(project.getId(), dbSession);
dbClient.groupPermissionDao().deleteByRootComponentId(dbSession, project.getId());
dbClient.userPermissionDao().deleteProjectPermissions(dbSession, project.getId());
Expand Down
Expand Up @@ -32,15 +32,15 @@
@Immutable
public class UserId {

private final long id;
private final int id;
private final String login;

public UserId(long userId, String login) {
public UserId(int userId, String login) {
this.id = userId;
this.login = requireNonNull(login);
}

public long getId() {
public int getId() {
return id;
}

Expand Down

0 comments on commit 33acda7

Please sign in to comment.