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 java.util.List;
022
023import javax.servlet.http.HttpServletRequest;
024import javax.ws.rs.GET;
025import javax.ws.rs.POST;
026import javax.ws.rs.Path;
027import javax.ws.rs.PathParam;
028import javax.ws.rs.core.Response;
029import javax.ws.rs.core.Response.Status;
030
031import org.nuxeo.ecm.core.api.NuxeoException;
032import org.nuxeo.ecm.core.api.NuxeoPrincipal;
033import org.nuxeo.ecm.platform.usermanager.UserManager;
034import org.nuxeo.ecm.restapi.server.jaxrs.PaginableObject;
035import org.nuxeo.ecm.webengine.WebException;
036import org.nuxeo.ecm.webengine.model.exceptions.WebResourceNotFoundException;
037import org.nuxeo.ecm.webengine.model.exceptions.WebSecurityException;
038import org.nuxeo.runtime.api.Framework;
039
040public abstract class AbstractUMRootObject<T> extends PaginableObject<T> {
041
042    protected String query;
043
044    protected UserManager um;
045
046    @Override
047    protected void initialize(Object... args) {
048        super.initialize(args);
049        um = Framework.getLocalService(UserManager.class);
050
051        final HttpServletRequest request = ctx.getRequest();
052        query = request.getParameter("q");
053    }
054
055    @Path("{artName}")
056    public Object getArtifactWebObject(@PathParam("artName") String artName) {
057        try {
058            T artifact = getArtifact(artName);
059            if (artifact == null) {
060                throw new WebResourceNotFoundException(getArtifactType() + " does not exist");
061            }
062            return newObject(getArtifactType(), artifact);
063        } catch (NuxeoException e) {
064            throw WebException.wrap(e);
065        }
066    }
067
068    @POST
069    public Response createNew(T artifact) {
070        try {
071            checkPrecondition(artifact);
072            artifact = createArtifact(artifact);
073            return Response.status(Status.CREATED).entity(artifact).build();
074
075        } catch (NuxeoException e) {
076            throw WebException.wrap(e);
077        }
078    }
079
080    @GET
081    @Path("search")
082    public List<T> search() {
083        return getPaginableEntries();
084    }
085
086    @Override
087    protected Object[] getParams() {
088        return new Object[] { query };
089    }
090
091    /**
092     * Returns the artifact given its id.
093     */
094    protected abstract T getArtifact(String id);
095
096    /**
097     * Returns the type of the current artifact needed for {@link #newObject(String, Object...)}.
098     */
099    protected abstract String getArtifactType();
100
101    /**
102     * Checks the precondition to create an artifact (for instance validity, duplicate detection, guards...).
103     */
104    protected abstract void checkPrecondition(T artifact);
105
106    /**
107     * Persist an artifact in the underlying persistence system.
108     */
109    protected abstract T createArtifact(T artifact);
110
111    protected void checkCurrentUserCanCreateArtifact(T artifact) {
112        NuxeoPrincipal currentUser = (NuxeoPrincipal) getContext().getCoreSession().getPrincipal();
113        if (!currentUser.isAdministrator()) {
114            if (!currentUser.isMemberOf("powerusers") || !isAPowerUserEditableArtifact(artifact)) {
115                throw new WebSecurityException("Cannot create artifact");
116            }
117        }
118    }
119
120    abstract boolean isAPowerUserEditableArtifact(T artifact);
121
122}