<commit_before>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.uiextension.internal;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;
import javax.inject.Singleton;

import org.slf4j.Logger;
import org.xwiki.component.annotation.Component;
import org.xwiki.component.manager.ComponentManager;
import org.xwiki.component.wiki.WikiComponent;
import org.xwiki.component.wiki.WikiComponentBuilder;
import org.xwiki.component.wiki.WikiComponentException;
import org.xwiki.component.wiki.WikiComponentScope;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.reference.EntityReferenceSerializer;
import org.xwiki.security.authorization.AuthorExecutor;
import org.xwiki.security.authorization.AuthorizationManager;
import org.xwiki.security.authorization.Right;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.objects.BaseObject;

/**
 * Provides {@link org.xwiki.uiextension.UIExtension} components from definitions stored in XObjects.
 * 
 * @version $Id$
 * @since 4.2M3
 */
@Component
@Singleton
@Named("uiextension")
public class WikiUIExtensionComponentBuilder implements WikiComponentBuilder, WikiUIExtensionConstants
{
    /**
     * The logger to log.
     */
    @Inject
    private Logger logger;

    /**
     * Used to transform the reference to the UI Extension XClass to a string usable in a query. {@see
     * #searchDocumentReferences()}
     */
    @Inject
    @Named("compactwiki")
    private EntityReferenceSerializer<String> compactWikiSerializer;

    /**
     * Used to generate a role hint for UI extensions based on their object reference.
     */
    @Inject
    private EntityReferenceSerializer<String> serializer;

    /**
     * Used to get the parser to transform the extension content to a XDOM.
     */
    @Inject
    @Named("wiki")
    private ComponentManager cm;

    @Inject
    private Provider<XWikiContext> xcontextProvider;

    @Inject
    private AuthorizationManager authorization;

    @Inject
    private AuthorExecutor authorExecutor;

    /**
     * Checks if the last author of the document holding the extension(s) has the rights required to register extensions
     * for the given scope. If the document author doesn't have the required rights a {@link WikiComponentException} is
     * thrown.
     *
     * @param extensionsDoc the document holding the extension(s)
     * @param scope the scope to check the rights for
     * @throws WikiComponentException if the document author doesn't have the required rights to register extensions
     */
    private void checkRights(XWikiDocument extensionsDoc, WikiComponentScope scope) throws WikiComponentException
    {
        if (scope == WikiComponentScope.GLOBAL
            && !this.authorization.hasAccess(Right.PROGRAM, extensionsDoc.getContentAuthorReference(), null)) {
            throw new WikiComponentException("Registering global UI extensions requires programming rights");
        } else if (scope == WikiComponentScope.WIKI
            && !this.authorization.hasAccess(Right.ADMIN, extensionsDoc.getContentAuthorReference(), extensionsDoc
                .getDocumentReference().getWikiReference())) {
            throw new WikiComponentException(
                "Registering UI extensions at wiki level requires wiki administration rights");
        }
    }

    /**
     * Retrieve the list of {@link BaseObject} defining UI extensions.
     *
     * @param extensionsDoc the document to retrieve the definitions from
     * @return the list of {@link BaseObject} defining UI extensions in the given document
     * @throws WikiComponentException if no extension definition can be found in the document
     */
    private List<BaseObject> getExtensionDefinitions(XWikiDocument extensionsDoc) throws WikiComponentException
    {
        // Check whether this document contains a listener definition.
        List<BaseObject> extensionDefinitions = extensionsDoc.getXObjects(UI_EXTENSION_CLASS);

        if (extensionDefinitions.size() == 0) {
            throw new WikiComponentException(String.format("No UI extension object could be found in document [%s]",<commit_msg>XWIKI-14039: UI extension registration code test the wrong author<commit_after>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.uiextension.internal;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;
import javax.inject.Singleton;

import org.slf4j.Logger;
import org.xwiki.component.annotation.Component;
import org.xwiki.component.manager.ComponentManager;
import org.xwiki.component.wiki.WikiComponent;
import org.xwiki.component.wiki.WikiComponentBuilder;
import org.xwiki.component.wiki.WikiComponentException;
import org.xwiki.component.wiki.WikiComponentScope;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.reference.EntityReferenceSerializer;
import org.xwiki.security.authorization.AuthorExecutor;
import org.xwiki.security.authorization.AuthorizationManager;
import org.xwiki.security.authorization.Right;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.objects.BaseObject;

/**
 * Provides {@link org.xwiki.uiextension.UIExtension} components from definitions stored in XObjects.
 * 
 * @version $Id$
 * @since 4.2M3
 */
@Component
@Singleton
@Named("uiextension")
public class WikiUIExtensionComponentBuilder implements WikiComponentBuilder, WikiUIExtensionConstants
{
    /**
     * The logger to log.
     */
    @Inject
    private Logger logger;

    /**
     * Used to transform the reference to the UI Extension XClass to a string usable in a query. {@see
     * #searchDocumentReferences()}
     */
    @Inject
    @Named("compactwiki")
    private EntityReferenceSerializer<String> compactWikiSerializer;

    /**
     * Used to generate a role hint for UI extensions based on their object reference.
     */
    @Inject
    private EntityReferenceSerializer<String> serializer;

    /**
     * Used to get the parser to transform the extension content to a XDOM.
     */
    @Inject
    @Named("wiki")
    private ComponentManager cm;

    @Inject
    private Provider<XWikiContext> xcontextProvider;

    @Inject
    private AuthorizationManager authorization;

    @Inject
    private AuthorExecutor authorExecutor;

    /**
     * Checks if the last author of the document holding the extension(s) has the rights required to register extensions
     * for the given scope. If the document author doesn't have the required rights a {@link WikiComponentException} is
     * thrown.
     *
     * @param extensionsDoc the document holding the extension(s)
     * @param scope the scope to check the rights for
     * @throws WikiComponentException if the document author doesn't have the required rights to register extensions
     */
    private void checkRights(XWikiDocument extensionsDoc, WikiComponentScope scope) throws WikiComponentException
    {
        if (scope == WikiComponentScope.GLOBAL
            && !this.authorization.hasAccess(Right.PROGRAM, extensionsDoc.getAuthorReference(), null)) {
            throw new WikiComponentException("Registering global UI extensions requires programming rights");
        } else if (scope == WikiComponentScope.WIKI && !this.authorization.hasAccess(Right.ADMIN,
            extensionsDoc.getAuthorReference(), extensionsDoc.getDocumentReference().getWikiReference())) {
            throw new WikiComponentException(
                "Registering UI extensions at wiki level requires wiki administration rights");
        }
    }

    /**
     * Retrieve the list of {@link BaseObject} defining UI extensions.
     *
     * @param extensionsDoc the document to retrieve the definitions from
     * @return the list of {@link BaseObject} defining UI extensions in the given document
     * @throws WikiComponentException if no extension definition can be found in the document
     */
    private List<BaseObject> getExtensionDefinitions(XWikiDocument extensionsDoc) throws WikiComponentException
    {
        // Check whether this document contains a listener definition.
        List<BaseObject> extensionDefinitions = extensionsDoc.getXObjects(UI_EXTENSION_CLASS);

        if (extensionDefinitions.size() == 0) {
            throw new WikiComponentException(String.format("No UI extension object could be found in document [%s]",
{'fraction_non_alphanumeric': 0.07499273326228079, 'fraction_numerical': 0.003972483286503246, 'mean_word_length': 3.830135704258306, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '6654139', 'n_tokens_mistral': 2668, 'n_tokens_neox': 2585, 'n_words': 944}