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