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.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.model.Group;
020    import com.liferay.portal.model.Role;
021    import com.liferay.portal.model.User;
022    import com.liferay.portal.security.permission.ActionKeys;
023    import com.liferay.portal.service.base.RoleServiceBaseImpl;
024    import com.liferay.portal.service.permission.PortalPermissionUtil;
025    import com.liferay.portal.service.permission.RolePermissionUtil;
026    import com.liferay.portal.service.permission.UserPermissionUtil;
027    
028    import java.util.ArrayList;
029    import java.util.List;
030    import java.util.Locale;
031    import java.util.Map;
032    
033    /**
034     * The implementation of the role remote service.
035     *
036     * @author Brian Wing Shun Chan
037     */
038    public class RoleServiceImpl extends RoleServiceBaseImpl {
039    
040            /**
041             * Adds a role. The user is reindexed after role is added.
042             *
043             * @param  name the role's name
044             * @param  titleMap the role's localized titles (optionally
045             *         <code>null</code>)
046             * @param  descriptionMap the role's localized descriptions (optionally
047             *         <code>null</code>)
048             * @param  type the role's type (optionally <code>0</code>)
049             * @return the role
050             * @throws PortalException if a user with the primary key could not be
051             *         found, if the user did not have permission to add roles, if the
052             *         class name or the role name were invalid, or if the role is a
053             *         duplicate
054             * @throws SystemException if a system exception occurred
055             */
056            public Role addRole(
057                            String name, Map<Locale, String> titleMap,
058                            Map<Locale, String> descriptionMap, int type)
059                    throws PortalException, SystemException {
060    
061                    PortalPermissionUtil.check(getPermissionChecker(), ActionKeys.ADD_ROLE);
062    
063                    User user = getUser();
064    
065                    return roleLocalService.addRole(
066                            user.getUserId(), user.getCompanyId(), name, titleMap,
067                            descriptionMap, type);
068            }
069    
070            /**
071             * Adds the roles to the user. The user is reindexed after the roles are
072             * added.
073             *
074             * @param  userId the primary key of the user
075             * @param  roleIds the primary keys of the roles
076             * @throws PortalException if a user with the primary key could not be found
077             *         or if the user did not have permission to assign members to one
078             *         of the roles
079             * @throws SystemException if a system exception occurred
080             */
081            public void addUserRoles(long userId, long[] roleIds)
082                    throws PortalException, SystemException {
083    
084                    checkUserRolesPermission(userId, roleIds);
085    
086                    roleLocalService.addUserRoles(userId, roleIds);
087            }
088    
089            /**
090             * Deletes the role with the primary key and its associated permissions.
091             *
092             * @param  roleId the primary key of the role
093             * @throws PortalException if the user did not have permission to delete the
094             *         role, if a role with the primary key could not be found, if the
095             *         role is a default system role, or if the role's resource could
096             *         not be found
097             * @throws SystemException if a system exception occurred
098             */
099            public void deleteRole(long roleId)
100                    throws PortalException, SystemException {
101    
102                    RolePermissionUtil.check(
103                            getPermissionChecker(), roleId, ActionKeys.DELETE);
104    
105                    roleLocalService.deleteRole(roleId);
106            }
107    
108            /**
109             * Returns all the roles associated with the group.
110             *
111             * @param  groupId the primary key of the group
112             * @return the roles associated with the group
113             * @throws PortalException if a portal exception occurred
114             * @throws SystemException if a system exception occurred
115             */
116            public List<Role> getGroupRoles(long groupId)
117                    throws PortalException, SystemException {
118    
119                    List<Role> roles = roleLocalService.getGroupRoles(groupId);
120    
121                    return filterRoles(roles);
122            }
123    
124            /**
125             * Returns the role with the primary key.
126             *
127             * @param  roleId the primary key of the role
128             * @return the role with the primary key
129             * @throws PortalException if a role with the primary key could not be found
130             *         or if the user did not have permission to view the role
131             * @throws SystemException if a system exception occurred
132             */
133            public Role getRole(long roleId) throws PortalException, SystemException {
134                    RolePermissionUtil.check(
135                            getPermissionChecker(), roleId, ActionKeys.VIEW);
136    
137                    return roleLocalService.getRole(roleId);
138            }
139    
140            /**
141             * Returns the role with the name in the company.
142             *
143             * <p>
144             * The method searches the system roles map first for default roles. If a
145             * role with the name is not found, then the method will query the database.
146             * </p>
147             *
148             * @param  companyId the primary key of the company
149             * @param  name the role's name
150             * @return the role with the name
151             * @throws PortalException if a role with the name could not be found in the
152             *         company or if the user did not have permission to view the role
153             * @throws SystemException if a system exception occurred
154             */
155            public Role getRole(long companyId, String name)
156                    throws PortalException, SystemException {
157    
158                    Role role = roleLocalService.getRole(companyId, name);
159    
160                    RolePermissionUtil.check(
161                            getPermissionChecker(), role.getRoleId(), ActionKeys.VIEW);
162    
163                    return role;
164            }
165    
166            /**
167             * Returns all the user's roles within the user group.
168             *
169             * @param  userId the primary key of the user
170             * @param  groupId the primary key of the group
171             * @return the user's roles within the user group
172             * @throws PortalException if a portal exception occurred
173             * @throws SystemException if a system exception occurred
174             */
175            public List<Role> getUserGroupGroupRoles(long userId, long groupId)
176                    throws PortalException, SystemException {
177    
178                    UserPermissionUtil.check(
179                            getPermissionChecker(), userId, ActionKeys.VIEW);
180    
181                    List<Role> roles = roleLocalService.getUserGroupGroupRoles(
182                            userId, groupId);
183    
184                    return filterRoles(roles);
185            }
186    
187            /**
188             * Returns all the user's roles within the user group.
189             *
190             * @param  userId the primary key of the user
191             * @param  groupId the primary key of the group
192             * @return the user's roles within the user group
193             * @throws PortalException if a portal exception occurred
194             * @throws SystemException if a system exception occurred
195             */
196            public List<Role> getUserGroupRoles(long userId, long groupId)
197                    throws PortalException, SystemException {
198    
199                    UserPermissionUtil.check(
200                            getPermissionChecker(), userId, ActionKeys.VIEW);
201    
202                    List<Role> roles = roleLocalService.getUserGroupRoles(userId, groupId);
203    
204                    return filterRoles(roles);
205            }
206    
207            /**
208             * Returns the union of all the user's roles within the groups.
209             *
210             * @param  userId the primary key of the user
211             * @param  groups the groups (optionally <code>null</code>)
212             * @return the union of all the user's roles within the groups
213             * @throws PortalException if a portal exception occurred
214             * @throws SystemException if a system exception occurred
215             */
216            public List<Role> getUserRelatedRoles(long userId, List<Group> groups)
217                    throws PortalException, SystemException {
218    
219                    UserPermissionUtil.check(
220                            getPermissionChecker(), userId, ActionKeys.VIEW);
221    
222                    List<Role> roles = roleLocalService.getUserRelatedRoles(userId, groups);
223    
224                    return filterRoles(roles);
225            }
226    
227            /**
228             * Returns all the roles associated with the user.
229             *
230             * @param  userId the primary key of the user
231             * @return the roles associated with the user
232             * @throws PortalException if a portal exception occurred
233             * @throws SystemException if a system exception occurred
234             */
235            public List<Role> getUserRoles(long userId)
236                    throws PortalException, SystemException {
237    
238                    UserPermissionUtil.check(
239                            getPermissionChecker(), userId, ActionKeys.VIEW);
240    
241                    List<Role> roles = roleLocalService.getUserRoles(userId);
242    
243                    return filterRoles(roles);
244            }
245    
246            /**
247             * Returns <code>true</code> if the user is associated with the named
248             * regular role.
249             *
250             * @param  userId the primary key of the user
251             * @param  companyId the primary key of the company
252             * @param  name the name of the role
253             * @param  inherited whether to include the user's inherited roles in the
254             *         search
255             * @return <code>true</code> if the user is associated with the regular
256             *         role; <code>false</code> otherwise
257             * @throws PortalException if a role with the name could not be found in the
258             *         company or if a default user for the company could not be found
259             * @throws SystemException if a system exception occurred
260             */
261            public boolean hasUserRole(
262                            long userId, long companyId, String name, boolean inherited)
263                    throws PortalException, SystemException {
264    
265                    UserPermissionUtil.check(
266                            getPermissionChecker(), userId, ActionKeys.VIEW);
267    
268                    return roleLocalService.hasUserRole(userId, companyId, name, inherited);
269            }
270    
271            /**
272             * Returns <code>true</code> if the user has any one of the named regular
273             * roles.
274             *
275             * @param  userId the primary key of the user
276             * @param  companyId the primary key of the company
277             * @param  names the names of the roles
278             * @param  inherited whether to include the user's inherited roles in the
279             *         search
280             * @return <code>true</code> if the user has any one of the regular roles;
281             *         <code>false</code> otherwise
282             * @throws PortalException if any one of the roles with the names could not
283             *         be found in the company or if the default user for the company
284             *         could not be found
285             * @throws SystemException if a system exception occurred
286             */
287            public boolean hasUserRoles(
288                            long userId, long companyId, String[] names, boolean inherited)
289                    throws PortalException, SystemException {
290    
291                    UserPermissionUtil.check(
292                            getPermissionChecker(), userId, ActionKeys.VIEW);
293    
294                    return roleLocalService.hasUserRoles(
295                            userId, companyId, names, inherited);
296            }
297    
298            /**
299             * Removes the matching roles associated with the user. The user is
300             * reindexed after the roles are removed.
301             *
302             * @param  userId the primary key of the user
303             * @param  roleIds the primary keys of the roles
304             * @throws PortalException if a user with the primary key could not be
305             *         found, if the user did not have permission to remove members from
306             *         a role, or if a role with any one of the primary keys could not
307             *         be found
308             * @throws SystemException if a system exception occurred
309             */
310            public void unsetUserRoles(long userId, long[] roleIds)
311                    throws PortalException, SystemException {
312    
313                    checkUserRolesPermission(userId, roleIds);
314    
315                    roleLocalService.unsetUserRoles(userId, roleIds);
316            }
317    
318            /**
319             * Updates the role with the primary key.
320             *
321             * @param  roleId the primary key of the role
322             * @param  name the role's new name
323             * @param  titleMap the new localized titles (optionally <code>null</code>)
324             *         to replace those existing for the role
325             * @param  descriptionMap the new localized descriptions (optionally
326             *         <code>null</code>) to replace those existing for the role
327             * @param  subtype the role's new subtype (optionally <code>null</code>)
328             * @return the role with the primary key
329             * @throws PortalException if the user did not have permission to update the
330             *         role, if a role with the primary could not be found, or if the
331             *         role's name was invalid
332             * @throws SystemException if a system exception occurred
333             */
334            public Role updateRole(
335                            long roleId, String name, Map<Locale, String> titleMap,
336                            Map<Locale, String> descriptionMap, String subtype)
337                    throws PortalException, SystemException {
338    
339                    RolePermissionUtil.check(
340                            getPermissionChecker(), roleId, ActionKeys.UPDATE);
341    
342                    return roleLocalService.updateRole(
343                            roleId, name, titleMap, descriptionMap, subtype);
344            }
345    
346            protected void checkUserRolesPermission(long userId, long[] roleIds)
347                    throws PortalException {
348    
349                    for (int i = 0; i < roleIds.length; i++) {
350                            RolePermissionUtil.check(
351                                    getPermissionChecker(), roleIds[i], ActionKeys.ASSIGN_MEMBERS);
352                    }
353            }
354    
355            protected List<Role> filterRoles(List<Role> roles) throws PortalException {
356                    List<Role> filteredRoles = new ArrayList<Role>();
357    
358                    for (Role role : roles) {
359                            if (RolePermissionUtil.contains(
360                                            getPermissionChecker(), role.getRoleId(),
361                                            ActionKeys.VIEW)) {
362    
363                                    filteredRoles.add(role);
364                            }
365                    }
366    
367                    return filteredRoles;
368            }
369    
370    }