001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.service.impl;
016    
017    import com.liferay.portal.NoSuchUserGroupRoleException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.model.Group;
021    import com.liferay.portal.model.ResourceConstants;
022    import com.liferay.portal.model.Role;
023    import com.liferay.portal.model.User;
024    import com.liferay.portal.model.UserGroupRole;
025    import com.liferay.portal.security.permission.PermissionCacheUtil;
026    import com.liferay.portal.service.base.UserGroupRoleLocalServiceBaseImpl;
027    import com.liferay.portal.service.persistence.UserGroupRolePK;
028    
029    import java.util.List;
030    
031    /**
032     * @author Jorge Ferrer
033     */
034    public class UserGroupRoleLocalServiceImpl
035            extends UserGroupRoleLocalServiceBaseImpl {
036    
037            public void addUserGroupRoles(long userId, long groupId, long[] roleIds)
038                    throws PortalException, SystemException {
039    
040                    checkGroupResource(groupId);
041    
042                    for (long roleId : roleIds) {
043                            UserGroupRolePK pk = new UserGroupRolePK(userId, groupId, roleId);
044    
045                            UserGroupRole userGroupRole =
046                                    userGroupRolePersistence.fetchByPrimaryKey(pk);
047    
048                            if (userGroupRole == null) {
049                                    userGroupRole = userGroupRolePersistence.create(pk);
050    
051                                    userGroupRolePersistence.update(userGroupRole, false);
052                            }
053                    }
054    
055                    PermissionCacheUtil.clearCache();
056            }
057    
058            public void addUserGroupRoles(long[] userIds, long groupId, long roleId)
059                    throws PortalException, SystemException {
060    
061                    checkGroupResource(groupId);
062    
063                    for (long userId : userIds) {
064                            UserGroupRolePK pk = new UserGroupRolePK(userId, groupId, roleId);
065    
066                            UserGroupRole userGroupRole =
067                                    userGroupRolePersistence.fetchByPrimaryKey(pk);
068    
069                            if (userGroupRole == null) {
070                                    userGroupRole = userGroupRolePersistence.create(pk);
071    
072                                    userGroupRolePersistence.update(userGroupRole, false);
073                            }
074                    }
075    
076                    PermissionCacheUtil.clearCache();
077            }
078    
079            @Override
080            public void deleteUserGroupRole(UserGroupRole userGroupRole)
081                    throws SystemException {
082    
083                    userGroupRolePersistence.remove(userGroupRole);
084    
085                    PermissionCacheUtil.clearCache();
086            }
087    
088            public void deleteUserGroupRoles(
089                            long userId, long groupId, long[] roleIds)
090                    throws SystemException {
091    
092                    for (long roleId : roleIds) {
093                            UserGroupRolePK pk = new UserGroupRolePK(userId, groupId, roleId);
094    
095                            try {
096                                    userGroupRolePersistence.remove(pk);
097                            }
098                            catch (NoSuchUserGroupRoleException nsugre) {
099                            }
100                    }
101    
102                    PermissionCacheUtil.clearCache();
103            }
104    
105            public void deleteUserGroupRoles(long userId, long[] groupIds)
106                    throws SystemException {
107    
108                    for (long groupId : groupIds) {
109                            userGroupRolePersistence.removeByU_G(userId, groupId);
110                    }
111    
112                    PermissionCacheUtil.clearCache();
113            }
114    
115            public void deleteUserGroupRoles(long[] userIds, long groupId)
116                    throws SystemException {
117    
118                    for (long userId : userIds) {
119                            userGroupRolePersistence.removeByU_G(userId, groupId);
120                    }
121    
122                    PermissionCacheUtil.clearCache();
123            }
124    
125            public void deleteUserGroupRoles(long[] userIds, long groupId, long roleId)
126                    throws SystemException {
127    
128                    for (long userId : userIds) {
129                            UserGroupRolePK pk = new UserGroupRolePK(userId, groupId, roleId);
130    
131                            try {
132                                    userGroupRolePersistence.remove(pk);
133                            }
134                            catch (NoSuchUserGroupRoleException nsugre) {
135                            }
136                    }
137    
138                    PermissionCacheUtil.clearCache();
139            }
140    
141            public void deleteUserGroupRolesByGroupId(long groupId)
142                    throws SystemException {
143    
144                    userGroupRolePersistence.removeByGroupId(groupId);
145    
146                    PermissionCacheUtil.clearCache();
147            }
148    
149            public void deleteUserGroupRolesByRoleId(long roleId)
150                    throws SystemException {
151    
152                    userGroupRolePersistence.removeByRoleId(roleId);
153    
154                    PermissionCacheUtil.clearCache();
155            }
156    
157            public void deleteUserGroupRolesByUserId(long userId)
158                    throws SystemException {
159    
160                    userGroupRolePersistence.removeByUserId(userId);
161    
162                    PermissionCacheUtil.clearCache();
163            }
164    
165            public List<UserGroupRole> getUserGroupRoles(long userId)
166                    throws SystemException {
167    
168                    return userGroupRolePersistence.findByUserId(userId);
169            }
170    
171            public List<UserGroupRole> getUserGroupRoles(long userId, long groupId)
172                    throws SystemException {
173    
174                    return userGroupRolePersistence.findByU_G(userId, groupId);
175            }
176    
177            public List<UserGroupRole> getUserGroupRolesByGroupAndRole(
178                            long groupId, long roleId)
179                    throws SystemException {
180    
181                    return userGroupRolePersistence.findByG_R(groupId, roleId);
182            }
183    
184            public List<UserGroupRole> getUserGroupRolesByUserUserGroupAndGroup(
185                            long userId, long groupId)
186                    throws SystemException {
187    
188                    return userGroupRoleFinder.findByUserUserGroupGroupRole(
189                            userId, groupId);
190            }
191    
192            public boolean hasUserGroupRole(long userId, long groupId, long roleId)
193                    throws SystemException {
194    
195                    return hasUserGroupRole(userId, groupId, roleId, false);
196            }
197    
198            public boolean hasUserGroupRole(
199                            long userId, long groupId, long roleId, boolean inherit)
200                    throws SystemException {
201    
202                    UserGroupRolePK pk = new UserGroupRolePK(userId, groupId, roleId);
203    
204                    UserGroupRole userGroupRole =
205                            userGroupRolePersistence.fetchByPrimaryKey(pk);
206    
207                    if (userGroupRole != null) {
208                            return true;
209                    }
210    
211                    if (inherit) {
212                            if (roleFinder.countByU_G_R(userId, groupId, roleId) > 0) {
213                                    return true;
214                            }
215                    }
216    
217                    return false;
218            }
219    
220            public boolean hasUserGroupRole(long userId, long groupId, String roleName)
221                    throws PortalException, SystemException {
222    
223                    return hasUserGroupRole(userId, groupId, roleName, false);
224            }
225    
226            public boolean hasUserGroupRole(
227                            long userId, long groupId, String roleName, boolean inherit)
228                    throws PortalException, SystemException {
229    
230                    User user = userPersistence.findByPrimaryKey(userId);
231    
232                    long companyId = user.getCompanyId();
233    
234                    Role role = rolePersistence.findByC_N(companyId, roleName);
235    
236                    long roleId = role.getRoleId();
237    
238                    return hasUserGroupRole(userId, groupId, roleId, inherit);
239            }
240    
241            protected void checkGroupResource(long groupId)
242                    throws PortalException, SystemException {
243    
244                    // Make sure that the individual resource for the group exists
245    
246                    Group group = groupPersistence.findByPrimaryKey(groupId);
247    
248                    resourceLocalService.addResource(
249                            group.getCompanyId(), Group.class.getName(),
250                            ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(groupId));
251            }
252    
253    }