<commit_before>package org.wyona.yanel.core.transformation;

import org.apache.log4j.Logger;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;

import org.wyona.security.core.api.Identity;
import org.wyona.security.core.api.PolicyManager;
import org.wyona.security.core.api.Usecase;

/**
 * Transformer to remove <li><a href="../../en/private/index.html"></a></li> for href a user/identity has no right to access. 
 */
public class AccessControlTransformer extends AbstractTransformer {

    private static Logger log = Logger.getLogger(AccessControlTransformer.class);

    private PolicyManager policyManager;
    private Identity identity;
    private Usecase usecase;

    private boolean insideParentElement;
    private boolean insideAnchor;
    private boolean accessGranted;
    private StringBuffer textBuffer;

    private String parentElementName;
    private Attributes parentAttrs;
    private Attributes anchorAttrs;

    public static final String NS_XHTML_URI = "http://www.w3.org/1999/xhtml";
    
    /**
     *
     */
    public AccessControlTransformer(PolicyManager pm, Identity identity, Usecase usecase, String messages, String language, String defaultLanguage) {
        this.policyManager = pm;
        this.identity = identity;
        this.usecase = usecase;

        parentElementName = "li";

        insideParentElement = false;
        insideAnchor = false;

        accessGranted = true;
    }

    /**
     *
     */
    public void startElement(String namespaceURI, String localName, String qName, Attributes attrs) throws SAXException {
        //log.debug("Element name: " + localName + ", " + qName);

        if (insideParentElement && isAnchorElement(namespaceURI, localName, qName)) {
            this.insideAnchor = true;
            //log.debug("Inside a 'a' element which is inside parent element '" + parentElementName + "'!");
            anchorAttrs = new AttributesImpl(attrs);

            String href = attrs.getValue("href");
            String path = href;
            if (path.startsWith("../")) {
                path = path.substring(path.lastIndexOf("../") + 2);
            }
            if (path.startsWith("/")) {
                //log.debug("Check authorization for: " + path + ", " + identity + ", " + usecase);
                try {
                    if (policyManager.authorize(path, identity, usecase)) {
                        //log.debug("Access granted for " + identity + ", " + usecase + ", " + path);
                    } else {
                        //log.debug("Access denied for " + identity + ", " + usecase + ", " + path);
                        accessGranted = false;<commit_msg>pass parent element name as argument instead hardcoded<commit_after>package org.wyona.yanel.core.transformation;

import org.apache.log4j.Logger;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;

import org.wyona.security.core.api.Identity;
import org.wyona.security.core.api.PolicyManager;
import org.wyona.security.core.api.Usecase;

/**
 * Transformer to remove <li><a href="../../en/private/index.html"></a></li> for href a user/identity has no right to access. 
 */
public class AccessControlTransformer extends AbstractTransformer {

    private static Logger log = Logger.getLogger(AccessControlTransformer.class);

    private PolicyManager policyManager;
    private Identity identity;
    private Usecase usecase;

    private boolean insideParentElement;
    private boolean insideAnchor;
    private boolean accessGranted;
    private StringBuffer textBuffer;

    private String parentElementName;
    private Attributes parentAttrs;
    private Attributes anchorAttrs;

    public static final String NS_XHTML_URI = "http://www.w3.org/1999/xhtml";
    
    /**
     * @parentElementName Element name of parent, for example "li" or "p" which can contain anchors
     */
    public AccessControlTransformer(String parentElementName, PolicyManager pm, Identity identity, Usecase usecase, String messages, String language, String defaultLanguage) {
        this.policyManager = pm;
        this.identity = identity;
        this.usecase = usecase;

        this.parentElementName = parentElementName;

        insideParentElement = false;
        insideAnchor = false;

        accessGranted = true;
    }

    /**
     *
     */
    public void startElement(String namespaceURI, String localName, String qName, Attributes attrs) throws SAXException {
        //log.debug("Element name: " + localName + ", " + qName);

        if (insideParentElement && isAnchorElement(namespaceURI, localName, qName)) {
            this.insideAnchor = true;
            //log.debug("Inside a 'a' element which is inside parent element '" + parentElementName + "'!");
            anchorAttrs = new AttributesImpl(attrs);

            String href = attrs.getValue("href");
            String path = href;
            if (path.startsWith("../")) {
                path = path.substring(path.lastIndexOf("../") + 2);
            }
            if (path.startsWith("/")) {
                //log.debug("Check authorization for: " + path + ", " + identity + ", " + usecase);
                try {
                    if (policyManager.authorize(path, identity, usecase)) {
                        //log.debug("Access granted for " + identity + ", " + usecase + ", " + path);
                    } else {
                        //log.debug("Access denied for " + identity + ", " + usecase + ", " + path);
                        accessGranted = false;
{'fraction_non_alphanumeric': 0.11146723646723647, 'fraction_numerical': 0.002492877492877493, 'mean_word_length': 2.521630094043887, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 6}, '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': '5399103', 'n_tokens_mistral': 1517, 'n_tokens_neox': 1481, 'n_words': 378}