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