<commit_before>/*
 * Copyright 2003 - 2009 The eFaps Team
 *
 * 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.
 *
 * Revision:        $Rev:1563 $
 * Last Changed:    $Date:2007-10-28 15:07:41 +0100 (So, 28 Okt 2007) $
 * Last Changed By: $Author:tmo $
 */

package org.efaps.esjp.admin.access;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.efaps.admin.access.AccessSet;
import org.efaps.admin.access.AccessType;
import org.efaps.admin.access.AccessTypeEnums;
import org.efaps.admin.datamodel.Type;
import org.efaps.admin.event.EventExecution;
import org.efaps.admin.event.Parameter;
import org.efaps.admin.event.Return;
import org.efaps.admin.event.Parameter.ParameterValues;
import org.efaps.admin.event.Return.ReturnValues;
import org.efaps.admin.program.esjp.EFapsRevision;
import org.efaps.admin.program.esjp.EFapsUUID;
import org.efaps.admin.user.Group;
import org.efaps.admin.user.Role;
import org.efaps.db.Context;
import org.efaps.db.Instance;
import org.efaps.db.transaction.ConnectionResource;
import org.efaps.util.EFapsException;

/**
 * This Class is used to check if a user can Access this Type.<br>
 * The method execute is called with the Instance and the Accesstype as
 * parameters. For the instance object it is checked if the current context user
 * has the access defined in the list of access types.
 *
 * @author The eFaps Team
 * @version $Id:SimpleAccessCheckOnType.java 1563 2007-10-28 14:07:41Z tmo $
 */
@EFapsUUID("fd1ecee1-a882-4fbe-8b3c-5e5c3ed4d6b7")
@EFapsRevision("$Rev$")
public class SimpleAccessCheckOnType implements EventExecution
{
    /**
     * Logging instance used in this class.
     */
    private static final Logger LOG = LoggerFactory.getLogger(SimpleAccessCheckOnType.class);

    /**
     * Check for the instance object if the current context user has the access
     * defined in the list of access types.
     *
     * @param _instance instance to check for access for
     * @param _accessType accesstyep to check the access for
     * @return true if access is granted, else false
     * @throws EFapsException on error
     */
    private boolean checkAccess(final Instance _instance, final AccessType _accessType) throws EFapsException
    {
        final Context context = Context.getThreadContext();

        final StringBuilder cmd = new StringBuilder();
        cmd.append("select count(*) from T_ACCESSSET2USER ");

        final Type type = _instance.getType();
        if (type.isCheckStatus() && !_accessType.equals(AccessTypeEnums.CREATE.getAccessType())) {
            cmd.append(" join T_ACCESSSET2STATUS on T_ACCESSSET2USER.ACCESSSET = T_ACCESSSET2STATUS.ACCESSSET")
                .append(" join ").append(type.getMainTable().getSqlTable()).append(" on ")
                .append(type.getMainTable().getSqlTable()).append(".")
                .append(type.getStatusAttribute().getSqlColNames().get(0))
                .append("=T_ACCESSSET2STATUS.ACCESSSTATUS");
        }

        cmd.append(" where T_ACCESSSET2USER.ACCESSSET in (0");
        for (final AccessSet accessSet : type.getAccessSets()) {
            if (accessSet.getAccessTypes().contains(_accessType)) {
                cmd.append(",").append(accessSet.getId());
            }
        }
        cmd.append(") ").append("and T_ACCESSSET2USER.USERABSTRACT in (").append(context.getPersonId());
        for (final Role role : context.getPerson().getRoles()) {
            cmd.append(",").append(role.getId());
        }
        for (final Group group : context.getPerson().getGroups()) {
            cmd.append(",").append(group.getId());
        }
        cmd.append(")");
        return executeStatement(context, cmd);
    }

