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 import com.liferay.portal.service.permission.UserPermissionUtil; 027 028 import java.util.ArrayList; 029 import java.util.List; 030 import java.util.Locale; 031 import java.util.Map; 032 033 /** 034 * The implementation of the role remote service. 035 * 036 * @author Brian Wing Shun Chan 037 */ 038 public class RoleServiceImpl extends RoleServiceBaseImpl { 039 040 /** 041 * Adds a role. The user is reindexed after role is added. 042 * 043 * @param className the name of the class for which the role is created 044 * @param classPK the primary key of the class for which the role is 045 * created (optionally <code>0</code>) 046 * @param name the role's name 047 * @param titleMap the role's localized titles (optionally 048 * <code>null</code>) 049 * @param descriptionMap the role's localized descriptions (optionally 050 * <code>null</code>) 051 * @param type the role's type (optionally <code>0</code>) 052 * @param subType the role's subtype (optionally <code>null</code>) 053 * @return the role 054 * @throws PortalException if a user with the primary key could not be 055 * found, if the user did not have permission to add roles, if the 056 * class name or the role name were invalid, or if the role is a 057 * duplicate 058 * @throws SystemException if a system exception occurred 059 */ 060 public Role addRole( 061 String className, long classPK, String name, 062 Map<Locale, String> titleMap, Map<Locale, String> descriptionMap, 063 int type, String subType) 064 throws PortalException, SystemException { 065 066 PortalPermissionUtil.check(getPermissionChecker(), ActionKeys.ADD_ROLE); 067 068 User user = getUser(); 069 070 return roleLocalService.addRole( 071 user.getUserId(), className, classPK, name, titleMap, 072 descriptionMap, type, subType); 073 } 074 075 /** 076 * Adds a role. The user is reindexed after role is added. 077 * 078 * @param name the role's name 079 * @param titleMap the role's localized titles (optionally 080 * <code>null</code>) 081 * @param descriptionMap the role's localized descriptions (optionally 082 * <code>null</code>) 083 * @param type the role's type (optionally <code>0</code>) 084 * @return the role 085 * @throws PortalException if a user with the primary key could not be 086 * found, if the user did not have permission to add roles, if 087 * the class name or the role name were invalid, or if the role 088 * is a duplicate 089 * @throws SystemException if a system exception occurred 090 * @deprecated {@link #addRole(String, long, String, Map, Map, int, String)} 091 */ 092 public Role addRole( 093 String name, Map<Locale, String> titleMap, 094 Map<Locale, String> descriptionMap, int type) 095 throws PortalException, SystemException { 096 097 return addRole(null, 0, name, titleMap, descriptionMap, type, null); 098 } 099 100 /** 101 * Adds the roles to the user. The user is reindexed after the roles are 102 * added. 103 * 104 * @param userId the primary key of the user 105 * @param roleIds the primary keys of the roles 106 * @throws PortalException if a user with the primary key could not be found 107 * or if the user did not have permission to assign members to one 108 * of the roles 109 * @throws SystemException if a system exception occurred 110 */ 111 public void addUserRoles(long userId, long[] roleIds) 112 throws PortalException, SystemException { 113 114 checkUserRolesPermission(userId, roleIds); 115 116 roleLocalService.addUserRoles(userId, roleIds); 117 } 118 119 /** 120 * Deletes the role with the primary key and its associated permissions. 121 * 122 * @param roleId the primary key of the role 123 * @throws PortalException if the user did not have permission to delete the 124 * role, if a role with the primary key could not be found, if the 125 * role is a default system role, or if the role's resource could 126 * not be found 127 * @throws SystemException if a system exception occurred 128 */ 129 public void deleteRole(long roleId) 130 throws PortalException, SystemException { 131 132 RolePermissionUtil.check( 133 getPermissionChecker(), roleId, ActionKeys.DELETE); 134 135 roleLocalService.deleteRole(roleId); 136 } 137 138 /** 139 * Returns all the roles associated with the group. 140 * 141 * @param groupId the primary key of the group 142 * @return the roles associated with the group 143 * @throws PortalException if a portal exception occurred 144 * @throws SystemException if a system exception occurred 145 */ 146 public List<Role> getGroupRoles(long groupId) 147 throws PortalException, SystemException { 148 149 List<Role> roles = roleLocalService.getGroupRoles(groupId); 150 151 return filterRoles(roles); 152 } 153 154 /** 155 * Returns the role with the primary key. 156 * 157 * @param roleId the primary key of the role 158 * @return the role with the primary key 159 * @throws PortalException if a role with the primary key could not be found 160 * or if the user did not have permission to view the role 161 * @throws SystemException if a system exception occurred 162 */ 163 public Role getRole(long roleId) throws PortalException, SystemException { 164 RolePermissionUtil.check( 165 getPermissionChecker(), roleId, ActionKeys.VIEW); 166 167 return roleLocalService.getRole(roleId); 168 } 169 170 /** 171 * Returns the role with the name in the company. 172 * 173 * <p> 174 * The method searches the system roles map first for default roles. If a 175 * role with the name is not found, then the method will query the database. 176 * </p> 177 * 178 * @param companyId the primary key of the company 179 * @param name the role's name 180 * @return the role with the name 181 * @throws PortalException if a role with the name could not be found in the 182 * company or if the user did not have permission to view the role 183 * @throws SystemException if a system exception occurred 184 */ 185 public Role getRole(long companyId, String name) 186 throws PortalException, SystemException { 187 188 Role role = roleLocalService.getRole(companyId, name); 189 190 RolePermissionUtil.check( 191 getPermissionChecker(), role.getRoleId(), ActionKeys.VIEW); 192 193 return role; 194 } 195 196 /** 197 * Returns all the user's roles within the user group. 198 * 199 * @param userId the primary key of the user 200 * @param groupId the primary key of the group 201 * @return the user's roles within the user group 202 * @throws PortalException if a portal exception occurred 203 * @throws SystemException if a system exception occurred 204 */ 205 public List<Role> getUserGroupGroupRoles(long userId, long groupId) 206 throws PortalException, SystemException { 207 208 UserPermissionUtil.check( 209 getPermissionChecker(), userId, ActionKeys.VIEW); 210 211 List<Role> roles = roleLocalService.getUserGroupGroupRoles( 212 userId, groupId); 213 214 return filterRoles(roles); 215 } 216 217 /** 218 * Returns all the user's roles within the user group. 219 * 220 * @param userId the primary key of the user 221 * @param groupId the primary key of the group 222 * @return the user's roles within the user group 223 * @throws PortalException if a portal exception occurred 224 * @throws SystemException if a system exception occurred 225 */ 226 public List<Role> getUserGroupRoles(long userId, long groupId) 227 throws PortalException, SystemException { 228 229 UserPermissionUtil.check( 230 getPermissionChecker(), userId, ActionKeys.VIEW); 231 232 List<Role> roles = roleLocalService.getUserGroupRoles(userId, groupId); 233 234 return filterRoles(roles); 235 } 236 237 /** 238 * Returns the union of all the user's roles within the groups. 239 * 240 * @param userId the primary key of the user 241 * @param groups the groups (optionally <code>null</code>) 242 * @return the union of all the user's roles within the groups 243 * @throws PortalException if a portal exception occurred 244 * @throws SystemException if a system exception occurred 245 */ 246 public List<Role> getUserRelatedRoles(long userId, List<Group> groups) 247 throws PortalException, SystemException { 248 249 UserPermissionUtil.check( 250 getPermissionChecker(), userId, ActionKeys.VIEW); 251 252 List<Role> roles = roleLocalService.getUserRelatedRoles(userId, groups); 253 254 return filterRoles(roles); 255 } 256 257 /** 258 * Returns all the roles associated with the user. 259 * 260 * @param userId the primary key of the user 261 * @return the roles associated with the user 262 * @throws PortalException if a portal exception occurred 263 * @throws SystemException if a system exception occurred 264 */ 265 public List<Role> getUserRoles(long userId) 266 throws PortalException, SystemException { 267 268 UserPermissionUtil.check( 269 getPermissionChecker(), userId, ActionKeys.VIEW); 270 271 List<Role> roles = roleLocalService.getUserRoles(userId); 272 273 return filterRoles(roles); 274 } 275 276 /** 277 * Returns <code>true</code> if the user is associated with the named 278 * regular role. 279 * 280 * @param userId the primary key of the user 281 * @param companyId the primary key of the company 282 * @param name the name of the role 283 * @param inherited whether to include the user's inherited roles in the 284 * search 285 * @return <code>true</code> if the user is associated with the regular 286 * role; <code>false</code> otherwise 287 * @throws PortalException if a role with the name could not be found in the 288 * company or if a default user for the company could not be found 289 * @throws SystemException if a system exception occurred 290 */ 291 public boolean hasUserRole( 292 long userId, long companyId, String name, boolean inherited) 293 throws PortalException, SystemException { 294 295 UserPermissionUtil.check( 296 getPermissionChecker(), userId, ActionKeys.VIEW); 297 298 return roleLocalService.hasUserRole(userId, companyId, name, inherited); 299 } 300 301 /** 302 * Returns <code>true</code> if the user has any one of the named regular 303 * roles. 304 * 305 * @param userId the primary key of the user 306 * @param companyId the primary key of the company 307 * @param names the names of the roles 308 * @param inherited whether to include the user's inherited roles in the 309 * search 310 * @return <code>true</code> if the user has any one of the regular roles; 311 * <code>false</code> otherwise 312 * @throws PortalException if any one of the roles with the names could not 313 * be found in the company or if the default user for the company 314 * could not be found 315 * @throws SystemException if a system exception occurred 316 */ 317 public boolean hasUserRoles( 318 long userId, long companyId, String[] names, boolean inherited) 319 throws PortalException, SystemException { 320 321 UserPermissionUtil.check( 322 getPermissionChecker(), userId, ActionKeys.VIEW); 323 324 return roleLocalService.hasUserRoles( 325 userId, companyId, names, inherited); 326 } 327 328 /** 329 * Removes the matching roles associated with the user. The user is 330 * reindexed after the roles are removed. 331 * 332 * @param userId the primary key of the user 333 * @param roleIds the primary keys of the roles 334 * @throws PortalException if a user with the primary key could not be 335 * found, if the user did not have permission to remove members from 336 * a role, or if a role with any one of the primary keys could not 337 * be found 338 * @throws SystemException if a system exception occurred 339 */ 340 public void unsetUserRoles(long userId, long[] roleIds) 341 throws PortalException, SystemException { 342 343 checkUserRolesPermission(userId, roleIds); 344 345 roleLocalService.unsetUserRoles(userId, roleIds); 346 } 347 348 /** 349 * Updates the role with the primary key. 350 * 351 * @param roleId the primary key of the role 352 * @param name the role's new name 353 * @param titleMap the new localized titles (optionally <code>null</code>) 354 * to replace those existing for the role 355 * @param descriptionMap the new localized descriptions (optionally 356 * <code>null</code>) to replace those existing for the role 357 * @param subtype the role's new subtype (optionally <code>null</code>) 358 * @return the role with the primary key 359 * @throws PortalException if the user did not have permission to update the 360 * role, if a role with the primary could not be found, or if the 361 * role's name was invalid 362 * @throws SystemException if a system exception occurred 363 */ 364 public Role updateRole( 365 long roleId, String name, Map<Locale, String> titleMap, 366 Map<Locale, String> descriptionMap, String subtype) 367 throws PortalException, SystemException { 368 369 RolePermissionUtil.check( 370 getPermissionChecker(), roleId, ActionKeys.UPDATE); 371 372 return roleLocalService.updateRole( 373 roleId, name, titleMap, descriptionMap, subtype); 374 } 375 376 protected void checkUserRolesPermission(long userId, long[] roleIds) 377 throws PortalException { 378 379 for (int i = 0; i < roleIds.length; i++) { 380 RolePermissionUtil.check( 381 getPermissionChecker(), roleIds[i], ActionKeys.ASSIGN_MEMBERS); 382 } 383 } 384 385 protected List<Role> filterRoles(List<Role> roles) throws PortalException { 386 List<Role> filteredRoles = new ArrayList<Role>(); 387 388 for (Role role : roles) { 389 if (RolePermissionUtil.contains( 390 getPermissionChecker(), role.getRoleId(), 391 ActionKeys.VIEW)) { 392 393 filteredRoles.add(role); 394 } 395 } 396 397 return filteredRoles; 398 } 399 400 }