<commit_before>/*
 * Copyright 2010 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 */
package net.sf.jpasecurity.persistence;

import static net.sf.jpasecurity.AccessType.READ;

import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import net.sf.jpasecurity.ExceptionFactory;
import net.sf.jpasecurity.configuration.Configuration;
import net.sf.jpasecurity.configuration.SecurityContext;
import net.sf.jpasecurity.entity.EmptyObjectCache;
import net.sf.jpasecurity.entity.SecureObjectCache;
import net.sf.jpasecurity.jpql.compiler.MappedPathEvaluator;
import net.sf.jpasecurity.jpql.compiler.PathEvaluator;
import net.sf.jpasecurity.jpql.compiler.SimpleSubselectEvaluator;
import net.sf.jpasecurity.jpql.compiler.SubselectEvaluator;
import net.sf.jpasecurity.mapping.MappingInformation;
import net.sf.jpasecurity.persistence.compiler.EntityManagerEvaluator;
import net.sf.jpasecurity.security.EntityFilter;
import net.sf.jpasecurity.security.FilterResult;

/**
 * @author <NAME>
 * @author <NAME>
 */
public class LightSecureEntityManager extends DelegatingEntityManager {

    private LightSecureEntityManagerFactory entityManagerFactory;
    private MappingInformation mappingInformation;
    private SecurityContext securityContext;
    private EntityFilter entityFilter;

    LightSecureEntityManager(LightSecureEntityManagerFactory parent,
                             EntityManager entityManager,
                             MappingInformation mappingInformation,
                             Configuration configuration) {
        super(entityManager);
        entityManagerFactory = parent;
        this.mappingInformation = mappingInformation;
        this.securityContext = configuration.getSecurityContext();
        SecureObjectCache emptyObjectCache = new EmptyObjectCache();
        ExceptionFactory exceptionFactory = configuration.getExceptionFactory();
        PathEvaluator pathEvaluator = new MappedPathEvaluator(mappingInformation, exceptionFactory);
        SubselectEvaluator simpleSubselectEvaluator = new SimpleSubselectEvaluator(exceptionFactory);
        SubselectEvaluator entityManagerEvaluator = new EntityManagerEvaluator(entityManager, pathEvaluator);
        this.entityFilter = new EntityFilter(emptyObjectCache,
                                             mappingInformation,
                                             securityContext,
                                             configuration.getExceptionFactory(),
                                             configuration.getAccessRulesProvider().getAccessRules(),
                                             simpleSubselectEvaluator,
                                             entityManagerEvaluator);
    }

    @Override
    public LightSecureEntityManagerFactory getEntityManagerFactory() {
        return entityManagerFactory;
    }

    public Query createNamedQuery(String name) {
        return createQuery(mappingInformation.getNamedQuery(name));
    }

    public <T> TypedQuery<T> createNamedQuery(String name, Class<T> resultClass) {
        return createQuery(mappingInformation.getNamedQuery(name), resultClass);
    }

    /**
     * This implementation filters the query according to the provided access rules
     * and the authenticated user and its roles.
     */
    public Query createQuery(String qlString) {
        return createQuery(qlString, Object.class, Query.class);
    }

    /**
     * This implementation filters the query according to the provided access rules
     * and the authenticated user and its roles.
     */
    public <T> TypedQuery<T> createQuery(String qlString, Class<T> resultClass) {
        return createQuery(qlString, resultClass, TypedQuery.class);
    }

    private <Q extends Query, T> Q createQuery(String qlString, Class<T> resultClass, Class<Q> queryType) {
        FilterResult<String> filterResult = entityFilter.filterQuery(qlString, READ);
        if (filterResult.getQuery() == null) {
            return (Q)new EmptyResultQuery<T>(createDelegateQuery(qlString, resultClass, queryType));
        } else {
            Q query = createDelegateQuery(filterResult.getQuery(), resultClass, queryType);
            if (filterResult.getParameters() != null) {
                for (Map.Entry<String, Object> roleParameter: filterResult.getParameters().entrySet()) {
                    query.setParameter(roleParameter.getKey(), roleParameter.getValue());
                }
            }
            return (Q)query;
        }
    }

    private <Q extends Query> Q createDelegateQuery(String qlString, Class<?> resultClass, Class<Q> queryClass) {
        if (TypedQuery.class.equals(queryClass)) {
            return (Q)super.createQuery(qlString, resultClass);
        } else {
            return (Q)super.createQuery(qlString);
        }
    }
}<commit_msg>Implemented createQuery(CriteriaQuery)<commit_after>/*
 * Copyright 2010 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 */
package net.sf.jpasecurity.persistence;

import static net.sf.jpasecurity.AccessType.READ;

import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaQuery;

