001    /**
002     * Copyright (c) 2000-2012 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.NoSuchUserGroupRoleException;
018    import com.liferay.portal.kernel.dao.orm.QueryUtil;
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.exception.SystemException;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.model.Group;
023    import com.liferay.portal.model.Organization;
024    import com.liferay.portal.model.Role;
025    import com.liferay.portal.model.User;
026    import com.liferay.portal.model.UserGroupRole;
027    import com.liferay.portal.security.auth.MembershipPolicyUtil;
028    import com.liferay.portal.security.permission.PermissionCacheUtil;
029    import com.liferay.portal.service.base.UserGroupRoleLocalServiceBaseImpl;
030    import com.liferay.portal.service.persistence.UserGroupRolePK;
031    
032    import java.util.LinkedHashMap;
033    import java.util.List;
034    import java.util.Set;
035    
036    /**
037     * @author Jorge Ferrer
038     */
039    public class UserGroupRoleLocalServiceImpl
040            extends UserGroupRoleLocalServiceBaseImpl {
041    
042            public void addUserGroupRoles(long userId, long groupId, long[] roleIds)
043                    throws SystemException {
044    
045                    for (long roleId : roleIds) {
046                            UserGroupRolePK userGroupRolePK = new UserGroupRolePK(
047                                    userId, groupId, roleId);
048    
049                            UserGroupRole userGroupRole =
050                                    userGroupRolePersistence.fetchByPrimaryKey(userGroupRolePK);
051    
052                            if (userGroupRole == null) {
053                                    userGroupRole = userGroupRolePersistence.create(
054                                            userGroupRolePK);
055    
056                                    userGroupRolePersistence.update(userGroupRole);
057                            }
058                    }
059    
060                    PermissionCacheUtil.clearCache();
061            }
062    
063            public void addUserGroupRoles(long[] userIds, long groupId, long roleId)
064                    throws SystemException {
065    
066                    for (long userId : userIds) {
067                            UserGroupRolePK userGroupRolePK = new UserGroupRolePK(
068                                    userId, groupId, roleId);
069    
070                            UserGroupRole userGroupRole =
071                                    userGroupRolePersistence.fetchByPrimaryKey(userGroupRolePK);
072    
073                            if (userGroupRole == null) {
074                                    userGroupRole = userGroupRolePersistence.create(
075                                            userGroupRolePK);
076    
077                                    userGroupRolePersistence.update(userGroupRole);
078                            }
079                    }
080    
081                    PermissionCacheUtil.clearCache();
082            }
083    
084            public void checkMembershipPolicy(User user)
085                    throws PortalException, SystemException {
086    
087                    LinkedHashMap<String, Object> groupParams =
088                            new LinkedHashMap<String, Object>();
089    
090                    groupParams.put("inheritance", Boolean.FALSE);
091                    groupParams.put("site", Boolean.TRUE);
092                    groupParams.put("usersGroups", user.getUserId());
093    
094                    List<Group> groups = groupLocalService.search(
095                            user.getCompanyId(), groupParams, QueryUtil.ALL_POS,
096                            QueryUtil.ALL_POS);
097    
098                    for (Group group : groups) {
099                            Set<Role> mandatoryRoles = MembershipPolicyUtil.getMandatoryRoles(
100                                    group, user);
101    
102                            for (Role role : mandatoryRoles) {
103                                    if (!hasUserGroupRole(
104                                                    user.getUserId(), group.getGroupId(), role.getRoleId(),
105                                                    false)) {
106    
107                                            addUserGroupRoles(
108                                                    user.getUserId(), group.getGroupId(),
109                                                    new long[] {role.getRoleId()});
110                                    }
111                            }
112    
113                            List<Role> roles = roleLocalService.getUserGroupRoles(
114                                    user.getUserId(), group.getGroupId());
115    
116                            for (Role role : roles) {
117                                    if (!MembershipPolicyUtil.isMembershipAllowed(
118                                                    group, role, user)) {
119    
120                                            deleteUserGroupRoles(
121                                                    user.getUserId(), group.getGroupId(),
122                                                    new long[] {role.getRoleId()});
123                                    }
124                            }
125                    }
126    
127                    List<Organization> organizations =
128                            organizationLocalService.getUserOrganizations(user.getUserId());
129    
130                    for (Organization organization : organizations) {
131                            Set<Role> mandatoryRoles = MembershipPolicyUtil.getMandatoryRoles(
132                                    organization, user);
133    
134                            for (Role role : mandatoryRoles) {
135                                    if (!hasUserGroupRole(
136                                                    user.getUserId(), organization.getGroupId(),
137                                                    role.getRoleId(), false)) {
138    
139                                            addUserGroupRoles(
140                                                    user.getUserId(), organization.getGroupId(),
141                                                    new long[] {role.getRoleId()});
142                                    }
143                            }
144    
145                            List<Role> roles = roleLocalService.getUserGroupRoles(
146                                    user.getUserId(), organization.getGroupId());
147    
148                            for (Role role : roles) {
149                                    if (!MembershipPolicyUtil.isMembershipAllowed(
150                                                    organization, role, user)) {
151    
152                                            deleteUserGroupRoles(
153                                                    user.getUserId(), organization.getGroupId(),
154                                                    new long[] {role.getRoleId()});
155                                    }
156                            }
157                    }
158            }
159    
160            @Override
161            public UserGroupRole deleteUserGroupRole(UserGroupRole userGroupRole)
162                    throws SystemException {
163    
164                    userGroupRolePersistence.remove(userGroupRole);
165    
166                    PermissionCacheUtil.clearCache();
167    
168                    return userGroupRole;
169            }
170    
171            public void deleteUserGroupRoles(long userId, long groupId, long[] roleIds)
172                    throws SystemException {
173    
174                    for (long roleId : roleIds) {
175                            UserGroupRolePK userGroupRolePK = new UserGroupRolePK(
176                                    userId, groupId, roleId);
177    
178                            try {
179                                    userGroupRolePersistence.remove(userGroupRolePK);
180                            }
181                            catch (NoSuchUserGroupRoleException nsugre) {
182                            }
183                    }
184    
185                    PermissionCacheUtil.clearCache();
186            }
187    
188            public void deleteUserGroupRoles(long userId, long[] groupIds)
189                    throws SystemException {
190    
191                    for (long groupId : groupIds) {
192                            userGroupRolePersistence.removeByU_G(userId, groupId);
193                    }
194    
195                    PermissionCacheUtil.clearCache();
196            }
197    
198            public void deleteUserGroupRoles(long[] userIds, long groupId)
199                    throws SystemException {
200    
201                    for (long userId : userIds) {
202                            userGroupRolePersistence.removeByU_G(userId, groupId);
203                    }
204    
205                    PermissionCacheUtil.clearCache();
206            }
207    
208            public void deleteUserGroupRoles(long[] userIds, long groupId, int roleType)
209                    throws SystemException {
210    
211                    List<Role> roles = rolePersistence.findByT_S(
212                            roleType, StringPool.BLANK);
213    
214                    for (long userId : userIds) {
215                            for (Role role : roles) {
216                                    UserGroupRolePK userGroupRolePK = new UserGroupRolePK(
217                                            userId, groupId, role.getRoleId());
218    
219                                    try {
220                                            userGroupRolePersistence.remove(userGroupRolePK);
221                                    }
222                                    catch (NoSuchUserGroupRoleException nsugre) {
223                                    }
224                            }
225                    }
226    
227                    PermissionCacheUtil.clearCache();
228            }
229    
230            public void deleteUserGroupRoles(long[] userIds, long groupId, long roleId)
231                    throws SystemException {
232    
233                    for (long userId : userIds) {
234                            UserGroupRolePK pk = new UserGroupRolePK(userId, groupId, roleId);
235    
236                            try {
237                                    userGroupRolePersistence.remove(pk);
238                            }
239                            catch (NoSuchUserGroupRoleException nsugre) {
240                            }
241                    }
242    
243                    PermissionCacheUtil.clearCache();
244            }
245    
246            public void deleteUserGroupRolesByGroupId(long groupId)
247                    throws SystemException {
248    
249                    userGroupRolePersistence.removeByGroupId(groupId);
250    
251                    PermissionCacheUtil.clearCache();
252            }
253    
254            public void deleteUserGroupRolesByRoleId(long roleId)
255                    throws SystemException {
256    
257                    userGroupRolePersistence.removeByRoleId(roleId);
258    
259                    PermissionCacheUtil.clearCache();
260            }
261    
262            public void deleteUserGroupRolesByUserId(long userId)
263                    throws SystemException {
264    
265                    userGroupRolePersistence.removeByUserId(userId);
266    
267                    PermissionCacheUtil.clearCache();
268            }
269    
270            public List<UserGroupRole> getUserGroupRoles(long userId)
271                    throws SystemException {
272    
273                    return userGroupRolePersistence.findByUserId(userId);
274            }
275    
276            public List<UserGroupRole> getUserGroupRoles(long userId, long groupId)
277                    throws SystemException {
278    
279                    return userGroupRolePersistence.findByU_G(userId, groupId);
280            }
281    
282            public List<UserGroupRole> getUserGroupRolesByGroupAndRole(
283                            long groupId, long roleId)
284                    throws SystemException {
285    
286                    return userGroupRolePersistence.findByG_R(groupId, roleId);
287            }
288    
289            public List<UserGroupRole> getUserGroupRolesByUserUserGroupAndGroup(
290                            long userId, long groupId)
291                    throws SystemException {
292    
293                    return userGroupRoleFinder.findByUserUserGroupGroupRole(
294                            userId, groupId);
295            }
296    
297            public boolean hasUserGroupRole(long userId, long groupId, long roleId)
298                    throws SystemException {
299    
300                    return hasUserGroupRole(userId, groupId, roleId, false);
301            }
302    
303            public boolean hasUserGroupRole(
304                            long userId, long groupId, long roleId, boolean inherit)
305                    throws SystemException {
306    
307                    UserGroupRolePK userGroupRolePK = new UserGroupRolePK(
308                            userId, groupId, roleId);
309    
310                    UserGroupRole userGroupRole =
311                            userGroupRolePersistence.fetchByPrimaryKey(userGroupRolePK);
312    
313                    if (userGroupRole != null) {
314                            return true;
315                    }
316    
317                    if (inherit) {
318                            if (roleFinder.countByU_G_R(userId, groupId, roleId) > 0) {
319                                    return true;
320                            }
321                    }
322    
323                    return false;
324            }
325    
326            public boolean hasUserGroupRole(long userId, long groupId, String roleName)
327                    throws PortalException, SystemException {
328    
329                    return hasUserGroupRole(userId, groupId, roleName, false);
330            }
331    
332            public boolean hasUserGroupRole(
333                            long userId, long groupId, String roleName, boolean inherit)
334                    throws PortalException, SystemException {
335    
336                    User user = userPersistence.findByPrimaryKey(userId);
337    
338                    long companyId = user.getCompanyId();
339    
340                    Role role = rolePersistence.findByC_N(companyId, roleName);
341    
342                    long roleId = role.getRoleId();
343    
344                    return hasUserGroupRole(userId, groupId, roleId, inherit);
345            }
346    
347    }