001/*
002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     dmetzler
018 */
019package org.nuxeo.ecm.restapi.server.jaxrs.usermanager;
020
021import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
022import static javax.servlet.http.HttpServletResponse.SC_CONFLICT;
023
024import org.nuxeo.ecm.core.api.NuxeoException;
025import org.nuxeo.ecm.core.api.NuxeoGroup;
026import org.nuxeo.ecm.platform.query.api.PageProviderDefinition;
027import org.nuxeo.ecm.platform.query.api.PageProviderService;
028import org.nuxeo.ecm.platform.usermanager.UserManager;
029import org.nuxeo.ecm.webengine.model.WebObject;
030import org.nuxeo.runtime.api.Framework;
031
032/**
033 * @since 5.7.3
034 */
035@WebObject(type = "groups")
036public class GroupRootObject extends AbstractUMRootObject<NuxeoGroup> {
037
038    public static final String PAGE_PROVIDER_NAME = "nuxeo_groups_listing";
039
040    @Override
041    protected NuxeoGroup getArtifact(String id) {
042        return um.getGroup(id);
043    }
044
045    @Override
046    protected String getArtifactType() {
047        return "group";
048    }
049
050    @Override
051    protected void checkPrecondition(NuxeoGroup group) {
052        checkCurrentUserCanCreateArtifact(group);
053        checkGroupHasAName(group);
054        checkGroupDoesNotAlreadyExists(group, um);
055    }
056
057    @Override
058    protected NuxeoGroup createArtifact(NuxeoGroup group) {
059        um.createGroup(group.getModel());
060        return um.getGroup(group.getName());
061    }
062
063    private void checkGroupDoesNotAlreadyExists(NuxeoGroup group, UserManager um) {
064        if (um.getGroup(group.getName()) != null) {
065            throw new NuxeoException("Group already exists", SC_CONFLICT);
066        }
067    }
068
069    private void checkGroupHasAName(NuxeoGroup group) {
070        if (group.getName() == null) {
071            throw new NuxeoException("Group MUST have a name", SC_BAD_REQUEST);
072        }
073    }
074
075    @Override
076    boolean isAPowerUserEditableArtifact(NuxeoGroup artifact) {
077        return isAPowerUserEditableGroup(artifact);
078
079    }
080
081    static boolean isAPowerUserEditableGroup(NuxeoGroup group) {
082        UserManager um = Framework.getService(UserManager.class);
083        return !um.getAdministratorsGroups().contains(group.getName());
084
085    }
086
087    @Override
088    protected PageProviderDefinition getPageProviderDefinition() {
089        PageProviderService ppService = Framework.getService(PageProviderService.class);
090        return ppService.getPageProviderDefinition(PAGE_PROVIDER_NAME);
091    }
092
093}