001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.repository.cmis.model;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.repository.RepositoryException;
020    import com.liferay.portal.kernel.repository.model.Folder;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.model.CompanyConstants;
024    import com.liferay.portal.model.User;
025    import com.liferay.portal.repository.cmis.CMISRepository;
026    import com.liferay.portal.security.permission.ActionKeys;
027    import com.liferay.portal.service.CompanyLocalServiceUtil;
028    import com.liferay.portal.service.UserLocalServiceUtil;
029    import com.liferay.portlet.expando.model.ExpandoBridge;
030    import com.liferay.portlet.expando.util.ExpandoBridgeFactoryUtil;
031    
032    import java.util.HashMap;
033    import java.util.HashSet;
034    import java.util.Map;
035    import java.util.Set;
036    
037    import org.apache.chemistry.opencmis.client.api.CmisObject;
038    import org.apache.chemistry.opencmis.commons.data.AllowableActions;
039    import org.apache.chemistry.opencmis.commons.enums.Action;
040    
041    /**
042     * @author Alexander Chow
043     */
044    public abstract class CMISModel {
045    
046            public abstract long getCompanyId();
047    
048            public String getDescription() {
049                    return StringPool.BLANK;
050            }
051    
052            public ExpandoBridge getExpandoBridge() {
053                    return ExpandoBridgeFactoryUtil.getExpandoBridge(
054                            getCompanyId(), getModelClassName(), getPrimaryKey());
055            }
056    
057            public abstract String getModelClassName();
058    
059            public abstract long getPrimaryKey();
060    
061            public void setParentFolder(Folder parentFolder) {
062                    _parentFolder = parentFolder;
063            }
064    
065            protected boolean containsPermission(CmisObject cmisObject, String actionId)
066                    throws RepositoryException {
067    
068                    CMISRepository cmisRepository = getCmisRepository();
069    
070                    if (cmisRepository.isRefreshBeforePermissionCheck()) {
071                            cmisObject.refresh();
072                    }
073    
074                    if (_unsupportedActionKeys.contains(actionId)) {
075                            return false;
076                    }
077    
078                    Action action = _mappedActionKeys.get(actionId);
079    
080                    if (action == null) {
081                            throw new RepositoryException(
082                                    "Unexpected permission action " + actionId);
083                    }
084    
085                    AllowableActions allowableActions = cmisObject.getAllowableActions();
086    
087                    Set<Action> allowableActionsSet =
088                            allowableActions.getAllowableActions();
089    
090                    return allowableActionsSet.contains(action);
091            }
092    
093            protected abstract CMISRepository getCmisRepository();
094    
095            @SuppressWarnings("unused")
096            protected Folder getParentFolder() throws PortalException, SystemException {
097                    return _parentFolder;
098            }
099    
100            protected User getUser(String createdBy) {
101                    User user = null;
102    
103                    try {
104                            String authType = CompanyLocalServiceUtil.getCompany(
105                                    getCompanyId()).getAuthType();
106    
107                            if (authType.equals(CompanyConstants.AUTH_TYPE_ID)) {
108                                    user = UserLocalServiceUtil.getUser(
109                                            GetterUtil.getLong(createdBy));
110                            }
111                            else if (authType.equals(CompanyConstants.AUTH_TYPE_EA)) {
112                                    user = UserLocalServiceUtil.getUserByEmailAddress(
113                                            getCompanyId(), createdBy);
114                            }
115                            else if (authType.equals(CompanyConstants.AUTH_TYPE_SN)) {
116                                    user = UserLocalServiceUtil.getUserByScreenName(
117                                            getCompanyId(), createdBy);
118                            }
119                    }
120                    catch (Exception e) {
121                    }
122    
123                    if (user == null) {
124                            try {
125                                    user = UserLocalServiceUtil.getDefaultUser(getCompanyId());
126                            }
127                            catch (Exception e) {
128                            }
129                    }
130    
131                    return user;
132            }
133    
134            private static Map<String, Action> _mappedActionKeys =
135                    new HashMap<String, Action>();
136            private static Set<String> _unsupportedActionKeys = new HashSet<String>();
137    
138            private Folder _parentFolder;
139    
140            static {
141                    _mappedActionKeys.put(ActionKeys.ACCESS, Action.CAN_GET_FOLDER_TREE);
142                    _mappedActionKeys.put(
143                            ActionKeys.ADD_DOCUMENT, Action.CAN_CREATE_DOCUMENT);
144                    _mappedActionKeys.put(ActionKeys.ADD_FOLDER, Action.CAN_CREATE_FOLDER);
145                    _mappedActionKeys.put(
146                            ActionKeys.ADD_SUBFOLDER, Action.CAN_CREATE_FOLDER);
147                    _mappedActionKeys.put(ActionKeys.DELETE, Action.CAN_DELETE_OBJECT);
148                    _mappedActionKeys.put(ActionKeys.UPDATE, Action.CAN_UPDATE_PROPERTIES);
149                    _mappedActionKeys.put(ActionKeys.VIEW, Action.CAN_GET_PROPERTIES);
150    
151                    _unsupportedActionKeys.add(ActionKeys.ADD_DISCUSSION);
152                    _unsupportedActionKeys.add(ActionKeys.ADD_SHORTCUT);
153                    _unsupportedActionKeys.add(ActionKeys.DELETE_DISCUSSION);
154                    _unsupportedActionKeys.add(ActionKeys.PERMISSIONS);
155                    _unsupportedActionKeys.add(ActionKeys.UPDATE_DISCUSSION);
156            }
157    
158    }