KEYCLOAK-1421 Added timestamp of keycloak user creation into user models

and persistence
This commit is contained in:
Vlastimil Elias 2015-06-15 17:02:52 +02:00
parent 4e8c6b3fc4
commit 7cff37e707
3 changed files with 56 additions and 33 deletions

View file

@ -106,6 +106,7 @@
<addForeignKeyConstraint baseColumnNames="REALM_ID" baseTableName="REQUIRED_ACTION_PROVIDER" constraintName="FK_REQ_ACT_REALM" referencedColumnNames="ID" referencedTableName="REALM"/> <addForeignKeyConstraint baseColumnNames="REALM_ID" baseTableName="REQUIRED_ACTION_PROVIDER" constraintName="FK_REQ_ACT_REALM" referencedColumnNames="ID" referencedTableName="REALM"/>
<addForeignKeyConstraint baseColumnNames="CLIENT_SESSION" baseTableName="CLIENT_USER_SESSION_NOTE" constraintName="FK_CL_USR_SES_NOTE" referencedColumnNames="ID" referencedTableName="CLIENT_SESSION"/> <addForeignKeyConstraint baseColumnNames="CLIENT_SESSION" baseTableName="CLIENT_USER_SESSION_NOTE" constraintName="FK_CL_USR_SES_NOTE" referencedColumnNames="ID" referencedTableName="CLIENT_SESSION"/>
<dropColumn tableName="CLIENT_SESSION" columnName="ACTION"/> <dropColumn tableName="CLIENT_SESSION" columnName="ACTION"/>
<addForeignKeyConstraint baseColumnNames="REALM_ID" baseTableName="DEFAULT_REQUIRED_ACTIONS" constraintName="FK_DEF_REQ_ACTS_REALM" referencedColumnNames="ID" referencedTableName="REALM"/>
<addColumn tableName="USER_ENTITY"> <addColumn tableName="USER_ENTITY">
<column name="CREATED_TIMESTAMP" type="BIGINT"/> <column name="CREATED_TIMESTAMP" type="BIGINT"/>
</addColumn> </addColumn>

View file

