<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.workspace.internal;

import java.util.List;

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

import org.slf4j.Logger;
import org.xwiki.component.annotation.Component;
import org.xwiki.context.Execution;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.reference.DocumentReferenceResolver;
import org.xwiki.model.reference.EntityReferenceSerializer;
import org.xwiki.script.service.ScriptService;
import org.xwiki.workspace.Workspace;
import org.xwiki.workspace.WorkspaceException;
import org.xwiki.workspace.WorkspaceManager;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.plugin.wikimanager.doc.XWikiServer;
import com.xpn.xwiki.user.api.XWikiRightService;

/**
 * Makes the WorkspaceManager API available to scripting.
 * 
 * @version $Id$
 */
@Component
@Named("workspace")
@Singleton
public class WorkspaceManagerScriptService implements ScriptService
{
    /** Field name of the last API exception inserted in context. */
    public static final String CONTEXT_LASTEXCEPTION = "lastexception";

    /** Workspace Manager wrapped component. */
    @Inject
    private WorkspaceManager workspaceManager;

    /** Execution context. */
    @Inject
    private Execution execution;

    /** Logging tool. */
    @Inject
    private Logger logger;

    /**
     * User to parse a document reference.
     */
    @Inject
    @Named("current")
    private DocumentReferenceResolver<String> currentResolver;

    /**
     * Used to serialize a document reference.
     */
    @Inject
    private EntityReferenceSerializer<String> defaultSerializer;

    /** @return the wrapped component */
    WorkspaceManager getManager()
    {
        return workspaceManager;
    }

    /**
     * @param userName the user to check
     * @param workspaceName the workspace name to check
     * @return true if the it's possible for the specified user to create the speicified workspace
     */
    public boolean canCreateWorkspace(String userName, String workspaceName)
    {
        return workspaceManager.canCreateWorkspace(getPrefixedUserName(userName), workspaceName);
    }

    /**
     * @param userName the user to check
     * @param workspaceName the workspace name to check
     * @return true if the it's possible for the specified user to edit the specified workspace
     */
    public boolean canEditWorkspace(String userName, String workspaceName)
    {
        return workspaceManager.canEditWorkspace(getPrefixedUserName(userName), workspaceName);
    }

    /**
     * @param userName the user to check
     * @param workspaceName the workspace name to check
     * @return true if the it's possible for the specified user to delete the specified workspace
     */
    public boolean canDeleteWorkspace(String userName, String workspaceName)
    {
        return workspaceManager.canDeleteWorkspace(getPrefixedUserName(userName), workspaceName);
    }

    /**
     * Creates a new workspace from a wiki descriptor.
     * 
     * @param workspaceName name of the new workspace
     * @param newWikiXObjectDocument a new (in-memory) wiki descriptor document from which the new wiki descriptor
     *            document will be created. This method will take care of saving the document.
     * @see {@link #getLastException()} to check for abnormal method termination
     */
    public void createWorkspace(String workspaceName, XWikiServer newWikiXObjectDocument)
    {
        createWorkspace(workspaceName, newWikiXObjectDocument, "workspacetemplate");
    }

    /**
     * Creates a new workspace from a wiki descriptor.
     * 
     * @param workspaceName name of the new workspace
     * @param newWikiXObjectDocument a new (in-memory) wiki descriptor document from which the new wiki descriptor
     *            document will be created. This method will take care of saving the document.
     * @param templateWikiName the name of the wiki from where to copy document to the new wiki.
     * @see {@link #getLastException()} to check for abnormal method termination
     */
    public void createWorkspace(String workspaceName, XWikiServer newWikiXObjectDocument, String templateWikiName)
    {
        clearException();

        try {
            if (!canCreateWorkspace(getXWikiContext().getUser(), workspaceName)) {
                throw new WorkspaceException(String.format("Access denied to create the workspace [%s]", workspaceName));
            }

            /* Avoid "traps" by making sure the page from where this is executed has PR. */
            if (!getXWikiContext().getWiki().getRightService().hasProgrammingRights(getXWikiContext())) {
                throw new WorkspaceException(String.format(
                    "The page requires programming rights in order to create the workspace [%s]", workspaceName));
            }

            if (workspaceName == null || workspaceName.trim().equals("")) {
                throw new WorkspaceException(String.format("Workspace name [%s] is invalid", workspaceName));
            }

            this.workspaceManager.createWorkspace(workspaceName, newWikiXObjectDocument, templateWikiName);
        } catch (Exception e) {
            error(String.format("Failed to create workspace [%s]", workspaceName), e);
        }
    }
<commit_msg>XWIKI-8395: The workspace creator cannot edit workspace metadata nor delete the workspace * fix checkstyle<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.workspace.internal;

import java.util.List;

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

import org.slf4j.Logger;
import org.xwiki.component.annotation.Component;
import org.xwiki.context.Execution;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.reference.DocumentReferenceResolver;
import org.xwiki.model.reference.EntityReferenceSerializer;
import org.xwiki.script.service.ScriptService;
import org.xwiki.workspace.Workspace;
import org.xwiki.workspace.WorkspaceException;
import org.xwiki.workspace.WorkspaceManager;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.plugin.wikimanager.doc.XWikiServer;
import com.xpn.xwiki.user.api.XWikiRightService;

/**
 * Makes the WorkspaceManager API available to scripting.
 * 
 * @version $Id$
 */
@Component
@Named("workspace")
@Singleton
public class WorkspaceManagerScriptService implements ScriptService
{
    /** Field name of the last API exception inserted in context. */
    public static final String CONTEXT_LASTEXCEPTION = "lastexception";

