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