Merge pull request #982 from girirajsharma/master
[KEYCLOAK-1031]-Multiple accounts with the same email.
This commit is contained in:
commit
0ad3c72c0a
6 changed files with 97 additions and 18 deletions
|
@ -26,7 +26,7 @@ public class UserFederationManager implements UserProvider {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UserModel addUser(RealmModel realm, String id, String username, boolean addDefaultRoles) {
|
public UserModel addUser(RealmModel realm, String id, String username, boolean addDefaultRoles) {
|
||||||
UserModel user = session.userStorage().addUser(realm, id, username, addDefaultRoles);
|
UserModel user = session.userStorage().addUser(realm, id, username.toLowerCase(), addDefaultRoles);
|
||||||
return registerWithFederation(realm, user);
|
return registerWithFederation(realm, user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,7 +38,7 @@ public class UserFederationManager implements UserProvider {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UserModel addUser(RealmModel realm, String username) {
|
public UserModel addUser(RealmModel realm, String username) {
|
||||||
UserModel user = session.userStorage().addUser(realm, username);
|
UserModel user = session.userStorage().addUser(realm, username.toLowerCase());
|
||||||
return registerWithFederation(realm, user);
|
return registerWithFederation(realm, user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -144,7 +144,7 @@ public class UserFederationManager implements UserProvider {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UserModel getUserByUsername(String username, RealmModel realm) {
|
public UserModel getUserByUsername(String username, RealmModel realm) {
|
||||||
UserModel user = session.userStorage().getUserByUsername(username, realm);
|
UserModel user = session.userStorage().getUserByUsername(username.toLowerCase(), realm);
|
||||||
if (user != null) {
|
if (user != null) {
|
||||||
user = validateAndProxyUser(realm, user);
|
user = validateAndProxyUser(realm, user);
|
||||||
if (user != null) return user;
|
if (user != null) return user;
|
||||||
|
@ -159,7 +159,7 @@ public class UserFederationManager implements UserProvider {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UserModel getUserByEmail(String email, RealmModel realm) {
|
public UserModel getUserByEmail(String email, RealmModel realm) {
|
||||||
UserModel user = session.userStorage().getUserByEmail(email, realm);
|
UserModel user = session.userStorage().getUserByEmail(email.toLowerCase(), realm);
|
||||||
if (user != null) {
|
if (user != null) {
|
||||||
user = validateAndProxyUser(realm, user);
|
user = validateAndProxyUser(realm, user);
|
||||||
if (user != null) return user;
|
if (user != null) return user;
|
||||||
|
@ -251,11 +251,11 @@ public class UserFederationManager implements UserProvider {
|
||||||
attributes.put(UserModel.FIRST_NAME, firstName);
|
attributes.put(UserModel.FIRST_NAME, firstName);
|
||||||
attributes.put(UserModel.LAST_NAME, lastName);
|
attributes.put(UserModel.LAST_NAME, lastName);
|
||||||
} else if (search.indexOf('@') > -1) {
|
} else if (search.indexOf('@') > -1) {
|
||||||
attributes.put(UserModel.USERNAME, search.trim());
|
attributes.put(UserModel.USERNAME, search.trim().toLowerCase());
|
||||||
attributes.put(UserModel.EMAIL, search.trim());
|
attributes.put(UserModel.EMAIL, search.trim().toLowerCase());
|
||||||
} else {
|
} else {
|
||||||
attributes.put(UserModel.LAST_NAME, search.trim());
|
attributes.put(UserModel.LAST_NAME, search.trim());
|
||||||
attributes.put(UserModel.USERNAME, search.trim());
|
attributes.put(UserModel.USERNAME, search.trim().toLowerCase());
|
||||||
}
|
}
|
||||||
federationLoad(realm, attributes);
|
federationLoad(realm, attributes);
|
||||||
return query(new PaginatedQuery() {
|
return query(new PaginatedQuery() {
|
||||||
|
|
|
@ -142,6 +142,9 @@ public class DefaultCacheUserProvider implements CacheUserProvider {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UserModel getUserByUsername(String username, RealmModel realm) {
|
public UserModel getUserByUsername(String username, RealmModel realm) {
|
||||||
|
|
||||||
|
username = username.toLowerCase();
|
||||||
|
|
||||||
if (!cache.isEnabled()) return getDelegate().getUserByUsername(username, realm);
|
if (!cache.isEnabled()) return getDelegate().getUserByUsername(username, realm);
|
||||||
if (realmInvalidations.contains(realm.getId())) {
|
if (realmInvalidations.contains(realm.getId())) {
|
||||||
return getDelegate().getUserByUsername(username, realm);
|
return getDelegate().getUserByUsername(username, realm);
|
||||||
|
@ -165,6 +168,9 @@ public class DefaultCacheUserProvider implements CacheUserProvider {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UserModel getUserByEmail(String email, RealmModel realm) {
|
public UserModel getUserByEmail(String email, RealmModel realm) {
|
||||||
|
|
||||||
|
email = email.toLowerCase();
|
||||||
|
|
||||||
if (!cache.isEnabled()) return getDelegate().getUserByEmail(email, realm);
|
if (!cache.isEnabled()) return getDelegate().getUserByEmail(email, realm);
|
||||||
if (realmInvalidations.contains(realm.getId())) {
|
if (realmInvalidations.contains(realm.getId())) {
|
||||||
return getDelegate().getUserByEmail(email, realm);
|
return getDelegate().getUserByEmail(email, realm);
|
||||||
|
|
|
@ -50,7 +50,7 @@ public class JpaUserProvider implements UserProvider {
|
||||||
|
|
||||||
UserEntity entity = new UserEntity();
|
UserEntity entity = new UserEntity();
|
||||||
entity.setId(id);
|
entity.setId(id);
|
||||||
entity.setUsername(username);
|
entity.setUsername(username.toLowerCase());
|
||||||
entity.setRealmId(realm.getId());
|
entity.setRealmId(realm.getId());
|
||||||
em.persist(entity);
|
em.persist(entity);
|
||||||
em.flush();
|
em.flush();
|
||||||
|
@ -73,7 +73,7 @@ public class JpaUserProvider implements UserProvider {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UserModel addUser(RealmModel realm, String username) {
|
public UserModel addUser(RealmModel realm, String username) {
|
||||||
return addUser(realm, KeycloakModelUtils.generateId(), username, true);
|
return addUser(realm, KeycloakModelUtils.generateId(), username.toLowerCase(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -96,7 +96,7 @@ public class JpaUserProvider implements UserProvider {
|
||||||
entity.setRealmId(realm.getId());
|
entity.setRealmId(realm.getId());
|
||||||
entity.setIdentityProvider(identity.getIdentityProvider());
|
entity.setIdentityProvider(identity.getIdentityProvider());
|
||||||
entity.setUserId(identity.getUserId());
|
entity.setUserId(identity.getUserId());
|
||||||
entity.setUserName(identity.getUserName());
|
entity.setUserName(identity.getUserName().toLowerCase());
|
||||||
entity.setToken(identity.getToken());
|
entity.setToken(identity.getToken());
|
||||||
UserEntity userEntity = em.getReference(UserEntity.class, user.getId());
|
UserEntity userEntity = em.getReference(UserEntity.class, user.getId());
|
||||||
entity.setUser(userEntity);
|
entity.setUser(userEntity);
|
||||||
|
@ -190,7 +190,7 @@ public class JpaUserProvider implements UserProvider {
|
||||||
@Override
|
@Override
|
||||||
public UserModel getUserByUsername(String username, RealmModel realm) {
|
public UserModel getUserByUsername(String username, RealmModel realm) {
|
||||||
TypedQuery<UserEntity> query = em.createNamedQuery("getRealmUserByUsername", UserEntity.class);
|
TypedQuery<UserEntity> query = em.createNamedQuery("getRealmUserByUsername", UserEntity.class);
|
||||||
query.setParameter("username", username);
|
query.setParameter("username", username.toLowerCase());
|
||||||
query.setParameter("realmId", realm.getId());
|
query.setParameter("realmId", realm.getId());
|
||||||
List<UserEntity> results = query.getResultList();
|
List<UserEntity> results = query.getResultList();
|
||||||
if (results.size() == 0) return null;
|
if (results.size() == 0) return null;
|
||||||
|
@ -200,7 +200,7 @@ public class JpaUserProvider implements UserProvider {
|
||||||
@Override
|
@Override
|
||||||
public UserModel getUserByEmail(String email, RealmModel realm) {
|
public UserModel getUserByEmail(String email, RealmModel realm) {
|
||||||
TypedQuery<UserEntity> query = em.createNamedQuery("getRealmUserByEmail", UserEntity.class);
|
TypedQuery<UserEntity> query = em.createNamedQuery("getRealmUserByEmail", UserEntity.class);
|
||||||
query.setParameter("email", email);
|
query.setParameter("email", email.toLowerCase());
|
||||||
query.setParameter("realmId", realm.getId());
|
query.setParameter("realmId", realm.getId());
|
||||||
List<UserEntity> results = query.getResultList();
|
List<UserEntity> results = query.getResultList();
|
||||||
return results.isEmpty() ? null : new UserAdapter(realm, em, results.get(0));
|
return results.isEmpty() ? null : new UserAdapter(realm, em, results.get(0));
|
||||||
|
|
|
@ -60,7 +60,7 @@ public class MongoUserProvider implements UserProvider {
|
||||||
@Override
|
@Override
|
||||||
public UserModel getUserByUsername(String username, RealmModel realm) {
|
public UserModel getUserByUsername(String username, RealmModel realm) {
|
||||||
DBObject query = new QueryBuilder()
|
DBObject query = new QueryBuilder()
|
||||||
.and("username").is(username)
|
.and("username").is(username.toLowerCase())
|
||||||
.and("realmId").is(realm.getId())
|
.and("realmId").is(realm.getId())
|
||||||
.get();
|
.get();
|
||||||
MongoUserEntity user = getMongoStore().loadSingleEntity(MongoUserEntity.class, query, invocationContext);
|
MongoUserEntity user = getMongoStore().loadSingleEntity(MongoUserEntity.class, query, invocationContext);
|
||||||
|
@ -75,7 +75,7 @@ public class MongoUserProvider implements UserProvider {
|
||||||
@Override
|
@Override
|
||||||
public UserModel getUserByEmail(String email, RealmModel realm) {
|
public UserModel getUserByEmail(String email, RealmModel realm) {
|
||||||
DBObject query = new QueryBuilder()
|
DBObject query = new QueryBuilder()
|
||||||
.and("email").is(email)
|
.and("email").is(email.toLowerCase())
|
||||||
.and("realmId").is(realm.getId())
|
.and("realmId").is(realm.getId())
|
||||||
.get();
|
.get();
|
||||||
MongoUserEntity user = getMongoStore().loadSingleEntity(MongoUserEntity.class, query, invocationContext);
|
MongoUserEntity user = getMongoStore().loadSingleEntity(MongoUserEntity.class, query, invocationContext);
|
||||||
|
@ -256,7 +256,7 @@ public class MongoUserProvider implements UserProvider {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UserAdapter addUser(RealmModel realm, String id, String username, boolean addDefaultRoles) {
|
public UserAdapter addUser(RealmModel realm, String id, String username, boolean addDefaultRoles) {
|
||||||
UserAdapter userModel = addUserEntity(realm, id, username);
|
UserAdapter userModel = addUserEntity(realm, id, username.toLowerCase());
|
||||||
|
|
||||||
if (addDefaultRoles) {
|
if (addDefaultRoles) {
|
||||||
for (String r : realm.getDefaultRoles()) {
|
for (String r : realm.getDefaultRoles()) {
|
||||||
|
@ -302,7 +302,7 @@ public class MongoUserProvider implements UserProvider {
|
||||||
FederatedIdentityEntity federatedIdentityEntity = new FederatedIdentityEntity();
|
FederatedIdentityEntity federatedIdentityEntity = new FederatedIdentityEntity();
|
||||||
federatedIdentityEntity.setIdentityProvider(identity.getIdentityProvider());
|
federatedIdentityEntity.setIdentityProvider(identity.getIdentityProvider());
|
||||||
federatedIdentityEntity.setUserId(identity.getUserId());
|
federatedIdentityEntity.setUserId(identity.getUserId());
|
||||||
federatedIdentityEntity.setUserName(identity.getUserName());
|
federatedIdentityEntity.setUserName(identity.getUserName().toLowerCase());
|
||||||
federatedIdentityEntity.setToken(identity.getToken());
|
federatedIdentityEntity.setToken(identity.getToken());
|
||||||
|
|
||||||
getMongoStore().pushItemToList(userEntity, "federatedIdentities", federatedIdentityEntity, true, invocationContext);
|
getMongoStore().pushItemToList(userEntity, "federatedIdentities", federatedIdentityEntity, true, invocationContext);
|
||||||
|
|
|
@ -30,7 +30,7 @@ public class UserTest extends AbstractClientTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void createDuplicatedUser() {
|
public void createDuplicatedUser1() {
|
||||||
createUser();
|
createUser();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -42,7 +42,80 @@ public class UserTest extends AbstractClientTest {
|
||||||
assertEquals(409, e.getResponse().getStatus());
|
assertEquals(409, e.getResponse().getStatus());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void createDuplicatedUser2() {
|
||||||
|
createUser();
|
||||||
|
|
||||||
|
try {
|
||||||
|
UserRepresentation user = new UserRepresentation();
|
||||||
|
user.setUsername("user2");
|
||||||
|
user.setEmail("user1@localhost");
|
||||||
|
realm.users().create(user);
|
||||||
|
fail("Expected failure");
|
||||||
|
} catch (ClientErrorException e) {
|
||||||
|
assertEquals(409, e.getResponse().getStatus());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void createDuplicatedUser3() {
|
||||||
|
createUser();
|
||||||
|
|
||||||
|
try {
|
||||||
|
UserRepresentation user = new UserRepresentation();
|
||||||
|
user.setUsername("User1");
|
||||||
|
realm.users().create(user);
|
||||||
|
fail("Expected failure");
|
||||||
|
} catch (ClientErrorException e) {
|
||||||
|
assertEquals(409, e.getResponse().getStatus());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void createDuplicatedUser4() {
|
||||||
|
createUser();
|
||||||
|
|
||||||
|
try {
|
||||||
|
UserRepresentation user = new UserRepresentation();
|
||||||
|
user.setUsername("USER1");
|
||||||
|
realm.users().create(user);
|
||||||
|
fail("Expected failure");
|
||||||
|
} catch (ClientErrorException e) {
|
||||||
|
assertEquals(409, e.getResponse().getStatus());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void createDuplicatedUser5() {
|
||||||
|
createUser();
|
||||||
|
|
||||||
|
try {
|
||||||
|
UserRepresentation user = new UserRepresentation();
|
||||||
|
user.setUsername("user2");
|
||||||
|
user.setEmail("User1@localhost");
|
||||||
|
realm.users().create(user);
|
||||||
|
fail("Expected failure");
|
||||||
|
} catch (ClientErrorException e) {
|
||||||
|
assertEquals(409, e.getResponse().getStatus());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void createDuplicatedUser6() {
|
||||||
|
createUser();
|
||||||
|
|
||||||
|
try {
|
||||||
|
UserRepresentation user = new UserRepresentation();
|
||||||
|
user.setUsername("user2");
|
||||||
|
user.setEmail("user1@LOCALHOST");
|
||||||
|
realm.users().create(user);
|
||||||
|
fail("Expected failure");
|
||||||
|
} catch (ClientErrorException e) {
|
||||||
|
assertEquals(409, e.getResponse().getStatus());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void createUsers() {
|
private void createUsers() {
|
||||||
for (int i = 1; i < 10; i++) {
|
for (int i = 1; i < 10; i++) {
|
||||||
UserRepresentation user = new UserRepresentation();
|
UserRepresentation user = new UserRepresentation();
|
||||||
|
|
|
@ -166,7 +166,7 @@ public class ImportTest extends AbstractModelTest {
|
||||||
} else if ("google".equals(federatedIdentityModel.getIdentityProvider())) {
|
} else if ("google".equals(federatedIdentityModel.getIdentityProvider())) {
|
||||||
googleFound = true;
|
googleFound = true;
|
||||||
Assert.assertEquals(federatedIdentityModel.getUserId(), "google1");
|
Assert.assertEquals(federatedIdentityModel.getUserId(), "google1");
|
||||||
Assert.assertEquals(federatedIdentityModel.getUserName(), "mySocialUser@gmail.com");
|
Assert.assertEquals(federatedIdentityModel.getUserName(), "mysocialuser@gmail.com");
|
||||||
} else if ("twitter".equals(federatedIdentityModel.getIdentityProvider())) {
|
} else if ("twitter".equals(federatedIdentityModel.getIdentityProvider())) {
|
||||||
twitterFound = true;
|
twitterFound = true;
|
||||||
Assert.assertEquals(federatedIdentityModel.getUserId(), "twitter1");
|
Assert.assertEquals(federatedIdentityModel.getUserId(), "twitter1");
|
||||||
|
|
Loading…
Reference in a new issue