<commit_before>/*
 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-2.1.html
 *
 * This library 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.
 *
 * Contributors:
 *     dmetzler
 */
package org.nuxeo.ecm.automation.rest.jaxrs.usermanager;

import java.io.Serializable;

import javax.ws.rs.core.Response;

import org.nuxeo.ecm.core.api.ClientException;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.api.NuxeoGroup;
import org.nuxeo.ecm.core.api.NuxeoPrincipal;
import org.nuxeo.ecm.platform.usermanager.UserManager;
import org.nuxeo.ecm.webengine.WebException;
import org.nuxeo.ecm.webengine.model.WebObject;
import org.nuxeo.ecm.webengine.model.exceptions.WebSecurityException;
import org.nuxeo.runtime.api.Framework;

/**
 *
 *
 * @since 5.7.3
 */
@WebObject(type = "groups")
public class GroupRootObject extends AbstractUMRootObject<NuxeoGroup> {

    @Override
    protected NuxeoGroup getArtifact(String id) throws ClientException {
        return um.getGroup(id);
    }

    @Override
    protected String getArtifactType() {
        return "group";
    }

    @Override
    protected void checkPrecondition(NuxeoGroup group) throws ClientException {
        checkCurrentUserCanCreateGroup(group, um);
        checkGroupHasAName(group);
        checkGroupDoesNotAlreadyExists(group, um);
    }

    @Override
    protected NuxeoGroup createArtifact(NuxeoGroup group)
            throws ClientException {
        DocumentModel groupModel = buildModelFromGroup(group, um);

        um.createGroup(groupModel);
        return um.getGroup(group.getName());
    }

    private void checkCurrentUserCanCreateGroup(NuxeoGroup group, UserManager um) {
        NuxeoPrincipal currentUser = (NuxeoPrincipal) getContext().getCoreSession().getPrincipal();
        if (!currentUser.isAdministrator()) {
            if (!currentUser.isMemberOf("powerusers")
                    || !isAPowerUserEditableArtifact(group)) {
                throw new WebSecurityException("Cannot create a group");
            }
        }
    }

    /**
     * Builds a DocumentModel from a group suitable to call UserManager methods.
     *
     * @param group
     * @param um
     * @return
     * @throws ClientException
     *
     */
    private DocumentModel buildModelFromGroup(NuxeoGroup group, UserManager um)
            throws ClientException {
        DocumentModel groupModel = um.getBareGroupModel();
        String schemaName = um.getGroupSchemaName();
        groupModel.setProperty(schemaName, um.getGroupIdField(),
                group.getName());
        groupModel.setProperty(schemaName, um.getGroupLabelField(),
                group.getLabel());

        groupModel.setPropertyValue(um.getGroupMembersField(),
                (Serializable) group.getMemberUsers());
        groupModel.setPropertyValue(um.getGroupSubGroupsField(),
                (Serializable) group.getMemberGroups());
        return groupModel;
    }

    /**
     * @param group<commit_msg>NXP-12286 Remove local check and use common one<commit_after>/*
 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-2.1.html
 *
 * This library 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.
 *
 * Contributors:
 *     dmetzler
 */
package org.nuxeo.ecm.automation.rest.jaxrs.usermanager;

import java.io.Serializable;

import javax.ws.rs.core.Response;

import org.nuxeo.ecm.core.api.ClientException;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.api.NuxeoGroup;
import org.nuxeo.ecm.platform.usermanager.UserManager;
import org.nuxeo.ecm.webengine.WebException;
import org.nuxeo.ecm.webengine.model.WebObject;
import org.nuxeo.runtime.api.Framework;

/**
 *
 *
 * @since 5.7.3
 */
@WebObject(type = "groups")
public class GroupRootObject extends AbstractUMRootObject<NuxeoGroup> {

    @Override
    protected NuxeoGroup getArtifact(String id) throws ClientException {
        return um.getGroup(id);
    }

    @Override
    protected String getArtifactType() {
        return "group";
    }

    @Override
    protected void checkPrecondition(NuxeoGroup group) throws ClientException {
        checkCurrentUserCanCreateArtifact(group);
        checkGroupHasAName(group);
        checkGroupDoesNotAlreadyExists(group, um);
    }

    @Override
    protected NuxeoGroup createArtifact(NuxeoGroup group)
            throws ClientException {
        DocumentModel groupModel = buildModelFromGroup(group, um);

        um.createGroup(groupModel);
        return um.getGroup(group.getName());
    }

    /**
     * Builds a DocumentModel from a group suitable to call UserManager methods.
     *
     * @param group
     * @param um
     * @return
     * @throws ClientException
     *
     */
    private DocumentModel buildModelFromGroup(NuxeoGroup group, UserManager um)
            throws ClientException {
        DocumentModel groupModel = um.getBareGroupModel();
        String schemaName = um.getGroupSchemaName();
        groupModel.setProperty(schemaName, um.getGroupIdField(),
                group.getName());
        groupModel.setProperty(schemaName, um.getGroupLabelField(),
                group.getLabel());

        groupModel.setPropertyValue(um.getGroupMembersField(),
                (Serializable) group.getMemberUsers());
        groupModel.setPropertyValue(um.getGroupSubGroupsField(),
                (Serializable) group.getMemberGroups());
        return groupModel;
    }

    /**
     * @param group
{'fraction_non_alphanumeric': 0.09316185696361355, 'fraction_numerical': 0.004234629861982434, 'mean_word_length': 3.170699803793329, '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': '3443134', 'n_tokens_mistral': 1871, 'n_tokens_neox': 1778, 'n_words': 434}