001    /**
002     * Copyright (c) 2000-2010 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            public void deleteUserGroupRole(UserGroupRole userGroupRole)
080                    throws SystemException {
081    
082                    userGroupRolePersistence.remove(userGroupRole);
083    
084                    PermissionCacheUtil.clearCache();
085            }
086    
087            public void deleteUserGroupRoles(
088                            long userId, long groupId, long[] roleIds)
089                    throws SystemException {
090    
091                    for (long roleId : roleIds) {
092                            UserGroupRolePK pk = new UserGroupRolePK(userId, groupId, roleId);
093    
094                            try {
095                                    userGroupRolePersistence.remove(pk);
096                            }
097                            catch (NoSuchUserGroupRoleException nsugre) {
098                            }
099                    }
100    
101                    PermissionCacheUtil.clearCache();
102            }
103    
104            public void deleteUserGroupRoles(long userId, long[] groupIds)
105                    throws SystemException {
106    
107                    for (long groupId : groupIds) {
108                            userGroupRolePersistence.removeByU_G(userId, groupId);
109                    }
110    
111                    PermissionCacheUtil.clearCache();
112            }
113    
114            public void deleteUserGroupRoles(long[] userIds, long groupId)
115                    throws SystemException {
116    
117                    for (long userId : userIds) {
118                            userGroupRolePersistence.removeByU_G(userId, groupId);
119                    }
120    
121                    PermissionCacheUtil.clearCache();
122            }
123    
124            public void deleteUserGroupRoles(long[] userIds, long groupId, long roleId)
125                    throws SystemException {
126    
127                    for (long userId : userIds) {
128                            UserGroupRolePK pk = new UserGroupRolePK(userId, groupId, roleId);
129    
130                            try {
131                                    userGroupRolePersistence.remove(pk);
132                            }
133                            catch (NoSuchUserGroupRoleException nsugre) {
134                            }
135                    }
136    
137                    PermissionCacheUtil.clearCache();
138            }
139    
140            public void deleteUserGroupRolesByGroupId(long groupId)
141                    throws SystemException {
142    
143                    userGroupRolePersistence.removeByGroupId(groupId);
144    
145                    PermissionCacheUtil.clearCache();
146            }
147    
148            public void deleteUserGroupRolesByRoleId(long roleId)
149                    throws SystemException {
150    
151                    userGroupRolePersistence.removeByRoleId(roleId);
152    
153                    PermissionCacheUtil.clearCache();
154            }
155    
156            public void deleteUserGroupRolesByUserId(long userId)
157                    throws SystemException {
158    
159                    userGroupRolePersistence.removeByUserId(userId);
160    
161                    PermissionCacheUtil.clearCache();
162            }
163    
164            public List<UserGroupRole> getUserGroupRoles(long userId)
165                    throws SystemException {
166    
167                    return userGroupRolePersistence.findByUserId(userId);
168            }
169    
170            public List<UserGroupRole> getUserGroupRoles(long userId, long groupId)
171                    throws SystemException {
172    
173                    return userGroupRolePersistence.findByU_G(userId, groupId);
174            }
175    
176            public List<UserGroupRole> getUserGroupRolesByGroupAndRole(
177                            long groupId, long roleId)
178                    throws SystemException {
179    
180                    return userGroupRolePersistence.findByG_R(groupId, roleId);
181            }
182    
183            public List<UserGroupRole> getUserGroupRolesByUserUserGroupAndGroup(
184                            long userId, long groupId)
185                    throws SystemException {
186    
187                    return userGroupRoleFinder.findByUserUserGroupGroupRole(
188                            userId, groupId);
189            }
190    
191            public boolean hasUserGroupRole(long userId, long groupId, long roleId)
192                    throws SystemException {
193    
194                    return hasUserGroupRole(userId, groupId, roleId, false);
195            }
196    
197            public boolean hasUserGroupRole(
198                            long userId, long groupId, long roleId, boolean inherit)
199                    throws SystemException {
200    
201                    UserGroupRolePK pk = new UserGroupRolePK(userId, groupId, roleId);
202    
203                    UserGroupRole userGroupRole =
204                            userGroupRolePersistence.fetchByPrimaryKey(pk);
205    
206                    if (userGroupRole != null) {
207                            return true;
208                    }
209    
210                    if (inherit) {
211                            if (roleFinder.countByU_G_R(userId, groupId, roleId) > 0) {
212                                    return true;
213                            }
214                    }
215    
216                    return false;
217            }
218    
219            public boolean hasUserGroupRole(long userId, long groupId, String roleName)
220                    throws PortalException, SystemException {
221    
222                    return hasUserGroupRole(userId, groupId, roleName, false);
223            }
224    
225            public boolean hasUserGroupRole(
226                            long userId, long groupId, String roleName, boolean inherit)
227                    throws PortalException, SystemException {
228    
229                    User user = userPersistence.findByPrimaryKey(userId);
230    
231                    long companyId = user.getCompanyId();
232    
233                    Role role = rolePersistence.findByC_N(companyId, roleName);
234    
235                    long roleId = role.getRoleId();
236    
237                    return hasUserGroupRole(userId, groupId, roleId, inherit);
238            }
239    
240            protected void checkGroupResource(long groupId)
241                    throws PortalException, SystemException {
242    
243                    // Make sure that the individual resource for the group exists
244    
245                    Group group = groupPersistence.findByPrimaryKey(groupId);
246    
247                    resourceLocalService.addResource(
248                            group.getCompanyId(), Group.class.getName(),
249                            ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(groupId));
250            }
251    
252    }