1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.service.impl;
16  
17  import com.liferay.portal.NoSuchUserGroupRoleException;
18  import com.liferay.portal.PortalException;
19  import com.liferay.portal.SystemException;
20  import com.liferay.portal.model.Group;
21  import com.liferay.portal.model.ResourceConstants;
22  import com.liferay.portal.model.Role;
23  import com.liferay.portal.model.User;
24  import com.liferay.portal.model.UserGroupRole;
25  import com.liferay.portal.security.permission.PermissionCacheUtil;
26  import com.liferay.portal.service.base.UserGroupRoleLocalServiceBaseImpl;
27  import com.liferay.portal.service.persistence.UserGroupRolePK;
28  
29  import java.util.List;
30  
31  /**
32   * <a href="UserGroupRoleLocalServiceImpl.java.html"><b><i>View Source</i></b>
33   * </a>
34   *
35   * @author Jorge Ferrer
36   */
37  public class UserGroupRoleLocalServiceImpl
38      extends UserGroupRoleLocalServiceBaseImpl {
39  
40      public void addUserGroupRoles(long userId, long groupId, long[] roleIds)
41          throws PortalException, SystemException {
42  
43          checkGroupResource(groupId);
44  
45          for (long roleId : roleIds) {
46              UserGroupRolePK pk = new UserGroupRolePK(userId, groupId, roleId);
47  
48              UserGroupRole userGroupRole =
49                  userGroupRolePersistence.fetchByPrimaryKey(pk);
50  
51              if (userGroupRole == null) {
52                  userGroupRole = userGroupRolePersistence.create(pk);
53  
54                  userGroupRolePersistence.update(userGroupRole, false);
55              }
56          }
57  
58          PermissionCacheUtil.clearCache();
59      }
60  
61      public void addUserGroupRoles(long[] userIds, long groupId, long roleId)
62          throws PortalException, SystemException {
63  
64          checkGroupResource(groupId);
65  
66          for (long userId : userIds) {
67              UserGroupRolePK pk = new UserGroupRolePK(userId, groupId, roleId);
68  
69              UserGroupRole userGroupRole =
70                  userGroupRolePersistence.fetchByPrimaryKey(pk);
71  
72              if (userGroupRole == null) {
73                  userGroupRole = userGroupRolePersistence.create(pk);
74  
75                  userGroupRolePersistence.update(userGroupRole, false);
76              }
77          }
78  
79          PermissionCacheUtil.clearCache();
80      }
81  
82      public void deleteUserGroupRole(UserGroupRole userGroupRole)
83          throws SystemException {
84  
85          userGroupRolePersistence.remove(userGroupRole);
86  
87          PermissionCacheUtil.clearCache();
88      }
89  
90      public void deleteUserGroupRoles(
91              long userId, long groupId, long[] roleIds)
92          throws SystemException {
93  
94          for (long roleId : roleIds) {
95              UserGroupRolePK pk = new UserGroupRolePK(userId, groupId, roleId);
96  
97              try {
98                  userGroupRolePersistence.remove(pk);
99              }
100             catch (NoSuchUserGroupRoleException nsugre) {
101             }
102         }
103 
104         PermissionCacheUtil.clearCache();
105     }
106 
107     public void deleteUserGroupRoles(long userId, long[] groupIds)
108         throws SystemException {
109 
110         for (long groupId : groupIds) {
111             userGroupRolePersistence.removeByU_G(userId, groupId);
112         }
113 
114         PermissionCacheUtil.clearCache();
115     }
116 
117     public void deleteUserGroupRoles(long[] userIds, long groupId)
118         throws SystemException {
119 
120         for (long userId : userIds) {
121             userGroupRolePersistence.removeByU_G(userId, groupId);
122         }
123 
124         PermissionCacheUtil.clearCache();
125     }
126 
127     public void deleteUserGroupRoles(long[] userIds, long groupId, long roleId)
128         throws SystemException {
129 
130         for (long userId : userIds) {
131             UserGroupRolePK pk = new UserGroupRolePK(userId, groupId, roleId);
132 
133             try {
134                 userGroupRolePersistence.remove(pk);
135             }
136             catch (NoSuchUserGroupRoleException nsugre) {
137             }
138         }
139 
140         PermissionCacheUtil.clearCache();
141     }
142 
143     public void deleteUserGroupRolesByGroupId(long groupId)
144         throws SystemException {
145 
146         userGroupRolePersistence.removeByGroupId(groupId);
147 
148         PermissionCacheUtil.clearCache();
149     }
150 
151     public void deleteUserGroupRolesByRoleId(long roleId)
152         throws SystemException {
153 
154         userGroupRolePersistence.removeByRoleId(roleId);
155 
156         PermissionCacheUtil.clearCache();
157     }
158 
159     public void deleteUserGroupRolesByUserId(long userId)
160         throws SystemException {
161 
162         userGroupRolePersistence.removeByUserId(userId);
163 
164         PermissionCacheUtil.clearCache();
165     }
166 
167     public List<UserGroupRole> getUserGroupRoles(long userId)
168         throws SystemException {
169 
170         return userGroupRolePersistence.findByUserId(userId);
171     }
172 
173     public List<UserGroupRole> getUserGroupRoles(long userId, long groupId)
174         throws SystemException {
175 
176         return userGroupRolePersistence.findByU_G(userId, groupId);
177     }
178 
179     public List<UserGroupRole> getUserGroupRolesByGroupAndRole(
180             long groupId, long roleId)
181         throws SystemException {
182 
183         return userGroupRolePersistence.findByG_R(groupId, roleId);
184     }
185 
186     public List<UserGroupRole> getUserGroupRolesByUserUserGroupAndGroup(
187             long userId, long groupId)
188         throws SystemException {
189 
190         return userGroupRoleFinder.findByUserUserGroupGroupRole(
191             userId, groupId);
192     }
193 
194     public boolean hasUserGroupRole(long userId, long groupId, long roleId)
195         throws SystemException {
196 
197         return hasUserGroupRole(userId, groupId, roleId, false);
198     }
199 
200     public boolean hasUserGroupRole(
201             long userId, long groupId, long roleId, boolean inherit)
202         throws SystemException {
203 
204         UserGroupRolePK pk = new UserGroupRolePK(userId, groupId, roleId);
205 
206         UserGroupRole userGroupRole =
207             userGroupRolePersistence.fetchByPrimaryKey(pk);
208 
209         if (userGroupRole != null) {
210             return true;
211         }
212 
213         if (inherit) {
214             if (roleFinder.countByU_G_R(userId, groupId, roleId) > 0) {
215                 return true;
216             }
217         }
218 
219         return false;
220     }
221 
222     public boolean hasUserGroupRole(long userId, long groupId, String roleName)
223         throws PortalException, SystemException {
224 
225         return hasUserGroupRole(userId, groupId, roleName, false);
226     }
227 
228     public boolean hasUserGroupRole(
229             long userId, long groupId, String roleName, boolean inherit)
230         throws PortalException, SystemException {
231 
232         User user = userPersistence.findByPrimaryKey(userId);
233 
234         long companyId = user.getCompanyId();
235 
236         Role role = rolePersistence.findByC_N(companyId, roleName);
237 
238         long roleId = role.getRoleId();
239 
240         return hasUserGroupRole(userId, groupId, roleId, inherit);
241     }
242 
243     protected void checkGroupResource(long groupId)
244         throws PortalException, SystemException {
245 
246         // Make sure that the individual resource for the group exists
247 
248         Group group = groupPersistence.findByPrimaryKey(groupId);
249 
250         resourceLocalService.addResource(
251             group.getCompanyId(), Group.class.getName(),
252             ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(groupId));
253     }
254 
255 }