Search in sources :

Example 1 with Role

use of org.apache.syncope.core.persistence.api.entity.Role in project syncope by apache.

the class JPARoleDAO method delete.

@Override
public void delete(final String key) {
    Role role = find(key);
    if (role == null) {
        return;
    }
    delete(role);
}
Also used : JPARole(org.apache.syncope.core.persistence.jpa.entity.JPARole) Role(org.apache.syncope.core.persistence.api.entity.Role)

Example 2 with Role

use of org.apache.syncope.core.persistence.api.entity.Role in project syncope by apache.

the class JPARoleDAO method save.

@Override
public Role save(final Role role) {
    Role merged = entityManager().merge(role);
    // refresh dynamic memberships
    if (merged.getDynMembership() != null) {
        List<User> matching = searchDAO().search(SearchCondConverter.convert(merged.getDynMembership().getFIQLCond()), AnyTypeKind.USER);
        clearDynMembers(merged);
        matching.forEach((user) -> {
            Query insert = entityManager().createNativeQuery("INSERT INTO " + DYNMEMB_TABLE + " VALUES(?, ?)");
            insert.setParameter(1, user.getKey());
            insert.setParameter(2, merged.getKey());
            insert.executeUpdate();
            publisher.publishEvent(new AnyCreatedUpdatedEvent<>(this, user, AuthContextUtils.getDomain()));
        });
    }
    return merged;
}
Also used : JPARole(org.apache.syncope.core.persistence.jpa.entity.JPARole) Role(org.apache.syncope.core.persistence.api.entity.Role) User(org.apache.syncope.core.persistence.api.entity.user.User) JPAUser(org.apache.syncope.core.persistence.jpa.entity.user.JPAUser) TypedQuery(javax.persistence.TypedQuery) Query(javax.persistence.Query)

Example 3 with Role

use of org.apache.syncope.core.persistence.api.entity.Role in project syncope by apache.

the class JPARoleDAO method refreshDynMemberships.

@Transactional
@Override
public void refreshDynMemberships(final User user) {
    findAll().stream().filter(role -> role.getDynMembership() != null).forEach(role -> {
        Query delete = entityManager().createNativeQuery("DELETE FROM " + DYNMEMB_TABLE + " WHERE role_id=? AND any_id=?");
        delete.setParameter(1, role.getKey());
        delete.setParameter(2, user.getKey());
        delete.executeUpdate();
        if (searchDAO().matches(user, SearchCondConverter.convert(role.getDynMembership().getFIQLCond()))) {
            Query insert = entityManager().createNativeQuery("INSERT INTO " + DYNMEMB_TABLE + " VALUES(?, ?)");
            insert.setParameter(1, user.getKey());
            insert.setParameter(2, role.getKey());
            insert.executeUpdate();
        }
    });
}
Also used : JPARole(org.apache.syncope.core.persistence.jpa.entity.JPARole) AnySearchDAO(org.apache.syncope.core.persistence.api.dao.AnySearchDAO) Realm(org.apache.syncope.core.persistence.api.entity.Realm) User(org.apache.syncope.core.persistence.api.entity.user.User) Autowired(org.springframework.beans.factory.annotation.Autowired) TypedQuery(javax.persistence.TypedQuery) RoleDAO(org.apache.syncope.core.persistence.api.dao.RoleDAO) ArrayList(java.util.ArrayList) AnyTypeKind(org.apache.syncope.common.lib.types.AnyTypeKind) AnyCreatedUpdatedEvent(org.apache.syncope.core.provisioning.api.event.AnyCreatedUpdatedEvent) List(java.util.List) Query(javax.persistence.Query) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) SearchCondConverter(org.apache.syncope.core.persistence.api.search.SearchCondConverter) Privilege(org.apache.syncope.core.persistence.api.entity.Privilege) JPAUser(org.apache.syncope.core.persistence.jpa.entity.user.JPAUser) AuthContextUtils(org.apache.syncope.core.spring.security.AuthContextUtils) ApplicationContextProvider(org.apache.syncope.core.spring.ApplicationContextProvider) Repository(org.springframework.stereotype.Repository) Collections(java.util.Collections) Role(org.apache.syncope.core.persistence.api.entity.Role) Transactional(org.springframework.transaction.annotation.Transactional) TypedQuery(javax.persistence.TypedQuery) Query(javax.persistence.Query) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with Role

use of org.apache.syncope.core.persistence.api.entity.Role in project syncope by apache.

the class JPAUserDAO method findDynRoles.

@Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = true)
@Override
@SuppressWarnings("unchecked")
public List<Role> findDynRoles(final String key) {
    Query query = entityManager().createNativeQuery("SELECT role_id FROM " + JPARoleDAO.DYNMEMB_TABLE + " WHERE any_id=?");
    query.setParameter(1, key);
    List<Role> result = new ArrayList<>();
    query.getResultList().stream().map(resultKey -> resultKey instanceof Object[] ? (String) ((Object[]) resultKey)[0] : ((String) resultKey)).forEachOrdered(actualKey -> {
        Role role = roleDAO.find(actualKey.toString());
        if (role == null) {
            LOG.error("Could not find role with id {}, even though returned by the native query", actualKey);
        } else if (!result.contains(role)) {
            result.add(role);
        }
    });
    return result;
}
Also used : Role(org.apache.syncope.core.persistence.api.entity.Role) AccountRule(org.apache.syncope.core.persistence.api.dao.AccountRule) Date(java.util.Date) Realm(org.apache.syncope.core.persistence.api.entity.Realm) NoResultException(javax.persistence.NoResultException) Autowired(org.springframework.beans.factory.annotation.Autowired) Entity(org.apache.syncope.core.persistence.api.entity.Entity) AnyDeletedEvent(org.apache.syncope.core.provisioning.api.event.AnyDeletedEvent) RoleDAO(org.apache.syncope.core.persistence.api.dao.RoleDAO) AnyTypeKind(org.apache.syncope.common.lib.types.AnyTypeKind) AnyCreatedUpdatedEvent(org.apache.syncope.core.provisioning.api.event.AnyCreatedUpdatedEvent) GroupDAO(org.apache.syncope.core.persistence.api.dao.GroupDAO) Pair(org.apache.commons.lang3.tuple.Pair) Map(java.util.Map) AuthContextUtils(org.apache.syncope.core.spring.security.AuthContextUtils) JPAUser(org.apache.syncope.core.persistence.jpa.entity.user.JPAUser) Repository(org.springframework.stereotype.Repository) EntityViolationType(org.apache.syncope.common.lib.types.EntityViolationType) Role(org.apache.syncope.core.persistence.api.entity.Role) UserDAO(org.apache.syncope.core.persistence.api.dao.UserDAO) Collection(java.util.Collection) Resource(javax.annotation.Resource) Set(java.util.Set) Implementation(org.apache.syncope.core.persistence.api.entity.Implementation) AccountPolicyException(org.apache.syncope.core.provisioning.api.utils.policy.AccountPolicyException) Collectors(java.util.stream.Collectors) ImplementationManager(org.apache.syncope.core.spring.ImplementationManager) List(java.util.List) Query(javax.persistence.Query) AccountPolicy(org.apache.syncope.core.persistence.api.entity.policy.AccountPolicy) PasswordPolicyException(org.apache.syncope.core.provisioning.api.utils.policy.PasswordPolicyException) Group(org.apache.syncope.core.persistence.api.entity.group.Group) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) StandardEntitlement(org.apache.syncope.common.lib.types.StandardEntitlement) AccessToken(org.apache.syncope.core.persistence.api.entity.AccessToken) TypedQuery(javax.persistence.TypedQuery) JPAAnyUtilsFactory(org.apache.syncope.core.persistence.jpa.entity.JPAAnyUtilsFactory) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) PasswordPolicy(org.apache.syncope.core.persistence.api.entity.policy.PasswordPolicy) DelegatedAdministrationException(org.apache.syncope.core.spring.security.DelegatedAdministrationException) Propagation(org.springframework.transaction.annotation.Propagation) RealmDAO(org.apache.syncope.core.persistence.api.dao.RealmDAO) SyncopeConstants(org.apache.syncope.common.lib.SyncopeConstants) User(org.apache.syncope.core.persistence.api.entity.user.User) AccessTokenDAO(org.apache.syncope.core.persistence.api.dao.AccessTokenDAO) PasswordRule(org.apache.syncope.core.persistence.api.dao.PasswordRule) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) InvalidEntityException(org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidEntityException) ExternalResource(org.apache.syncope.core.persistence.api.entity.resource.ExternalResource) ApplicationContextProvider(org.apache.syncope.core.spring.ApplicationContextProvider) AnyUtils(org.apache.syncope.core.persistence.api.entity.AnyUtils) Collections(java.util.Collections) SecurityQuestion(org.apache.syncope.core.persistence.api.entity.user.SecurityQuestion) Transactional(org.springframework.transaction.annotation.Transactional) Query(javax.persistence.Query) TypedQuery(javax.persistence.TypedQuery) ArrayList(java.util.ArrayList) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with Role

use of org.apache.syncope.core.persistence.api.entity.Role in project syncope by apache.

the class UserDataBinderImpl method create.

