public static ModelException convert(Throwable t) { if (t.getCause() != null && t.getCause() instanceof ConstraintViolationException) { throw new ModelDuplicateException(t); } if (t instanceof EntityExistsException || t instanceof ConstraintViolationException) { throw new ModelDuplicateException(t); } else { throw new ModelException(t); } }
public static ModelException convert(Throwable t) { if (t.getCause() != null && t.getCause() instanceof ConstraintViolationException) { throw new ModelDuplicateException(t); } if (t instanceof EntityExistsException) { throw new ModelDuplicateException(t); } else { throw new ModelException(t); } }
public static ModelException convert(Throwable t) { if (t.getCause() != null && t.getCause() instanceof ConstraintViolationException) { throw new ModelDuplicateException(t); } if (t instanceof EntityExistsException) { throw new ModelDuplicateException(t); } else { throw new ModelException(t); } }
public static ModelException convert(Throwable t) { if (t.getCause() != null && t.getCause() instanceof ConstraintViolationException) { throw new ModelDuplicateException(t); } if (t instanceof EntityExistsException) { throw new ModelDuplicateException(t); } else { throw new ModelException(t); } }
public static ModelException convertException(MongoException e) { if (e instanceof DuplicateKeyException) { return new ModelDuplicateException(e); } else { return new ModelException(e); } }
/** * Ensure that displayName of myProvider (if not null) is unique and there is no other provider with same displayName in the list. * * @param displayName to check for duplications * @param myProvider provider, which is excluded from the list (if present) * @param federationProviders * @throws ModelDuplicateException if there is other provider with same displayName */ public static void ensureUniqueDisplayName(String displayName, UserFederationProviderModel myProvider, List<UserFederationProviderModel> federationProviders) throws ModelDuplicateException { if (displayName != null) { for (UserFederationProviderModel federationProvider : federationProviders) { if (myProvider != null && (myProvider.equals(federationProvider) || (myProvider.getId() != null && myProvider.getId().equals(federationProvider.getId())))) { continue; } if (displayName.equals(federationProvider.getDisplayName())) { throw new ModelDuplicateException("There is already existing federation provider with display name: " + displayName); } } } }
protected void ensureEmailConstraint(List<UserEntity> users, RealmModel realm) { UserEntity user = users.get(0); if (users.size() > 1) { // Realm settings have been changed from allowing duplicate emails to not allowing them // but duplicates haven't been removed. throw new ModelDuplicateException("Multiple users with email '" + user.getEmail() + "' exist in Keycloak."); } if (realm.isDuplicateEmailsAllowed()) { return; } if (user.getEmail() != null && !user.getEmail().equals(user.getEmailConstraint())) { // Realm settings have been changed from allowing duplicate emails to not allowing them. // We need to update the email constraint to reflect this change in the user entities. user.setEmailConstraint(user.getEmail()); em.persist(user); } }
@Override public UsernameLoginFailureModel addUserLoginFailure(RealmModel realm, String username) { UsernameLoginFailureKey key = new UsernameLoginFailureKey(realm.getId(), username); UsernameLoginFailureEntity entity = new UsernameLoginFailureEntity(username, realm.getId()); if (loginFailures.putIfAbsent(key, entity) != null) { throw new ModelDuplicateException(); } return new UsernameLoginFailureAdapter(entity); }
protected void ensureEmailConstraint(List<MongoUserEntity> users, RealmModel realm) { MongoUserEntity user = users.get(0); if (users.size() > 1) { // Realm settings have been changed from allowing duplicate emails to not allowing them // but duplicates haven't been removed. throw new ModelDuplicateException("Multiple users with email '" + user.getEmail() + "' exist in Keycloak."); } if (realm.isDuplicateEmailsAllowed()) { return; } if (user.getEmail() != null && user.getEmailIndex() == null) { // Realm settings have been changed from allowing duplicate emails to not allowing them. // We need to update the email index to reflect this change in the user entities. user.setEmail(user.getEmail(), false); getMongoStore().updateEntity(user, invocationContext); } } }
@Override public RoleModel addRealmRole(RealmModel realm, String id, String name) { if (getRealmRole(realm, name) != null) { throw new ModelDuplicateException(); } RoleEntity entity = new RoleEntity(); entity.setId(id); entity.setName(name); RealmEntity ref = em.getReference(RealmEntity.class, realm.getId()); entity.setRealm(ref); entity.setRealmId(realm.getId()); em.persist(entity); em.flush(); RoleAdapter adapter = new RoleAdapter(session, realm, em, entity); return adapter; }
@Override public RoleModel addClientRole(RealmModel realm, ClientModel client, String id, String name) { if (getClientRole(realm, client, name) != null) { throw new ModelDuplicateException(); } ClientEntity clientEntity = em.getReference(ClientEntity.class, client.getId()); RoleEntity roleEntity = new RoleEntity(); roleEntity.setId(id); roleEntity.setName(name); roleEntity.setClient(clientEntity); roleEntity.setClientRole(true); roleEntity.setRealmId(realm.getId()); em.persist(roleEntity); RoleAdapter adapter = new RoleAdapter(session, realm, em, roleEntity); return adapter; }
@Override public ProtocolMapperModel addProtocolMapper(ProtocolMapperModel model) { if (getProtocolMapperByName(model.getProtocol(), model.getName()) != null) { throw new ModelDuplicateException("Protocol mapper name must be unique per protocol"); } String id = model.getId() != null ? model.getId() : KeycloakModelUtils.generateId(); ProtocolMapperEntity entity = new ProtocolMapperEntity(); entity.setId(id); entity.setName(model.getName()); entity.setProtocol(model.getProtocol()); entity.setProtocolMapper(model.getProtocolMapper()); entity.setClientScope(this.entity); entity.setConfig(model.getConfig()); em.persist(entity); this.entity.getProtocolMappers().add(entity); return entityToModel(entity); }
@Override public ProtocolMapperModel addProtocolMapper(ProtocolMapperModel model) { if (getProtocolMapperByName(model.getProtocol(), model.getName()) != null) { throw new ModelDuplicateException("Protocol mapper name must be unique per protocol"); } String id = model.getId() != null ? model.getId() : KeycloakModelUtils.generateId(); ProtocolMapperEntity entity = new ProtocolMapperEntity(); entity.setId(id); entity.setName(model.getName()); entity.setProtocol(model.getProtocol()); entity.setProtocolMapper(model.getProtocolMapper()); entity.setClient(this.entity); entity.setConfig(model.getConfig()); em.persist(entity); this.entity.getProtocolMappers().add(entity); return entityToModel(entity); }
@Override public void addConsent(RealmModel realm, String userId, UserConsentModel consent) { String clientId = consent.getClient().getId(); if (getConsentEntityByClientId(userId, clientId) != null) { throw new ModelDuplicateException("Consent already exists for client [" + clientId + "] and user [" + userId + "]"); } long currentTime = Time.currentTimeMillis(); MongoUserConsentEntity consentEntity = new MongoUserConsentEntity(); consentEntity.setUserId(userId); consentEntity.setClientId(clientId); consentEntity.setCreatedDate(currentTime); consentEntity.setLastUpdatedDate(currentTime); fillEntityFromModel(consent, consentEntity); getMongoStore().insertEntity(consentEntity, invocationContext); }
@Override public void addConsent(RealmModel realm, String userId, UserConsentModel consent) { String clientId = consent.getClient().getId(); UserConsentEntity consentEntity = getGrantedConsentEntity(userId, clientId); if (consentEntity != null) { throw new ModelDuplicateException("Consent already exists for client [" + clientId + "] and user [" + userId + "]"); } long currentTime = Time.currentTimeMillis(); consentEntity = new UserConsentEntity(); consentEntity.setId(KeycloakModelUtils.generateId()); consentEntity.setUser(em.getReference(UserEntity.class, userId)); StorageId clientStorageId = new StorageId(clientId); if (clientStorageId.isLocal()) { consentEntity.setClientId(clientId); } else { consentEntity.setClientStorageProvider(clientStorageId.getProviderId()); consentEntity.setExternalClientId(clientStorageId.getExternalId()); } consentEntity.setCreatedDate(currentTime); consentEntity.setLastUpdatedDate(currentTime); em.persist(consentEntity); em.flush(); updateGrantedConsentEntity(consentEntity, consent); }
@Override public void addConsent(RealmModel realm, String userId, UserConsentModel consent) { createIndex(realm, userId); String clientId = consent.getClient().getId(); FederatedUserConsentEntity consentEntity = getGrantedConsentEntity(userId, clientId); if (consentEntity != null) { throw new ModelDuplicateException("Consent already exists for client [" + clientId + "] and user [" + userId + "]"); } consentEntity = new FederatedUserConsentEntity(); consentEntity.setId(KeycloakModelUtils.generateId()); consentEntity.setUserId(userId); StorageId clientStorageId = new StorageId(clientId); if (clientStorageId.isLocal()) { consentEntity.setClientId(clientId); } else { consentEntity.setClientStorageProvider(clientStorageId.getProviderId()); consentEntity.setExternalClientId(clientStorageId.getExternalId()); } consentEntity.setRealmId(realm.getId()); consentEntity.setStorageProviderId(new StorageId(userId).getProviderId()); long currentTime = Time.currentTimeMillis(); consentEntity.setCreatedDate(currentTime); consentEntity.setLastUpdatedDate(currentTime); em.persist(consentEntity); em.flush(); updateGrantedConsentEntity(consentEntity, consent); }
@Override public ProtocolMapperModel addProtocolMapper(ProtocolMapperModel model) { if (getProtocolMapperByName(model.getProtocol(), model.getName()) != null) { throw new ModelDuplicateException("Protocol mapper name must be unique per protocol"); } ProtocolMapperEntity entity = new ProtocolMapperEntity(); String id = model.getId() != null ? model.getId() : KeycloakModelUtils.generateId(); entity.setId(id); entity.setProtocol(model.getProtocol()); entity.setName(model.getName()); entity.setProtocolMapper(model.getProtocolMapper()); entity.setConfig(model.getConfig()); entity.setConsentRequired(model.isConsentRequired()); entity.setConsentText(model.getConsentText()); getMongoEntity().getProtocolMappers().add(entity); updateMongoEntity(); return entityToModel(entity); }
@Override public ProtocolMapperModel addProtocolMapper(ProtocolMapperModel model) { if (getProtocolMapperByName(model.getProtocol(), model.getName()) != null) { throw new ModelDuplicateException("Protocol mapper name must be unique per protocol"); } ProtocolMapperEntity entity = new ProtocolMapperEntity(); String id = model.getId() != null ? model.getId() : KeycloakModelUtils.generateId(); entity.setId(id); entity.setProtocol(model.getProtocol()); entity.setName(model.getName()); entity.setProtocolMapper(model.getProtocolMapper()); entity.setConfig(model.getConfig()); entity.setConsentRequired(model.isConsentRequired()); entity.setConsentText(model.getConsentText()); getMongoEntity().getProtocolMappers().add(entity); updateMongoEntity(); return entityToModel(entity); }