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.platform.userworkspace.core.service;
021
022import java.io.Serializable;
023import java.security.Principal;
024import java.util.HashMap;
025import java.util.Map;
026
027import org.nuxeo.common.utils.Path;
028import org.nuxeo.ecm.core.api.CoreSession;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.NuxeoPrincipal;
031import org.nuxeo.ecm.core.api.PathRef;
032import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
033import org.nuxeo.ecm.core.api.security.ACE;
034import org.nuxeo.ecm.core.api.security.ACL;
035import org.nuxeo.ecm.core.api.security.ACP;
036import org.nuxeo.ecm.core.api.security.SecurityConstants;
037import org.nuxeo.ecm.core.api.security.impl.ACLImpl;
038import org.nuxeo.ecm.core.api.security.impl.ACPImpl;
039import org.nuxeo.ecm.platform.userworkspace.api.UserWorkspaceService;
040import org.nuxeo.ecm.platform.userworkspace.constants.UserWorkspaceConstants;
041
042/**
043 * Default implementation of the {@link UserWorkspaceService}.
044 * 
045 * @author tiry
046 */
047public class DefaultUserWorkspaceServiceImpl extends AbstractUserWorkspaceImpl implements UserWorkspaceService {
048
049    private static final long serialVersionUID = 1L;
050
051    protected String getUserWorkspaceRootType() {
052        return getComponent().getConfiguration().getUserWorkspaceRootType();
053    }
054
055    protected String getUserWorkspaceType() {
056        return getComponent().getConfiguration().getUserWorkspaceType();
057    }
058
059    protected void setUserWorkspaceRootACL(DocumentModel doc) {
060        ACP acp = new ACPImpl();
061        ACE denyEverything = new ACE(SecurityConstants.EVERYONE, SecurityConstants.EVERYTHING, false);
062        ACL acl = new ACLImpl();
063        acl.setACEs(new ACE[] { denyEverything });
064        acp.addACL(acl);
065        doc.setACP(acp, true);
066    }
067
068    protected void setUserWorkspaceACL(DocumentModel doc, String userName) {
069        ACP acp = new ACPImpl();
070        ACE grantEverything = new ACE(userName, SecurityConstants.EVERYTHING, true);
071        ACL acl = new ACLImpl();
072        acl.setACEs(new ACE[] { grantEverything });
073        acp.addACL(acl);
074        doc.setACP(acp, true);
075    }
076
077    protected DocumentModel doCreateUserWorkspacesRoot(CoreSession unrestrictedSession, PathRef rootRef)
078            {
079
080        String parentPath = new Path(rootRef.toString()).removeLastSegments(1).toString();
081        DocumentModel doc = unrestrictedSession.createDocumentModel(parentPath,
082                UserWorkspaceConstants.USERS_PERSONAL_WORKSPACES_ROOT, getUserWorkspaceRootType());
083        doc.setProperty("dublincore", "title", UserWorkspaceConstants.USERS_PERSONAL_WORKSPACES_ROOT);
084        doc.setProperty("dublincore", "description", "");
085        doc = unrestrictedSession.createDocument(doc);
086
087        setUserWorkspaceRootACL(doc);
088
089        return doc;
090    }
091
092    protected DocumentModel doCreateUserWorkspace(CoreSession unrestrictedSession, PathRef wsRef, Principal principal,
093            String userName) {
094
095        String parentPath = new Path(wsRef.toString()).removeLastSegments(1).toString();
096        String wsName = new Path(wsRef.toString()).lastSegment();
097        DocumentModel doc = unrestrictedSession.createDocumentModel(parentPath, wsName, getUserWorkspaceType());
098
099        doc.setProperty("dublincore", "title", buildUserWorkspaceTitle(principal, userName));
100        doc.setProperty("dublincore", "description", "");
101        doc = unrestrictedSession.createDocument(doc);
102
103        setUserWorkspaceACL(doc, userName);
104
105        /**
106         * @since 5.7
107         */
108        Map<String, Serializable> properties = new HashMap<String, Serializable>();
109        properties.put("username", userName);
110        notifyEvent(unrestrictedSession, doc, (NuxeoPrincipal) unrestrictedSession.getPrincipal(),
111                DocumentEventTypes.USER_WORKSPACE_CREATED, properties);
112        return doc;
113    }
114
115}