1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.service.impl;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.security.permission.ActionKeys;
28  import com.liferay.portal.model.Role;
29  import com.liferay.portal.model.User;
30  import com.liferay.portal.service.base.RoleServiceBaseImpl;
31  import com.liferay.portal.service.permission.PortalPermissionUtil;
32  import com.liferay.portal.service.permission.RolePermissionUtil;
33  
34  import java.util.List;
35  
36  /**
37   * <a href="RoleServiceImpl.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   *
41   */
42  public class RoleServiceImpl extends RoleServiceBaseImpl {
43  
44      public Role addRole(String name, String description, int type)
45          throws PortalException, SystemException {
46  
47          User user = getUser();
48  
49          PortalPermissionUtil.check(getPermissionChecker(), ActionKeys.ADD_ROLE);
50  
51          return roleLocalService.addRole(
52              user.getUserId(), user.getCompanyId(), name, description, type);
53      }
54  
55      public void addUserRoles(long userId, long[] roleIds)
56          throws PortalException, SystemException {
57  
58          checkUserRolesPermission(userId, roleIds);
59  
60          roleLocalService.addUserRoles(userId, roleIds);
61      }
62  
63      public void deleteRole(long roleId)
64          throws PortalException, SystemException {
65  
66          RolePermissionUtil.check(
67              getPermissionChecker(), roleId, ActionKeys.DELETE);
68  
69          roleLocalService.deleteRole(roleId);
70      }
71  
72      public Role getGroupRole(long companyId, long groupId)
73          throws PortalException, SystemException {
74  
75          return roleLocalService.getGroupRole(companyId, groupId);
76      }
77  
78      public List getGroupRoles(long groupId)
79          throws PortalException, SystemException {
80  
81          return roleLocalService.getGroupRoles(groupId);
82      }
83  
84      public Role getRole(long roleId)
85          throws PortalException, SystemException {
86  
87          return roleLocalService.getRole(roleId);
88      }
89  
90      public Role getRole(long companyId, String name)
91          throws PortalException, SystemException {
92  
93          return roleLocalService.getRole(companyId, name);
94      }
95  
96      public List getUserGroupRoles(long userId, long groupId)
97          throws PortalException, SystemException {
98  
99          return roleLocalService.getUserGroupRoles(userId, groupId);
100     }
101 
102     public List getUserRelatedRoles(long userId, List groups)
103         throws PortalException, SystemException {
104 
105         return roleLocalService.getUserRelatedRoles(userId, groups);
106     }
107 
108     public List getUserRoles(long userId)
109         throws PortalException, SystemException {
110 
111         return roleLocalService.getUserRoles(userId);
112     }
113 
114     public boolean hasUserRole(
115             long userId, long companyId, String name, boolean inherited)
116         throws PortalException, SystemException {
117 
118         return roleLocalService.hasUserRole(userId, companyId, name, inherited);
119     }
120 
121     public boolean hasUserRoles(
122             long userId, long companyId, String[] names, boolean inherited)
123         throws PortalException, SystemException {
124 
125         return roleLocalService.hasUserRoles(
126             userId, companyId, names, inherited);
127     }
128 
129     public void unsetUserRoles(long userId, long[] roleIds)
130         throws PortalException, SystemException {
131 
132         checkUserRolesPermission(userId, roleIds);
133 
134         roleLocalService.unsetUserRoles(userId, roleIds);
135     }
136 
137     public Role updateRole(long roleId, String name, String description)
138         throws PortalException, SystemException {
139 
140         RolePermissionUtil.check(
141             getPermissionChecker(), roleId, ActionKeys.UPDATE);
142 
143         return roleLocalService.updateRole(roleId, name, description);
144     }
145 
146     protected void checkUserRolesPermission(long userId, long[] roleIds)
147         throws PortalException, SystemException {
148 
149         for (int i = 0; i < roleIds.length; i++) {
150             RolePermissionUtil.check(
151                 getPermissionChecker(), roleIds[i], ActionKeys.ASSIGN_MEMBERS);
152         }
153     }
154 
155 }