1   /**
2    * Copyright (c) 2000-2009 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  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> getUserGroupRoles(long userId, long groupId)
98          throws SystemException {
99  
100         return roleLocalService.getUserGroupRoles(userId, groupId);
101     }
102 
103     public List<Role> getUserRelatedRoles(long userId, List<Group> groups)
104         throws SystemException {
105 
106         return roleLocalService.getUserRelatedRoles(userId, groups);
107     }
108 
109     public List<Role> getUserRoles(long userId) throws SystemException {
110         return roleLocalService.getUserRoles(userId);
111     }
112 
113     public boolean hasUserRole(
114             long userId, long companyId, String name, boolean inherited)
115         throws PortalException, SystemException {
116 
117         return roleLocalService.hasUserRole(userId, companyId, name, inherited);
118     }
119 
120     public boolean hasUserRoles(
121             long userId, long companyId, String[] names, boolean inherited)
122         throws PortalException, SystemException {
123 
124         return roleLocalService.hasUserRoles(
125             userId, companyId, names, inherited);
126     }
127 
128     public void unsetUserRoles(long userId, long[] roleIds)
129         throws PortalException, SystemException {
130 
131         checkUserRolesPermission(userId, roleIds);
132 
133         roleLocalService.unsetUserRoles(userId, roleIds);
134     }
135 
136     public Role updateRole(
137             long roleId, String name, Map<Locale, String> localeTitlesMap,
138             String description, String subtype)
139         throws PortalException, SystemException {
140 
141         RolePermissionUtil.check(
142             getPermissionChecker(), roleId, ActionKeys.UPDATE);
143 
144         return roleLocalService.updateRole(
145             roleId, name, localeTitlesMap, description, subtype);
146     }
147 
148     protected void checkUserRolesPermission(long userId, long[] roleIds)
149         throws PortalException {
150 
151         for (int i = 0; i < roleIds.length; i++) {
152             RolePermissionUtil.check(
153                 getPermissionChecker(), roleIds[i], ActionKeys.ASSIGN_MEMBERS);
154         }
155     }
156 
157 }