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