1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  import java.util.Locale;
37  import java.util.Map;
38  
39  /**
40   * <a href="RoleServiceImpl.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   *
44   */
45  public class RoleServiceImpl extends RoleServiceBaseImpl {
46  
47      public Role addRole(String name, String description, int type)
48          throws PortalException, SystemException {
49  
50          User user = getUser();
51  
52          PortalPermissionUtil.check(getPermissionChecker(), ActionKeys.ADD_ROLE);
53  
54          return roleLocalService.addRole(
55              user.getUserId(), user.getCompanyId(), name, description, type);
56      }
57  
58      public void addUserRoles(long userId, long[] roleIds)
59          throws PortalException, SystemException {
60  
61          checkUserRolesPermission(userId, roleIds);
62  
63          roleLocalService.addUserRoles(userId, roleIds);
64      }
65  
66      public void deleteRole(long roleId)
67          throws PortalException, SystemException {
68  
69          RolePermissionUtil.check(
70              getPermissionChecker(), roleId, ActionKeys.DELETE);
71  
72          roleLocalService.deleteRole(roleId);
73      }
74  
75      public Role getGroupRole(long companyId, long groupId)
76          throws PortalException, SystemException {
77  
78          return roleLocalService.getGroupRole(companyId, groupId);
79      }
80  
81      public List<Role> getGroupRoles(long groupId) throws SystemException {
82          return roleLocalService.getGroupRoles(groupId);
83      }
84  
85      public Role getRole(long roleId)
86          throws PortalException, SystemException {
87  
88          return roleLocalService.getRole(roleId);
89      }
90  
91      public Role getRole(long companyId, String name)
92          throws PortalException, SystemException {
93  
94          return roleLocalService.getRole(companyId, name);
95      }
96  
97      public List<Role> getUserGroupGroupRoles(long userId, long groupId)
98          throws SystemException {
99  
100         return roleLocalService.getUserGroupGroupRoles(userId, groupId);
101     }
102 
103     public List<Role> getUserGroupRoles(long userId, long groupId)
104         throws SystemException {
105 
106         return roleLocalService.getUserGroupRoles(userId, groupId);
107     }
108 
109     public List<Role> getUserRelatedRoles(long userId, List<Group> groups)
110         throws SystemException {
111 
112         return roleLocalService.getUserRelatedRoles(userId, groups);
113     }
114 
115     public List<Role> getUserRoles(long userId) throws SystemException {
116         return roleLocalService.getUserRoles(userId);
117     }
118 
119     public boolean hasUserRole(
120             long userId, long companyId, String name, boolean inherited)
121         throws PortalException, SystemException {
122 
123         return roleLocalService.hasUserRole(userId, companyId, name, inherited);
124     }
125 
126     public boolean hasUserRoles(
127             long userId, long companyId, String[] names, boolean inherited)
128         throws PortalException, SystemException {
129 
130         return roleLocalService.hasUserRoles(
131             userId, companyId, names, inherited);
132     }
133 
134     public void unsetUserRoles(long userId, long[] roleIds)
135         throws PortalException, SystemException {
136 
137         checkUserRolesPermission(userId, roleIds);
138 
139         roleLocalService.unsetUserRoles(userId, roleIds);
140     }
141 
142     public Role updateRole(
143             long roleId, String name, Map<Locale, String> localeTitlesMap,
144             String description, String subtype)
145         throws PortalException, SystemException {
146 
147         RolePermissionUtil.check(
148             getPermissionChecker(), roleId, ActionKeys.UPDATE);
149 
150         return roleLocalService.updateRole(
151             roleId, name, localeTitlesMap, description, subtype);
152     }
153 
154     protected void checkUserRolesPermission(long userId, long[] roleIds)
155         throws PortalException {
156 
157         for (int i = 0; i < roleIds.length; i++) {
158             RolePermissionUtil.check(
159                 getPermissionChecker(), roleIds[i], ActionKeys.ASSIGN_MEMBERS);
160         }
161     }
162 
163 }