001
014
015 package com.liferay.portal.service.impl;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.util.OrderByComparator;
019 import com.liferay.portal.model.Group;
020 import com.liferay.portal.model.Role;
021 import com.liferay.portal.model.RoleConstants;
022 import com.liferay.portal.model.User;
023 import com.liferay.portal.security.membershippolicy.OrganizationMembershipPolicyUtil;
024 import com.liferay.portal.security.membershippolicy.RoleMembershipPolicyUtil;
025 import com.liferay.portal.security.membershippolicy.SiteMembershipPolicyUtil;
026 import com.liferay.portal.security.permission.ActionKeys;
027 import com.liferay.portal.service.ServiceContext;
028 import com.liferay.portal.service.base.RoleServiceBaseImpl;
029 import com.liferay.portal.service.permission.PortalPermissionUtil;
030 import com.liferay.portal.service.permission.RolePermissionUtil;
031 import com.liferay.portal.service.permission.UserPermissionUtil;
032 import com.liferay.portlet.expando.model.ExpandoBridge;
033
034 import java.io.Serializable;
035
036 import java.util.ArrayList;
037 import java.util.LinkedHashMap;
038 import java.util.List;
039 import java.util.Locale;
040 import java.util.Map;
041
042
048 public class RoleServiceImpl extends RoleServiceBaseImpl {
049
050
072 @Override
073 public Role addRole(
074 String className, long classPK, String name,
075 Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
076 int type, String subtype, ServiceContext serviceContext)
077 throws PortalException {
078
079 PortalPermissionUtil.check(getPermissionChecker(), ActionKeys.ADD_ROLE);
080
081 User user = getUser();
082
083 Role role = roleLocalService.addRole(
084 user.getUserId(), className, classPK, name, titleMap,
085 descriptionMap, type, subtype, serviceContext);
086
087 if (type == RoleConstants.TYPE_ORGANIZATION) {
088 OrganizationMembershipPolicyUtil.verifyPolicy(role);
089 }
090 else if (type == RoleConstants.TYPE_SITE) {
091 SiteMembershipPolicyUtil.verifyPolicy(role);
092 }
093 else {
094 RoleMembershipPolicyUtil.verifyPolicy(role);
095 }
096
097 return role;
098 }
099
100
117 @Deprecated
118 @Override
119 public Role addRole(
120 String name, Map<Locale, String> titleMap,
121 Map<Locale, String> descriptionMap, int type)
122 throws PortalException {
123
124 return addRole(
125 null, 0, name, titleMap, descriptionMap, type, null, null);
126 }
127
128
138 @Override
139 public void addUserRoles(long userId, long[] roleIds)
140 throws PortalException {
141
142 if (roleIds.length == 0) {
143 return;
144 }
145
146 checkUserRolesPermission(userId, roleIds);
147
148 RoleMembershipPolicyUtil.checkRoles(new long[] {userId}, roleIds, null);
149
150 roleLocalService.addUserRoles(userId, roleIds);
151
152 RoleMembershipPolicyUtil.propagateRoles(
153 new long[] {userId}, roleIds, null);
154 }
155
156
165 @Override
166 public void deleteRole(long roleId) throws PortalException {
167 RolePermissionUtil.check(
168 getPermissionChecker(), roleId, ActionKeys.DELETE);
169
170 roleLocalService.deleteRole(roleId);
171 }
172
173 @Override
174 public Role fetchRole(long roleId) throws PortalException {
175 RolePermissionUtil.check(
176 getPermissionChecker(), roleId, ActionKeys.VIEW);
177
178 return roleLocalService.fetchRole(roleId);
179 }
180
181
188 @Override
189 public List<Role> getGroupRoles(long groupId) throws PortalException {
190 List<Role> roles = roleLocalService.getGroupRoles(groupId);
191
192 return filterRoles(roles);
193 }
194
195
203 @Override
204 public Role getRole(long roleId) throws PortalException {
205 RolePermissionUtil.check(
206 getPermissionChecker(), roleId, ActionKeys.VIEW);
207
208 return roleLocalService.getRole(roleId);
209 }
210
211
225 @Override
226 public Role getRole(long companyId, String name) throws PortalException {
227 Role role = roleLocalService.getRole(companyId, name);
228
229 RolePermissionUtil.check(
230 getPermissionChecker(), role.getRoleId(), ActionKeys.VIEW);
231
232 return role;
233 }
234
235 @Override
236 public List<Role> getRoles(int type, String subtype)
237 throws PortalException {
238
239 return filterRoles(roleLocalService.getRoles(type, subtype));
240 }
241
242 @Override
243 public List<Role> getRoles(long companyId, int[] types)
244 throws PortalException {
245
246 return filterRoles(roleLocalService.getRoles(companyId, types));
247 }
248
249
257 @Override
258 public List<Role> getUserGroupGroupRoles(long userId, long groupId)
259 throws PortalException {
260
261 UserPermissionUtil.check(
262 getPermissionChecker(), userId, ActionKeys.VIEW);
263
264 List<Role> roles = roleLocalService.getUserGroupGroupRoles(
265 userId, groupId);
266
267 return filterRoles(roles);
268 }
269
270
278 @Override
279 public List<Role> getUserGroupRoles(long userId, long groupId)
280 throws PortalException {
281
282 UserPermissionUtil.check(
283 getPermissionChecker(), userId, ActionKeys.VIEW);
284
285 List<Role> roles = roleLocalService.getUserGroupRoles(userId, groupId);
286
287 return filterRoles(roles);
288 }
289
290
298 @Override
299 public List<Role> getUserRelatedRoles(long userId, List<Group> groups)
300 throws PortalException {
301
302 UserPermissionUtil.check(
303 getPermissionChecker(), userId, ActionKeys.VIEW);
304
305 List<Role> roles = roleLocalService.getUserRelatedRoles(userId, groups);
306
307 return filterRoles(roles);
308 }
309
310
317 @Override
318 public List<Role> getUserRoles(long userId) throws PortalException {
319 UserPermissionUtil.check(
320 getPermissionChecker(), userId, ActionKeys.VIEW);
321
322 List<Role> roles = roleLocalService.getUserRoles(userId);
323
324 return filterRoles(roles);
325 }
326
327
341 @Override
342 public boolean hasUserRole(
343 long userId, long companyId, String name, boolean inherited)
344 throws PortalException {
345
346 UserPermissionUtil.check(
347 getPermissionChecker(), userId, ActionKeys.VIEW);
348
349 return roleLocalService.hasUserRole(userId, companyId, name, inherited);
350 }
351
352
367 @Override
368 public boolean hasUserRoles(
369 long userId, long companyId, String[] names, boolean inherited)
370 throws PortalException {
371
372 UserPermissionUtil.check(
373 getPermissionChecker(), userId, ActionKeys.VIEW);
374
375 return roleLocalService.hasUserRoles(
376 userId, companyId, names, inherited);
377 }
378
379 @Override
380 public List<Role> search(
381 long companyId, String keywords, Integer[] types,
382 LinkedHashMap<String, Object> params, int start, int end,
383 OrderByComparator<Role> obc) {
384
385 return roleFinder.filterFindByKeywords(
386 companyId, keywords, types, params, start, end, obc);
387 }
388
389 @Override
390 public int searchCount(
391 long companyId, String keywords, Integer[] types,
392 LinkedHashMap<String, Object> params) {
393
394 return roleFinder.filterCountByKeywords(
395 companyId, keywords, types, params);
396 }
397
398
409 @Override
410 public void unsetUserRoles(long userId, long[] roleIds)
411 throws PortalException {
412
413 if (roleIds.length == 0) {
414 return;
415 }
416
417 checkUserRolesPermission(userId, roleIds);
418
419 RoleMembershipPolicyUtil.checkRoles(new long[] {userId}, null, roleIds);
420
421 roleLocalService.unsetUserRoles(userId, roleIds);
422
423 RoleMembershipPolicyUtil.propagateRoles(
424 new long[] {userId}, null, roleIds);
425 }
426
427
445 @Override
446 public Role updateRole(
447 long roleId, String name, Map<Locale, String> titleMap,
448 Map<Locale, String> descriptionMap, String subtype,
449 ServiceContext serviceContext)
450 throws PortalException {
451
452 RolePermissionUtil.check(
453 getPermissionChecker(), roleId, ActionKeys.UPDATE);
454
455 Role oldRole = rolePersistence.findByPrimaryKey(roleId);
456
457 ExpandoBridge oldExpandoBridge = oldRole.getExpandoBridge();
458
459 Map<String, Serializable> oldExpandoAttributes =
460 oldExpandoBridge.getAttributes();
461
462 Role role = roleLocalService.updateRole(
463 roleId, name, titleMap, descriptionMap, subtype, serviceContext);
464
465 if (role.getType() == RoleConstants.TYPE_ORGANIZATION) {
466 OrganizationMembershipPolicyUtil.verifyPolicy(
467 role, oldRole, oldExpandoAttributes);
468 }
469 else if (role.getType() == RoleConstants.TYPE_SITE) {
470 SiteMembershipPolicyUtil.verifyPolicy(
471 role, oldRole, oldExpandoAttributes);
472 }
473 else {
474 RoleMembershipPolicyUtil.verifyPolicy(
475 role, oldRole, oldExpandoAttributes);
476 }
477
478 return role;
479 }
480
481 protected void checkUserRolesPermission(long userId, long[] roleIds)
482 throws PortalException {
483
484 for (int i = 0; i < roleIds.length; i++) {
485 RolePermissionUtil.check(
486 getPermissionChecker(), roleIds[i], ActionKeys.ASSIGN_MEMBERS);
487 }
488 }
489
490 protected List<Role> filterRoles(List<Role> roles) throws PortalException {
491 List<Role> filteredRoles = new ArrayList<>();
492
493 for (Role role : roles) {
494 if (RolePermissionUtil.contains(
495 getPermissionChecker(), role.getRoleId(),
496 ActionKeys.VIEW)) {
497
498 filteredRoles.add(role);
499 }
500 }
501
502 return filteredRoles;
503 }
504
505 }