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