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.portlet.messageboards.security.permission;
016    
017    import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
018    import com.liferay.portal.kernel.dao.orm.DynamicQuery;
019    import com.liferay.portal.kernel.dao.orm.Property;
020    import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
021    import com.liferay.portal.kernel.exception.PortalException;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.kernel.workflow.WorkflowConstants;
024    import com.liferay.portal.security.permission.BasePermissionPropagator;
025    import com.liferay.portlet.messageboards.model.MBCategory;
026    import com.liferay.portlet.messageboards.model.MBMessage;
027    import com.liferay.portlet.messageboards.model.MBThread;
028    import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
029    import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
030    
031    import java.util.ArrayList;
032    import java.util.List;
033    
034    import javax.portlet.ActionRequest;
035    
036    /**
037     * @author Kenneth Chang
038     * @author Hugo Huijser
039     */
040    public class MBPermissionPropagatorImpl extends BasePermissionPropagator {
041    
042            @Override
043            public void propagateRolePermissions(
044                            ActionRequest actionRequest, String className, String primKey,
045                            long[] roleIds)
046                    throws PortalException {
047    
048                    if (className.equals(MBCategory.class.getName())) {
049                            propagateCategoryRolePermissions(
050                                    actionRequest, className, primKey, roleIds);
051                    }
052                    else if (className.equals(MBMessage.class.getName())) {
053                            long messageId = GetterUtil.getLong(primKey);
054    
055                            MBMessage message = MBMessageLocalServiceUtil.getMessage(messageId);
056    
057                            if (message.isRoot()) {
058                                    propagateThreadRolePermissions(
059                                            actionRequest, className, messageId, message.getThreadId(),
060                                            roleIds);
061                            }
062                    }
063                    else if (className.equals("com.liferay.portlet.messageboards")) {
064                            propagateMBRolePermissions(
065                                    actionRequest, className, primKey, roleIds);
066                    }
067            }
068    
069            protected void propagateCategoryRolePermissions(
070                            ActionRequest actionRequest, String className, long primaryKey,
071                            long categoryId, long[] roleIds)
072                    throws PortalException {
073    
074                    for (long roleId : roleIds) {
075                            propagateRolePermissions(
076                                    actionRequest, roleId, className, primaryKey,
077                                    MBCategory.class.getName(), categoryId);
078                    }
079            }
080    
081            protected void propagateCategoryRolePermissions(
082                            final ActionRequest actionRequest, final String className,
083                            String primKey, final long[] roleIds)
084                    throws PortalException {
085    
086                    final long categoryId = GetterUtil.getLong(primKey);
087    
088                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(
089                            categoryId);
090    
091                    List<Object> categoriesAndThreads =
092                            MBCategoryLocalServiceUtil.getCategoriesAndThreads(
093                                    category.getGroupId(), categoryId);
094    
095                    for (Object categoryOrThread : categoriesAndThreads) {
096                            if (categoryOrThread instanceof MBThread) {
097                                    MBThread thread = (MBThread)categoryOrThread;
098    
099                                    List<MBMessage> messages =
100                                            MBMessageLocalServiceUtil.getThreadMessages(
101                                                    thread.getThreadId(), WorkflowConstants.STATUS_ANY);
102    
103                                    for (MBMessage message : messages) {
104                                            propagateMessageRolePermissions(
105                                                    actionRequest, className, categoryId,
106                                                    message.getMessageId(), roleIds);
107                                    }
108                            }
109                            else {
110                                    category = (MBCategory)categoryOrThread;
111    
112                                    List<Long> categoryIds = new ArrayList<>();
113    
114                                    categoryIds.add(category.getCategoryId());
115    
116                                    categoryIds = MBCategoryLocalServiceUtil.getSubcategoryIds(
117                                            categoryIds, category.getGroupId(),
118                                            category.getCategoryId());
119    
120                                    for (final long addCategoryId : categoryIds) {
121                                            propagateCategoryRolePermissions(
122                                                    actionRequest, className, categoryId, addCategoryId,
123                                                    roleIds);
124    
125                                            ActionableDynamicQuery actionableDynamicQuery =
126                                                    MBMessageLocalServiceUtil.getActionableDynamicQuery();
127    
128                                            actionableDynamicQuery.setAddCriteriaMethod(
129                                                    new ActionableDynamicQuery.AddCriteriaMethod() {
130    
131                                                            @Override
132                                                            public void addCriteria(DynamicQuery dynamicQuery) {
133                                                                    Property categoryIdProperty =
134                                                                            PropertyFactoryUtil.forName("categoryId");
135    
136                                                                    dynamicQuery.add(
137                                                                            categoryIdProperty.eq(addCategoryId));
138                                                            }
139    
140                                                    });
141                                            actionableDynamicQuery.setGroupId(category.getGroupId());
142                                            actionableDynamicQuery.setPerformActionMethod(
143                                                    new ActionableDynamicQuery.PerformActionMethod() {
144    
145                                                            @Override
146                                                            public void performAction(Object object)
147                                                                    throws PortalException {
148    
149                                                                    MBMessage message = (MBMessage)object;
150    
151                                                                    propagateMessageRolePermissions(
152                                                                            actionRequest, className, categoryId,
153                                                                            message.getMessageId(), roleIds);
154                                                            }
155    
156                                                    });
157    
158                                            actionableDynamicQuery.performActions();
159                                    }
160                            }
161                    }
162            }
163    
164            protected void propagateMBRolePermissions(
165                            final ActionRequest actionRequest, final String className,
166                            String primKey, final long[] roleIds)
167                    throws PortalException {
168    
169                    final long groupId = GetterUtil.getLong(primKey);
170    
171                    List<MBCategory> categories = MBCategoryLocalServiceUtil.getCategories(
172                            groupId);
173    
174                    for (MBCategory category : categories) {
175                            propagateCategoryRolePermissions(
176                                    actionRequest, className, groupId, category.getCategoryId(),
177                                    roleIds);
178                    }
179    
180                    ActionableDynamicQuery actionableDynamicQuery =
181                            MBMessageLocalServiceUtil.getActionableDynamicQuery();
182    
183                    actionableDynamicQuery.setGroupId(groupId);
184                    actionableDynamicQuery.setPerformActionMethod(
185                            new ActionableDynamicQuery.PerformActionMethod() {
186    
187                                    @Override
188                                    public void performAction(Object object)
189                                            throws PortalException {
190    
191                                            MBMessage message = (MBMessage)object;
192    
193                                            propagateMessageRolePermissions(
194                                                    actionRequest, className, groupId,
195                                                    message.getMessageId(), roleIds);
196                                    }
197    
198                            });
199    
200                    actionableDynamicQuery.performActions();
201            }
202    
203            protected void propagateMessageRolePermissions(
204                            ActionRequest actionRequest, String className, long primaryKey,
205                            long messageId, long[] roleIds)
206                    throws PortalException {
207    
208                    for (long roleId : roleIds) {
209                            propagateRolePermissions(
210                                    actionRequest, roleId, className, primaryKey,
211                                    MBMessage.class.getName(), messageId);
212                    }
213            }
214    
215            protected void propagateThreadRolePermissions(
216                            ActionRequest actionRequest, String className, long messageId,
217                            long threadId, long[] roleIds)
218                    throws PortalException {
219    
220                    List<MBMessage> messages = MBMessageLocalServiceUtil.getThreadMessages(
221                            threadId, WorkflowConstants.STATUS_ANY);
222    
223                    for (MBMessage message : messages) {
224                            propagateMessageRolePermissions(
225                                    actionRequest, className, messageId, message.getMessageId(),
226                                    roleIds);
227                    }
228            }
229    
230    }