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