001/*
002 * (C) Copyright 2006-2009 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 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.webengine.admin;
023
024import java.util.Arrays;
025import java.util.List;
026
027import javax.servlet.http.HttpServletRequest;
028import javax.ws.rs.DELETE;
029import javax.ws.rs.GET;
030import javax.ws.rs.POST;
031import javax.ws.rs.PUT;
032import javax.ws.rs.Path;
033import javax.ws.rs.Produces;
034import javax.ws.rs.core.Response;
035
036import org.nuxeo.ecm.platform.usermanager.NuxeoPrincipalImpl;
037import org.nuxeo.ecm.platform.usermanager.UserManager;
038import org.nuxeo.ecm.webengine.model.WebObject;
039import org.nuxeo.ecm.webengine.model.impl.DefaultObject;
040import org.nuxeo.runtime.api.Framework;
041
042@WebObject(type = "User")
043@Produces("text/html;charset=UTF-8")
044public class User extends DefaultObject {
045
046    NuxeoPrincipalImpl principal;
047
048    @Override
049    protected void initialize(Object... args) {
050        assert args != null && args.length > 0;
051        principal = (NuxeoPrincipalImpl) args[0];
052    }
053
054    @GET
055    public Object doGet() {
056        return getView("index").arg("user", principal);
057    }
058
059    @POST
060    public Response doPost() {
061        return redirect(getPrevious().getPath());
062    }
063
064    @PUT
065    public Response doPut() {
066        UserManager userManager = Framework.getService(UserManager.class);
067        HttpServletRequest req = ctx.getRequest();
068        // update
069        principal.setFirstName(req.getParameter("firstName"));
070        principal.setLastName(req.getParameter("lastName"));
071        principal.setPassword(req.getParameter("password"));
072        principal.setEmail(req.getParameter("email"));
073
074        String[] selectedGroups = req.getParameterValues("groups");
075        List<String> listGroups = Arrays.asList(selectedGroups);
076        principal.setGroups(listGroups);
077
078        userManager.updatePrincipal(principal);
079        return redirect(getPath());
080    }
081
082    @DELETE
083    public Response doDelete() {
084        UserManager userManager = Framework.getService(UserManager.class);
085        userManager.deletePrincipal(principal);
086        return redirect(getPrevious().getPath());
087    }
088
089    @POST
090    @Path("@put")
091    public Response simulatePut() {
092        return doPut();
093    }
094
095    @GET
096    @Path("@delete")
097    public Response simulateDelete() {
098        return doDelete();
099    }
100
101}