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.portal.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.model.Group;
020    import com.liferay.portal.model.Role;
021    import com.liferay.portal.model.RoleConstants;
022    import com.liferay.portal.model.UserGroupRole;
023    import com.liferay.portal.security.membershippolicy.OrganizationMembershipPolicyUtil;
024    import com.liferay.portal.security.membershippolicy.SiteMembershipPolicyUtil;
025    import com.liferay.portal.service.base.UserGroupRoleServiceBaseImpl;
026    import com.liferay.portal.service.permission.UserGroupRolePermissionUtil;
027    import com.liferay.portal.service.persistence.UserGroupRolePK;
028    
029    import java.util.ArrayList;
030    import java.util.List;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    public class UserGroupRoleServiceImpl extends UserGroupRoleServiceBaseImpl {
036    
037            @Override
038            public void addUserGroupRoles(long userId, long groupId, long[] roleIds)
039                    throws PortalException, SystemException {
040    
041                    List<UserGroupRole> organizationUserGroupRoles =
042                            new ArrayList<UserGroupRole>();
043                    List<UserGroupRole> siteUserGroupRoles = new ArrayList<UserGroupRole>();
044    
045                    for (long roleId : roleIds) {
046                            UserGroupRolePermissionUtil.check(
047                                    getPermissionChecker(), groupId, roleId);
048    
049                            UserGroupRolePK userGroupRolePK = new UserGroupRolePK(
050                                    userId, groupId, roleId);
051    
052                            UserGroupRole userGroupRole = userGroupRolePersistence.create(
053                                    userGroupRolePK);
054    
055                            Role role = rolePersistence.findByPrimaryKey(roleId);
056    
057                            if (role.getType() == RoleConstants.TYPE_ORGANIZATION) {
058                                    organizationUserGroupRoles.add(userGroupRole);
059                            }
060                            else if (role.getType() == RoleConstants.TYPE_SITE) {
061                                    siteUserGroupRoles.add(userGroupRole);
062                            }
063                    }
064    
065                    if (!siteUserGroupRoles.isEmpty()) {
066                            SiteMembershipPolicyUtil.checkRoles(siteUserGroupRoles, null);
067                    }
068    
069                    if (!organizationUserGroupRoles.isEmpty()) {
070                            OrganizationMembershipPolicyUtil.checkRoles(
071                                    organizationUserGroupRoles, null);
072                    }
073    
074                    userGroupRoleLocalService.addUserGroupRoles(userId, groupId, roleIds);
075    
076                    if (!siteUserGroupRoles.isEmpty()) {
077                            SiteMembershipPolicyUtil.propagateRoles(siteUserGroupRoles, null);
078                    }
079    
080                    if (!organizationUserGroupRoles.isEmpty()) {
081                            OrganizationMembershipPolicyUtil.propagateRoles(
082                                    organizationUserGroupRoles, null);
083                    }
084            }
085    
086            @Override
087            public void addUserGroupRoles(long[] userIds, long groupId, long roleId)
088                    throws PortalException, SystemException {
089    
090                    UserGroupRolePermissionUtil.check(
091                            getPermissionChecker(), groupId, roleId);
092    
093                    List<UserGroupRole> userGroupRoles = new ArrayList<UserGroupRole>();
094    
095                    for (long userId : userIds) {
096                            UserGroupRolePermissionUtil.check(
097                                    getPermissionChecker(), groupId, roleId);
098    
099                            UserGroupRolePK userGroupRolePK = new UserGroupRolePK(
100                                    userId, groupId, roleId);
101    
102                            UserGroupRole userGroupRole = userGroupRolePersistence.create(
103                                    userGroupRolePK);
104    
105                            userGroupRoles.add(userGroupRole);
106                    }
107    
108                    if (userGroupRoles.isEmpty()) {
109                            return;
110                    }
111    
112                    Role role = rolePersistence.findByPrimaryKey(roleId);
113    
114                    if (role.getType() == RoleConstants.TYPE_ORGANIZATION) {
115                            OrganizationMembershipPolicyUtil.checkRoles(userGroupRoles, null);
116                    }
117                    else if (role.getType() == RoleConstants.TYPE_SITE) {
118                            SiteMembershipPolicyUtil.checkRoles(userGroupRoles, null);
119                    }
120    
121                    userGroupRoleLocalService.addUserGroupRoles(userIds, groupId, roleId);
122    
123                    if (role.getType() == RoleConstants.TYPE_ORGANIZATION) {
124                            OrganizationMembershipPolicyUtil.propagateRoles(
125                                    userGroupRoles, null);
126                    }
127                    else if (role.getType() == RoleConstants.TYPE_SITE) {
128                            SiteMembershipPolicyUtil.propagateRoles(userGroupRoles, null);
129                    }
130            }
131    
132            @Override
133            public void deleteUserGroupRoles(long userId, long groupId, long[] roleIds)
134                    throws PortalException, SystemException {
135    
136                    List<UserGroupRole> filteredOrganizationUserGroupRoles =
137                            new ArrayList<UserGroupRole>();
138                    List<UserGroupRole> filteredSiteUserGroupRoles =
139                            new ArrayList<UserGroupRole>();
140    
141                    for (long roleId : roleIds) {
142                            UserGroupRolePermissionUtil.check(
143                                    getPermissionChecker(), groupId, roleId);
144    
145                            Role role = roleLocalService.getRole(roleId);
146    
147                            UserGroupRolePK userGroupRolePK = new UserGroupRolePK(
148                                    userId, groupId, roleId);
149    
150                            UserGroupRole userGroupRole = userGroupRolePersistence.create(
151                                    userGroupRolePK);
152    
153                            if (role.getType() == RoleConstants.TYPE_ORGANIZATION) {
154                                    Group group = groupPersistence.findByPrimaryKey(groupId);
155    
156                                    if (!OrganizationMembershipPolicyUtil.isRoleProtected(
157                                                    getPermissionChecker(), userId,
158                                                    group.getOrganizationId(), roleId)) {
159    
160                                            filteredOrganizationUserGroupRoles.add(userGroupRole);
161                                    }
162                            }
163                            else if ((role.getType() == RoleConstants.TYPE_SITE) &&
164                                             !SiteMembershipPolicyUtil.isRoleProtected(
165                                                    getPermissionChecker(), userId, groupId, roleId)) {
166    
167                                            filteredSiteUserGroupRoles.add(userGroupRole);
168                            }
169                    }
170    
171                    if (filteredOrganizationUserGroupRoles.isEmpty() &&
172                            filteredSiteUserGroupRoles.isEmpty()) {
173    
174                            return;
175                    }
176    
177                    if (!filteredOrganizationUserGroupRoles.isEmpty()) {
178                            OrganizationMembershipPolicyUtil.checkRoles(
179                                    null, filteredOrganizationUserGroupRoles);
180                    }
181    
182                    if (!filteredSiteUserGroupRoles.isEmpty()) {
183                            SiteMembershipPolicyUtil.checkRoles(
184                                    null, filteredSiteUserGroupRoles);
185                    }
186    
187                    userGroupRoleLocalService.deleteUserGroupRoles(
188                            userId, groupId, roleIds);
189    
190                    if (!filteredOrganizationUserGroupRoles.isEmpty()) {
191                            OrganizationMembershipPolicyUtil.propagateRoles(
192                                    null, filteredOrganizationUserGroupRoles);
193                    }
194    
195                    if (!filteredSiteUserGroupRoles.isEmpty()) {
196                            SiteMembershipPolicyUtil.propagateRoles(
197                                    null, filteredSiteUserGroupRoles);
198                    }
199            }
200    
201            @Override
202            public void deleteUserGroupRoles(long[] userIds, long groupId, long roleId)
203                    throws PortalException, SystemException {
204    
205                    UserGroupRolePermissionUtil.check(
206                            getPermissionChecker(), groupId, roleId);
207    
208                    List<UserGroupRole> filteredUserGroupRoles =
209                            new ArrayList<UserGroupRole>();
210    
211                    Role role = rolePersistence.findByPrimaryKey(roleId);
212    
213                    for (long userId : userIds) {
214                            UserGroupRolePK userGroupRolePK = new UserGroupRolePK(
215                                    userId, groupId, roleId);
216    
217                            UserGroupRole userGroupRole = userGroupRolePersistence.create(
218                                    userGroupRolePK);
219    
220                            if (role.getType() == RoleConstants.TYPE_ORGANIZATION) {
221                                    Group group = groupPersistence.findByPrimaryKey(groupId);
222    
223                                    if (!OrganizationMembershipPolicyUtil.isRoleProtected(
224                                                    getPermissionChecker(), userId,
225                                                    group.getOrganizationId(), roleId)) {
226    
227                                            filteredUserGroupRoles.add(userGroupRole);
228                                    }
229                            }
230                            else if ((role.getType() == RoleConstants.TYPE_SITE) &&
231                                             !SiteMembershipPolicyUtil.isRoleProtected(
232                                                     getPermissionChecker(), userId, groupId, roleId)) {
233    
234                                            filteredUserGroupRoles.add(userGroupRole);
235                            }
236                    }
237    
238                    if (filteredUserGroupRoles.isEmpty()) {
239                            return;
240                    }
241    
242                    if (role.getType() == RoleConstants.TYPE_ORGANIZATION) {
243                            OrganizationMembershipPolicyUtil.checkRoles(
244                                    null, filteredUserGroupRoles);
245                    }
246                    else if (role.getType() == RoleConstants.TYPE_SITE) {
247                            SiteMembershipPolicyUtil.checkRoles(null, filteredUserGroupRoles);
248                    }
249    
250                    userGroupRoleLocalService.deleteUserGroupRoles(
251                            userIds, groupId, roleId);
252    
253                    if (role.getType() == RoleConstants.TYPE_SITE) {
254                            SiteMembershipPolicyUtil.propagateRoles(
255                                    null, filteredUserGroupRoles);
256                    }
257                    else if (role.getType() == RoleConstants.TYPE_ORGANIZATION) {
258                            OrganizationMembershipPolicyUtil.propagateRoles(
259                                    null, filteredUserGroupRoles);
260                    }
261            }
262    
263    }