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 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, long groupId)
168         throws SystemException {
169 
170         return userGroupRolePersistence.findByU_G(userId, groupId);
171     }
172 
173     public List<UserGroupRole> getUserGroupRolesByGroupAndRole(
174             long groupId, long roleId)
175         throws SystemException {
176 
177         return userGroupRolePersistence.findByG_R(groupId, roleId);
178     }
179 
180     public boolean hasUserGroupRole(long userId, long groupId, long roleId)
181         throws SystemException {
182 
183         UserGroupRolePK pk = new UserGroupRolePK(userId, groupId, roleId);
184 
185         UserGroupRole userGroupRole =
186             userGroupRolePersistence.fetchByPrimaryKey(pk);
187 
188         if (userGroupRole != null) {
189             return true;
190         }
191         else {
192             return false;
193         }
194     }
195 
196     public boolean hasUserGroupRole(long userId, long groupId, String roleName)
197         throws PortalException, SystemException {
198 
199         User user = userPersistence.findByPrimaryKey(userId);
200 
201         long companyId = user.getCompanyId();
202 
203         Role role = rolePersistence.findByC_N(companyId, roleName);
204 
205         long roleId = role.getRoleId();
206 
207         return hasUserGroupRole(userId, groupId, roleId);
208     }
209 
210     protected void checkGroupResource(long groupId)
211         throws PortalException, SystemException {
212 
213         // Make sure that the individual resource for the group exists
214 
215         Group group = groupPersistence.findByPrimaryKey(groupId);
216 
217         resourceLocalService.addResource(
218             group.getCompanyId(), Group.class.getName(),
219             ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(groupId));
220     }
221 
222 }