1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.service.impl;
24  
25  import com.liferay.portal.NoSuchUserGroupRoleException;
26  import com.liferay.portal.PortalException;
27  import com.liferay.portal.SystemException;
28  import com.liferay.portal.model.Group;
29  import com.liferay.portal.model.ResourceConstants;
30  import com.liferay.portal.model.Role;
31  import com.liferay.portal.model.User;
32  import com.liferay.portal.model.UserGroupRole;
33  import com.liferay.portal.security.permission.PermissionCacheUtil;
34  import com.liferay.portal.service.base.UserGroupRoleLocalServiceBaseImpl;
35  import com.liferay.portal.service.persistence.UserGroupRolePK;
36  
37  import java.util.List;
38  
39  /**
40   * <a href="UserGroupRoleLocalServiceImpl.java.html"><b><i>View Source</i></b>
41   * </a>
42   *
43   * @author Jorge Ferrer
44   */
45  public class UserGroupRoleLocalServiceImpl
46      extends UserGroupRoleLocalServiceBaseImpl {
47  
48      public void addUserGroupRoles(long userId, long groupId, long[] roleIds)
49          throws PortalException, SystemException {
50  
51          checkGroupResource(groupId);
52  
53          for (long roleId : roleIds) {
54              UserGroupRolePK pk = new UserGroupRolePK(userId, groupId, roleId);
55  
56              UserGroupRole userGroupRole =
57                  userGroupRolePersistence.fetchByPrimaryKey(pk);
58  
59              if (userGroupRole == null) {
60                  userGroupRole = userGroupRolePersistence.create(pk);
61  
62                  userGroupRolePersistence.update(userGroupRole, false);
63              }
64          }
65  
66          PermissionCacheUtil.clearCache();
67      }
68  
69      public void addUserGroupRoles(long[] userIds, long groupId, long roleId)
70          throws PortalException, SystemException {
71  
72          checkGroupResource(groupId);
73  
74          for (long userId : userIds) {
75              UserGroupRolePK pk = new UserGroupRolePK(userId, groupId, roleId);
76  
77              UserGroupRole userGroupRole =
78                  userGroupRolePersistence.fetchByPrimaryKey(pk);
79  
80              if (userGroupRole == null) {
81                  userGroupRole = userGroupRolePersistence.create(pk);
82  
83                  userGroupRolePersistence.update(userGroupRole, false);
84              }
85          }
86  
87          PermissionCacheUtil.clearCache();
88      }
89  
90      public void deleteUserGroupRole(UserGroupRole userGroupRole)
91          throws SystemException {
92  
93          userGroupRolePersistence.remove(userGroupRole);
94  
95          PermissionCacheUtil.clearCache();
96      }
97  
98      public void deleteUserGroupRoles(
99              long userId, long groupId, long[] roleIds)
100         throws SystemException {
101 
102         for (long roleId : roleIds) {
103             UserGroupRolePK pk = new UserGroupRolePK(userId, groupId, roleId);
104 
105             try {
106                 userGroupRolePersistence.remove(pk);
107             }
108             catch (NoSuchUserGroupRoleException nsugre) {
109             }
110         }
111 
112         PermissionCacheUtil.clearCache();
113     }
114 
115     public void deleteUserGroupRoles(long userId, long[] groupIds)
116         throws SystemException {
117 
118         for (long groupId : groupIds) {
119             userGroupRolePersistence.removeByU_G(userId, groupId);
120         }
121 
122         PermissionCacheUtil.clearCache();
123     }
124 
125     public void deleteUserGroupRoles(long[] userIds, long groupId)
126         throws SystemException {
127 
128         for (long userId : userIds) {
129             userGroupRolePersistence.removeByU_G(userId, groupId);
130         }
131 
132         PermissionCacheUtil.clearCache();
133     }
134 
135     public void deleteUserGroupRoles(long[] userIds, long groupId, long roleId)
136         throws SystemException {
137 
138         for (long userId : userIds) {
139             UserGroupRolePK pk = new UserGroupRolePK(userId, groupId, roleId);
140 
141             try {
142                 userGroupRolePersistence.remove(pk);
143             }
144             catch (NoSuchUserGroupRoleException nsugre) {
145             }
146         }
147 
148         PermissionCacheUtil.clearCache();
149     }
150 
151     public void deleteUserGroupRolesByGroupId(long groupId)
152         throws SystemException {
153 
154         userGroupRolePersistence.removeByGroupId(groupId);
155 
156         PermissionCacheUtil.clearCache();
157     }
158 
159     public void deleteUserGroupRolesByRoleId(long roleId)
160         throws SystemException {
161 
162         userGroupRolePersistence.removeByRoleId(roleId);
163 
164         PermissionCacheUtil.clearCache();
165     }
166 
167     public void deleteUserGroupRolesByUserId(long userId)
168         throws SystemException {
169 
170         userGroupRolePersistence.removeByUserId(userId);
171 
172         PermissionCacheUtil.clearCache();
173     }
174 
175     public List<UserGroupRole> getUserGroupRoles(long userId)
176         throws SystemException {
177 
178         return userGroupRolePersistence.findByUserId(userId);
179     }
180 
181     public List<UserGroupRole> getUserGroupRoles(long userId, long groupId)
182         throws SystemException {
183 
184         return userGroupRolePersistence.findByU_G(userId, groupId);
185     }
186 
187     public List<UserGroupRole> getUserGroupRolesByGroupAndRole(
188             long groupId, long roleId)
189         throws SystemException {
190 
191         return userGroupRolePersistence.findByG_R(groupId, roleId);
192     }
193 
194     public List<UserGroupRole> getUserGroupRolesByUserUserGroupAndGroup(
195             long userId, long groupId)
196         throws SystemException {
197 
198         return userGroupRoleFinder.findByUserUserGroupGroupRole(
199             userId, groupId);
200     }
201 
202     public boolean hasUserGroupRole(long userId, long groupId, long roleId)
203         throws SystemException {
204 
205         return hasUserGroupRole(userId, groupId, roleId, false);
206     }
207 
208     public boolean hasUserGroupRole(
209             long userId, long groupId, long roleId, boolean inherit)
210         throws SystemException {
211 
212         UserGroupRolePK pk = new UserGroupRolePK(userId, groupId, roleId);
213 
214         UserGroupRole userGroupRole =
215             userGroupRolePersistence.fetchByPrimaryKey(pk);
216 
217         if (userGroupRole != null) {
218             return true;
219         }
220 
221         if (inherit) {
222             if (roleFinder.countByU_G_R(userId, groupId, roleId) > 0) {
223                 return true;
224             }
225         }
226 
227         return false;
228     }
229 
230     public boolean hasUserGroupRole(long userId, long groupId, String roleName)
231         throws PortalException, SystemException {
232 
233         return hasUserGroupRole(userId, groupId, roleName, false);
234     }
235 
236     public boolean hasUserGroupRole(
237             long userId, long groupId, String roleName, boolean inherit)
238         throws PortalException, SystemException {
239 
240         User user = userPersistence.findByPrimaryKey(userId);
241 
242         long companyId = user.getCompanyId();
243 
244         Role role = rolePersistence.findByC_N(companyId, roleName);
245 
246         long roleId = role.getRoleId();
247 
248         return hasUserGroupRole(userId, groupId, roleId, inherit);
249     }
250 
251     protected void checkGroupResource(long groupId)
252         throws PortalException, SystemException {
253 
254         // Make sure that the individual resource for the group exists
255 
256         Group group = groupPersistence.findByPrimaryKey(groupId);
257 
258         resourceLocalService.addResource(
259             group.getCompanyId(), Group.class.getName(),
260             ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(groupId));
261     }
262 
263 }