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.service.permission;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.util.Validator;
019    import com.liferay.portal.kernel.workflow.WorkflowInstance;
020    import com.liferay.portal.model.Layout;
021    import com.liferay.portal.security.auth.PrincipalException;
022    import com.liferay.portal.security.permission.ActionKeys;
023    import com.liferay.portal.security.permission.PermissionChecker;
024    import com.liferay.portal.security.permission.PermissionCheckerUtil;
025    import com.liferay.portal.util.PortletKeys;
026    import com.liferay.portlet.messageboards.model.MBDiscussion;
027    import com.liferay.portlet.messageboards.model.MBThread;
028    import com.liferay.portlet.messageboards.service.MBDiscussionLocalServiceUtil;
029    import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
030    import com.liferay.portlet.messageboards.service.permission.MBDiscussionPermission;
031    
032    /**
033     * @author Mate Thurzo
034     * @author Raymond Aug??
035     */
036    public class SubscriptionPermissionImpl implements SubscriptionPermission {
037    
038            /**
039             * @deprecated As of 6.2.0, replaced by {@link #check(PermissionChecker,
040             *             String, long, String, long)}
041             */
042            @Deprecated
043            @Override
044            public void check(
045                            PermissionChecker permissionChecker, String className, long classPK)
046                    throws PortalException {
047    
048                    check(permissionChecker, className, classPK, null, 0);
049            }
050    
051            @Override
052            public void check(
053                            PermissionChecker permissionChecker, String subscriptionClassName,
054                            long subscriptionClassPK, String inferredClassName,
055                            long inferredClassPK)
056                    throws PortalException {
057    
058                    if (!contains(
059                                    permissionChecker, subscriptionClassName, subscriptionClassPK,
060                                    inferredClassName, inferredClassPK)) {
061    
062                            throw new PrincipalException();
063                    }
064            }
065    
066            /**
067             * @deprecated As of 6.2.0, replaced by {@link #contains(PermissionChecker,
068             *             String, long, String, long)}
069             */
070            @Deprecated
071            @Override
072            public boolean contains(
073                            PermissionChecker permissionChecker, String className, long classPK)
074                    throws PortalException {
075    
076                    return contains(permissionChecker, className, classPK, null, 0);
077            }
078    
079            @Override
080            public boolean contains(
081                            PermissionChecker permissionChecker, String subscriptionClassName,
082                            long subscriptionClassPK, String inferredClassName,
083                            long inferredClassPK)
084                    throws PortalException {
085    
086                    if (subscriptionClassName == null) {
087                            return false;
088                    }
089    
090                    if (Validator.isNotNull(inferredClassName)) {
091                            Boolean hasPermission = hasPermission(
092                                    permissionChecker, inferredClassName, inferredClassPK,
093                                    ActionKeys.VIEW);
094    
095                            if ((hasPermission == null) || !hasPermission) {
096                                    return false;
097                            }
098                    }
099    
100                    Boolean hasPermission = hasPermission(
101                            permissionChecker, subscriptionClassName, subscriptionClassPK,
102                            ActionKeys.SUBSCRIBE);
103    
104                    if (hasPermission != null) {
105                            return hasPermission;
106                    }
107    
108                    return true;
109            }
110    
111            protected Boolean hasPermission(
112                            PermissionChecker permissionChecker, String className, long classPK,
113                            String actionId)
114                    throws PortalException {
115    
116                    MBDiscussion mbDiscussion =
117                            MBDiscussionLocalServiceUtil.fetchDiscussion(className, classPK);
118    
119                    if (mbDiscussion != null) {
120                            if (className.equals(Layout.class.getName())) {
121                                    return LayoutPermissionUtil.contains(
122                                            permissionChecker, classPK, ActionKeys.VIEW);
123                            }
124    
125                            MBThread mbThread = MBThreadLocalServiceUtil.fetchThread(
126                                    mbDiscussion.getThreadId());
127    
128                            if (className.equals(WorkflowInstance.class.getName())) {
129                                    return permissionChecker.hasPermission(
130                                            mbThread.getGroupId(), PortletKeys.WORKFLOW_DEFINITIONS,
131                                            mbThread.getGroupId(), ActionKeys.VIEW);
132                            }
133    
134                            return MBDiscussionPermission.contains(
135                                    permissionChecker, mbThread.getCompanyId(),
136                                    mbThread.getGroupId(), className, classPK, mbThread.getUserId(),
137                                    ActionKeys.VIEW);
138                    }
139    
140                    return PermissionCheckerUtil.containsResourcePermission(
141                            permissionChecker, className, classPK, actionId);
142            }
143    
144    }