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.announcements.util;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.ListUtil;
021    import com.liferay.portal.kernel.util.PropsKeys;
022    import com.liferay.portal.kernel.util.PropsUtil;
023    import com.liferay.portal.kernel.util.UniqueList;
024    import com.liferay.portal.model.Group;
025    import com.liferay.portal.model.Organization;
026    import com.liferay.portal.model.Role;
027    import com.liferay.portal.model.RoleConstants;
028    import com.liferay.portal.model.Team;
029    import com.liferay.portal.model.User;
030    import com.liferay.portal.model.UserGroup;
031    import com.liferay.portal.security.permission.ActionKeys;
032    import com.liferay.portal.service.GroupLocalServiceUtil;
033    import com.liferay.portal.service.OrganizationLocalServiceUtil;
034    import com.liferay.portal.service.RoleLocalServiceUtil;
035    import com.liferay.portal.service.TeamLocalServiceUtil;
036    import com.liferay.portal.service.UserGroupLocalServiceUtil;
037    import com.liferay.portal.service.UserLocalServiceUtil;
038    import com.liferay.portal.service.permission.GroupPermissionUtil;
039    import com.liferay.portal.service.permission.OrganizationPermissionUtil;
040    import com.liferay.portal.service.permission.RolePermissionUtil;
041    import com.liferay.portal.service.permission.UserGroupPermissionUtil;
042    import com.liferay.portal.theme.ThemeDisplay;
043    import com.liferay.portal.util.PortalUtil;
044    
045    import java.util.ArrayList;
046    import java.util.LinkedHashMap;
047    import java.util.List;
048    
049    /**
050     * @author Raymond Aug??
051     */
052    public class AnnouncementsUtil {
053    
054            public static LinkedHashMap<Long, long[]> getAnnouncementScopes(long userId)
055                    throws PortalException, SystemException {
056    
057                    LinkedHashMap<Long, long[]> scopes = new LinkedHashMap<Long, long[]>();
058    
059                    // General announcements
060    
061                    scopes.put(new Long(0), new long[] {0});
062    
063                    // Personal announcements
064    
065                    scopes.put(_USER_CLASS_NAME_ID, new long[] {userId});
066    
067                    // Organization announcements
068    
069                    List<Group> groupsList = new ArrayList<Group>();
070    
071                    List<Organization> organizations =
072                            OrganizationLocalServiceUtil.getUserOrganizations(userId);
073    
074                    if (!organizations.isEmpty()) {
075                            List<Organization> organizationsList =
076                                    new ArrayList<Organization>();
077    
078                            organizationsList.addAll(organizations);
079    
080                            for (Organization organization : organizations) {
081                                    groupsList.add(organization.getGroup());
082    
083                                    List<Organization> parentOrganizations =
084                                            OrganizationLocalServiceUtil.getParentOrganizations(
085                                                    organization.getOrganizationId());
086    
087                                    for (Organization parentOrganization : parentOrganizations) {
088                                            organizationsList.add(parentOrganization);
089                                            groupsList.add(parentOrganization.getGroup());
090                                    }
091                            }
092    
093                            scopes.put(
094                                    _ORGANIZATION_CLASS_NAME_ID,
095                                    _getOrganizationIds(organizationsList));
096                    }
097    
098                    // Site announcements
099    
100                    List<Group> groups = GroupLocalServiceUtil.getUserGroups(userId, true);
101    
102                    if (!groups.isEmpty()) {
103                            scopes.put(_GROUP_CLASS_NAME_ID, _getGroupIds(groups));
104    
105                            groupsList.addAll(groups);
106                    }
107    
108                    // User group announcements
109    
110                    List<UserGroup> userGroups =
111                            UserGroupLocalServiceUtil.getUserUserGroups(userId);
112    
113                    if (!userGroups.isEmpty()) {
114                            scopes.put(_USER_GROUP_CLASS_NAME_ID, _getUserGroupIds(userGroups));
115    
116                            for (UserGroup userGroup : userGroups) {
117                                    groupsList.add(userGroup.getGroup());
118                            }
119                    }
120    
121                    // Role announcements
122    
123                    List<Role> roles = new UniqueList<Role>();
124    
125                    if (!groupsList.isEmpty()) {
126                            roles = RoleLocalServiceUtil.getUserRelatedRoles(
127                                    userId, groupsList);
128    
129                            roles = ListUtil.copy(roles);
130    
131                            for (Group group : groupsList) {
132                                    roles.addAll(
133                                            RoleLocalServiceUtil.getUserGroupRoles(
134                                                    userId, group.getGroupId()));
135                                    roles.addAll(
136                                            RoleLocalServiceUtil.getUserGroupGroupRoles(
137                                                    userId, group.getGroupId()));
138                            }
139                    }
140                    else {
141                            roles = RoleLocalServiceUtil.getUserRoles(userId);
142    
143                            roles = ListUtil.copy(roles);
144                    }
145    
146                    List<Team> teams = TeamLocalServiceUtil.getUserTeams(userId);
147    
148                    for (Team team : teams) {
149                            roles.add(team.getRole());
150                    }
151    
152                    if (_PERMISSIONS_CHECK_GUEST_ENABLED) {
153                            User user = UserLocalServiceUtil.getUserById(userId);
154    
155                            Role guestRole = RoleLocalServiceUtil.getRole(
156                                    user.getCompanyId(), RoleConstants.GUEST);
157    
158                            roles.add(guestRole);
159                    }
160    
161                    if (roles.size() > 0) {
162                            scopes.put(_ROLE_CLASS_NAME_ID, _getRoleIds(roles));
163                    }
164    
165                    return scopes;
166            }
167    
168            public static List<Group> getGroups(ThemeDisplay themeDisplay)
169                    throws Exception {
170    
171                    List<Group> filteredGroups = new ArrayList<Group>();
172    
173                    List<Group> groups = GroupLocalServiceUtil.getUserGroups(
174                            themeDisplay.getUserId(), true);
175    
176                    for (Group group : groups) {
177                            if (((group.isOrganization() && group.isSite()) ||
178                                     group.isRegularSite()) &&
179                                    GroupPermissionUtil.contains(
180                                            themeDisplay.getPermissionChecker(), group.getGroupId(),
181                                            ActionKeys.MANAGE_ANNOUNCEMENTS)) {
182    
183                                    filteredGroups.add(group);
184                            }
185                    }
186    
187                    return filteredGroups;
188            }
189    
190            public static List<Organization> getOrganizations(ThemeDisplay themeDisplay)
191                    throws Exception {
192    
193                    List<Organization> filteredOrganizations =
194                            new ArrayList<Organization>();
195    
196                    List<Organization> organizations =
197                            OrganizationLocalServiceUtil.getUserOrganizations(
198                                    themeDisplay.getUserId());
199    
200                    for (Organization organization : organizations) {
201                            if (OrganizationPermissionUtil.contains(
202                                            themeDisplay.getPermissionChecker(),
203                                            organization.getOrganizationId(),
204                                            ActionKeys.MANAGE_ANNOUNCEMENTS)) {
205    
206                                    filteredOrganizations.add(organization);
207                            }
208                    }
209    
210                    return filteredOrganizations;
211            }
212    
213            public static List<Role> getRoles(ThemeDisplay themeDisplay)
214                    throws Exception {
215    
216                    List<Role> filteredRoles = new ArrayList<Role>();
217    
218                    List<Role> roles = RoleLocalServiceUtil.getRoles(
219                            themeDisplay.getCompanyId());
220    
221                    for (Role role : roles) {
222                            if (role.isTeam()) {
223                                    Team team = TeamLocalServiceUtil.getTeam(role.getClassPK());
224    
225                                    if (GroupPermissionUtil.contains(
226                                                    themeDisplay.getPermissionChecker(), team.getGroupId(),
227                                                    ActionKeys.MANAGE_ANNOUNCEMENTS) ||
228                                            RolePermissionUtil.contains(
229                                                    themeDisplay.getPermissionChecker(),
230                                                    themeDisplay.getScopeGroupId(), role.getRoleId(),
231                                                    ActionKeys.MANAGE_ANNOUNCEMENTS)) {
232    
233                                            filteredRoles.add(role);
234                                    }
235                            }
236                            else if (RolePermissionUtil.contains(
237                                                    themeDisplay.getPermissionChecker(), role.getRoleId(),
238                                                    ActionKeys.MANAGE_ANNOUNCEMENTS)) {
239    
240                                    filteredRoles.add(role);
241                            }
242                    }
243    
244                    return filteredRoles;
245            }
246    
247            public static List<UserGroup> getUserGroups(ThemeDisplay themeDisplay)
248                    throws Exception {
249    
250                    List<UserGroup> filteredUserGroups = new ArrayList<UserGroup>();
251    
252                    List<UserGroup> userGroups = UserGroupLocalServiceUtil.getUserGroups(
253                            themeDisplay.getCompanyId());
254    
255                    for (UserGroup userGroup : userGroups) {
256                            if (UserGroupPermissionUtil.contains(
257                                            themeDisplay.getPermissionChecker(),
258                                            userGroup.getUserGroupId(),
259                                            ActionKeys.MANAGE_ANNOUNCEMENTS)) {
260    
261                                    filteredUserGroups.add(userGroup);
262                            }
263                    }
264    
265                    return filteredUserGroups;
266            }
267    
268            private static long[] _getGroupIds(List<Group> groups) {
269                    long[] groupIds = new long[groups.size()];
270    
271                    int i = 0;
272    
273                    for (Group group : groups) {
274                            groupIds[i++] = group.getGroupId();
275                    }
276    
277                    return groupIds;
278            }
279    
280            private static long[] _getOrganizationIds(
281                    List<Organization> organizations) {
282    
283                    long[] organizationIds = new long[organizations.size()];
284    
285                    int i = 0;
286    
287                    for (Organization organization : organizations) {
288                            organizationIds[i++] = organization.getOrganizationId();
289                    }
290    
291                    return organizationIds;
292            }
293    
294            private static long[] _getRoleIds(List<Role> roles) {
295                    long[] roleIds = new long[roles.size()];
296    
297                    int i = 0;
298    
299                    for (Role role : roles) {
300                            roleIds[i++] = role.getRoleId();
301                    }
302    
303                    return roleIds;
304            }
305    
306            private static long[] _getUserGroupIds(List<UserGroup> userGroups) {
307                    long[] userGroupIds = new long[userGroups.size()];
308    
309                    int i = 0;
310    
311                    for (UserGroup userGroup : userGroups) {
312                            userGroupIds[i++] = userGroup.getUserGroupId();
313                    }
314    
315                    return userGroupIds;
316            }
317    
318            private static final long _GROUP_CLASS_NAME_ID = PortalUtil.getClassNameId(
319                    Group.class.getName());
320    
321            private static final long _ORGANIZATION_CLASS_NAME_ID =
322                    PortalUtil.getClassNameId(Organization.class.getName());
323    
324            private static final boolean _PERMISSIONS_CHECK_GUEST_ENABLED =
325                    GetterUtil.getBoolean(
326                            PropsUtil.get(PropsKeys.PERMISSIONS_CHECK_GUEST_ENABLED));
327    
328            private static final long _ROLE_CLASS_NAME_ID = PortalUtil.getClassNameId(
329                    Role.class.getName());
330    
331            private static final long _USER_CLASS_NAME_ID = PortalUtil.getClassNameId(
332                    User.class.getName());
333    
334            private static final long _USER_GROUP_CLASS_NAME_ID =
335                    PortalUtil.getClassNameId(UserGroup.class.getName());
336    
337    }