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