1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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 deleteUserGroupRoles(
83              long userId, long groupId, long[] roleIds)
84          throws SystemException {
85  
86          for (long roleId : roleIds) {
87              UserGroupRolePK pk = new UserGroupRolePK(userId, groupId, roleId);
88  
89              try {
90                  userGroupRolePersistence.remove(pk);
91              }
92              catch (NoSuchUserGroupRoleException nsugre) {
93              }
94          }
95  
96          PermissionCacheUtil.clearCache();
97      }
98  
99      public void deleteUserGroupRoles(long userId, long[] groupIds)
100         throws SystemException {
101 
102         for (long groupId : groupIds) {
103             userGroupRolePersistence.removeByU_G(userId, groupId);
104         }
105 
106         PermissionCacheUtil.clearCache();
107     }
108 
109     public void deleteUserGroupRoles(long[] userIds, long groupId)
110         throws SystemException {
111 
112         for (long userId : userIds) {
113             userGroupRolePersistence.removeByU_G(userId, groupId);
114         }
115 
116         PermissionCacheUtil.clearCache();
117     }
118 
119     public void deleteUserGroupRoles(long[] userIds, long groupId, long roleId)
120         throws SystemException {
121 
122         for (long userId : userIds) {
123             UserGroupRolePK pk = new UserGroupRolePK(userId, groupId, roleId);
124 
125             try {
126                 userGroupRolePersistence.remove(pk);
127             }
128             catch (NoSuchUserGroupRoleException nsugre) {
129             }
130         }
131 
132         PermissionCacheUtil.clearCache();
133     }
134 
135     public void deleteUserGroupRolesByGroupId(long groupId)
136         throws SystemException {
137 
138         userGroupRolePersistence.removeByGroupId(groupId);
139 
140         PermissionCacheUtil.clearCache();
141     }
142 
143     public void deleteUserGroupRolesByRoleId(long roleId)
144         throws SystemException {
145 
146         userGroupRolePersistence.removeByRoleId(roleId);
147 
148         PermissionCacheUtil.clearCache();
149     }
150 
151     public void deleteUserGroupRolesByUserId(long userId)
152         throws SystemException {
153 
154         userGroupRolePersistence.removeByUserId(userId);
155 
156         PermissionCacheUtil.clearCache();
157     }
158 
159     public List<UserGroupRole> getUserGroupRoles(long userId, long groupId)
160         throws SystemException {
161 
162         return userGroupRolePersistence.findByU_G(userId, groupId);
163     }
164 
165     public List<UserGroupRole> getUserGroupRolesByGroupAndRole(
166             long groupId, long roleId)
167         throws SystemException {
168 
169         return userGroupRolePersistence.findByG_R(groupId, roleId);
170     }
171 
172     public boolean hasUserGroupRole(long userId, long groupId, long roleId)
173         throws SystemException {
174 
175         UserGroupRolePK pk = new UserGroupRolePK(userId, groupId, roleId);
176 
177         UserGroupRole userGroupRole =
178             userGroupRolePersistence.fetchByPrimaryKey(pk);
179 
180         if (userGroupRole != null) {
181             return true;
182         }
183         else {
184             return false;
185         }
186     }
187 
188     public boolean hasUserGroupRole(long userId, long groupId, String roleName)
189         throws PortalException, SystemException {
190 
191         User user = userPersistence.findByPrimaryKey(userId);
192 
193         long companyId = user.getCompanyId();
194 
195         Role role = rolePersistence.findByC_N(companyId, roleName);
196 
197         long roleId = role.getRoleId();
198 
199         return hasUserGroupRole(userId, groupId, roleId);
200     }
201 
202     protected void checkGroupResource(long groupId)
203         throws PortalException, SystemException {
204 
205         // Make sure that the individual resource for the group exists
206 
207         Group group = groupPersistence.findByPrimaryKey(groupId);
208 
209         resourceLocalService.addResource(
210             group.getCompanyId(), Group.class.getName(),
211             ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(groupId));
212     }
213 
214 }