001    /**
002     * Copyright (c) 2000-present 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.repository.RepositoryException;
019    import com.liferay.portal.kernel.repository.model.Folder;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.model.Company;
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 {
097                    return _parentFolder;
098            }
099    
100            protected User getUser(String createdBy) {
101                    User user = null;
102    
103                    try {
104                            Company company = CompanyLocalServiceUtil.getCompany(
105                                    getCompanyId());
106    
107                            String authType = company.getAuthType();
108    
109                            if (authType.equals(CompanyConstants.AUTH_TYPE_ID)) {
110                                    user = UserLocalServiceUtil.getUser(
111                                            GetterUtil.getLong(createdBy));
112                            }
113                            else if (authType.equals(CompanyConstants.AUTH_TYPE_EA)) {
114                                    user = UserLocalServiceUtil.getUserByEmailAddress(
115                                            getCompanyId(), createdBy);
116                            }
117                            else if (authType.equals(CompanyConstants.AUTH_TYPE_SN)) {
118                                    user = UserLocalServiceUtil.getUserByScreenName(
119                                            getCompanyId(), createdBy);
120                            }
121                    }
122                    catch (Exception e) {
123                    }
124    
125                    if (user == null) {
126                            try {
127                                    user = UserLocalServiceUtil.getDefaultUser(getCompanyId());
128                            }
129                            catch (Exception e) {
130                            }
131                    }
132    
133                    return user;
134            }
135    
136            private static final Map<String, Action> _mappedActionKeys =
137                    new HashMap<String, Action>();
138            private static final Set<String> _unsupportedActionKeys =
139                    new HashSet<String>();
140    
141            static {
142                    _mappedActionKeys.put(ActionKeys.ACCESS, Action.CAN_GET_FOLDER_TREE);
143                    _mappedActionKeys.put(
144                            ActionKeys.ADD_DOCUMENT, Action.CAN_CREATE_DOCUMENT);
145                    _mappedActionKeys.put(ActionKeys.ADD_FOLDER, Action.CAN_CREATE_FOLDER);
146                    _mappedActionKeys.put(
147                            ActionKeys.ADD_SUBFOLDER, Action.CAN_CREATE_FOLDER);
148                    _mappedActionKeys.put(ActionKeys.DELETE, Action.CAN_DELETE_OBJECT);
149                    _mappedActionKeys.put(ActionKeys.UPDATE, Action.CAN_UPDATE_PROPERTIES);
150                    _mappedActionKeys.put(ActionKeys.VIEW, Action.CAN_GET_PROPERTIES);
151    
152                    _unsupportedActionKeys.add(ActionKeys.ADD_DISCUSSION);
153                    _unsupportedActionKeys.add(ActionKeys.ADD_SHORTCUT);
154                    _unsupportedActionKeys.add(ActionKeys.DELETE_DISCUSSION);
155                    _unsupportedActionKeys.add(ActionKeys.PERMISSIONS);
156                    _unsupportedActionKeys.add(ActionKeys.SUBSCRIBE);
157                    _unsupportedActionKeys.add(ActionKeys.UPDATE_DISCUSSION);
158            }
159    
160            private Folder _parentFolder;
161    
162    }