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