Search in sources :

Example 31 with ModelDuplicateException

use of org.keycloak.models.ModelDuplicateException in project keycloak by keycloak.

the class ClientScopesResource method createClientScope.

/**
 * Create a new client scope
 *
 * Client Scope's name must be unique!
 *
 * @param rep
 * @return
 */
@POST
@Consumes(MediaType.APPLICATION_JSON)
@NoCache
public Response createClientScope(ClientScopeRepresentation rep) {
    auth.clients().requireManageClientScopes();
    ClientScopeResource.validateDynamicClientScope(rep);
    try {
        ClientScopeModel clientModel = RepresentationToModel.createClientScope(session, realm, rep);
        adminEvent.operation(OperationType.CREATE).resourcePath(session.getContext().getUri(), clientModel.getId()).representation(rep).success();
        return Response.created(session.getContext().getUri().getAbsolutePathBuilder().path(clientModel.getId()).build()).build();
    } catch (ModelDuplicateException e) {
        return ErrorResponse.exists("Client Scope " + rep.getName() + " already exists");
    }
}
Also used : ModelDuplicateException(org.keycloak.models.ModelDuplicateException) ClientScopeModel(org.keycloak.models.ClientScopeModel) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) NoCache(org.jboss.resteasy.annotations.cache.NoCache)

Example 32 with ModelDuplicateException

use of org.keycloak.models.ModelDuplicateException in project keycloak by keycloak.

the class JpaRealmProvider method addClientRole.

@Override
public RoleModel addClientRole(ClientModel client, String id, String name) {
    if (getClientRole(client, name) != null) {
        throw new ModelDuplicateException();
    }
    RoleEntity roleEntity = new RoleEntity();
    roleEntity.setId(id);
    roleEntity.setName(name);
    roleEntity.setRealmId(client.getRealm().getId());
    roleEntity.setClientId(client.getId());
    roleEntity.setClientRole(true);
    em.persist(roleEntity);
    RoleAdapter adapter = new RoleAdapter(session, client.getRealm(), em, roleEntity);
    return adapter;
}
Also used : RoleEntity(org.keycloak.models.jpa.entities.RoleEntity) ModelDuplicateException(org.keycloak.models.ModelDuplicateException)

Example 33 with ModelDuplicateException

use of org.keycloak.models.ModelDuplicateException in project keycloak by keycloak.

the class JpaUserFederatedStorageProvider method addConsent.

@Override
public void addConsent(RealmModel realm, String userId, UserConsentModel consent) {
    createIndex(realm, userId);
    String clientId = consent.getClient().getId();
    FederatedUserConsentEntity consentEntity = getGrantedConsentEntity(userId, clientId, LockModeType.NONE);
    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);
}
Also used : ModelDuplicateException(org.keycloak.models.ModelDuplicateException) StorageId(org.keycloak.storage.StorageId) FederatedUserConsentEntity(org.keycloak.storage.jpa.entity.FederatedUserConsentEntity)

Example 34 with ModelDuplicateException

use of org.keycloak.models.ModelDuplicateException in project keycloak by keycloak.

the class JpaUserProvider method ensureEmailConstraint.

// Could override this to provide a custom behavior.
protected void ensureEmailConstraint(List<UserEntity> users, RealmModel realm) {
    UserEntity user = users.get(0);
    if (users.size() > 1) {
        // 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);
    }
}
Also used : ModelDuplicateException(org.keycloak.models.ModelDuplicateException) UserEntity(org.keycloak.models.jpa.entities.UserEntity)

Example 35 with ModelDuplicateException

use of org.keycloak.models.ModelDuplicateException in project keycloak by keycloak.

the class JpaUserProvider method addConsent.

@Override
public void addConsent(RealmModel realm, String userId, UserConsentModel consent) {
    String clientId = consent.getClient().getId();
    UserConsentEntity consentEntity = getGrantedConsentEntity(userId, clientId, LockModeType.NONE);
    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);
}
Also used : ModelDuplicateException(org.keycloak.models.ModelDuplicateException) StorageId(org.keycloak.storage.StorageId) UserConsentEntity(org.keycloak.models.jpa.entities.UserConsentEntity) UserEntity(org.keycloak.models.jpa.entities.UserEntity)

Aggregations

ModelDuplicateException (org.keycloak.models.ModelDuplicateException)42 Consumes (javax.ws.rs.Consumes)12 UserModel (org.keycloak.models.UserModel)11 POST (javax.ws.rs.POST)9 Response (javax.ws.rs.core.Response)6 NotFoundException (javax.ws.rs.NotFoundException)5 ClientModel (org.keycloak.models.ClientModel)5 RealmModel (org.keycloak.models.RealmModel)5 BadRequestException (javax.ws.rs.BadRequestException)4 PUT (javax.ws.rs.PUT)4 Path (javax.ws.rs.Path)4 ModelException (org.keycloak.models.ModelException)4 X509Certificate (java.security.cert.X509Certificate)3 NoCache (org.jboss.resteasy.annotations.cache.NoCache)3 ErrorResponseException (org.keycloak.services.ErrorResponseException)3 ClientPolicyException (org.keycloak.services.clientpolicy.ClientPolicyException)3 URI (java.net.URI)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 ClientScopeModel (org.keycloak.models.ClientScopeModel)2 ProtocolMapperModel (org.keycloak.models.ProtocolMapperModel)2