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.Company;
024    import com.liferay.portal.model.CompanyConstants;
025    import com.liferay.portal.model.User;
026    import com.liferay.portal.repository.cmis.CMISRepository;
027    import com.liferay.portal.security.permission.ActionKeys;
028    import com.liferay.portal.service.CompanyLocalServiceUtil;
029    import com.liferay.portal.service.UserLocalServiceUtil;
030    import com.liferay.portlet.expando.model.ExpandoBridge;
031    import com.liferay.portlet.expando.util.ExpandoBridgeFactoryUtil;
032    
033    import java.util.HashMap;
034    import java.util.HashSet;
035    import java.util.Map;
036    import java.util.Set;
037    
038    import org.apache.chemistry.opencmis.client.api.CmisObject;
039    import org.apache.chemistry.opencmis.commons.data.AllowableActions;
040    import org.apache.chemistry.opencmis.commons.enums.Action;
041    
042    /**
043     * @author Alexander Chow
044     */
045    public abstract class CMISModel {
046    
047            public abstract long getCompanyId();
048    
049            public String getDescription() {
050                    return StringPool.BLANK;
051            }
052    
053            public ExpandoBridge getExpandoBridge() {
054                    return ExpandoBridgeFactoryUtil.getExpandoBridge(
055                            getCompanyId(), getModelClassName(), getPrimaryKey());
056            }
057    
058            public abstract String getModelClassName();
059    
060            public abstract long getPrimaryKey();
061    
062            public void setParentFolder(Folder parentFolder) {
063                    _parentFolder = parentFolder;
064            }
065    
066            protected boolean containsPermission(CmisObject cmisObject, String actionId)
067                    throws RepositoryException {
068    
069                    CMISRepository cmisRepository = getCmisRepository();
070    
071                    if (cmisRepository.isRefreshBeforePermissionCheck()) {
072                            cmisObject.refresh();
073                    }
074    
075                    if (_unsupportedActionKeys.contains(actionId)) {
076                            return false;
077                    }
078    
079                    Action action = _mappedActionKeys.get(actionId);
080    
081                    if (action == null) {
082                            throw new RepositoryException(
083                                    "Unexpected permission action " + actionId);
084                    }
085    
086                    AllowableActions allowableActions = cmisObject.getAllowableActions();
087    
088                    Set<Action> allowableActionsSet =
089                            allowableActions.getAllowableActions();
090    
091                    return allowableActionsSet.contains(action);
092            }
093    
094            protected abstract CMISRepository getCmisRepository();
095    
096            @SuppressWarnings("unused")
097            protected Folder getParentFolder() throws PortalException, SystemException {
098                    return _parentFolder;
099            }
100    
101            protected User getUser(String createdBy) {
102                    User user = null;
103    
104                    try {
105                            Company company = CompanyLocalServiceUtil.getCompany(
106                                    getCompanyId());
107    
108                            String authType = company.getAuthType();
109    
110                            if (authType.equals(CompanyConstants.AUTH_TYPE_ID)) {
111                                    user = UserLocalServiceUtil.getUser(
112                                            GetterUtil.getLong(createdBy));
113                            }
114                            else if (authType.equals(CompanyConstants.AUTH_TYPE_EA)) {
115                                    user = UserLocalServiceUtil.getUserByEmailAddress(
116                                            getCompanyId(), createdBy);
117                            }
118                            else if (authType.equals(CompanyConstants.AUTH_TYPE_SN)) {
119                                    user = UserLocalServiceUtil.getUserByScreenName(
120                                            getCompanyId(), createdBy);
121                            }
122                    }
123                    catch (Exception e) {
124                    }
125    
126                    if (user == null) {
127                            try {
128                                    user = UserLocalServiceUtil.getDefaultUser(getCompanyId());
129                            }
130                            catch (Exception e) {
131                            }
132                    }
133    
134                    return user;
135            }
136    
137            private static Map<String, Action> _mappedActionKeys =
138                    new HashMap<String, Action>();
139            private static Set<String> _unsupportedActionKeys = new HashSet<String>();
140    
141            private Folder _parentFolder;
142    
143            static {
144                    _mappedActionKeys.put(ActionKeys.ACCESS, Action.CAN_GET_FOLDER_TREE);
145                    _mappedActionKeys.put(
146                            ActionKeys.ADD_DOCUMENT, Action.CAN_CREATE_DOCUMENT);
147                    _mappedActionKeys.put(ActionKeys.ADD_FOLDER, Action.CAN_CREATE_FOLDER);
148                    _mappedActionKeys.put(
149                            ActionKeys.ADD_SUBFOLDER, Action.CAN_CREATE_FOLDER);
150                    _mappedActionKeys.put(ActionKeys.DELETE, Action.CAN_DELETE_OBJECT);
151                    _mappedActionKeys.put(ActionKeys.UPDATE, Action.CAN_UPDATE_PROPERTIES);
152                    _mappedActionKeys.put(ActionKeys.VIEW, Action.CAN_GET_PROPERTIES);
153    
154                    _unsupportedActionKeys.add(ActionKeys.ADD_DISCUSSION);
155                    _unsupportedActionKeys.add(ActionKeys.ADD_SHORTCUT);
156                    _unsupportedActionKeys.add(ActionKeys.DELETE_DISCUSSION);
157                    _unsupportedActionKeys.add(ActionKeys.PERMISSIONS);
158                    _unsupportedActionKeys.add(ActionKeys.UPDATE_DISCUSSION);
159            }
160    
161    }