1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portal.service.impl;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.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(String name, String description, int type)
39          throws PortalException, SystemException {
40  
41          User user = getUser();
42  
43          PortalPermissionUtil.check(getPermissionChecker(), ActionKeys.ADD_ROLE);
44  
45          return roleLocalService.addRole(
46              user.getUserId(), user.getCompanyId(), name, description, type);
47      }
48  
49      public void addUserRoles(long userId, long[] roleIds)
50          throws PortalException, SystemException {
51  
52          checkUserRolesPermission(userId, roleIds);
53  
54          roleLocalService.addUserRoles(userId, roleIds);
55      }
56  
57      public void deleteRole(long roleId)
58          throws PortalException, SystemException {
59  
60          RolePermissionUtil.check(
61              getPermissionChecker(), roleId, ActionKeys.DELETE);
62  
63          roleLocalService.deleteRole(roleId);
64      }
65  
66      public Role getGroupRole(long companyId, long groupId)
67          throws PortalException, SystemException {
68  
69          return roleLocalService.getGroupRole(companyId, groupId);
70      }
71  
72      public List<Role> getGroupRoles(long groupId) throws SystemException {
73          return roleLocalService.getGroupRoles(groupId);
74      }
75  
76      public Role getRole(long roleId)
77          throws PortalException, SystemException {
78  
79          return roleLocalService.getRole(roleId);
80      }
81  
82      public Role getRole(long companyId, String name)
83          throws PortalException, SystemException {
84  
85          return roleLocalService.getRole(companyId, name);
86      }
87  
88      public List<Role> getUserGroupGroupRoles(long userId, long groupId)
89          throws SystemException {
90  
91          return roleLocalService.getUserGroupGroupRoles(userId, groupId);
92      }
93  
94      public List<Role> getUserGroupRoles(long userId, long groupId)
95          throws SystemException {
96  
97          return roleLocalService.getUserGroupRoles(userId, groupId);
98      }
99  
100     public List<Role> getUserRelatedRoles(long userId, List<Group> groups)
101         throws SystemException {
102 
103         return roleLocalService.getUserRelatedRoles(userId, groups);
104     }
105 
106     public List<Role> getUserRoles(long userId) throws SystemException {
107         return roleLocalService.getUserRoles(userId);
108     }
109 
110     public boolean hasUserRole(
111             long userId, long companyId, String name, boolean inherited)
112         throws PortalException, SystemException {
113 
114         return roleLocalService.hasUserRole(userId, companyId, name, inherited);
115     }
116 
117     public boolean hasUserRoles(
118             long userId, long companyId, String[] names, boolean inherited)
119         throws PortalException, SystemException {
120 
121         return roleLocalService.hasUserRoles(
122             userId, companyId, names, inherited);
123     }
124 
125     public void unsetUserRoles(long userId, long[] roleIds)
126         throws PortalException, SystemException {
127 
128         checkUserRolesPermission(userId, roleIds);
129 
130         roleLocalService.unsetUserRoles(userId, roleIds);
131     }
132 
133     public Role updateRole(
134             long roleId, String name, Map<Locale, String> localeTitlesMap,
135             String description, String subtype)
136         throws PortalException, SystemException {
137 
138         RolePermissionUtil.check(
139             getPermissionChecker(), roleId, ActionKeys.UPDATE);
140 
141         return roleLocalService.updateRole(
142             roleId, name, localeTitlesMap, description, subtype);
143     }
144 
145     protected void checkUserRolesPermission(long userId, long[] roleIds)
146         throws PortalException {
147 
148         for (int i = 0; i < roleIds.length; i++) {
149             RolePermissionUtil.check(
150                 getPermissionChecker(), roleIds[i], ActionKeys.ASSIGN_MEMBERS);
151         }
152     }
153 
154 }