001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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.Role;
021    import com.liferay.portal.model.User;
022    import com.liferay.portal.model.UserGroupRole;
023    import com.liferay.portal.security.permission.PermissionCacheUtil;
024    import com.liferay.portal.service.base.UserGroupRoleLocalServiceBaseImpl;
025    import com.liferay.portal.service.persistence.UserGroupRolePK;
026    
027    import java.util.List;
028    
029    /**
030     * @author Jorge Ferrer
031     */
032    public class UserGroupRoleLocalServiceImpl
033            extends UserGroupRoleLocalServiceBaseImpl {
034    
035            public void addUserGroupRoles(long userId, long groupId, long[] roleIds)
036                    throws SystemException {
037    
038                    for (long roleId : roleIds) {
039                            UserGroupRolePK pk = new UserGroupRolePK(userId, groupId, roleId);
040    
041                            UserGroupRole userGroupRole =
042                                    userGroupRolePersistence.fetchByPrimaryKey(pk);
043    
044                            if (userGroupRole == null) {
045                                    userGroupRole = userGroupRolePersistence.create(pk);
046    
047                                    userGroupRolePersistence.update(userGroupRole);
048                            }
049                    }
050    
051                    PermissionCacheUtil.clearCache();
052            }
053    
054            public void addUserGroupRoles(long[] userIds, long groupId, long roleId)
055                    throws SystemException {
056    
057                    for (long userId : userIds) {
058                            UserGroupRolePK pk = new UserGroupRolePK(userId, groupId, roleId);
059    
060                            UserGroupRole userGroupRole =
061                                    userGroupRolePersistence.fetchByPrimaryKey(pk);
062    
063                            if (userGroupRole == null) {
064                                    userGroupRole = userGroupRolePersistence.create(pk);
065    
066                                    userGroupRolePersistence.update(userGroupRole);
067                            }
068                    }
069    
070                    PermissionCacheUtil.clearCache();
071            }
072    
073            @Override
074            public UserGroupRole deleteUserGroupRole(UserGroupRole userGroupRole)
075                    throws SystemException {
076    
077                    userGroupRolePersistence.remove(userGroupRole);
078    
079                    PermissionCacheUtil.clearCache();
080    
081                    return userGroupRole;
082            }
083    
084            public void deleteUserGroupRoles(long userId, long groupId, long[] roleIds)
085                    throws SystemException {
086    
087                    for (long roleId : roleIds) {
088                            UserGroupRolePK pk = new UserGroupRolePK(userId, groupId, roleId);
089    
090                            try {
091                                    userGroupRolePersistence.remove(pk);
092                            }
093                            catch (NoSuchUserGroupRoleException nsugre) {
094                            }
095                    }
096    
097                    PermissionCacheUtil.clearCache();
098            }
099    
100            public void deleteUserGroupRoles(long userId, long[] groupIds)
101                    throws SystemException {
102    
103                    for (long groupId : groupIds) {
104                            userGroupRolePersistence.removeByU_G(userId, groupId);
105                    }
106    
107                    PermissionCacheUtil.clearCache();
108            }
109    
110            public void deleteUserGroupRoles(long[] userIds, long groupId)
111                    throws SystemException {
112    
113                    for (long userId : userIds) {
114                            userGroupRolePersistence.removeByU_G(userId, groupId);
115                    }
116    
117                    PermissionCacheUtil.clearCache();
118            }
119    
120            public void deleteUserGroupRoles(long[] userIds, long groupId, long roleId)
121                    throws SystemException {
122    
123                    for (long userId : userIds) {
124                            UserGroupRolePK pk = new UserGroupRolePK(userId, groupId, roleId);
125    
126                            try {
127                                    userGroupRolePersistence.remove(pk);
128                            }
129                            catch (NoSuchUserGroupRoleException nsugre) {
130                            }
131                    }
132    
133                    PermissionCacheUtil.clearCache();
134            }
135    
136            public void deleteUserGroupRolesByGroupId(long groupId)
137                    throws SystemException {
138    
139                    userGroupRolePersistence.removeByGroupId(groupId);
140    
141                    PermissionCacheUtil.clearCache();
142            }
143    
144            public void deleteUserGroupRolesByRoleId(long roleId)
145                    throws SystemException {
146    
147                    userGroupRolePersistence.removeByRoleId(roleId);
148    
149                    PermissionCacheUtil.clearCache();
150            }
151    
152            public void deleteUserGroupRolesByUserId(long userId)
153                    throws SystemException {
154    
155                    userGroupRolePersistence.removeByUserId(userId);
156    
157                    PermissionCacheUtil.clearCache();
158            }
159    
160            public List<UserGroupRole> getUserGroupRoles(long userId)
161                    throws SystemException {
162    
163                    return userGroupRolePersistence.findByUserId(userId);
164            }
165    
166            public List<UserGroupRole> getUserGroupRoles(long userId, long groupId)
167                    throws SystemException {
168    
169                    return userGroupRolePersistence.findByU_G(userId, groupId);
170            }
171    
172            public List<UserGroupRole> getUserGroupRolesByGroupAndRole(
173                            long groupId, long roleId)
174                    throws SystemException {
175    
176                    return userGroupRolePersistence.findByG_R(groupId, roleId);
177            }
178    
179            public List<UserGroupRole> getUserGroupRolesByUserUserGroupAndGroup(
180                            long userId, long groupId)
181                    throws SystemException {
182    
183                    return userGroupRoleFinder.findByUserUserGroupGroupRole(
184                            userId, groupId);
185            }
186    
187            public boolean hasUserGroupRole(long userId, long groupId, long roleId)
188                    throws SystemException {
189    
190                    return hasUserGroupRole(userId, groupId, roleId, false);
191            }
192    
193            public boolean hasUserGroupRole(
194                            long userId, long groupId, long roleId, boolean inherit)
195                    throws SystemException {
196    
197                    UserGroupRolePK pk = new UserGroupRolePK(userId, groupId, roleId);
198    
199                    UserGroupRole userGroupRole =
200                            userGroupRolePersistence.fetchByPrimaryKey(pk);
201    
202                    if (userGroupRole != null) {
203                            return true;
204                    }
205    
206                    if (inherit) {
207                            if (roleFinder.countByU_G_R(userId, groupId, roleId) > 0) {
208                                    return true;
209                            }
210                    }
211    
212                    return false;
213            }
214    
215            public boolean hasUserGroupRole(long userId, long groupId, String roleName)
216                    throws PortalException, SystemException {
217    
218                    return hasUserGroupRole(userId, groupId, roleName, false);
219            }
220    
221            public boolean hasUserGroupRole(
222                            long userId, long groupId, String roleName, boolean inherit)
223                    throws PortalException, SystemException {
224    
225                    User user = userPersistence.findByPrimaryKey(userId);
226    
227                    long companyId = user.getCompanyId();
228    
229                    Role role = rolePersistence.findByC_N(companyId, roleName);
230    
231                    long roleId = role.getRoleId();
232    
233                    return hasUserGroupRole(userId, groupId, roleId, inherit);
234            }
235    
236    }