1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.service.impl;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.model.Group;
20  import com.liferay.portal.model.Role;
21  import com.liferay.portal.model.User;
22  import com.liferay.portal.security.permission.ActionKeys;
23  import com.liferay.portal.service.base.RoleServiceBaseImpl;
24  import com.liferay.portal.service.permission.PortalPermissionUtil;
25  import com.liferay.portal.service.permission.RolePermissionUtil;
26  
27  import java.util.List;
28  import java.util.Locale;
29  import java.util.Map;
30  
31  /**
32   * <a href="RoleServiceImpl.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   */
36  public class RoleServiceImpl extends RoleServiceBaseImpl {
37  
38      public Role addRole(
39              String name, Map<Locale, String> titleMap, String description,
40              int type)
41          throws PortalException, SystemException {
42  
43          User user = getUser();
44  
45          PortalPermissionUtil.check(getPermissionChecker(), ActionKeys.ADD_ROLE);
46  
47          return roleLocalService.addRole(
48              user.getUserId(), user.getCompanyId(), name, titleMap, description,
49              type);
50      }
51  
52      public void addUserRoles(long userId, long[] roleIds)
53          throws PortalException, SystemException {
54  
55          checkUserRolesPermission(userId, roleIds);
56  
57          roleLocalService.addUserRoles(userId, roleIds);
58      }
59  
60      public void deleteRole(long roleId)
61          throws PortalException, SystemException {
62  
63          RolePermissionUtil.check(
64              getPermissionChecker(), roleId, ActionKeys.DELETE);
65  
66          roleLocalService.deleteRole(roleId);
67      }
68  
69      public Role getGroupRole(long companyId, long groupId)
70          throws PortalException, SystemException {
71  
72          return roleLocalService.getGroupRole(companyId, groupId);
73      }
74  
75      public List<Role> getGroupRoles(long groupId) throws SystemException {
76          return roleLocalService.getGroupRoles(groupId);
77      }
78  
79      public Role getRole(long roleId)
80          throws PortalException, SystemException {
81  
82          return roleLocalService.getRole(roleId);
83      }
84  
85      public Role getRole(long companyId, String name)
86          throws PortalException, SystemException {
87  
88          return roleLocalService.getRole(companyId, name);
89      }
90  
91      public List<Role> getUserGroupGroupRoles(long userId, long groupId)
92          throws SystemException {
93  
94          return roleLocalService.getUserGroupGroupRoles(userId, groupId);
95      }
96  
97      public List<Role> getUserGroupRoles(long userId, long groupId)
98          throws SystemException {
99  
100         return roleLocalService.getUserGroupRoles(userId, groupId);
101     }
102 
103     public List<Role> getUserRelatedRoles(long userId, List<Group> groups)
104         throws SystemException {
105 
106         return roleLocalService.getUserRelatedRoles(userId, groups);
107     }
108 
109     public List<Role> getUserRoles(long userId) throws SystemException {
110         return roleLocalService.getUserRoles(userId);
111     }
112 
113     public boolean hasUserRole(
114             long userId, long companyId, String name, boolean inherited)
115         throws PortalException, SystemException {
116 
117         return roleLocalService.hasUserRole(userId, companyId, name, inherited);
118     }
119 
120     public boolean hasUserRoles(
121             long userId, long companyId, String[] names, boolean inherited)
122         throws PortalException, SystemException {
123 
124         return roleLocalService.hasUserRoles(
125             userId, companyId, names, inherited);
126     }
127 
128     public void unsetUserRoles(long userId, long[] roleIds)
129         throws PortalException, SystemException {
130 
131         checkUserRolesPermission(userId, roleIds);
132 
133         roleLocalService.unsetUserRoles(userId, roleIds);
134     }
135 
136     public Role updateRole(
137             long roleId, String name, Map<Locale, String> titleMap,
138             String description, String subtype)
139         throws PortalException, SystemException {
140 
141         RolePermissionUtil.check(
142             getPermissionChecker(), roleId, ActionKeys.UPDATE);
143 
144         return roleLocalService.updateRole(
145             roleId, name, titleMap, description, subtype);
146     }
147 
148     protected void checkUserRolesPermission(long userId, long[] roleIds)
149         throws PortalException {
150 
151         for (int i = 0; i < roleIds.length; i++) {
152             RolePermissionUtil.check(
153                 getPermissionChecker(), roleIds[i], ActionKeys.ASSIGN_MEMBERS);
154         }
155     }
156 
157 }