001
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.kernel.util.OrderByComparator;
020 import com.liferay.portal.model.Group;
021 import com.liferay.portal.model.Role;
022 import com.liferay.portal.model.RoleConstants;
023 import com.liferay.portal.model.User;
024 import com.liferay.portal.security.membershippolicy.OrganizationMembershipPolicyUtil;
025 import com.liferay.portal.security.membershippolicy.RoleMembershipPolicyUtil;
026 import com.liferay.portal.security.membershippolicy.SiteMembershipPolicyUtil;
027 import com.liferay.portal.security.permission.ActionKeys;
028 import com.liferay.portal.service.ServiceContext;
029 import com.liferay.portal.service.base.RoleServiceBaseImpl;
030 import com.liferay.portal.service.permission.PortalPermissionUtil;
031 import com.liferay.portal.service.permission.RolePermissionUtil;
032 import com.liferay.portal.service.permission.UserPermissionUtil;
033 import com.liferay.portlet.expando.model.ExpandoBridge;
034
035 import java.io.Serializable;
036
037 import java.util.ArrayList;
038 import java.util.LinkedHashMap;
039 import java.util.List;
040 import java.util.Locale;
041 import java.util.Map;
042
043
049 public class RoleServiceImpl extends RoleServiceBaseImpl {
050
051
074 @Override
075 public Role addRole(
076 String className, long classPK, String name,
077 Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
078 int type, String subtype, ServiceContext serviceContext)
079 throws PortalException, SystemException {
080
081 PortalPermissionUtil.check(getPermissionChecker(), ActionKeys.ADD_ROLE);
082
083 User user = getUser();
084
085 Role role = roleLocalService.addRole(
086 user.getUserId(), className, classPK, name, titleMap,
087 descriptionMap, type, subtype, serviceContext);
088
089 if (type == RoleConstants.TYPE_ORGANIZATION) {
090 OrganizationMembershipPolicyUtil.verifyPolicy(role);
091 }
092 else if (type == RoleConstants.TYPE_SITE) {
093 SiteMembershipPolicyUtil.verifyPolicy(role);
094 }
095 else {
096 RoleMembershipPolicyUtil.verifyPolicy(role);
097 }
098
099 return role;
100 }
101
102
120 @Override
121 public Role addRole(
122 String name, Map<Locale, String> titleMap,
123 Map<Locale, String> descriptionMap, int type)
124 throws PortalException, SystemException {
125
126 return addRole(
127 null, 0, name, titleMap, descriptionMap, type, null, null);
128 }
129
130
141 @Override
142 public void addUserRoles(long userId, long[] roleIds)
143 throws PortalException, SystemException {
144
145 if (roleIds.length == 0) {
146 return;
147 }
148
149 checkUserRolesPermission(userId, roleIds);
150
151 RoleMembershipPolicyUtil.checkRoles(new long[] {userId}, roleIds, null);
152
153 roleLocalService.addUserRoles(userId, roleIds);
154
155 RoleMembershipPolicyUtil.propagateRoles(
156 new long[] {userId}, roleIds, null);
157 }
158
159
169 @Override
170 public void deleteRole(long roleId)
171 throws PortalException, SystemException {
172
173 RolePermissionUtil.check(
174 getPermissionChecker(), roleId, ActionKeys.DELETE);
175
176 roleLocalService.deleteRole(roleId);
177 }
178
179
187 @Override
188 public List<Role> getGroupRoles(long groupId)
189 throws PortalException, SystemException {
190
191 List<Role> roles = roleLocalService.getGroupRoles(groupId);
192
193 return filterRoles(roles);
194 }
195
196 @Override
197 public List<Role> getGroupRolesAndTeamRoles(
198 long companyId, String keywords, List<String> excludedNames,
199 int[] types, long excludedTeamRoleId, long teamGroupId, int start,
200 int end)
201 throws SystemException {
202
203 return roleFinder.filterFindByGroupRoleAndTeamRole(
204 companyId, keywords, excludedNames, types, excludedTeamRoleId,
205 teamGroupId, start, end);
206 }
207
208 @Override
209 public int getGroupRolesAndTeamRolesCount(
210 long companyId, String keywords, List<String> excludedNames,
211 int[] types, long excludedTeamRoleId, long teamGroupId)
212 throws SystemException {
213
214 return roleFinder.filterCountByGroupRoleAndTeamRole(
215 companyId, keywords, excludedNames, types, excludedTeamRoleId,
216 teamGroupId);
217 }
218
219
228 @Override
229 public Role getRole(long roleId) throws PortalException, SystemException {
230 RolePermissionUtil.check(
231 getPermissionChecker(), roleId, ActionKeys.VIEW);
232
233 return roleLocalService.getRole(roleId);
234 }
235
236
251 @Override
252 public Role getRole(long companyId, String name)
253 throws PortalException, SystemException {
254
255 Role role = roleLocalService.getRole(companyId, name);
256
257 RolePermissionUtil.check(
258 getPermissionChecker(), role.getRoleId(), ActionKeys.VIEW);
259
260 return role;
261 }
262
263
272 @Override
273 public List<Role> getUserGroupGroupRoles(long userId, long groupId)
274 throws PortalException, SystemException {
275
276 UserPermissionUtil.check(
277 getPermissionChecker(), userId, ActionKeys.VIEW);
278
279 List<Role> roles = roleLocalService.getUserGroupGroupRoles(
280 userId, groupId);
281
282 return filterRoles(roles);
283 }
284
285
294 @Override
295 public List<Role> getUserGroupRoles(long userId, long groupId)
296 throws PortalException, SystemException {
297
298 UserPermissionUtil.check(
299 getPermissionChecker(), userId, ActionKeys.VIEW);
300
301 List<Role> roles = roleLocalService.getUserGroupRoles(userId, groupId);
302
303 return filterRoles(roles);
304 }
305
306
315 @Override
316 public List<Role> getUserRelatedRoles(long userId, List<Group> groups)
317 throws PortalException, SystemException {
318
319 UserPermissionUtil.check(
320 getPermissionChecker(), userId, ActionKeys.VIEW);
321
322 List<Role> roles = roleLocalService.getUserRelatedRoles(userId, groups);
323
324 return filterRoles(roles);
325 }
326
327
335 @Override
336 public List<Role> getUserRoles(long userId)
337 throws PortalException, SystemException {
338
339 UserPermissionUtil.check(
340 getPermissionChecker(), userId, ActionKeys.VIEW);
341
342 List<Role> roles = roleLocalService.getUserRoles(userId);
343
344 return filterRoles(roles);
345 }
346
347
362 @Override
363 public boolean hasUserRole(
364 long userId, long companyId, String name, boolean inherited)
365 throws PortalException, SystemException {
366
367 UserPermissionUtil.check(
368 getPermissionChecker(), userId, ActionKeys.VIEW);
369
370 return roleLocalService.hasUserRole(userId, companyId, name, inherited);
371 }
372
373
389 @Override
390 public boolean hasUserRoles(
391 long userId, long companyId, String[] names, boolean inherited)
392 throws PortalException, SystemException {
393
394 UserPermissionUtil.check(
395 getPermissionChecker(), userId, ActionKeys.VIEW);
396
397 return roleLocalService.hasUserRoles(
398 userId, companyId, names, inherited);
399 }
400
401 @Override
402 public List<Role> search(
403 long companyId, String keywords, Integer[] types,
404 LinkedHashMap<String, Object> params, int start, int end,
405 OrderByComparator obc)
406 throws SystemException {
407
408 return roleFinder.filterFindByKeywords(
409 companyId, keywords, types, params, start, end, obc);
410 }
411
412 @Override
413 public int searchCount(
414 long companyId, String keywords, Integer[] types,
415 LinkedHashMap<String, Object> params)
416 throws SystemException {
417
418 return roleFinder.filterCountByKeywords(
419 companyId, keywords, types, params);
420 }
421
422
434 @Override
435 public void unsetUserRoles(long userId, long[] roleIds)
436 throws PortalException, SystemException {
437
438 if (roleIds.length == 0) {
439 return;
440 }
441
442 checkUserRolesPermission(userId, roleIds);
443
444 RoleMembershipPolicyUtil.checkRoles(new long[] {userId}, null, roleIds);
445
446 roleLocalService.unsetUserRoles(userId, roleIds);
447
448 RoleMembershipPolicyUtil.propagateRoles(
449 new long[] {userId}, null, roleIds);
450 }
451
452
471 @Override
472 public Role updateRole(
473 long roleId, String name, Map<Locale, String> titleMap,
474 Map<Locale, String> descriptionMap, String subtype,
475 ServiceContext serviceContext)
476 throws PortalException, SystemException {
477
478 RolePermissionUtil.check(
479 getPermissionChecker(), roleId, ActionKeys.UPDATE);
480
481 Role oldRole = rolePersistence.findByPrimaryKey(roleId);
482
483 ExpandoBridge oldExpandoBridge = oldRole.getExpandoBridge();
484
485 Map<String, Serializable> oldExpandoAttributes =
486 oldExpandoBridge.getAttributes();
487
488 Role role = roleLocalService.updateRole(
489 roleId, name, titleMap, descriptionMap, subtype, serviceContext);
490
491 if (role.getType() == RoleConstants.TYPE_ORGANIZATION) {
492 OrganizationMembershipPolicyUtil.verifyPolicy(
493 role, oldRole, oldExpandoAttributes);
494 }
495 else if (role.getType() == RoleConstants.TYPE_SITE) {
496 SiteMembershipPolicyUtil.verifyPolicy(
497 role, oldRole, oldExpandoAttributes);
498 }
499 else {
500 RoleMembershipPolicyUtil.verifyPolicy(
501 role, oldRole, oldExpandoAttributes);
502 }
503
504 return role;
505 }
506
507 protected void checkUserRolesPermission(long userId, long[] roleIds)
508 throws PortalException {
509
510 for (int i = 0; i < roleIds.length; i++) {
511 RolePermissionUtil.check(
512 getPermissionChecker(), roleIds[i], ActionKeys.ASSIGN_MEMBERS);
513 }
514 }
515
516 protected List<Role> filterRoles(List<Role> roles) throws PortalException {
517 List<Role> filteredRoles = new ArrayList<Role>();
518
519 for (Role role : roles) {
520 if (RolePermissionUtil.contains(
521 getPermissionChecker(), role.getRoleId(),
522 ActionKeys.VIEW)) {
523
524 filteredRoles.add(role);
525 }
526 }
527
528 return filteredRoles;
529 }
530
531 }