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.util.HashMap;
026import java.util.Map;
027
028import org.nuxeo.common.utils.Path;
029import org.nuxeo.ecm.core.api.CoreSession;
030import org.nuxeo.ecm.core.api.DocumentModel;
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    @Override
060    protected DocumentModel doCreateUserWorkspacesRoot(CoreSession unrestrictedSession, PathRef rootRef) {
061        String parentPath = new Path(rootRef.toString()).removeLastSegments(1).toString();
062        DocumentModel doc = unrestrictedSession.createDocumentModel(parentPath,
063                UserWorkspaceConstants.USERS_PERSONAL_WORKSPACES_ROOT, getUserWorkspaceRootType());
064        doc.setProperty("dublincore", "title", UserWorkspaceConstants.USERS_PERSONAL_WORKSPACES_ROOT);
065        doc.setProperty("dublincore", "description", "");
066
067        return doc;
068    }
069
070    @Override
071    protected DocumentModel initCreateUserWorkspacesRoot(CoreSession unrestrictedSession, DocumentModel doc) {
072        ACP acp = new ACPImpl();
073        ACE denyEverything = new ACE(SecurityConstants.EVERYONE, SecurityConstants.EVERYTHING, false);
074        ACL acl = new ACLImpl();
075        acl.setACEs(new ACE[] { denyEverything });
076        acp.addACL(acl);
077
078        doc.setACP(acp, true);
079        return doc;
080    }
081
082    @Override
083    protected DocumentModel doCreateUserWorkspace(CoreSession unrestrictedSession, PathRef wsRef, String userName) {
084        String parentPath = new Path(wsRef.toString()).removeLastSegments(1).toString();
085        String wsName = new Path(wsRef.toString()).lastSegment();
086        DocumentModel doc = unrestrictedSession.createDocumentModel(parentPath, wsName, getUserWorkspaceType());
087        doc.setProperty("dublincore", "title", buildUserWorkspaceTitle(userName));
088        doc.setProperty("dublincore", "description", "");
089
090        return doc;
091    }
092
093    @Override
094    protected DocumentModel initCreateUserWorkspace(CoreSession unrestrictedSession, DocumentModel doc,
095            String username) {
096        ACP acp = new ACPImpl();
097        ACE grantEverything = new ACE(username, SecurityConstants.EVERYTHING, true);
098        ACL acl = new ACLImpl();
099        acl.setACEs(new ACE[] { grantEverything });
100        acp.addACL(acl);
101
102        doc.setACP(acp, true);
103
104        Map<String, Serializable> properties = new HashMap<>();
105        properties.put("username", username);
106        notifyEvent(unrestrictedSession, doc, unrestrictedSession.getPrincipal(),
107                DocumentEventTypes.USER_WORKSPACE_CREATED, properties);
108
109        return doc;
110    }
111
112}