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