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