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