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   */
46  public class UserGroupRoleLocalServiceImpl
47      extends UserGroupRoleLocalServiceBaseImpl {
48  
49      public void addUserGroupRoles(long userId, long groupId, long[] roleIds)
50          throws PortalException, SystemException {
51  
52          checkGroupResource(groupId);
53  
54          for (long roleId : roleIds) {
55              UserGroupRolePK pk = new UserGroupRolePK(userId, groupId, roleId);
56  
57              UserGroupRole userGroupRole =
58                  userGroupRolePersistence.fetchByPrimaryKey(pk);
59  
60              if (userGroupRole == null) {
61                  userGroupRole = userGroupRolePersistence.create(pk);
62  
63                  userGroupRolePersistence.update(userGroupRole, false);
64              }
65          }
66  
67          PermissionCacheUtil.clearCache();
68      }
69  
70      public void addUserGroupRoles(long[] userIds, long groupId, long roleId)
71          throws PortalException, SystemException {
72  
73          checkGroupResource(groupId);
74  
75          for (long userId : userIds) {
76              UserGroupRolePK pk = new UserGroupRolePK(userId, groupId, roleId);
77  
78              UserGroupRole userGroupRole =
79                  userGroupRolePersistence.fetchByPrimaryKey(pk);
80  
81              if (userGroupRole == null) {
82                  userGroupRole = userGroupRolePersistence.create(pk);
83  
84                  userGroupRolePersistence.update(userGroupRole, false);
85              }
86          }
87  
88          PermissionCacheUtil.clearCache();
89      }
90  
91      public void deleteUserGroupRole(UserGroupRole userGroupRole)
92          throws SystemException {
93  
94          userGroupRolePersistence.remove(userGroupRole);
95  
96          PermissionCacheUtil.clearCache();
97      }
98  
99      public void deleteUserGroupRoles(
100             long userId, long groupId, long[] roleIds)
101         throws SystemException {
102 
103         for (long roleId : roleIds) {
104             UserGroupRolePK pk = new UserGroupRolePK(userId, groupId, roleId);
105 
106             try {
107                 userGroupRolePersistence.remove(pk);
108             }
109             catch (NoSuchUserGroupRoleException nsugre) {
110             }
111         }
112 
113         PermissionCacheUtil.clearCache();
114     }
115 
116     public void deleteUserGroupRoles(long userId, long[] groupIds)
117         throws SystemException {
118 
119         for (long groupId : groupIds) {
120             userGroupRolePersistence.removeByU_G(userId, groupId);
121         }
122 
123         PermissionCacheUtil.clearCache();
124     }
125 
126     public void deleteUserGroupRoles(long[] userIds, long groupId)
127         throws SystemException {
128 
129         for (long userId : userIds) {
130             userGroupRolePersistence.removeByU_G(userId, groupId);
131         }
132 
133         PermissionCacheUtil.clearCache();
134     }
135 
136     public void deleteUserGroupRoles(long[] userIds, long groupId, long roleId)
137         throws SystemException {
138 
139         for (long userId : userIds) {
140             UserGroupRolePK pk = new UserGroupRolePK(userId, groupId, roleId);
141 
142             try {
143                 userGroupRolePersistence.remove(pk);
144             }
145             catch (NoSuchUserGroupRoleException nsugre) {
146             }
147         }
148 
149         PermissionCacheUtil.clearCache();
150     }
151 
152     public void deleteUserGroupRolesByGroupId(long groupId)
153         throws SystemException {
154 
155         userGroupRolePersistence.removeByGroupId(groupId);
156 
157         PermissionCacheUtil.clearCache();
158     }
159 
160     public void deleteUserGroupRolesByRoleId(long roleId)
161         throws SystemException {
162 
163         userGroupRolePersistence.removeByRoleId(roleId);
164 
165         PermissionCacheUtil.clearCache();
166     }
167 
168     public void deleteUserGroupRolesByUserId(long userId)
169         throws SystemException {
170 
171         userGroupRolePersistence.removeByUserId(userId);
172 
173         PermissionCacheUtil.clearCache();
174     }
175 
176     public List<UserGroupRole> getUserGroupRoles(long userId)
177         throws SystemException {
178 
179         return userGroupRolePersistence.findByUserId(userId);
180     }
181 
182     public List<UserGroupRole> getUserGroupRoles(long userId, long groupId)
183         throws SystemException {
184 
185         return userGroupRolePersistence.findByU_G(userId, groupId);
186     }
187 
188     public List<UserGroupRole> getUserGroupRolesByGroupAndRole(
189             long groupId, long roleId)
190         throws SystemException {
191 
192         return userGroupRolePersistence.findByG_R(groupId, roleId);
193     }
194 
195     public boolean hasUserGroupRole(long userId, long groupId, long roleId)
196         throws SystemException {
197 
198         return hasUserGroupRole(userId, groupId, roleId, false);
199     }
200 
201     public boolean hasUserGroupRole(
202             long userId, long groupId, long roleId, boolean inherit)
203         throws SystemException {
204 
205         UserGroupRolePK pk = new UserGroupRolePK(userId, groupId, roleId);
206 
207         UserGroupRole userGroupRole =
208             userGroupRolePersistence.fetchByPrimaryKey(pk);
209 
210         if (userGroupRole != null) {
211             return true;
212         }
213 
214         if (inherit) {
215             if (roleFinder.countByU_G_R(userId, groupId, roleId) > 0) {
216                 return true;
217             }
218         }
219 
220         return false;
221     }
222 
223     public boolean hasUserGroupRole(long userId, long groupId, String roleName)
224         throws PortalException, SystemException {
225 
226         return hasUserGroupRole(userId, groupId, roleName, false);
227     }
228 
229     public boolean hasUserGroupRole(
230             long userId, long groupId, String roleName, boolean inherit)
231         throws PortalException, SystemException {
232 
233         User user = userPersistence.findByPrimaryKey(userId);
234 
235         long companyId = user.getCompanyId();
236 
237         Role role = rolePersistence.findByC_N(companyId, roleName);
238 
239         long roleId = role.getRoleId();
240 
241         return hasUserGroupRole(userId, groupId, roleId, inherit);
242     }
243 
244     protected void checkGroupResource(long groupId)
245         throws PortalException, SystemException {
246 
247         // Make sure that the individual resource for the group exists
248 
249         Group group = groupPersistence.findByPrimaryKey(groupId);
250 
251         resourceLocalService.addResource(
252             group.getCompanyId(), Group.class.getName(),
253             ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(groupId));
254     }
255 
256 }