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
068 @Override
069 public Role addRole(
070 String className, long classPK, String name,
071 Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
072 int type, String subtype, ServiceContext serviceContext)
073 throws PortalException {
074
075 PortalPermissionUtil.check(getPermissionChecker(), ActionKeys.ADD_ROLE);
076
077 User user = getUser();
078
079 Role role = roleLocalService.addRole(
080 user.getUserId(), className, classPK, name, titleMap,
081 descriptionMap, type, subtype, serviceContext);
082
083 if (type == RoleConstants.TYPE_ORGANIZATION) {
084 OrganizationMembershipPolicyUtil.verifyPolicy(role);
085 }
086 else if (type == RoleConstants.TYPE_SITE) {
087 SiteMembershipPolicyUtil.verifyPolicy(role);
088 }
089 else {
090 RoleMembershipPolicyUtil.verifyPolicy(role);
091 }
092
093 return role;
094 }
095
096
109 @Deprecated
110 @Override
111 public Role addRole(
112 String name, Map<Locale, String> titleMap,
113 Map<Locale, String> descriptionMap, int type)
114 throws PortalException {
115
116 return addRole(
117 null, 0, name, titleMap, descriptionMap, type, null, null);
118 }
119
120
127 @Override
128 public void addUserRoles(long userId, long[] roleIds)
129 throws PortalException {
130
131 if (roleIds.length == 0) {
132 return;
133 }
134
135 checkUserRolesPermission(userId, roleIds);
136
137 RoleMembershipPolicyUtil.checkRoles(new long[] {userId}, roleIds, null);
138
139 roleLocalService.addUserRoles(userId, roleIds);
140
141 RoleMembershipPolicyUtil.propagateRoles(
142 new long[] {userId}, roleIds, null);
143 }
144
145
150 @Override
151 public void deleteRole(long roleId) throws PortalException {
152 RolePermissionUtil.check(
153 getPermissionChecker(), roleId, ActionKeys.DELETE);
154
155 roleLocalService.deleteRole(roleId);
156 }
157
158 @Override
159 public Role fetchRole(long roleId) throws PortalException {
160 Role role = roleLocalService.fetchRole(roleId);
161
162 if (role != null) {
163 RolePermissionUtil.check(
164 getPermissionChecker(), roleId, ActionKeys.VIEW);
165 }
166
167 return role;
168 }
169
170
176 @Override
177 public List<Role> getGroupRoles(long groupId) throws PortalException {
178 List<Role> roles = roleLocalService.getGroupRoles(groupId);
179
180 return filterRoles(roles);
181 }
182
183
189 @Override
190 public Role getRole(long roleId) throws PortalException {
191 RolePermissionUtil.check(
192 getPermissionChecker(), roleId, ActionKeys.VIEW);
193
194 return roleLocalService.getRole(roleId);
195 }
196
197
209 @Override
210 public Role getRole(long companyId, String name) throws PortalException {
211 Role role = roleLocalService.getRole(companyId, name);
212
213 RolePermissionUtil.check(
214 getPermissionChecker(), role.getRoleId(), ActionKeys.VIEW);
215
216 return role;
217 }
218
219 @Override
220 public List<Role> getRoles(int type, String subtype)
221 throws PortalException {
222
223 return filterRoles(roleLocalService.getRoles(type, subtype));
224 }
225
226 @Override
227 public List<Role> getRoles(long companyId, int[] types)
228 throws PortalException {
229
230 return filterRoles(roleLocalService.getRoles(companyId, types));
231 }
232
233
240 @Override
241 public List<Role> getUserGroupGroupRoles(long userId, long groupId)
242 throws PortalException {
243
244 UserPermissionUtil.check(
245 getPermissionChecker(), userId, ActionKeys.VIEW);
246
247 List<Role> roles = roleLocalService.getUserGroupGroupRoles(
248 userId, groupId);
249
250 return filterRoles(roles);
251 }
252
253
260 @Override
261 public List<Role> getUserGroupRoles(long userId, long groupId)
262 throws PortalException {
263
264 UserPermissionUtil.check(
265 getPermissionChecker(), userId, ActionKeys.VIEW);
266
267 List<Role> roles = roleLocalService.getUserGroupRoles(userId, groupId);
268
269 return filterRoles(roles);
270 }
271
272
279 @Override
280 public List<Role> getUserRelatedRoles(long userId, List<Group> groups)
281 throws PortalException {
282
283 UserPermissionUtil.check(
284 getPermissionChecker(), userId, ActionKeys.VIEW);
285
286 List<Role> roles = roleLocalService.getUserRelatedRoles(userId, groups);
287
288 return filterRoles(roles);
289 }
290
291
297 @Override
298 public List<Role> getUserRoles(long userId) throws PortalException {
299 UserPermissionUtil.check(
300 getPermissionChecker(), userId, ActionKeys.VIEW);
301
302 List<Role> roles = roleLocalService.getUserRoles(userId);
303
304 return filterRoles(roles);
305 }
306
307
319 @Override
320 public boolean hasUserRole(
321 long userId, long companyId, String name, boolean inherited)
322 throws PortalException {
323
324 UserPermissionUtil.check(
325 getPermissionChecker(), userId, ActionKeys.VIEW);
326
327 return roleLocalService.hasUserRole(userId, companyId, name, inherited);
328 }
329
330
342 @Override
343 public boolean hasUserRoles(
344 long userId, long companyId, String[] names, boolean inherited)
345 throws PortalException {
346
347 UserPermissionUtil.check(
348 getPermissionChecker(), userId, ActionKeys.VIEW);
349
350 return roleLocalService.hasUserRoles(
351 userId, companyId, names, inherited);
352 }
353
354 @Override
355 public List<Role> search(
356 long companyId, String keywords, Integer[] types,
357 LinkedHashMap<String, Object> params, int start, int end,
358 OrderByComparator<Role> obc) {
359
360 return roleFinder.filterFindByKeywords(
361 companyId, keywords, types, params, start, end, obc);
362 }
363
364 @Override
365 public int searchCount(
366 long companyId, String keywords, Integer[] types,
367 LinkedHashMap<String, Object> params) {
368
369 return roleFinder.filterCountByKeywords(
370 companyId, keywords, types, params);
371 }
372
373
380 @Override
381 public void unsetUserRoles(long userId, long[] roleIds)
382 throws PortalException {
383
384 if (roleIds.length == 0) {
385 return;
386 }
387
388 checkUserRolesPermission(userId, roleIds);
389
390 RoleMembershipPolicyUtil.checkRoles(new long[] {userId}, null, roleIds);
391
392 roleLocalService.unsetUserRoles(userId, roleIds);
393
394 RoleMembershipPolicyUtil.propagateRoles(
395 new long[] {userId}, null, roleIds);
396 }
397
398
413 @Override
414 public Role updateRole(
415 long roleId, String name, Map<Locale, String> titleMap,
416 Map<Locale, String> descriptionMap, String subtype,
417 ServiceContext serviceContext)
418 throws PortalException {
419
420 RolePermissionUtil.check(
421 getPermissionChecker(), roleId, ActionKeys.UPDATE);
422
423 Role oldRole = rolePersistence.findByPrimaryKey(roleId);
424
425 ExpandoBridge oldExpandoBridge = oldRole.getExpandoBridge();
426
427 Map<String, Serializable> oldExpandoAttributes =
428 oldExpandoBridge.getAttributes();
429
430 Role role = roleLocalService.updateRole(
431 roleId, name, titleMap, descriptionMap, subtype, serviceContext);
432
433 if (role.getType() == RoleConstants.TYPE_ORGANIZATION) {
434 OrganizationMembershipPolicyUtil.verifyPolicy(
435 role, oldRole, oldExpandoAttributes);
436 }
437 else if (role.getType() == RoleConstants.TYPE_SITE) {
438 SiteMembershipPolicyUtil.verifyPolicy(
439 role, oldRole, oldExpandoAttributes);
440 }
441 else {
442 RoleMembershipPolicyUtil.verifyPolicy(
443 role, oldRole, oldExpandoAttributes);
444 }
445
446 return role;
447 }
448
449 protected void checkUserRolesPermission(long userId, long[] roleIds)
450 throws PortalException {
451
452 for (int i = 0; i < roleIds.length; i++) {
453 RolePermissionUtil.check(
454 getPermissionChecker(), roleIds[i], ActionKeys.ASSIGN_MEMBERS);
455 }
456 }
457
458 protected List<Role> filterRoles(List<Role> roles) throws PortalException {
459 List<Role> filteredRoles = new ArrayList<>();
460
461 for (Role role : roles) {
462 if (RolePermissionUtil.contains(
463 getPermissionChecker(), role.getRoleId(),
464 ActionKeys.VIEW)) {
465
466 filteredRoles.add(role);
467 }
468 }
469
470 return filteredRoles;
471 }
472
473 }