@ -1,21 +1,21 @@
package org.keycloak.models.cache; package org.keycloak.models.cache;
import org.keycloak.models.ClientModel;
import org.keycloak.models.UserConsentModel;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel;
import org.keycloak.models.RoleContainerModel;
import org.keycloak.models.RoleModel;
import org.keycloak.models.UserCredentialModel;
import org.keycloak.models.UserCredentialValueModel;
import org.keycloak.models.UserModel;
import org.keycloak.models.cache.entities.CachedUser;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import org.keycloak.models.ClientModel;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel;
import org.keycloak.models.RoleContainerModel;
import org.keycloak.models.RoleModel;
import org.keycloak.models.UserConsentModel;
import org.keycloak.models.UserCredentialModel;
import org.keycloak.models.UserCredentialValueModel;
import org.keycloak.models.UserModel;
import org.keycloak.models.cache.entities.CachedUser;
/** /**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a> * @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
* @version $Revision: 1 $ * @version $Revision: 1 $
@ -38,18 +38,22 @@ public class UserAdapter implements UserModel {
if (updated == null) { if (updated == null) {
userProviderCache.registerUserInvalidation(realm, getId()); userProviderCache.registerUserInvalidation(realm, getId());
updated = userProviderCache.getDelegate().getUserById(getId(), realm); updated = userProviderCache.getDelegate().getUserById(getId(), realm);
if (updated == null) throw new IllegalStateException("Not found in database"); if (updated == null)
throw new IllegalStateException("Not found in database");
} }
} }
@Override @Override
public String getId() { public String getId() {
if (updated != null) return updated.getId(); if (updated != null)
return updated.getId();
return cached.getId(); return cached.getId();
} }
@Override @Override
public String getUsername() { public String getUsername() {
if (updated != null) return updated.getUsername(); if (updated != null)
return updated.getUsername();
return cached.getUsername(); return cached.getUsername();
} }
@ -72,13 +76,15 @@ public class UserAdapter implements UserModel {
@Override @Override
public boolean isEnabled() { public boolean isEnabled() {
if (updated != null) return updated.isEnabled(); if (updated != null)
return updated.isEnabled();
return cached.isEnabled(); return cached.isEnabled();
} }
@Override @Override
public boolean isTotp() { public boolean isTotp() {
if (updated != null) return updated.isTotp(); if (updated != null)
return updated.isTotp();
return cached.isTotp(); return cached.isTotp();
} }
@ -102,19 +108,22 @@ public class UserAdapter implements UserModel {
@Override @Override
public String getAttribute(String name) { public String getAttribute(String name) {
if (updated != null) return updated.getAttribute(name); if (updated != null)
return updated.getAttribute(name);
return cached.getAttributes().get(name); return cached.getAttributes().get(name);
} }
@Override @Override
public Map<String, String> getAttributes() { public Map<String, String> getAttributes() {
if (updated != null) return updated.getAttributes(); if (updated != null)
return updated.getAttributes();
return cached.getAttributes(); return cached.getAttributes();
} }
@Override @Override
public Set<String> getRequiredActions() { public Set<String> getRequiredActions() {
if (updated != null) return updated.getRequiredActions(); if (updated != null)
return updated.getRequiredActions();
return cached.getRequiredActions(); return cached.getRequiredActions();
} }
@ -144,7 +153,8 @@ public class UserAdapter implements UserModel {
@Override @Override
public String getFirstName() { public String getFirstName() {
if (updated != null) return updated.getFirstName(); if (updated != null)
return updated.getFirstName();
return cached.getFirstName(); return cached.getFirstName();
} }
@ -156,7 +166,8 @@ public class UserAdapter implements UserModel {
@Override @Override
public String getLastName() { public String getLastName() {
if (updated != null) return updated.getLastName(); if (updated != null)
return updated.getLastName();
return cached.getLastName(); return cached.getLastName();
} }
@ -168,7 +179,8 @@ public class UserAdapter implements UserModel {
@Override @Override
public String getEmail() { public String getEmail() {
if (updated != null) return updated.getEmail(); if (updated != null)
return updated.getEmail();
return cached.getEmail(); return cached.getEmail();
} }
@ -180,7 +192,8 @@ public class UserAdapter implements UserModel {
@Override @Override
public boolean isEmailVerified() { public boolean isEmailVerified() {
if (updated != null) return updated.isEmailVerified(); if (updated != null)
return updated.isEmailVerified();
return cached.isEmailVerified(); return cached.isEmailVerified();
} }
@ -204,7 +217,8 @@ public class UserAdapter implements UserModel {
@Override @Override
public List<UserCredentialValueModel> getCredentialsDirectly() { public List<UserCredentialValueModel> getCredentialsDirectly() {
if (updated != null) return updated.getCredentialsDirectly(); if (updated != null)
return updated.getCredentialsDirectly();
return cached.getCredentials(); return cached.getCredentials();
} }
@ -216,7 +230,8 @@ public class UserAdapter implements UserModel {
@Override @Override
public String getFederationLink() { public String getFederationLink() {
if (updated != null) return updated.getFederationLink(); if (updated != null)
return updated.getFederationLink();
return cached.getFederationLink(); return cached.getFederationLink();
} }
@ -224,11 +239,12 @@ public class UserAdapter implements UserModel {
public void setFederationLink(String link) { public void setFederationLink(String link) {
getDelegateForUpdate(); getDelegateForUpdate();
updated.setFederationLink(link); updated.setFederationLink(link);
} }
@Override @Override
public Set<RoleModel> getRealmRoleMappings() { public Set<RoleModel> getRealmRoleMappings() {
if (updated != null) return updated.getRealmRoleMappings(); if (updated != null)
return updated.getRealmRoleMappings();
Set<RoleModel> roleMappings = getRoleMappings(); Set<RoleModel> roleMappings = getRoleMappings();
Set<RoleModel> realmMappings = new HashSet<RoleModel>(); Set<RoleModel> realmMappings = new HashSet<RoleModel>();
for (RoleModel role : roleMappings) { for (RoleModel role : roleMappings) {
@ -244,7 +260,8 @@ public class UserAdapter implements UserModel {
@Override @Override
public Set<RoleModel> getClientRoleMappings(ClientModel app) { public Set<RoleModel> getClientRoleMappings(ClientModel app) {
if (updated != null) return updated.getClientRoleMappings(app); if (updated != null)
return updated.getClientRoleMappings(app);
Set<RoleModel> roleMappings = getRoleMappings(); Set<RoleModel> roleMappings = getRoleMappings();
Set<RoleModel> appMappings = new HashSet<RoleModel>(); Set<RoleModel> appMappings = new HashSet<RoleModel>();
for (RoleModel role : roleMappings) { for (RoleModel role : roleMappings) {
@ -260,12 +277,15 @@ public class UserAdapter implements UserModel {
@Override @Override
public boolean hasRole(RoleModel role) { public boolean hasRole(RoleModel role) {
if (updated != null) return updated.hasRole(role); if (updated != null)
if (cached.getRoleMappings().contains(role.getId())) return true; return updated.hasRole(role);
if (cached.getRoleMappings().contains(role.getId()))
return true;
Set<RoleModel> mappings = getRoleMappings(); Set<RoleModel> mappings = getRoleMappings();
for (RoleModel mapping: mappings) { for (RoleModel mapping : mappings) {
if (mapping.hasRole(role)) return true; if (mapping.hasRole(role))
return true;
} }
return false; return false;
} }
@ -278,7 +298,8 @@ public class UserAdapter implements UserModel {
@Override @Override
public Set<RoleModel> getRoleMappings() { public Set<RoleModel> getRoleMappings() {
if (updated != null) return updated.getRoleMappings(); if (updated != null)
return updated.getRoleMappings();
Set<RoleModel> roles = new HashSet<RoleModel>(); Set<RoleModel> roles = new HashSet<RoleModel>();
for (String id : cached.getRoleMappings()) { for (String id : cached.getRoleMappings()) {
RoleModel roleById = keycloakSession.realms().getRoleById(id, realm); RoleModel roleById = keycloakSession.realms().getRoleById(id, realm);

View file

@ -277,6 +277,7 @@ public class RegisterTest {
Assert.assertNotNull(user.getCreatedTimestamp()); Assert.assertNotNull(user.getCreatedTimestamp());
// test that timestamp is current with 10s tollerance // test that timestamp is current with 10s tollerance
Assert.assertTrue((System.currentTimeMillis() - user.getCreatedTimestamp()) < 10000); Assert.assertTrue((System.currentTimeMillis() - user.getCreatedTimestamp()) < 10000);
} finally { } finally {
configureRelamRegistrationEmailAsUsername(false); configureRelamRegistrationEmailAsUsername(false);
} }