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