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