From 22650cfd9cae653a9cdb14705350e617166f5ab0 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Tue, 15 Mar 2016 13:55:10 +0100 Subject: [PATCH] SONAR-7254 Add USERS.LOCAL column --- .../db/migrate/1119_add_users_local_column.rb | 31 +++++++++++++++++++ .../main/java/org/sonar/db/user/UserDao.java | 12 ++++--- .../main/java/org/sonar/db/user/UserDto.java | 10 ++++++ .../org/sonar/db/version/DatabaseVersion.java | 2 +- .../org/sonar/db/user/UserMapper.xml | 5 ++- .../org/sonar/db/version/rows-h2.sql | 3 +- .../org/sonar/db/version/schema-h2.ddl | 1 + .../java/org/sonar/db/user/UserDaoTest.java | 25 ++++++++++----- 8 files changed, 75 insertions(+), 14 deletions(-) create mode 100644 server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1119_add_users_local_column.rb diff --git a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1119_add_users_local_column.rb b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1119_add_users_local_column.rb new file mode 100644 index 00000000000..339033edeed --- /dev/null +++ b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1119_add_users_local_column.rb @@ -0,0 +1,31 @@ +# +# SonarQube, open source software quality management tool. +# Copyright (C) 2008-2014 SonarSource +# mailto:contact AT sonarsource DOT com +# +# SonarQube 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. +# +# SonarQube 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. +# + +# +# SonarQube 5.5 +# SONAR-7254 +# +class AddUsersLocalColumn < ActiveRecord::Migration + + def self.up + add_column 'users', 'user_local', :boolean + end + +end 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 7a9751dfc85..d5119727b48 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 @@ -108,15 +108,19 @@ public class UserDao implements Dao { } public List selectUsers(UserQuery query) { - SqlSession session = mybatis.openSession(false); + DbSession session = mybatis.openSession(false); try { - UserMapper mapper = session.getMapper(UserMapper.class); - return mapper.selectUsers(query); + return selectUsers(session, query); } finally { MyBatis.closeQuietly(session); } } + public List selectUsers(DbSession dbSession, UserQuery query) { + UserMapper mapper = dbSession.getMapper(UserMapper.class); + return mapper.selectUsers(query); + } + public UserDto insert(SqlSession session, UserDto dto) { session.getMapper(UserMapper.class).insert(dto); return dto; @@ -177,7 +181,7 @@ public class UserDao implements Dao { * * Please note that email is case insensitive, result for searching 'mail@email.com' or 'Mail@Email.com' will be the same */ - public boolean doesEmailExist(DbSession dbSession, String email){ + public boolean doesEmailExist(DbSession dbSession, String email) { return mapper(dbSession).countByEmail(email.toLowerCase()) > 0; } 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 5e24f886255..57db727c48c 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 @@ -49,6 +49,7 @@ public class UserDto { private Date rememberTokenExpiresAt; private Long createdAt; private Long updatedAt; + private boolean local = true; public Long getId() { return id; @@ -156,6 +157,15 @@ public class UserDto { return this; } + public boolean isLocal() { + return local; + } + + public UserDto setLocal(boolean local) { + this.local = local; + return this; + } + public String getCryptedPassword() { return cryptedPassword; } 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 148c2af5255..bcfa9d1a242 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 = 1118; + public static final int LAST_VERSION = 1119; /** * The minimum supported version which can be upgraded. Lower 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 bd16343959a..7d9e4d69230 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 @@ -14,6 +14,7 @@ u.crypted_password as "cryptedPassword", u.external_identity as "externalIdentity", u.external_identity_provider as "externalIdentityProvider", + u.user_local as "local", u.remember_token as "rememberToken", u.remember_token_expires_at as "rememberTokenExpiresAt", u.created_at as "createdAt", @@ -147,10 +148,11 @@ - INSERT INTO users (login, name, email, active, scm_accounts, external_identity, external_identity_provider, salt, crypted_password, + INSERT INTO users (login, name, email, active, scm_accounts, external_identity, external_identity_provider, user_local, salt, crypted_password, remember_token, remember_token_expires_at, created_at, updated_at) VALUES (#{login,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR}, #{active,jdbcType=BOOLEAN}, #{scmAccounts,jdbcType=VARCHAR}, #{externalIdentity,jdbcType=VARCHAR}, #{externalIdentityProvider,jdbcType=VARCHAR}, + #{local,jdbcType=BOOLEAN}, #{salt,jdbcType=VARCHAR}, #{cryptedPassword,jdbcType=VARCHAR}, #{rememberToken,jdbcType=VARCHAR}, #{rememberTokenExpiresAt,jdbcType=TIMESTAMP}, #{createdAt,jdbcType=BIGINT}, #{updatedAt,jdbcType=BIGINT}) @@ -159,6 +161,7 @@ UPDATE users set name=#{name,jdbcType=VARCHAR}, email=#{email,jdbcType=VARCHAR}, active=#{active,jdbcType=BOOLEAN}, scm_accounts=#{scmAccounts,jdbcType=VARCHAR}, external_identity=#{externalIdentity,jdbcType=VARCHAR}, external_identity_provider=#{externalIdentityProvider,jdbcType=VARCHAR}, + user_local=#{local,jdbcType=BOOLEAN}, salt=#{salt,jdbcType=VARCHAR}, crypted_password=#{cryptedPassword,jdbcType=BIGINT}, remember_token=#{rememberToken,jdbcType=VARCHAR}, remember_token_expires_at=#{rememberTokenExpiresAt,jdbcType=TIMESTAMP}, updated_at=#{updatedAt,jdbcType=BIGINT} diff --git a/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql b/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql index 036642f4356..67b26290e70 100644 --- a/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql +++ b/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql @@ -396,6 +396,7 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1115'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1116'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1117'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1118'); +INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1119'); -INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, EXTERNAL_IDENTITY, EXTERNAL_IDENTITY_PROVIDER, CRYPTED_PASSWORD, SALT, CREATED_AT, UPDATED_AT, REMEMBER_TOKEN, REMEMBER_TOKEN_EXPIRES_AT) VALUES (1, 'admin', 'Administrator', '', 'admin', 'sonarqube', 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', '1418215735482', '1418215735482', null, null); +INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, EXTERNAL_IDENTITY, EXTERNAL_IDENTITY_PROVIDER, USER_LOCAL, CRYPTED_PASSWORD, SALT, CREATED_AT, UPDATED_AT, REMEMBER_TOKEN, REMEMBER_TOKEN_EXPIRES_AT) VALUES (1, 'admin', 'Administrator', '', 'admin', 'sonarqube', true, 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', '1418215735482', '1418215735482', null, null); ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2; diff --git a/sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl b/sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl index 36775ed6ed1..eb657209f2e 100644 --- a/sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl +++ b/sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl @@ -303,6 +303,7 @@ CREATE TABLE "USERS" ( "SCM_ACCOUNTS" VARCHAR(4000), "EXTERNAL_IDENTITY" VARCHAR(255), "EXTERNAL_IDENTITY_PROVIDER" VARCHAR(100), + "USER_LOCAL" BOOLEAN, "CREATED_AT" BIGINT, "UPDATED_AT" BIGINT ); diff --git a/sonar-db/src/test/java/org/sonar/db/user/UserDaoTest.java b/sonar-db/src/test/java/org/sonar/db/user/UserDaoTest.java index 6724dbe11ad..09f42002021 100644 --- a/sonar-db/src/test/java/org/sonar/db/user/UserDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/user/UserDaoTest.java @@ -187,6 +187,7 @@ public class UserDaoTest { .setCryptedPassword("abcd") .setExternalIdentity("johngithub") .setExternalIdentityProvider("github") + .setLocal(true) .setRememberToken("1234") .setRememberTokenExpiresAt(new Date()) .setCreatedAt(date) @@ -206,6 +207,7 @@ public class UserDaoTest { assertThat(user.getCryptedPassword()).isEqualTo("abcd"); assertThat(user.getExternalIdentity()).isEqualTo("johngithub"); assertThat(user.getExternalIdentityProvider()).isEqualTo("github"); + assertThat(user.isLocal()).isTrue(); assertThat(user.getRememberToken()).isEqualTo("1234"); assertThat(user.getRememberTokenExpiresAt()).isNotNull(); assertThat(user.getCreatedAt()).isEqualTo(date); @@ -214,9 +216,16 @@ public class UserDaoTest { @Test public void update_user() { - db.prepareDbUnit(getClass(), "update_user.xml"); - - Long date = DateUtils.parseDate("2014-06-21").getTime(); + UserDto existingUser = new UserDto() + .setLogin("john") + .setName("John") + .setEmail("jo@hn.com") + .setCreatedAt(1418215735482L) + .setUpdatedAt(1418215735482L) + .setActive(true) + .setLocal(true); + db.getDbClient().userDao().insert(db.getSession(), existingUser); + db.getSession().commit(); UserDto userDto = new UserDto() .setId(1L) @@ -229,13 +238,14 @@ public class UserDaoTest { .setCryptedPassword("abcde") .setExternalIdentity("johngithub") .setExternalIdentityProvider("github") - .setUpdatedAt(date); + .setLocal(false) + .setUpdatedAt(1500000000000L); underTest.update(db.getSession(), userDto); db.getSession().commit(); - UserDto user = underTest.selectUserById(1); + UserDto user = underTest.selectUserById(db.getSession(), existingUser.getId()); assertThat(user).isNotNull(); - assertThat(user.getId()).isEqualTo(1L); + assertThat(user.getId()).isEqualTo(existingUser.getId()); assertThat(user.getLogin()).isEqualTo("john"); assertThat(user.getName()).isEqualTo("John Doo"); assertThat(user.getEmail()).isEqualTo("jodoo@hn.com"); @@ -245,8 +255,9 @@ public class UserDaoTest { assertThat(user.getCryptedPassword()).isEqualTo("abcde"); assertThat(user.getExternalIdentity()).isEqualTo("johngithub"); assertThat(user.getExternalIdentityProvider()).isEqualTo("github"); + assertThat(user.isLocal()).isFalse(); assertThat(user.getCreatedAt()).isEqualTo(1418215735482L); - assertThat(user.getUpdatedAt()).isEqualTo(date); + assertThat(user.getUpdatedAt()).isEqualTo(1500000000000L); } @Test -- 2.39.5