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.model.Group;
28  import com.liferay.portal.model.Role;
29  import com.liferay.portal.model.User;
30  import com.liferay.portal.security.permission.ActionKeys;
31  import com.liferay.portal.service.base.RoleServiceBaseImpl;
32  import com.liferay.portal.service.permission.PortalPermissionUtil;
33  import com.liferay.portal.service.permission.RolePermissionUtil;
34  
35  import java.util.List;
36  
37  /**
38   * <a href="RoleServiceImpl.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class RoleServiceImpl extends RoleServiceBaseImpl {
44  
45      public Role addRole(String name, String description, int type)
46          throws PortalException, SystemException {
47  
48          User user = getUser();
49  
50          PortalPermissionUtil.check(getPermissionChecker(), ActionKeys.ADD_ROLE);
51  
52          return roleLocalService.addRole(
53              user.getUserId(), user.getCompanyId(), name, description, type);
54      }
55  
56      public void addUserRoles(long userId, long[] roleIds)
57          throws PortalException, SystemException {
58  
59          checkUserRolesPermission(userId, roleIds);
60  
61          roleLocalService.addUserRoles(userId, roleIds);
62      }
63  
64      public void deleteRole(long roleId)
65          throws PortalException, SystemException {
66  
67          RolePermissionUtil.check(
68              getPermissionChecker(), roleId, ActionKeys.DELETE);
69  
70          roleLocalService.deleteRole(roleId);
71      }
72  
73      public Role getGroupRole(long companyId, long groupId)
74          throws PortalException, SystemException {
75  
76          return roleLocalService.getGroupRole(companyId, groupId);
77      }
78  
79      public List<Role> getGroupRoles(long groupId) throws SystemException {
80          return roleLocalService.getGroupRoles(groupId);
81      }
82  
83      public Role getRole(long roleId)
84          throws PortalException, SystemException {
85  
86          return roleLocalService.getRole(roleId);
87      }
88  
89      public Role getRole(long companyId, String name)
90          throws PortalException, SystemException {
91  
92          return roleLocalService.getRole(companyId, name);
93      }
94  
95      public List<Role> getUserGroupRoles(long userId, long groupId)
96          throws SystemException {
97  
98          return roleLocalService.getUserGroupRoles(userId, groupId);
99      }
100 
101     public List<Role> getUserRelatedRoles(long userId, List<Group> groups)
102         throws SystemException {
103 
104         return roleLocalService.getUserRelatedRoles(userId, groups);
105     }
106 
107     public List<Role> getUserRoles(long userId) throws SystemException {
108         return roleLocalService.getUserRoles(userId);
109     }
110 
111     public boolean hasUserRole(
112             long userId, long companyId, String name, boolean inherited)
113         throws PortalException, SystemException {
114 
115         return roleLocalService.hasUserRole(userId, companyId, name, inherited);
116     }
117 
118     public boolean hasUserRoles(
119             long userId, long companyId, String[] names, boolean inherited)
120         throws PortalException, SystemException {
121 
122         return roleLocalService.hasUserRoles(
123             userId, companyId, names, inherited);
124     }
125 
126     public void unsetUserRoles(long userId, long[] roleIds)
127         throws PortalException, SystemException {
128 
129         checkUserRolesPermission(userId, roleIds);
130 
131         roleLocalService.unsetUserRoles(userId, roleIds);
132     }
133 
134     public Role updateRole(long roleId, String name, String description)
135         throws PortalException, SystemException {
136 
137         RolePermissionUtil.check(
138             getPermissionChecker(), roleId, ActionKeys.UPDATE);
139 
140         return roleLocalService.updateRole(roleId, name, description);
141     }
142 
143     protected void checkUserRolesPermission(long userId, long[] roleIds)
144         throws PortalException {
145 
146         for (int i = 0; i < roleIds.length; i++) {
147             RolePermissionUtil.check(
148                 getPermissionChecker(), roleIds[i], ActionKeys.ASSIGN_MEMBERS);
149         }
150     }
151 
152 }