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