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