From 3a9d42f4075e23448349b2f96c332fe6eae7a55b Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Tue, 19 Jan 2016 11:48:05 +0100 Subject: SONAR-6226 Add ext identity columns in USERS table --- .../src/main/java/org/sonar/db/user/UserDao.java | 15 ++++ .../src/main/java/org/sonar/db/user/UserDto.java | 20 +++++ .../main/java/org/sonar/db/user/UserMapper.java | 3 + .../java/org/sonar/db/version/DatabaseVersion.java | 2 +- .../org/sonar/db/version/MigrationStepModule.java | 7 +- .../db/version/v54/AddUsersIdentityColumns.java | 55 +++++++++++++ .../sonar/db/version/v54/MigrateUsersIdentity.java | 67 ++++++++++++++++ .../resources/org/sonar/db/user/UserMapper.xml | 15 +++- .../resources/org/sonar/db/version/rows-h2.sql | 3 + .../resources/org/sonar/db/version/schema-h2.ddl | 4 + .../test/java/org/sonar/db/user/UserDaoTest.java | 28 +++++++ .../test/java/org/sonar/db/user/UserTesting.java | 18 +++-- .../sonar/db/version/MigrationStepModuleTest.java | 2 +- .../version/v54/AddUsersIdentityColumnsTest.java | 51 ++++++++++++ .../db/version/v54/MigrateUsersIdentityTest.java | 91 ++++++++++++++++++++++ .../UserDaoTest/select_users_by_ext_identity.xml | 12 +++ .../v54/AddUsersIdentityColumnsTest/schema.sql | 14 ++++ .../MigrateUsersIdentityTest/migrate-result.xml | 13 ++++ .../v54/MigrateUsersIdentityTest/migrate.xml | 13 ++++ .../v54/MigrateUsersIdentityTest/schema.sql | 16 ++++ 20 files changed, 437 insertions(+), 12 deletions(-) create mode 100644 sonar-db/src/main/java/org/sonar/db/version/v54/AddUsersIdentityColumns.java create mode 100644 sonar-db/src/main/java/org/sonar/db/version/v54/MigrateUsersIdentity.java create mode 100644 sonar-db/src/test/java/org/sonar/db/version/v54/AddUsersIdentityColumnsTest.java create mode 100644 sonar-db/src/test/java/org/sonar/db/version/v54/MigrateUsersIdentityTest.java create mode 100644 sonar-db/src/test/resources/org/sonar/db/user/UserDaoTest/select_users_by_ext_identity.xml create mode 100644 sonar-db/src/test/resources/org/sonar/db/version/v54/AddUsersIdentityColumnsTest/schema.sql create mode 100644 sonar-db/src/test/resources/org/sonar/db/version/v54/MigrateUsersIdentityTest/migrate-result.xml create mode 100644 sonar-db/src/test/resources/org/sonar/db/version/v54/MigrateUsersIdentityTest/migrate.xml create mode 100644 sonar-db/src/test/resources/org/sonar/db/version/v54/MigrateUsersIdentityTest/schema.sql (limited to 'sonar-db') diff --git a/sonar-db/src/main/java/org/sonar/db/user/UserDao.java b/sonar-db/src/main/java/org/sonar/db/user/UserDao.java index 8172f465b3d..8799754185a 100644 --- a/sonar-db/src/main/java/org/sonar/db/user/UserDao.java +++ b/sonar-db/src/main/java/org/sonar/db/user/UserDao.java @@ -20,6 +20,7 @@ package org.sonar.db.user; import com.google.common.base.Function; +import com.google.common.base.Optional; import java.util.Collection; import java.util.List; import javax.annotation.CheckForNull; @@ -33,6 +34,8 @@ import org.sonar.db.DbSession; import org.sonar.db.MyBatis; import org.sonar.db.RowNotFoundException; +import static com.google.common.base.Optional.fromNullable; + public class UserDao implements Dao { private final MyBatis mybatis; @@ -172,6 +175,18 @@ public class UserDao implements Dao { return mapper(session).selectNullableByScmAccountOrLoginOrEmail(scmAccountOrLoginOrEmail, like); } + public Optional selectByExternalIdentity(DbSession session, String extIdentity, String extIdentityProvider){ + return fromNullable(mapper(session).selectByIdentity(extIdentity, extIdentityProvider)); + } + + public UserDto selectOrFailByExternalIdentity(DbSession session, String extIdentity, String extIdentityProvider) { + Optional user = selectByExternalIdentity(session, extIdentity, extIdentityProvider); + if (user.isPresent()) { + return user.get(); + } + throw new RowNotFoundException(String.format("User with identity provider '%s' and id '%s' has not been found", extIdentityProvider, extIdentity)); + } + protected UserMapper mapper(DbSession session) { return session.getMapper(UserMapper.class); } diff --git a/sonar-db/src/main/java/org/sonar/db/user/UserDto.java b/sonar-db/src/main/java/org/sonar/db/user/UserDto.java index 8c2b067dd72..a6983bf1638 100644 --- a/sonar-db/src/main/java/org/sonar/db/user/UserDto.java +++ b/sonar-db/src/main/java/org/sonar/db/user/UserDto.java @@ -40,6 +40,8 @@ public class UserDto { private String email; private boolean active = true; private String scmAccounts; + private String externalIdentity; + private String externalIdentityProvider; private String cryptedPassword; private String salt; private Long createdAt; @@ -129,6 +131,24 @@ public class UserDto { } } + public String getExternalIdentity() { + return externalIdentity; + } + + public UserDto setExternalIdentity(String authorithy) { + this.externalIdentity = authorithy; + return this; + } + + public String getExternalIdentityProvider() { + return externalIdentityProvider; + } + + public UserDto setExternalIdentityProvider(String externalIdentityProvider) { + this.externalIdentityProvider = externalIdentityProvider; + return this; + } + public String getCryptedPassword() { return cryptedPassword; } diff --git a/sonar-db/src/main/java/org/sonar/db/user/UserMapper.java b/sonar-db/src/main/java/org/sonar/db/user/UserMapper.java index 7835c44bcec..605998588db 100644 --- a/sonar-db/src/main/java/org/sonar/db/user/UserMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/user/UserMapper.java @@ -51,6 +51,9 @@ public interface UserMapper { List selectByLogins(List logins); + @CheckForNull + UserDto selectByIdentity(@Param("extIdentity") String authorityId, @Param("extIdentityProvider") String authorityProvider); + @CheckForNull GroupDto selectGroupByName(String name); diff --git a/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java b/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java index b5f895b29ab..43be9647d12 100644 --- a/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java +++ b/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java @@ -29,7 +29,7 @@ import org.sonar.db.MyBatis; public class DatabaseVersion { - public static final int LAST_VERSION = 1010; + public static final int LAST_VERSION = 1013; /** * The minimum supported version which can be upgraded. Lower diff --git a/sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java b/sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java index d4f25e25adc..4591717227e 100644 --- a/sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java +++ b/sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java @@ -61,7 +61,9 @@ import org.sonar.db.version.v52.RemoveRuleMeasuresOnIssues; import org.sonar.db.version.v52.RemoveSnapshotLibraries; import org.sonar.db.version.v53.FixMsSqlCollation; import org.sonar.db.version.v53.UpdateCustomDashboardInLoadedTemplates; +import org.sonar.db.version.v54.AddUsersIdentityColumns; import org.sonar.db.version.v54.InsertGateAdminPermissionForEachProfileAdmin; +import org.sonar.db.version.v54.MigrateUsersIdentity; import org.sonar.db.version.v54.RemoveComponentPageProperties; public class MigrationStepModule extends Module { @@ -121,6 +123,9 @@ public class MigrationStepModule extends Module { // 5.4 InsertGateAdminPermissionForEachProfileAdmin.class, - RemoveComponentPageProperties.class); + RemoveComponentPageProperties.class, + AddUsersIdentityColumns.class, + MigrateUsersIdentity.class + ); } } diff --git a/sonar-db/src/main/java/org/sonar/db/version/v54/AddUsersIdentityColumns.java b/sonar-db/src/main/java/org/sonar/db/version/v54/AddUsersIdentityColumns.java new file mode 100644 index 00000000000..989ae5db81c --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/version/v54/AddUsersIdentityColumns.java @@ -0,0 +1,55 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.db.version.v54; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.db.version.AddColumnsBuilder; +import org.sonar.db.version.DdlChange; + +import static org.sonar.db.version.VarcharColumnDef.newVarcharColumnDefBuilder; + +/** + * Add the following columns to the USERS table : + * - external_identity + * - external_identity_provider + */ +public class AddUsersIdentityColumns extends DdlChange { + + private final Database db; + + public AddUsersIdentityColumns(Database db) { + super(db); + this.db = db; + } + + @Override + public void execute(DdlChange.Context context) throws SQLException { + context.execute(generateSql()); + } + + private String generateSql() { + return new AddColumnsBuilder(db.getDialect(), "users") + .addColumn(newVarcharColumnDefBuilder().setColumnName("external_identity").setLimit(4000).setIsNullable(true).build()) + .addColumn(newVarcharColumnDefBuilder().setColumnName("external_identity_provider").setLimit(100).setIsNullable(true).build()) + .build(); + } + +} diff --git a/sonar-db/src/main/java/org/sonar/db/version/v54/MigrateUsersIdentity.java b/sonar-db/src/main/java/org/sonar/db/version/v54/MigrateUsersIdentity.java new file mode 100644 index 00000000000..476153feb5c --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/version/v54/MigrateUsersIdentity.java @@ -0,0 +1,67 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.db.version.v54; + +import java.sql.SQLException; +import org.sonar.api.utils.System2; +import org.sonar.db.Database; +import org.sonar.db.version.BaseDataChange; +import org.sonar.db.version.MassUpdate; +import org.sonar.db.version.Select; +import org.sonar.db.version.SqlStatement; + +/** + * Update all users to feed external_identity_provider with 'sonarqube' and external_identity with the login + */ +public class MigrateUsersIdentity extends BaseDataChange { + + private final System2 system2; + + public MigrateUsersIdentity(Database db, System2 system2) { + super(db); + this.system2 = system2; + } + + @Override + public void execute(Context context) throws SQLException { + MassUpdate update = context.prepareMassUpdate().rowPluralName("users"); + update.select("SELECT u.id, u.login FROM users u"); + update.update("UPDATE users SET external_identity_provider=?, external_identity=?, updated_at=? WHERE id=? " + + "AND external_identity_provider IS NULL AND external_identity IS NULL"); + update.execute(new MigrationHandler(system2.now())); + } + + private static class MigrationHandler implements MassUpdate.Handler { + private final long now; + + public MigrationHandler(long now) { + this.now = now; + } + + @Override + public boolean handle(Select.Row row, SqlStatement update) throws SQLException { + update.setString(1, "sonarqube"); + update.setString(2, row.getString(2)); + update.setLong(3, now); + update.setLong(4, row.getLong(1)); + return true; + } + } +} diff --git a/sonar-db/src/main/resources/org/sonar/db/user/UserMapper.xml b/sonar-db/src/main/resources/org/sonar/db/user/UserMapper.xml index 14e57dcd5df..a4d3b636448 100644 --- a/sonar-db/src/main/resources/org/sonar/db/user/UserMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/user/UserMapper.xml @@ -12,6 +12,8 @@ u.scm_accounts as "scmAccounts", u.salt as "salt", u.crypted_password as "cryptedPassword", + u.external_identity as "externalIdentity", + u.external_identity_provider as "externalIdentityProvider", u.created_at as "createdAt", u.updated_at as "updatedAt" @@ -87,6 +89,15 @@ ORDER BY u.name + +