001/*
002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     dmetzler
016 */
017package org.nuxeo.ecm.restapi.server.jaxrs.usermanager;
018
019import java.util.List;
020
021import javax.servlet.http.HttpServletRequest;
022import javax.ws.rs.GET;
023import javax.ws.rs.POST;
024import javax.ws.rs.Path;
025import javax.ws.rs.PathParam;
026import javax.ws.rs.core.Response;
027import javax.ws.rs.core.Response.Status;
028
029import org.nuxeo.ecm.core.api.NuxeoException;
030import org.nuxeo.ecm.core.api.NuxeoPrincipal;
031import org.nuxeo.ecm.platform.usermanager.UserManager;
032import org.nuxeo.ecm.restapi.server.jaxrs.PaginableObject;
033import org.nuxeo.ecm.webengine.WebException;
034import org.nuxeo.ecm.webengine.model.exceptions.WebResourceNotFoundException;
035import org.nuxeo.ecm.webengine.model.exceptions.WebSecurityException;
036import org.nuxeo.runtime.api.Framework;
037
038public abstract class AbstractUMRootObject<T> extends PaginableObject<T> {
039
040    protected String query;
041
042    protected UserManager um;
043
044    @Override
045    protected void initialize(Object... args) {
046        super.initialize(args);
047        um = Framework.getLocalService(UserManager.class);
048
049        final HttpServletRequest request = ctx.getRequest();
050        query = request.getParameter("q");
051    }
052
053    @Path("{artName}")
054    public Object getArtifactWebObject(@PathParam("artName") String artName) {
055        try {
056            T artifact = getArtifact(artName);
057            if (artifact == null) {
058                throw new WebResourceNotFoundException(getArtifactType() + " does not exist");
059            }
060            return newObject(getArtifactType(), artifact);
061        } catch (NuxeoException e) {
062            throw WebException.wrap(e);
063        }
064    }
065
066    @POST
067    public Response createNew(T artifact) {
068        try {
069            checkPrecondition(artifact);
070            artifact = createArtifact(artifact);
071            return Response.status(Status.CREATED).entity(artifact).build();
072
073        } catch (NuxeoException e) {
074            throw WebException.wrap(e);
075        }
076    }
077
078    @GET
079    @Path("search")
080    public List<T> search() {
081        return getPaginableEntries();
082    }
083
084    @Override
085    protected Object[] getParams() {
086        return new Object[] { query };
087    }
088
089    /**
090     * Returns the artifact given its id.
091     */
092    protected abstract T getArtifact(String id);
093
094    /**
095     * Returns the type of the current artifact needed for {@link #newObject(String, Object...)}.
096     */
097    protected abstract String getArtifactType();
098
099    /**
100     * Checks the precondition to create an artifact (for instance validity, duplicate detection, guards...).
101     */
102    protected abstract void checkPrecondition(T artifact);
103
104    /**
105     * Persist an artifact in the underlying persistence system.
106     */
107    protected abstract T createArtifact(T artifact);
108
109    protected void checkCurrentUserCanCreateArtifact(T artifact) {
110        NuxeoPrincipal currentUser = (NuxeoPrincipal) getContext().getCoreSession().getPrincipal();
111        if (!currentUser.isAdministrator()) {
112            if (!currentUser.isMemberOf("powerusers") || !isAPowerUserEditableArtifact(artifact)) {
113                throw new WebSecurityException("Cannot create artifact");
114            }
115        }
116    }
117
118    abstract boolean isAPowerUserEditableArtifact(T artifact);
119
120}