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.GET;
029import javax.ws.rs.POST;
030import javax.ws.rs.Path;
031import javax.ws.rs.PathParam;
032import javax.ws.rs.Produces;
033import javax.ws.rs.QueryParam;
034import javax.ws.rs.core.Response;
035
036import org.nuxeo.ecm.core.api.DocumentModelList;
037import org.nuxeo.ecm.core.api.NuxeoGroup;
038import org.nuxeo.ecm.core.api.NuxeoPrincipal;
039import org.nuxeo.ecm.core.api.impl.NuxeoGroupImpl;
040import org.nuxeo.ecm.platform.usermanager.NuxeoPrincipalImpl;
041import org.nuxeo.ecm.platform.usermanager.UserManager;
042import org.nuxeo.ecm.webengine.model.WebObject;
043import org.nuxeo.ecm.webengine.model.exceptions.WebResourceNotFoundException;
044import org.nuxeo.ecm.webengine.model.impl.DefaultObject;
045import org.nuxeo.runtime.api.Framework;
046
047@WebObject(type = "UserManager")
048@Produces("text/html;charset=UTF-8")
049public class UserService extends DefaultObject {
050
051    @GET
052    public Object getIndex(@QueryParam("query") String query, @QueryParam("group") String group) {
053        if (query != null && !query.isEmpty()) {
054            UserManager userManager = Framework.getService(UserManager.class);
055            if (group != null) {
056                DocumentModelList results = userManager.searchGroups(query);
057                return getView("index").arg("groups", results);
058            } else {
059                List<NuxeoPrincipal> results = userManager.searchPrincipals(query);
060                return getView("index").arg("users", results);
061            }
062        }
063        return getView("index");
064    }
065
066    @Path("user/{user}")
067    public Object searchUsers(@PathParam("user") String user) {
068        UserManager userManager = Framework.getService(UserManager.class);
069        NuxeoPrincipalImpl principal = (NuxeoPrincipalImpl) userManager.getPrincipal(user);
070        if (principal == null) {
071            throw new WebResourceNotFoundException("User not found: " + user);
072        }
073        return newObject("User", principal);
074    }
075
076    @Path("group/{group}")
077    public Object searchGroups(@PathParam("group") String group) {
078        UserManager userManager = Framework.getService(UserManager.class);
079        // FIXME: find better name for it
080        NuxeoGroup principal = userManager.getGroup(group);
081        if (principal == null) {
082            throw new WebResourceNotFoundException("Group not found: " + group);
083        }
084        return newObject("Group", principal);
085    }
086
087    @POST
088    @Path("user")
089    public Response postUser() {
090        HttpServletRequest req = ctx.getRequest();
091        String username = req.getParameter("username");
092        UserManager userManager = Framework.getService(UserManager.class);
093        if (username != null && !username.isEmpty()) {
094            NuxeoPrincipalImpl user = (NuxeoPrincipalImpl) userManager.getPrincipal(username);
095            String[] selectedGroups;
096            if (user != null) {
097                // update
098                user.setFirstName(req.getParameter("firstName"));
099                user.setLastName(req.getParameter("lastName"));
100                user.setPassword(req.getParameter("password"));
101                user.setEmail(req.getParameter("email"));
102
103                selectedGroups = req.getParameterValues("groups");
104                List<String> listGroups = Arrays.asList(selectedGroups);
105                user.setGroups(listGroups);
106
107                userManager.updatePrincipal(user);
108            } else {
109                // create
110                user = new NuxeoPrincipalImpl(req.getParameter("username"));
111                user.setFirstName(req.getParameter("firstName"));
112                user.setLastName(req.getParameter("lastName"));
113                user.setPassword(req.getParameter("password"));
114                user.setEmail(req.getParameter("email"));
115
116                selectedGroups = req.getParameterValues("groups");
117                List<String> listGroups = Arrays.asList(selectedGroups);
118                user.setGroups(listGroups);
119
120                userManager.createPrincipal(user);
121            }
122            return redirect(getPath() + "/user/" + user.getName());
123        }
124        // FIXME
125        return null;
126    }
127
128    @POST
129    @Path("group")
130    public Response postGroup() {
131        String groupName = ctx.getRequest().getParameter("groupName");
132        UserManager userManager = Framework.getService(UserManager.class);
133        if (groupName != null && !groupName.equals("")) {
134            NuxeoGroup group = new NuxeoGroupImpl(groupName);
135            userManager.createGroup(group);
136            return redirect(getPath() + "/group/" + group.getName());
137        }
138        // FIXME
139        return null;
140    }
141
142    public List<NuxeoGroup> getGroups() {
143        return Framework.getService(UserManager.class).getAvailableGroups();
144    }
145
146    public List<NuxeoPrincipal> getUsers() {
147        return Framework.getService(UserManager.class).getAvailablePrincipals();
148    }
149
150}