001 /** 002 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved. 003 * 004 * This library is free software; you can redistribute it and/or modify it under 005 * the terms of the GNU Lesser General Public License as published by the Free 006 * Software Foundation; either version 2.1 of the License, or (at your option) 007 * any later version. 008 * 009 * This library is distributed in the hope that it will be useful, but WITHOUT 010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 011 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 012 * details. 013 */ 014 015 package com.liferay.portal.service.impl; 016 017 import com.liferay.portal.kernel.exception.PortalException; 018 import com.liferay.portal.kernel.exception.SystemException; 019 import com.liferay.portal.model.Group; 020 import com.liferay.portal.model.Role; 021 import com.liferay.portal.model.User; 022 import com.liferay.portal.security.permission.ActionKeys; 023 import com.liferay.portal.service.base.RoleServiceBaseImpl; 024 import com.liferay.portal.service.permission.PortalPermissionUtil; 025 import com.liferay.portal.service.permission.RolePermissionUtil; 026 027 import java.util.ArrayList; 028 import java.util.List; 029 import java.util.Locale; 030 import java.util.Map; 031 032 /** 033 * The implementation of the role remote service. 034 * 035 * @author Brian Wing Shun Chan 036 */ 037 public class RoleServiceImpl extends RoleServiceBaseImpl { 038 039 /** 040 * Adds a role. The user is reindexed after role is added. 041 * 042 * @param name the role's name 043 * @param titleMap the role's localized titles (optionally 044 * <code>null</code>) 045 * @param descriptionMap the role's localized descriptions (optionally 046 * <code>null</code>) 047 * @param type the role's type (optionally <code>0</code>) 048 * @return the role 049 * @throws PortalException if a user with the primary key could not be 050 * found, if the user did not have permission to add roles, if the 051 * class name or the role name were invalid, or if the role is a 052 * duplicate 053 * @throws SystemException if a system exception occurred 054 */ 055 public Role addRole( 056 String name, Map<Locale, String> titleMap, 057 Map<Locale, String> descriptionMap, int type) 058 throws PortalException, SystemException { 059 060 PortalPermissionUtil.check(getPermissionChecker(), ActionKeys.ADD_ROLE); 061 062 User user = getUser(); 063 064 return roleLocalService.addRole( 065 user.getUserId(), user.getCompanyId(), name, titleMap, 066 descriptionMap, type); 067 } 068 069 /** 070 * Adds the roles to the user. The user is reindexed after the roles are 071 * added. 072 * 073 * @param userId the primary key of the user 074 * @param roleIds the primary keys of the roles 075 * @throws PortalException if a user with the primary key could not be found 076 * or if the user did not have permission to assign members to one 077 * of the roles 078 * @throws SystemException if a system exception occurred 079 */ 080 public void addUserRoles(long userId, long[] roleIds) 081 throws PortalException, SystemException { 082 083 checkUserRolesPermission(userId, roleIds); 084 085 roleLocalService.addUserRoles(userId, roleIds); 086 } 087 088 /** 089 * Deletes the role with the primary key and its associated permissions. 090 * 091 * @param roleId the primary key of the role 092 * @throws PortalException if the user did not have permission to delete the 093 * role, if a role with the primary key could not be found, if the 094 * role is a default system role, or if the role's resource could 095 * not be found 096 * @throws SystemException if a system exception occurred 097 */ 098 public void deleteRole(long roleId) 099 throws PortalException, SystemException { 100 101 RolePermissionUtil.check( 102 getPermissionChecker(), roleId, ActionKeys.DELETE); 103 104 roleLocalService.deleteRole(roleId); 105 } 106 107 /** 108 * Returns all the roles associated with the group. 109 * 110 * @param groupId the primary key of the group 111 * @return the roles associated with the group 112 * @throws PortalException if a portal exception occurred 113 * @throws SystemException if a system exception occurred 114 */ 115 public List<Role> getGroupRoles(long groupId) 116 throws PortalException, SystemException { 117 118 List<Role> roles = roleLocalService.getGroupRoles(groupId); 119 120 return filterRoles(roles); 121 } 122 123 /** 124 * Returns the role with the primary key. 125 * 126 * @param roleId the primary key of the role 127 * @return the role with the primary key 128 * @throws PortalException if a role with the primary key could not be found 129 * or if the user did not have permission to view the role 130 * @throws SystemException if a system exception occurred 131 */ 132 public Role getRole(long roleId) 133 throws PortalException, SystemException { 134 135 RolePermissionUtil.check( 136 getPermissionChecker(), roleId, ActionKeys.VIEW); 137 138 return roleLocalService.getRole(roleId); 139 } 140 141 /** 142 * Returns the role with the name in the company. 143 * 144 * <p> 145 * The method searches the system roles map first for default roles. If a 146 * role with the name is not found, then the method will query the database. 147 * </p> 148 * 149 * @param companyId the primary key of the company 150 * @param name the role's name 151 * @return the role with the name 152 * @throws PortalException if a role with the name could not be found in the 153 * company or if the user did not have permission to view the role 154 * @throws SystemException if a system exception occurred 155 */ 156 public Role getRole(long companyId, String name) 157 throws PortalException, SystemException { 158 159 Role role = roleLocalService.getRole(companyId, name); 160 161 RolePermissionUtil.check( 162 getPermissionChecker(), role.getRoleId(), ActionKeys.VIEW); 163 164 return role; 165 } 166 167 /** 168 * Returns all the user's roles within the user group. 169 * 170 * @param userId the primary key of the user 171 * @param groupId the primary key of the group 172 * @return the user's roles within the user group 173 * @throws PortalException if a portal exception occurred 174 * @throws SystemException if a system exception occurred 175 */ 176 public List<Role> getUserGroupGroupRoles(long userId, long groupId) 177 throws PortalException, SystemException { 178 179 List<Role> roles = roleLocalService.getUserGroupGroupRoles( 180 userId, groupId); 181 182 return filterRoles(roles); 183 } 184 185 /** 186 * Returns all the user's roles within the user group. 187 * 188 * @param userId the primary key of the user 189 * @param groupId the primary key of the group 190 * @return the user's roles within the user group 191 * @throws PortalException if a portal exception occurred 192 * @throws SystemException if a system exception occurred 193 */ 194 public List<Role> getUserGroupRoles(long userId, long groupId) 195 throws PortalException, SystemException { 196 197 List<Role> roles = roleLocalService.getUserGroupRoles(userId, groupId); 198 199 return filterRoles(roles); 200 } 201 202 /** 203 * Returns the union of all the user's roles within the groups. 204 * 205 * @param userId the primary key of the user 206 * @param groups the groups (optionally <code>null</code>) 207 * @return the union of all the user's roles within the groups 208 * @throws PortalException if a portal exception occurred 209 * @throws SystemException if a system exception occurred 210 */ 211 public List<Role> getUserRelatedRoles(long userId, List<Group> groups) 212 throws PortalException, SystemException { 213 214 List<Role> roles = roleLocalService.getUserRelatedRoles(userId, groups); 215 216 return filterRoles(roles); 217 } 218 219 /** 220 * Returns all the roles associated with the user. 221 * 222 * @param userId the primary key of the user 223 * @return the roles associated with the user 224 * @throws PortalException if a portal exception occurred 225 * @throws SystemException if a system exception occurred 226 */ 227 public List<Role> getUserRoles(long userId) 228 throws PortalException, SystemException { 229 230 List<Role> roles = roleLocalService.getUserRoles(userId); 231 232 return filterRoles(roles); 233 } 234 235 /** 236 * Returns <code>true</code> if the user is associated with the named 237 * regular role. 238 * 239 * @param userId the primary key of the user 240 * @param companyId the primary key of the company 241 * @param name the name of the role 242 * @param inherited whether to include the user's inherited roles in the 243 * search 244 * @return <code>true</code> if the user is associated with the regular 245 * role; <code>false</code> otherwise 246 * @throws PortalException if a role with the name could not be found in the 247 * company or if a default user for the company could not be found 248 * @throws SystemException if a system exception occurred 249 */ 250 public boolean hasUserRole( 251 long userId, long companyId, String name, boolean inherited) 252 throws PortalException, SystemException { 253 254 return roleLocalService.hasUserRole(userId, companyId, name, inherited); 255 } 256 257 /** 258 * Returns <code>true</code> if the user has any one of the named regular 259 * roles. 260 * 261 * @param userId the primary key of the user 262 * @param companyId the primary key of the company 263 * @param names the names of the roles 264 * @param inherited whether to include the user's inherited roles in the 265 * search 266 * @return <code>true</code> if the user has any one of the regular roles; 267 * <code>false</code> otherwise 268 * @throws PortalException if any one of the roles with the names could not 269 * be found in the company or if the default user for the company 270 * could not be found 271 * @throws SystemException if a system exception occurred 272 */ 273 public boolean hasUserRoles( 274 long userId, long companyId, String[] names, boolean inherited) 275 throws PortalException, SystemException { 276 277 return roleLocalService.hasUserRoles( 278 userId, companyId, names, inherited); 279 } 280 281 /** 282 * Removes the matching roles associated with the user. The user is 283 * reindexed after the roles are removed. 284 * 285 * @param userId the primary key of the user 286 * @param roleIds the primary keys of the roles 287 * @throws PortalException if a user with the primary key could not be 288 * found, if the user did not have permission to remove members from 289 * a role, or if a role with any one of the primary keys could not 290 * be found 291 * @throws SystemException if a system exception occurred 292 */ 293 public void unsetUserRoles(long userId, long[] roleIds) 294 throws PortalException, SystemException { 295 296 checkUserRolesPermission(userId, roleIds); 297 298 roleLocalService.unsetUserRoles(userId, roleIds); 299 } 300 301 /** 302 * Updates the role with the primary key. 303 * 304 * @param roleId the primary key of the role 305 * @param name the role's new name 306 * @param titleMap the new localized titles (optionally <code>null</code>) 307 * to replace those existing for the role 308 * @param descriptionMap the new localized descriptions (optionally 309 * <code>null</code>) to replace those existing for the role 310 * @param subtype the role's new subtype (optionally <code>null</code>) 311 * @return the role with the primary key 312 * @throws PortalException if the user did not have permission to update the 313 * role, if a role with the primary could not be found, or if the 314 * role's name was invalid 315 * @throws SystemException if a system exception occurred 316 */ 317 public Role updateRole( 318 long roleId, String name, Map<Locale, String> titleMap, 319 Map<Locale, String> descriptionMap, String subtype) 320 throws PortalException, SystemException { 321 322 RolePermissionUtil.check( 323 getPermissionChecker(), roleId, ActionKeys.UPDATE); 324 325 return roleLocalService.updateRole( 326 roleId, name, titleMap, descriptionMap, subtype); 327 } 328 329 protected void checkUserRolesPermission(long userId, long[] roleIds) 330 throws PortalException { 331 332 for (int i = 0; i < roleIds.length; i++) { 333 RolePermissionUtil.check( 334 getPermissionChecker(), roleIds[i], ActionKeys.ASSIGN_MEMBERS); 335 } 336 } 337 338 protected List<Role> filterRoles(List<Role> roles) throws PortalException { 339 List<Role> filteredRoles = new ArrayList<Role>(); 340 341 for (Role role : roles) { 342 if (RolePermissionUtil.contains( 343 getPermissionChecker(), role.getRoleId(), 344 ActionKeys.VIEW)) { 345 346 filteredRoles.add(role); 347 } 348 } 349 350 return filteredRoles; 351 } 352 353 }