@Override
public void create(final User user, final UserTO userTO, final boolean storePassword) {
    SyncopeClientCompositeException scce = SyncopeClientException.buildComposite();
    // set username
    user.setUsername(userTO.getUsername());
    // set password
    if (StringUtils.isBlank(userTO.getPassword()) || !storePassword) {
        LOG.debug("Password was not provided or not required to be stored");
    } else {
        setPassword(user, userTO.getPassword(), scce);
    }
    user.setMustChangePassword(userTO.isMustChangePassword());
    // security question / answer
    if (userTO.getSecurityQuestion() != null) {
        SecurityQuestion securityQuestion = securityQuestionDAO.find(userTO.getSecurityQuestion());
        if (securityQuestion != null) {
            user.setSecurityQuestion(securityQuestion);
        }
    }
    user.setSecurityAnswer(userTO.getSecurityAnswer());
    // roles
    userTO.getRoles().forEach(roleKey -> {
        Role role = roleDAO.find(roleKey);
        if (role == null) {
            LOG.warn("Ignoring unknown role with id {}", roleKey);
        } else {
            user.add(role);
        }
    });
    // realm
    Realm realm = realmDAO.findByFullPath(userTO.getRealm());
    if (realm == null) {
        SyncopeClientException noRealm = SyncopeClientException.build(ClientExceptionType.InvalidRealm);
        noRealm.getElements().add("Invalid or null realm specified: " + userTO.getRealm());
        scce.addException(noRealm);
    }
    user.setRealm(realm);
    AnyUtils anyUtils = anyUtilsFactory.getInstance(AnyTypeKind.USER);
    if (user.getRealm() != null) {
        // relationships
        userTO.getRelationships().forEach(relationshipTO -> {
            AnyObject otherEnd = anyObjectDAO.find(relationshipTO.getOtherEndKey());
            if (otherEnd == null) {
                LOG.debug("Ignoring invalid anyObject " + relationshipTO.getOtherEndKey());
            } else if (user.getRealm().getFullPath().startsWith(otherEnd.getRealm().getFullPath())) {
                RelationshipType relationshipType = relationshipTypeDAO.find(relationshipTO.getType());
                if (relationshipType == null) {
                    LOG.debug("Ignoring invalid relationship type {}", relationshipTO.getType());
                } else {
                    URelationship relationship = entityFactory.newEntity(URelationship.class);
                    relationship.setType(relationshipType);
                    relationship.setRightEnd(otherEnd);
                    relationship.setLeftEnd(user);
                    user.add(relationship);
                }
            } else {
                LOG.error("{} cannot be assigned to {}", otherEnd, user);
                SyncopeClientException unassignabled = SyncopeClientException.build(ClientExceptionType.InvalidRelationship);
                unassignabled.getElements().add("Cannot be assigned: " + otherEnd);
                scce.addException(unassignabled);
            }
        });
        // memberships
        userTO.getMemberships().forEach(membershipTO -> {
            Group group = membershipTO.getGroupKey() == null ? groupDAO.findByName(membershipTO.getGroupName()) : groupDAO.find(membershipTO.getGroupKey());
            if (group == null) {
                LOG.debug("Ignoring invalid group " + membershipTO.getGroupKey() + " / " + membershipTO.getGroupName());
            } else if (user.getRealm().getFullPath().startsWith(group.getRealm().getFullPath())) {
                UMembership membership = entityFactory.newEntity(UMembership.class);
                membership.setRightEnd(group);
                membership.setLeftEnd(user);
                user.add(membership);
                // membership attributes
                fill(user, membership, membershipTO, anyUtils, scce);
            } else {
                LOG.error("{} cannot be assigned to {}", group, user);
                SyncopeClientException unassignable = SyncopeClientException.build(ClientExceptionType.InvalidMembership);
                unassignable.getElements().add("Cannot be assigned: " + group);
                scce.addException(unassignable);
            }
        });
    }
    // attributes and resources
    fill(user, userTO, anyUtils, scce);
    // Throw composite exception if there is at least one element set in the composing exceptions
    if (scce.hasExceptions()) {
        throw scce;
    }
}
Also used : Role(org.apache.syncope.core.persistence.api.entity.Role) Group(org.apache.syncope.core.persistence.api.entity.group.Group) SyncopeClientCompositeException(org.apache.syncope.common.lib.SyncopeClientCompositeException) AnyObject(org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject) UMembership(org.apache.syncope.core.persistence.api.entity.user.UMembership) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) RelationshipType(org.apache.syncope.core.persistence.api.entity.RelationshipType) Realm(org.apache.syncope.core.persistence.api.entity.Realm) AnyUtils(org.apache.syncope.core.persistence.api.entity.AnyUtils) URelationship(org.apache.syncope.core.persistence.api.entity.user.URelationship) SecurityQuestion(org.apache.syncope.core.persistence.api.entity.user.SecurityQuestion)

Aggregations

Role (org.apache.syncope.core.persistence.api.entity.Role)19 User (org.apache.syncope.core.persistence.api.entity.user.User)8 Realm (org.apache.syncope.core.persistence.api.entity.Realm)7 AbstractTest (org.apache.syncope.core.persistence.jpa.AbstractTest)7 Test (org.junit.jupiter.api.Test)7 Group (org.apache.syncope.core.persistence.api.entity.group.Group)5 Collections (java.util.Collections)4 List (java.util.List)4 Query (javax.persistence.Query)4 AnyTypeKind (org.apache.syncope.common.lib.types.AnyTypeKind)4 ArrayList (java.util.ArrayList)3 Collection (java.util.Collection)3 Date (java.util.Date)3 HashSet (java.util.HashSet)3 Map (java.util.Map)3 Optional (java.util.Optional)3 Set (java.util.Set)3 Collectors (java.util.stream.Collectors)3 Resource (javax.annotation.Resource)3 TypedQuery (javax.persistence.TypedQuery)3