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.io.Serializable;
020
021import javax.ws.rs.Path;
022import javax.ws.rs.PathParam;
023
024import org.nuxeo.ecm.core.api.DocumentModel;
025import org.nuxeo.ecm.core.api.NuxeoException;
026import org.nuxeo.ecm.core.api.NuxeoGroup;
027import org.nuxeo.ecm.core.api.NuxeoPrincipal;
028import org.nuxeo.ecm.platform.usermanager.UserManager;
029import org.nuxeo.ecm.webengine.WebException;
030import org.nuxeo.ecm.webengine.model.WebObject;
031import org.nuxeo.ecm.webengine.model.exceptions.WebResourceNotFoundException;
032import org.nuxeo.runtime.api.Framework;
033
034/**
035 * @since 5.7.3
036 */
037@WebObject(type = "group")
038public class GroupObject extends AbstractUMObject<NuxeoGroup> {
039
040    @Path("user/{username}")
041    public Object doGetUserToGroup(@PathParam("username") String username) {
042        try {
043            UserManager um = Framework.getLocalService(UserManager.class);
044            NuxeoPrincipal principal = um.getPrincipal(username);
045            if (principal == null) {
046                throw new WebResourceNotFoundException("User not found");
047            }
048            return newObject("userToGroup", principal, currentArtifact);
049
050        } catch (NuxeoException e) {
051            throw WebException.wrap(e);
052        }
053
054    }
055
056    @Override
057    protected NuxeoGroup updateArtifact(NuxeoGroup updateGroup) {
058        DocumentModel groupModel = um.getGroupModel(currentArtifact.getName());
059        groupModel.setPropertyValue(um.getGroupLabelField(), updateGroup.getLabel());
060        groupModel.setPropertyValue(um.getGroupMembersField(), (Serializable) updateGroup.getMemberUsers());
061        groupModel.setPropertyValue(um.getGroupSubGroupsField(), (Serializable) updateGroup.getMemberGroups());
062
063        um.updateGroup(groupModel);
064        return um.getGroup(currentArtifact.getName());
065    }
066
067    @Override
068    protected void deleteArtifact() {
069        um.deleteGroup(um.getGroupModel(currentArtifact.getName()));
070    }
071
072    @Override
073    protected boolean isAPowerUserEditableArtifact() {
074        return GroupRootObject.isAPowerUserEditableGroup(currentArtifact);
075    }
076}