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