    /**
     * Method that queries against the database.
     *<commit_msg>- bugFix: the check for access in the case of a status was incorrect<commit_after>/*
 * Copyright 2003 - 2009 The eFaps Team
 *
 * 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.
 *
 * Revision:        $Rev:1563 $
 * Last Changed:    $Date:2007-10-28 15:07:41 +0100 (So, 28 Okt 2007) $
 * Last Changed By: $Author:tmo $
 */

package org.efaps.esjp.admin.access;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.efaps.admin.access.AccessSet;
import org.efaps.admin.access.AccessType;
import org.efaps.admin.access.AccessTypeEnums;
import org.efaps.admin.datamodel.Type;
import org.efaps.admin.event.EventExecution;
import org.efaps.admin.event.Parameter;
import org.efaps.admin.event.Return;
import org.efaps.admin.event.Parameter.ParameterValues;
import org.efaps.admin.event.Return.ReturnValues;
import org.efaps.admin.program.esjp.EFapsRevision;
import org.efaps.admin.program.esjp.EFapsUUID;
import org.efaps.admin.user.Group;
import org.efaps.admin.user.Role;
import org.efaps.db.Context;
import org.efaps.db.Instance;
import org.efaps.db.transaction.ConnectionResource;
import org.efaps.util.EFapsException;

/**
 * This Class is used to check if a user can Access this Type.<br>
 * The method execute is called with the Instance and the Accesstype as
 * parameters. For the instance object it is checked if the current context user
 * has the access defined in the list of access types.
 *
 * @author The eFaps Team
 * @version $Id:SimpleAccessCheckOnType.java 1563 2007-10-28 14:07:41Z tmo $
 */
@EFapsUUID("fd1ecee1-a882-4fbe-8b3c-5e5c3ed4d6b7")
@EFapsRevision("$Rev$")
public class SimpleAccessCheckOnType implements EventExecution
{
    /**
     * Logging instance used in this class.
     */
    private static final Logger LOG = LoggerFactory.getLogger(SimpleAccessCheckOnType.class);

    /**
     * Check for the instance object if the current context user has the access
     * defined in the list of access types.
     *
     * @param _instance instance to check for access for
     * @param _accessType accesstyep to check the access for
     * @return true if access is granted, else false
     * @throws EFapsException on error
     */
    private boolean checkAccess(final Instance _instance, final AccessType _accessType) throws EFapsException
    {
        final Context context = Context.getThreadContext();

        final StringBuilder cmd = new StringBuilder();
        cmd.append("select count(*) from T_ACCESSSET2USER ");

        final Type type = _instance.getType();
        if (type.isCheckStatus() && !_accessType.equals(AccessTypeEnums.CREATE.getAccessType())) {
            cmd.append(" join T_ACCESSSET2STATUS on T_ACCESSSET2USER.ACCESSSET = T_ACCESSSET2STATUS.ACCESSSET")
                .append(" join ").append(type.getMainTable().getSqlTable()).append(" on ")
                .append(type.getMainTable().getSqlTable()).append(".")
                .append(type.getStatusAttribute().getSqlColNames().get(0))
                .append("=T_ACCESSSET2STATUS.ACCESSSTATUS");
        }

        cmd.append(" where T_ACCESSSET2USER.ACCESSSET in (0");
        for (final AccessSet accessSet : type.getAccessSets()) {
            if (accessSet.getAccessTypes().contains(_accessType)) {
                cmd.append(",").append(accessSet.getId());
            }
        }
        cmd.append(") ").append("and T_ACCESSSET2USER.USERABSTRACT in (").append(context.getPersonId());
        for (final Role role : context.getPerson().getRoles()) {
            cmd.append(",").append(role.getId());
        }
        for (final Group group : context.getPerson().getGroups()) {
            cmd.append(",").append(group.getId());
        }
        cmd.append(")");
        if (type.isCheckStatus() && !_accessType.equals(AccessTypeEnums.CREATE.getAccessType())) {
            cmd.append(" and ").append(type.getMainTable().getSqlTable()).append(".ID=").append(_instance.getId());
        }
        return executeStatement(context, cmd);
    }

    /**
     * Method that queries against the database.
     *
{'fraction_non_alphanumeric': 0.1126653654304923, 'fraction_numerical': 0.018000433745391457, 'mean_word_length': 3.3752371916508537, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6965769', 'n_tokens_mistral': 2787, 'n_tokens_neox': 2726, 'n_words': 747}