    /** Workspace Manager wrapped component. */
    @Inject
    private WorkspaceManager workspaceManager;

    /** Execution context. */
    @Inject
    private Execution execution;

    /** Logging tool. */
    @Inject
    private Logger logger;

    /**
     * User to parse a document reference.
     */
    @Inject
    @Named("current")
    private DocumentReferenceResolver<String> currentResolver;

    /**
     * Used to serialize a document reference.
     */
    @Inject
    private EntityReferenceSerializer<String> defaultSerializer;

    /** @return the wrapped component */
    WorkspaceManager getManager()
    {
        return workspaceManager;
    }

    /**
     * @param userName the user to check
     * @param workspaceName the workspace name to check
     * @return true if the it's possible for the specified user to create the speicified workspace
     */
    public boolean canCreateWorkspace(String userName, String workspaceName)
    {
        return workspaceManager.canCreateWorkspace(getPrefixedUserName(userName), workspaceName);
    }

    /**
     * @param userName the user to check
     * @param workspaceName the workspace name to check
     * @return true if the it's possible for the specified user to edit the specified workspace
     */
    public boolean canEditWorkspace(String userName, String workspaceName)
    {
        return workspaceManager.canEditWorkspace(getPrefixedUserName(userName), workspaceName);
    }

    /**
     * @param userName the user to check
     * @param workspaceName the workspace name to check
     * @return true if the it's possible for the specified user to delete the specified workspace
     */
    public boolean canDeleteWorkspace(String userName, String workspaceName)
    {
        return workspaceManager.canDeleteWorkspace(getPrefixedUserName(userName), workspaceName);
    }

    /**
     * Creates a new workspace from a wiki descriptor.
     * 
     * @param workspaceName name of the new workspace
     * @param newWikiXObjectDocument a new (in-memory) wiki descriptor document from which the new wiki descriptor
     *            document will be created. This method will take care of saving the document.
     * @see {@link #getLastException()} to check for abnormal method termination
     */
    public void createWorkspace(String workspaceName, XWikiServer newWikiXObjectDocument)
    {
        createWorkspace(workspaceName, newWikiXObjectDocument, "workspacetemplate");
    }

    /**
     * Creates a new workspace from a wiki descriptor.
     * 
     * @param workspaceName name of the new workspace
     * @param newWikiXObjectDocument a new (in-memory) wiki descriptor document from which the new wiki descriptor
     *            document will be created. This method will take care of saving the document.
     * @param templateWikiName the name of the wiki from where to copy document to the new wiki.
     * @see {@link #getLastException()} to check for abnormal method termination
     */
    public void createWorkspace(String workspaceName, XWikiServer newWikiXObjectDocument, String templateWikiName)
    {
        clearException();

        try {
            if (!canCreateWorkspace(getXWikiContext().getUser(), workspaceName)) {
                throw new WorkspaceException(String.format("Access denied to create the workspace [%s]",
                    workspaceName));
            }

            /* Avoid "traps" by making sure the page from where this is executed has PR. */
            if (!getXWikiContext().getWiki().getRightService().hasProgrammingRights(getXWikiContext())) {
                throw new WorkspaceException(String.format(
                    "The page requires programming rights in order to create the workspace [%s]", workspaceName));
            }

            if (workspaceName == null || workspaceName.trim().equals("")) {
                throw new WorkspaceException(String.format("Workspace name [%s] is invalid", workspaceName));
            }

            this.workspaceManager.createWorkspace(workspaceName, newWikiXObjectDocument, templateWikiName);
        } catch (Exception e) {
            error(String.format("Failed to create workspace [%s]", workspaceName), e);
        }
    }

{'fraction_non_alphanumeric': 0.07781760254878535, 'fraction_numerical': 0.0025487853444842693, 'mean_word_length': 3.297056810403833, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '7327904', 'n_tokens_mistral': 3208, 'n_tokens_neox': 3153, 'n_words': 1158}