1   /**
2    * Copyright (c) 2000-2007 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.impl.ResourceImpl;
30  import com.liferay.portal.security.permission.PermissionCacheUtil;
31  import com.liferay.portal.service.GroupLocalServiceUtil;
32  import com.liferay.portal.service.ResourceLocalServiceUtil;
33  import com.liferay.portal.service.base.UserGroupRoleLocalServiceBaseImpl;
34  import com.liferay.portal.service.persistence.UserGroupRolePK;
35  import com.liferay.portal.service.persistence.UserGroupRoleUtil;
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              if (UserGroupRoleUtil.fetchByPrimaryKey(pk) == null) {
60                  UserGroupRoleUtil.update(UserGroupRoleUtil.create(pk));
61              }
62          }
63  
64          PermissionCacheUtil.clearCache();
65      }
66  
67      public void addUserGroupRoles(long[] userIds, long groupId, long roleId)
68          throws PortalException, SystemException {
69  
70          checkGroupResource(groupId);
71  
72          for (int i = 0; i < userIds.length; i++) {
73              long userId = userIds[i];
74  
75              UserGroupRolePK pk = new UserGroupRolePK(userId, groupId, roleId);
76  
77              if (UserGroupRoleUtil.fetchByPrimaryKey(pk) == null) {
78                  UserGroupRoleUtil.update(UserGroupRoleUtil.create(pk));
79              }
80          }
81  
82          PermissionCacheUtil.clearCache();
83      }
84  
85      public void deleteUserGroupRoles(
86              long userId, long groupId, long[] roleIds)
87          throws PortalException, SystemException {
88  
89          for (int i = 0; i < roleIds.length; i++) {
90              long roleId = roleIds[i];
91  
92              try {
93                  UserGroupRoleUtil.remove(
94                      new UserGroupRolePK(userId, groupId, roleId));
95              }
96              catch (NoSuchUserGroupRoleException nsugre) {
97              }
98          }
99  
100         PermissionCacheUtil.clearCache();
101     }
102 
103     public void deleteUserGroupRoles(long[] userIds, long groupId, long roleId)
104         throws PortalException, SystemException {
105 
106         for (int i = 0; i < userIds.length; i++) {
107             long userId = userIds[i];
108 
109             try {
110                 UserGroupRoleUtil.remove(
111                     new UserGroupRolePK(userId, groupId, roleId));
112             }
113             catch (NoSuchUserGroupRoleException nsugre) {
114             }
115         }
116 
117         PermissionCacheUtil.clearCache();
118     }
119 
120     public void deleteUserGroupRoles(long[] userIds, long groupId)
121         throws SystemException {
122 
123         for (int i = 0; i < userIds.length; i++) {
124             long userId = userIds[i];
125 
126             UserGroupRoleUtil.removeByU_G(userId, groupId);
127         }
128 
129         PermissionCacheUtil.clearCache();
130     }
131 
132     public void deleteUserGroupRolesByGroupId(long groupId)
133         throws SystemException {
134 
135         UserGroupRoleUtil.removeByGroupId(groupId);
136 
137         PermissionCacheUtil.clearCache();
138     }
139 
140     public void deleteUserGroupRolesByRoleId(long roleId)
141         throws SystemException {
142 
143         UserGroupRoleUtil.removeByRoleId(roleId);
144 
145         PermissionCacheUtil.clearCache();
146     }
147 
148     public void deleteUserGroupRolesByUserId(long userId)
149         throws SystemException {
150 
151         UserGroupRoleUtil.removeByUserId(userId);
152 
153         PermissionCacheUtil.clearCache();
154     }
155 
156     public List getUserGroupRoles(long userId, long groupId)
157         throws PortalException, SystemException {
158 
159         return UserGroupRoleUtil.findByU_G(userId, groupId);
160     }
161 
162     public boolean hasUserGroupRole(long userId, long groupId, long roleId)
163         throws PortalException, SystemException {
164 
165         UserGroupRolePK pk = new UserGroupRolePK(userId, groupId, roleId);
166 
167         if (UserGroupRoleUtil.fetchByPrimaryKey(pk) != null) {
168             return true;
169         }
170         else {
171             return false;
172         }
173     }
174 
175     protected void checkGroupResource(long groupId)
176         throws PortalException, SystemException {
177 
178         // Make sure that the individual resource for the group exists
179 
180         Group group = GroupLocalServiceUtil.getGroup(groupId);
181 
182         ResourceLocalServiceUtil.addResource(
183             group.getCompanyId(), Group.class.getName(),
184             ResourceImpl.SCOPE_INDIVIDUAL, String.valueOf(groupId));
185     }
186 
187 }