import net.sf.jpasecurity.ExceptionFactory;
import net.sf.jpasecurity.configuration.Configuration;
import net.sf.jpasecurity.configuration.SecurityContext;
import net.sf.jpasecurity.entity.EmptyObjectCache;
import net.sf.jpasecurity.entity.SecureObjectCache;
import net.sf.jpasecurity.jpql.compiler.MappedPathEvaluator;
import net.sf.jpasecurity.jpql.compiler.PathEvaluator;
import net.sf.jpasecurity.jpql.compiler.SimpleSubselectEvaluator;
import net.sf.jpasecurity.jpql.compiler.SubselectEvaluator;
import net.sf.jpasecurity.mapping.MappingInformation;
import net.sf.jpasecurity.persistence.compiler.EntityManagerEvaluator;
import net.sf.jpasecurity.persistence.security.CriteriaEntityFilter;
import net.sf.jpasecurity.security.FilterResult;

/**
 * @author <NAME>
 * @author <NAME>
 */
public class LightSecureEntityManager extends DelegatingEntityManager {

    private LightSecureEntityManagerFactory entityManagerFactory;
    private MappingInformation mappingInformation;
    private SecurityContext securityContext;
    private CriteriaEntityFilter entityFilter;

    LightSecureEntityManager(LightSecureEntityManagerFactory parent,
                             EntityManager entityManager,
                             MappingInformation mappingInformation,
                             Configuration configuration) {
        super(entityManager);
        entityManagerFactory = parent;
        this.mappingInformation = mappingInformation;
        this.securityContext = configuration.getSecurityContext();
        SecureObjectCache emptyObjectCache = new EmptyObjectCache();
        ExceptionFactory exceptionFactory = configuration.getExceptionFactory();
        PathEvaluator pathEvaluator = new MappedPathEvaluator(mappingInformation, exceptionFactory);
        SubselectEvaluator simpleSubselectEvaluator = new SimpleSubselectEvaluator(exceptionFactory);
        SubselectEvaluator entityManagerEvaluator = new EntityManagerEvaluator(entityManager, pathEvaluator);
        this.entityFilter = new CriteriaEntityFilter(emptyObjectCache,
                                                     mappingInformation,
                                                     securityContext,
                                                     entityManager.getCriteriaBuilder(),
                                                     configuration.getExceptionFactory(),
                                                     configuration.getAccessRulesProvider().getAccessRules(),
                                                     simpleSubselectEvaluator,
                                                     entityManagerEvaluator);
    }

    @Override
    public LightSecureEntityManagerFactory getEntityManagerFactory() {
        return entityManagerFactory;
    }

    public Query createNamedQuery(String name) {
        return createQuery(mappingInformation.getNamedQuery(name));
    }

    public <T> TypedQuery<T> createNamedQuery(String name, Class<T> resultClass) {
        return createQuery(mappingInformation.getNamedQuery(name), resultClass);
    }

    /**
     * This implementation filters the query according to the provided access rules
     * and the authenticated user and its roles.
     */
    public Query createQuery(String qlString) {
        return createQuery(qlString, Object.class, Query.class);
    }

    /**
     * This implementation filters the query according to the provided access rules
     * and the authenticated user and its roles.
     */
    public <T> TypedQuery<T> createQuery(String qlString, Class<T> resultClass) {
        return createQuery(qlString, resultClass, TypedQuery.class);
    }

    private <Q extends Query, T> Q createQuery(String qlString, Class<T> resultClass, Class<Q> queryType) {
        FilterResult<String> filterResult = entityFilter.filterQuery(qlString, READ);
        if (filterResult.getQuery() == null) {
            return (Q)new EmptyResultQuery<T>(createDelegateQuery(qlString, resultClass, queryType));
        } else {
            Q query = createDelegateQuery(filterResult.getQuery(), resultClass, queryType);
            if (filterResult.getParameters() != null) {
                for (Map.Entry<String, Object> roleParameter: filterResult.getParameters().entrySet()) {
                    query.setParameter(roleParameter.getKey(), roleParameter.getValue());
                }
            }
            return (Q)query;
        }
    }

    private <Q extends Query> Q createDelegateQuery(String qlString, Class<?> resultClass, Class<Q> queryClass) {
        if (TypedQuery.class.equals(queryClass)) {
            return (Q)super.createQuery(qlString, resultClass);
        } else {
            return (Q)super.createQuery(qlString);
        }
    }

    public <T> TypedQuery<T> createQuery(CriteriaQuery<T> criteriaQuery) {
        FilterResult<CriteriaQuery<T>> filterResult = entityFilter.filterQuery(criteriaQuery);
        if (filterResult.getQuery() == null) {
            return new EmptyResultQuery<T>(super.createQuery(criteriaQuery));
        } else {
            return super.createQuery(filterResult.getQuery());
        }
    }
}
{'fraction_non_alphanumeric': 0.07870168397173022, 'fraction_numerical': 0.0013960387400750371, 'mean_word_length': 2.9743411927877945, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3638327', 'n_tokens_mistral': 2812, 'n_tokens_neox': 2775, 'n_words': 671}