public class FakeBaseIdProvider implements BaseIdentityProvider {
private static final String ENABLED = "sonar.auth.fake-base-id-provider.enabled";
+ private static final String ALLOWS_USERS_TO_SIGN_UP = "sonar.auth.fake-base-id-provider.allowsUsersToSignUp";
private static final String USER_INFO = "sonar.auth.fake-base-id-provider.user";
private final Settings settings;
}
String[] userInfos = userInfoProperty.split(",");
context.authenticate(UserIdentity.builder()
- .setId(userInfos[0])
- .setName(userInfos[1])
- .setEmail(userInfos[2])
+ .setLogin(userInfos[0])
+ .setProviderLogin(userInfos[1])
+ .setName(userInfos[2])
+ .setEmail(userInfos[3])
.build());
try {
@Override
public boolean allowsUsersToSignUp() {
+ if (settings.hasKey(ALLOWS_USERS_TO_SIGN_UP)) {
+ return settings.getBoolean(ALLOWS_USERS_TO_SIGN_UP);
+ }
+ // If property is not defined, default behaviour is not always allow users to sign up
return true;
}
}
}
String[] userInfos = userInfoProperty.split(",");
context.authenticate(UserIdentity.builder()
- .setId(userInfos[0])
- .setName(userInfos[1])
- .setEmail(userInfos[2])
+ .setLogin(userInfos[0])
+ .setProviderLogin(userInfos[1])
+ .setName(userInfos[2])
+ .setEmail(userInfos[3])
.build());
context.redirectToRequestedPage();
}
@Override
public String getIconPath() {
- return "/static/baseauthplugin/base.png";
+ return "/static/oauth2authplugin/oauth2.png";
}
@Override
*/
package org.sonar.server.authentication;
-import com.google.common.base.Optional;
import javax.servlet.http.HttpSession;
import org.sonar.api.server.authentication.IdentityProvider;
import org.sonar.api.server.authentication.UserIdentity;
-import org.sonar.core.util.UuidFactory;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.user.UserDto;
+import org.sonar.server.user.ExternalIdentity;
import org.sonar.server.user.NewUser;
import org.sonar.server.user.UpdateUser;
import org.sonar.server.user.UserUpdater;
private final DbClient dbClient;
private final UserUpdater userUpdater;
- private final UuidFactory uuidFactory;
- public UserIdentityAuthenticator(DbClient dbClient, UserUpdater userUpdater, UuidFactory uuidFactory) {
+ public UserIdentityAuthenticator(DbClient dbClient, UserUpdater userUpdater) {
this.dbClient = dbClient;
this.userUpdater = userUpdater;
- this.uuidFactory = uuidFactory;
}
public void authenticate(UserIdentity user, IdentityProvider provider, HttpSession session) {
private long register(UserIdentity user, IdentityProvider provider) {
DbSession dbSession = dbClient.openSession(false);
try {
- String userId = user.getId();
- Optional<UserDto> userDto = dbClient.userDao().selectByExternalIdentity(dbSession, userId, provider.getKey());
- if (userDto.isPresent() && userDto.get().isActive()) {
- userUpdater.update(dbSession, UpdateUser.create(userDto.get().getLogin())
+ String userLogin = user.getLogin();
+ UserDto userDto = dbClient.userDao().selectByLogin(dbSession, userLogin);
+ if (userDto != null && userDto.isActive()) {
+ userUpdater.update(dbSession, UpdateUser.create(userDto.getLogin())
.setEmail(user.getEmail())
.setName(user.getName())
- );
- return userDto.get().getId();
+ .setExternalIdentity(new ExternalIdentity(provider.getKey(), user.getProviderLogin())));
+ return userDto.getId();
}
if (!provider.allowsUsersToSignUp()) {
}
userUpdater.create(dbSession, NewUser.create()
- .setLogin(uuidFactory.create())
+ .setLogin(userLogin)
.setEmail(user.getEmail())
.setName(user.getName())
- .setExternalIdentity(new NewUser.ExternalIdentity(provider.getKey(), userId))
+ .setExternalIdentity(new ExternalIdentity(provider.getKey(), user.getProviderLogin()))
);
- return dbClient.userDao().selectOrFailByExternalIdentity(dbSession, userId, provider.getKey()).getId();
+ return dbClient.userDao().selectOrFailByLogin(dbSession, userLogin).getId();
} finally {
dbClient.closeSession(dbSession);
--- /dev/null
+/*
+ * 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.server.user;
+
+import static java.util.Objects.requireNonNull;
+
+public class ExternalIdentity {
+ private String provider;
+ private String id;
+
+ public ExternalIdentity(String provider, String id) {
+ this.provider = requireNonNull(provider, "Identity provider cannot be null");
+ this.id = requireNonNull(id, "Identity id cannot be null");
+ }
+
+ public String getProvider() {
+ return provider;
+ }
+
+ public String getId() {
+ return id;
+ }
+}
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
-import static java.util.Objects.requireNonNull;
-
public class NewUser {
private String login;
return new NewUser();
}
- public static class ExternalIdentity {
- private String provider;
- private String id;
-
- public ExternalIdentity(String provider, String id) {
- this.provider = requireNonNull(provider, "Identity provider cannot be null");
- this.id = requireNonNull(id, "Identity id cannot be null");
- }
-
- public String getProvider() {
- return provider;
- }
-
- public String getId() {
- return id;
- }
- }
}
*/
package org.sonar.server.user;
-import com.google.common.base.Preconditions;
-
+import java.util.List;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
-import java.util.List;
+import static com.google.common.base.Preconditions.checkNotNull;
public class UpdateUser {
private String password;
private String passwordConfirmation;
- boolean isNameChanged;
- boolean isEmailChanged;
- boolean isScmAccountsChanged;
- boolean isPasswordChanged;
+ private ExternalIdentity externalIdentity;
+
+ boolean nameChanged;
+ boolean emailChanged;
+ boolean scmAccountsChanged;
+ boolean passwordChanged;
+ boolean externalIdentityChanged;
private UpdateUser(String login) {
// No direct call to this constructor
public UpdateUser setName(@Nullable String name) {
this.name = name;
- isNameChanged = true;
+ nameChanged = true;
return this;
}
public UpdateUser setEmail(@Nullable String email) {
this.email = email;
- isEmailChanged = true;
+ emailChanged = true;
return this;
}
public UpdateUser setScmAccounts(@Nullable List<String> scmAccounts) {
this.scmAccounts = scmAccounts;
- isScmAccountsChanged = true;
+ scmAccountsChanged = true;
return this;
}
public UpdateUser setPassword(@Nullable String password) {
this.password = password;
- isPasswordChanged = true;
+ passwordChanged = true;
return this;
}
public UpdateUser setPasswordConfirmation(@Nullable String passwordConfirmation) {
this.passwordConfirmation = passwordConfirmation;
- isPasswordChanged = true;
+ passwordChanged = true;
+ return this;
+ }
+
+ @CheckForNull
+ public ExternalIdentity externalIdentity() {
+ return externalIdentity;
+ }
+
+ public UpdateUser setExternalIdentity(@Nullable ExternalIdentity externalIdentity) {
+ this.externalIdentity = externalIdentity;
+ externalIdentityChanged = true;
return this;
}
public boolean isNameChanged() {
- return isNameChanged;
+ return nameChanged;
}
public boolean isEmailChanged() {
- return isEmailChanged;
+ return emailChanged;
}
public boolean isScmAccountsChanged() {
- return isScmAccountsChanged;
+ return scmAccountsChanged;
}
public boolean isPasswordChanged() {
- return isPasswordChanged;
+ return passwordChanged;
+ }
+
+ public boolean isExternalIdentityChanged() {
+ return externalIdentityChanged;
}
public static UpdateUser create(String login) {
- Preconditions.checkNotNull(login);
+ checkNotNull(login);
return new UpdateUser(login);
}
}
package org.sonar.server.user;
import com.google.common.base.Joiner;
-import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.base.Strings;
import com.google.common.collect.Iterables;
boolean isUserReactivated = false;
UserDto userDto = createNewUserDto(dbSession, newUser);
String login = userDto.getLogin();
- Optional<UserDto> existingUser = dbClient.userDao().selectByExternalIdentity(dbSession, userDto.getExternalIdentity(), userDto.getExternalIdentityProvider());
- if (!existingUser.isPresent()) {
+ UserDto existingUser = dbClient.userDao().selectByLogin(dbSession, userDto.getLogin());
+ if (existingUser == null) {
saveUser(dbSession, userDto);
addDefaultGroup(dbSession, userDto);
} else {
- isUserReactivated = reactivateUser(dbSession, existingUser.get(), login, newUser);
+ isUserReactivated = reactivateUser(dbSession, existingUser, login, newUser);
}
dbSession.commit();
notifyNewUser(userDto.getLogin(), userDto.getName(), newUser.email());
UpdateUser updateUser = UpdateUser.create(login)
.setName(newUser.name())
.setEmail(newUser.email())
- .setScmAccounts(newUser.scmAccounts());
+ .setScmAccounts(newUser.scmAccounts())
+ .setExternalIdentity(newUser.externalIdentity());
if (newUser.password() != null) {
updateUser.setPassword(newUser.password());
}
userDto.setScmAccounts(scmAccounts);
}
- NewUser.ExternalIdentity externalIdentity = newUser.externalIdentity();
- if (externalIdentity == null) {
- userDto.setExternalIdentity(login);
- userDto.setExternalIdentityProvider(SQ_AUTHORITY);
- } else {
- userDto.setExternalIdentity(externalIdentity.getId());
- userDto.setExternalIdentityProvider(externalIdentity.getProvider());
- }
+ setExternalIdentity(userDto, newUser.externalIdentity());
if (!messages.isEmpty()) {
throw new BadRequestException(messages);
}
}
+ setExternalIdentity(userDto, updateUser.externalIdentity());
+
if (!messages.isEmpty()) {
throw new BadRequestException(messages);
}
}
+ private static void setExternalIdentity(UserDto dto, @Nullable ExternalIdentity externalIdentity) {
+ if (externalIdentity == null) {
+ dto.setExternalIdentity(dto.getLogin());
+ dto.setExternalIdentityProvider(SQ_AUTHORITY);
+ } else {
+ dto.setExternalIdentity(externalIdentity.getId());
+ dto.setExternalIdentityProvider(externalIdentity.getProvider());
+ }
+ }
+
private static void checkNotEmptyParam(@Nullable String value, String param, List<Message> messages) {
if (Strings.isNullOrEmpty(value)) {
messages.add(Message.of(Validation.CANT_BE_EMPTY_MESSAGE, param));
static String PUBLIC_ROOT_URL = "https://mydomain.com";
static UserIdentity USER_IDENTITY = UserIdentity.builder()
- .setId("johndoo")
+ .setProviderLogin("johndoo")
+ .setLogin("id:johndoo")
.setName("John")
.setEmail("john@email.com")
.build();
static String NOT_SECURED_PUBLIC_URL = "http://mydomain.com";
static UserIdentity USER_IDENTITY = UserIdentity.builder()
- .setId("johndoo")
+ .setProviderLogin("johndoo")
+ .setLogin("id:johndoo")
.setName("John")
.setEmail("john@email.com")
.build();
*/
package org.sonar.server.authentication;
-import com.google.common.base.Optional;
import javax.servlet.http.HttpSession;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.ExpectedException;
import org.mockito.ArgumentCaptor;
import org.sonar.api.server.authentication.UserIdentity;
-import org.sonar.core.util.UuidFactory;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.user.UserDao;
public class UserIdentityAuthenticatorTest {
- static String USER_LOGIN = "ABCD";
+ static String USER_LOGIN = "github-johndoo";
static UserDto ACTIVE_USER = new UserDto().setId(10L).setLogin(USER_LOGIN).setActive(true);
static UserDto UNACTIVE_USER = new UserDto().setId(11L).setLogin("UNACTIVE").setActive(false);
static UserIdentity USER_IDENTITY = UserIdentity.builder()
- .setId("johndoo")
+ .setProviderLogin("johndoo")
+ .setLogin(USER_LOGIN)
.setName("John")
.setEmail("john@email.com")
.build();
HttpSession httpSession = mock(HttpSession.class);
UserUpdater userUpdater = mock(UserUpdater.class);
- UuidFactory uuidFactory = mock(UuidFactory.class);
- UserIdentityAuthenticator underTest = new UserIdentityAuthenticator(dbClient, userUpdater, uuidFactory);
+ UserIdentityAuthenticator underTest = new UserIdentityAuthenticator(dbClient, userUpdater);
@Before
public void setUp() throws Exception {
when(dbClient.openSession(false)).thenReturn(dbSession);
when(dbClient.userDao()).thenReturn(userDao);
- when(uuidFactory.create()).thenReturn(USER_LOGIN);
}
@Test
public void authenticate_new_user() throws Exception {
- when(userDao.selectByExternalIdentity(dbSession, USER_IDENTITY.getId(), IDENTITY_PROVIDER.getKey())).thenReturn(Optional.<UserDto>absent());
- when(userDao.selectOrFailByExternalIdentity(dbSession, USER_IDENTITY.getId(), IDENTITY_PROVIDER.getKey())).thenReturn(ACTIVE_USER);
+ when(userDao.selectByLogin(dbSession, USER_IDENTITY.getLogin())).thenReturn(null);
+ when(userDao.selectOrFailByLogin(dbSession, USER_IDENTITY.getLogin())).thenReturn(ACTIVE_USER);
underTest.authenticate(USER_IDENTITY, IDENTITY_PROVIDER, httpSession);
@Test
public void authenticate_existing_user() throws Exception {
- when(userDao.selectByExternalIdentity(dbSession, USER_IDENTITY.getId(), IDENTITY_PROVIDER.getKey())).thenReturn(Optional.of(ACTIVE_USER));
+ when(userDao.selectByLogin(dbSession, USER_IDENTITY.getLogin())).thenReturn(ACTIVE_USER);
underTest.authenticate(USER_IDENTITY, IDENTITY_PROVIDER, httpSession);
ArgumentCaptor<UpdateUser> updateUserArgumentCaptor = ArgumentCaptor.forClass(UpdateUser.class);
verify(userUpdater).update(eq(dbSession), updateUserArgumentCaptor.capture());
- UpdateUser newUser = updateUserArgumentCaptor.getValue();
+ UpdateUser updateUser = updateUserArgumentCaptor.getValue();
- assertThat(newUser.login()).isEqualTo(USER_LOGIN);
- assertThat(newUser.name()).isEqualTo("John");
- assertThat(newUser.email()).isEqualTo("john@email.com");
+ assertThat(updateUser.login()).isEqualTo(USER_LOGIN);
+ assertThat(updateUser.name()).isEqualTo("John");
+ assertThat(updateUser.email()).isEqualTo("john@email.com");
+ assertThat(updateUser.externalIdentity().getProvider()).isEqualTo("github");
+ assertThat(updateUser.externalIdentity().getId()).isEqualTo("johndoo");
}
@Test
public void authenticate_existing_disabled_user() throws Exception {
- when(userDao.selectByExternalIdentity(dbSession, USER_IDENTITY.getId(), IDENTITY_PROVIDER.getKey())).thenReturn(Optional.of(UNACTIVE_USER));
- when(userDao.selectOrFailByExternalIdentity(dbSession, USER_IDENTITY.getId(), IDENTITY_PROVIDER.getKey())).thenReturn(UNACTIVE_USER);
+ when(userDao.selectByLogin(dbSession, USER_IDENTITY.getLogin())).thenReturn(UNACTIVE_USER);
+ when(userDao.selectOrFailByLogin(dbSession, USER_IDENTITY.getLogin())).thenReturn(UNACTIVE_USER);
underTest.authenticate(USER_IDENTITY, IDENTITY_PROVIDER, httpSession);
@Test
public void update_session_for_rails() throws Exception {
- when(userDao.selectByExternalIdentity(dbSession, USER_IDENTITY.getId(), IDENTITY_PROVIDER.getKey())).thenReturn(Optional.of(ACTIVE_USER));
+ when(userDao.selectByLogin(dbSession, USER_IDENTITY.getLogin())).thenReturn(ACTIVE_USER);
underTest.authenticate(USER_IDENTITY, IDENTITY_PROVIDER, httpSession);
@Test
public void fail_to_authenticate_new_user_when_allow_users_to_signup_is_false() throws Exception {
- when(userDao.selectByExternalIdentity(dbSession, USER_IDENTITY.getId(), IDENTITY_PROVIDER.getKey())).thenReturn(Optional.<UserDto>absent());
- when(userDao.selectOrFailByExternalIdentity(dbSession, USER_IDENTITY.getId(), IDENTITY_PROVIDER.getKey())).thenReturn(ACTIVE_USER);
+ when(userDao.selectByLogin(dbSession, USER_IDENTITY.getLogin())).thenReturn(null);
+ when(userDao.selectOrFailByLogin(dbSession, USER_IDENTITY.getLogin())).thenReturn(ACTIVE_USER);
TestIdentityProvider identityProvider = new TestIdentityProvider()
.setKey("github")
@Test
public void fail_to_authenticate_new_user_when_email_already_exists() throws Exception {
- when(userDao.selectByExternalIdentity(dbSession, USER_IDENTITY.getId(), IDENTITY_PROVIDER.getKey())).thenReturn(Optional.<UserDto>absent());
- when(userDao.selectOrFailByExternalIdentity(dbSession, USER_IDENTITY.getId(), IDENTITY_PROVIDER.getKey())).thenReturn(ACTIVE_USER);
+ when(userDao.selectByLogin(dbSession, USER_IDENTITY.getLogin())).thenReturn(null);
+ when(userDao.selectOrFailByLogin(dbSession, USER_IDENTITY.getLogin())).thenReturn(ACTIVE_USER);
when(userDao.doesEmailExist(dbSession, USER_IDENTITY.getEmail())).thenReturn(true);
thrown.expect(EmailAlreadyExistsException.class);
--- /dev/null
+/*
+ * 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.server.user;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class ExternalIdentityTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ @Test
+ public void create_external_identity() throws Exception {
+ ExternalIdentity externalIdentity = new ExternalIdentity("github", "login");
+ assertThat(externalIdentity.getId()).isEqualTo("login");
+ assertThat(externalIdentity.getProvider()).isEqualTo("github");
+ }
+
+ @Test
+ public void fail_with_NPE_when_identity_provider_is_null() throws Exception {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("Identity provider cannot be null");
+
+ new ExternalIdentity(null, "login");
+ }
+
+ @Test
+ public void fail_with_NPE_when_identity_id_is_null() throws Exception {
+ thrown.expect(NullPointerException.class);
+ thrown.expectMessage("Identity id cannot be null");
+
+ new ExternalIdentity("github", null);
+ }
+
+}
.setName("name")
.setEmail("email")
.setPassword("password")
- .setExternalIdentity(new NewUser.ExternalIdentity("github", "github_login"));
+ .setExternalIdentity(new ExternalIdentity("github", "github_login"));
assertThat(newUser.externalIdentity().getProvider()).isEqualTo("github");
assertThat(newUser.externalIdentity().getId()).isEqualTo("github_login");
}
-
- @Test
- public void fail_with_NPE_when_identity_provider_is_null() throws Exception {
- thrown.expect(NullPointerException.class);
- thrown.expectMessage("Identity provider cannot be null");
-
- new NewUser.ExternalIdentity(null, "github_login");
- }
-
- @Test
- public void fail_with_NPE_when_identity_id_is_null() throws Exception {
- thrown.expect(NullPointerException.class);
- thrown.expectMessage("Identity id cannot be null");
-
- new NewUser.ExternalIdentity("github", null);
- }
}
.setLogin("ABCD")
.setName("User")
.setPassword("password")
- .setExternalIdentity(new NewUser.ExternalIdentity("github", "user")));
+ .setExternalIdentity(new ExternalIdentity("github", "user")));
UserDto dto = userDao.selectByLogin(session, "ABCD");
assertThat(dto.getExternalIdentity()).isEqualTo("user");
}
@Test
- public void reactivate_user_when_creating_user_with_existing_login_and_with_authority() {
+ public void reactivate_user_when_creating_user_with_existing_login() {
db.prepareDbUnit(getClass(), "reactivate_user.xml");
when(system2.now()).thenReturn(1418215735486L);
createDefaultGroup();
assertThat(result).isTrue();
}
+ @Test
+ public void update_external_provider_when_reactivating_user() {
+ db.prepareDbUnit(getClass(), "reactivate_user.xml");
+ when(system2.now()).thenReturn(1418215735486L);
+ createDefaultGroup();
+
+ userUpdater.create(NewUser.create()
+ .setLogin(DEFAULT_LOGIN)
+ .setName("Marius2")
+ .setPassword("password2")
+ .setExternalIdentity(new ExternalIdentity("github", "john")));
+ session.commit();
+
+ UserDto dto = userDao.selectByLogin(session, DEFAULT_LOGIN);
+ assertThat(dto.getExternalIdentity()).isEqualTo("john");
+ assertThat(dto.getExternalIdentityProvider()).isEqualTo("github");
+ }
+
@Test
public void fail_to_reactivate_user_if_not_disabled() {
db.prepareDbUnit(getClass(), "fail_to_reactivate_user_if_not_disabled.xml");
entry("email", "marius2@mail.com"));
}
+ @Test
+ public void update_user_external_identity() {
+ db.prepareDbUnit(getClass(), "update_user.xml");
+ when(system2.now()).thenReturn(1418215735486L);
+ createDefaultGroup();
+
+ userUpdater.update(UpdateUser.create(DEFAULT_LOGIN)
+ .setName("Marius2")
+ .setPassword("password2")
+ .setExternalIdentity(new ExternalIdentity("github", "john")));
+ session.commit();
+ session.clearCache();
+
+ UserDto dto = userDao.selectByLogin(session, DEFAULT_LOGIN);
+ assertThat(dto.getExternalIdentity()).isEqualTo("john");
+ assertThat(dto.getExternalIdentityProvider()).isEqualTo("github");
+ }
+
@Test
public void reactivate_user_on_update() {
db.prepareDbUnit(getClass(), "reactivate_user.xml");
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;
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;
return mapper(session).selectNullableByScmAccountOrLoginOrEmail(scmAccountOrLoginOrEmail, like);
}
- public Optional<UserDto> selectByExternalIdentity(DbSession session, String extIdentity, String extIdentityProvider){
- return fromNullable(mapper(session).selectByIdentity(extIdentity, extIdentityProvider));
- }
-
- public UserDto selectOrFailByExternalIdentity(DbSession session, String extIdentity, String extIdentityProvider) {
- Optional<UserDto> 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));
- }
-
/**
* Please note that email is case insensitive, result for searching 'mail@email.com' or 'Mail@Email.com' will be the same
*/
List<UserDto> selectByLogins(List<String> logins);
- @CheckForNull
- UserDto selectByIdentity(@Param("extIdentity") String authorityId, @Param("extIdentityProvider") String authorityProvider);
-
@CheckForNull
GroupDto selectGroupByName(String name);
ORDER BY u.name
</select>
- <select id="selectByIdentity" parameterType="map" resultType="User">
- SELECT <include refid="userColumns"/>
- FROM users u
- <where>
- u.external_identity=#{extIdentity}
- AND u.external_identity_provider=#{extIdentityProvider}
- </where>
- </select>
-
<select id="countByEmail" parameterType="String" resultType="long">
SELECT count(u.id)
FROM users u
<insert id="update" parameterType="User" useGeneratedKeys="false">
UPDATE users set name=#{name,jdbcType=VARCHAR}, email=#{email,jdbcType=VARCHAR}, active=#{active,jdbcType=BOOLEAN},
- scm_accounts=#{scmAccounts,jdbcType=VARCHAR},
+ scm_accounts=#{scmAccounts,jdbcType=VARCHAR}, external_identity=#{externalIdentity,jdbcType=VARCHAR}, external_identity_provider=#{externalIdentityProvider,jdbcType=VARCHAR},
salt=#{salt,jdbcType=VARCHAR}, crypted_password=#{cryptedPassword,jdbcType=BIGINT},
updated_at=#{updatedAt,jdbcType=BIGINT}
WHERE login = #{login}
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.guava.api.Assertions.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
.setActive(false)
.setSalt("12345")
.setCryptedPassword("abcde")
+ .setExternalIdentity("johngithub")
+ .setExternalIdentityProvider("github")
.setUpdatedAt(date);
underTest.update(db.getSession(), userDto);
db.getSession().commit();
assertThat(user.getScmAccounts()).isEqualTo(",jo.hn,john2,johndoo,");
assertThat(user.getSalt()).isEqualTo("12345");
assertThat(user.getCryptedPassword()).isEqualTo("abcde");
+ assertThat(user.getExternalIdentity()).isEqualTo("johngithub");
+ assertThat(user.getExternalIdentityProvider()).isEqualTo("github");
assertThat(user.getCreatedAt()).isEqualTo(1418215735482L);
assertThat(user.getUpdatedAt()).isEqualTo(date);
}
assertThat(underTest.selectByLogin(session, "unknown")).isNull();
}
- @Test
- public void select_user_by_external_identity() {
- db.prepareDbUnit(getClass(), "select_users_by_ext_identity.xml");
-
- assertThat(underTest.selectByExternalIdentity(session, "mariusgithub", "github")).isPresent();
- assertThat(underTest.selectByExternalIdentity(session, "mariusgithub", "google")).isAbsent();
- assertThat(underTest.selectByExternalIdentity(session, "unknown", "unknown")).isAbsent();
- }
-
- @Test
- public void select_or_fail_by_external_identity() throws Exception {
- db.prepareDbUnit(getClass(), "select_users_by_ext_identity.xml");
- assertThat(underTest.selectOrFailByExternalIdentity(session, "mariusgithub", "github")).isNotNull();
-
- thrown.expect(RowNotFoundException.class);
- thrown.expectMessage("User with identity provider 'unknown' and id 'unknown' has not been found");
- underTest.selectOrFailByExternalIdentity(session, "unknown", "unknown");
- }
-
@Test
public void exists_by_email() throws Exception {
db.prepareDbUnit(getClass(), "exists_by_email.xml");
assertThat(underTest.doesEmailExist(session, "Marius@LesBronzes.fr")).isTrue();
assertThat(underTest.doesEmailExist(session, "unknown")).isFalse();
}
+
}
+++ /dev/null
-<dataset>
-
- <users id="101" login="marius" name="Marius" email="marius@lesbronzes.fr" active="[true]" scm_accounts=" ma marius33 "
- external_identity_provider="github" external_identity="mariusgithub"
- created_at="1418215735482" updated_at="1418215735485"
- salt="79bd6a8e79fb8c76ac8b121cc7e8e11ad1af8365" crypted_password="650d2261c98361e2f67f90ce5c65a95e7d8ea2fg"/>
- <users id="102" login="sbrandhof" name="Simon Brandhof" email="marius@lesbronzes.fr" active="[true]" scm_accounts="[null]"
- external_identity_provider="google" external_identity="mariusgoogle"
- created_at="1418215735482" updated_at="1418215735485"
- salt="79bd6a8e79fb8c76ac8b121cc7e8e11ad1af8366" crypted_password="650d2261c98361e2f67f90ce5c65a95e7d8ea2fh"/>
-
-</dataset>
@Immutable
public final class UserIdentity {
- private final String id;
+ private final String providerLogin;
+ private final String login;
private final String name;
private final String email;
private UserIdentity(Builder builder) {
- this.id = builder.id;
+ this.providerLogin = builder.providerLogin;
+ this.login = builder.login;
this.name = builder.name;
this.email = builder.email;
}
/**
- * Non-blank user ID, unique for the related {@link IdentityProvider}. If two {@link IdentityProvider}
- * define two users with the same ID, then users are considered as different.
+ * Non-blank user login for the related {@link IdentityProvider}.
*/
- public String getId() {
- return id;
+ public String getProviderLogin() {
+ return providerLogin;
+ }
+
+ /**
+ * Non-blank user login, unique for the SonarQube platform.
+ * If two {@link IdentityProvider} define two users with the same login, then users are considered as identical.
+ */
+ public String getLogin() {
+ return login;
}
/**
}
public static class Builder {
- private String id;
+ private String providerLogin;
+ private String login;
private String name;
private String email;
}
/**
- * @see UserIdentity#getId()
+ * @see UserIdentity#getProviderLogin()
*/
- public Builder setId(String id) {
- this.id = id;
+ public Builder setProviderLogin(String providerLogin) {
+ this.providerLogin = providerLogin;
+ return this;
+ }
+
+ /**
+ * @see UserIdentity#getLogin() ()
+ */
+ public Builder setLogin(String login) {
+ this.login = login;
return this;
}
}
public UserIdentity build() {
- validateId(id);
+ validateProviderLogin(providerLogin);
+ validateLogin(login);
validateName(name);
validateEmail(email);
return new UserIdentity(this);
}
- private static void validateId(String id){
- checkArgument(isNotBlank(id), "User id must not be blank");
- checkArgument(id.length() <= 255 && id.length() >= 3, "User id size is incorrect (Between 3 and 255 characters)");
+ private static void validateProviderLogin(String providerLogin){
+ checkArgument(isNotBlank(providerLogin), "Provider login must not be blank");
+ checkArgument(providerLogin.length() <= 255, "Provider login size is incorrect (maximum 255 characters)");
+ }
+
+ private static void validateLogin(String login){
+ checkArgument(isNotBlank(login), "User login must not be blank");
+ checkArgument(login.length() <= 255 && login.length() >= 3, "User login size is incorrect (Between 3 and 255 characters)");
}
private static void validateName(String name){
public class UserIdentityTest {
@Rule
- public ExpectedException thrown= ExpectedException.none();
+ public ExpectedException thrown = ExpectedException.none();
@Test
public void create_user() throws Exception {
UserIdentity underTest = UserIdentity.builder()
- .setId("john")
+ .setProviderLogin("john")
+ .setLogin("1234")
.setName("John")
.setEmail("john@email.com")
.build();
- assertThat(underTest.getId()).isEqualTo("john");
+ assertThat(underTest.getProviderLogin()).isEqualTo("john");
+ assertThat(underTest.getLogin()).isEqualTo("1234");
assertThat(underTest.getName()).isEqualTo("John");
assertThat(underTest.getEmail()).isEqualTo("john@email.com");
}
@Test
public void create_user_without_email() throws Exception {
UserIdentity underTest = UserIdentity.builder()
- .setId("john")
+ .setProviderLogin("john")
+ .setLogin("1234")
.setName("John")
.build();
}
@Test
- public void fail_when_id_is_null() throws Exception {
+ public void fail_when_login_is_empty() throws Exception {
thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("User id must not be blank");
+ thrown.expectMessage("User login must not be blank");
UserIdentity.builder()
+ .setProviderLogin("john")
+ .setLogin("")
.setName("John")
.setEmail("john@email.com")
.build();
}
@Test
- public void fail_when_id_is_empty() throws Exception {
+ public void fail_when_login_is_too_long() throws Exception {
thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("User id must not be blank");
+ thrown.expectMessage("User login size is incorrect (Between 3 and 255 characters)");
UserIdentity.builder()
- .setId("")
+ .setProviderLogin("john")
+ .setLogin(Strings.repeat("1", 256))
.setName("John")
.setEmail("john@email.com")
.build();
}
@Test
- public void fail_when_id_is_loo_long() throws Exception {
+ public void fail_when_login_is_too_small() throws Exception {
thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("User id size is incorrect (Between 3 and 255 characters)");
+ thrown.expectMessage("User login size is incorrect (Between 3 and 255 characters)");
UserIdentity.builder()
- .setId(Strings.repeat("1", 256))
+ .setProviderLogin("john")
+ .setLogin("12")
.setName("John")
.setEmail("john@email.com")
.build();
}
@Test
- public void fail_when_id_is_loo_small() throws Exception {
+ public void fail_when_provider_login_is_null() throws Exception {
thrown.expect(IllegalArgumentException.class);
- thrown.expectMessage("User id size is incorrect (Between 3 and 255 characters)");
+ thrown.expectMessage("Provider login must not be blank");
UserIdentity.builder()
- .setId("ab")
+ .setLogin("1234")
+ .setName("John")
+ .setEmail("john@email.com")
+ .build();
+ }
+
+ @Test
+ public void fail_when_provider_login_is_empty() throws Exception {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("Provider login must not be blank");
+ UserIdentity.builder()
+ .setProviderLogin("")
+ .setLogin("1234")
+ .setName("John")
+ .setEmail("john@email.com")
+ .build();
+ }
+
+ @Test
+ public void fail_when_provider_login_is_too_long() throws Exception {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("Provider login size is incorrect (maximum 255 characters)");
+ UserIdentity.builder()
+ .setProviderLogin(Strings.repeat("1", 256))
+ .setLogin("1234")
.setName("John")
.setEmail("john@email.com")
.build();
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("User name must not be blank");
UserIdentity.builder()
- .setId("john")
+ .setProviderLogin("john")
+ .setLogin("1234")
.setEmail("john@email.com")
.build();
}
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("User name must not be blank");
UserIdentity.builder()
- .setId("john")
+ .setProviderLogin("john")
+ .setLogin("1234")
.setName("")
.setEmail("john@email.com")
.build();
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("User name size is too big (200 characters max)");
UserIdentity.builder()
- .setId("john")
+ .setProviderLogin("john")
+ .setLogin("1234")
.setName(Strings.repeat("1", 201))
.setEmail("john@email.com")
.build();
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("User email size is too big (100 characters max)");
UserIdentity.builder()
- .setId("john")
+ .setProviderLogin("john")
+ .setLogin("1234")
.setName("John")
.setEmail(Strings.repeat("1", 101))
.build();