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