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