001
014
015 package com.liferay.portal.service.impl;
016
017 import com.liferay.portal.DuplicateRoleException;
018 import com.liferay.portal.NoSuchRoleException;
019 import com.liferay.portal.RequiredRoleException;
020 import com.liferay.portal.RoleNameException;
021 import com.liferay.portal.kernel.cache.Lifecycle;
022 import com.liferay.portal.kernel.cache.ThreadLocalCachable;
023 import com.liferay.portal.kernel.cache.ThreadLocalCache;
024 import com.liferay.portal.kernel.cache.ThreadLocalCacheManager;
025 import com.liferay.portal.kernel.exception.PortalException;
026 import com.liferay.portal.kernel.portlet.PortletProvider;
027 import com.liferay.portal.kernel.portlet.PortletProviderUtil;
028 import com.liferay.portal.kernel.search.Indexer;
029 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
030 import com.liferay.portal.kernel.search.SearchException;
031 import com.liferay.portal.kernel.spring.aop.Skip;
032 import com.liferay.portal.kernel.systemevent.SystemEvent;
033 import com.liferay.portal.kernel.transaction.Propagation;
034 import com.liferay.portal.kernel.transaction.Transactional;
035 import com.liferay.portal.kernel.util.ArrayUtil;
036 import com.liferay.portal.kernel.util.CharPool;
037 import com.liferay.portal.kernel.util.GetterUtil;
038 import com.liferay.portal.kernel.util.ListUtil;
039 import com.liferay.portal.kernel.util.LocaleUtil;
040 import com.liferay.portal.kernel.util.OrderByComparator;
041 import com.liferay.portal.kernel.util.SetUtil;
042 import com.liferay.portal.kernel.util.StringUtil;
043 import com.liferay.portal.kernel.util.Validator;
044 import com.liferay.portal.model.Company;
045 import com.liferay.portal.model.Group;
046 import com.liferay.portal.model.Layout;
047 import com.liferay.portal.model.ResourceAction;
048 import com.liferay.portal.model.ResourceBlockPermission;
049 import com.liferay.portal.model.ResourceConstants;
050 import com.liferay.portal.model.ResourcePermission;
051 import com.liferay.portal.model.ResourceTypePermission;
052 import com.liferay.portal.model.Role;
053 import com.liferay.portal.model.RoleConstants;
054 import com.liferay.portal.model.SystemEventConstants;
055 import com.liferay.portal.model.Team;
056 import com.liferay.portal.model.User;
057 import com.liferay.portal.security.auth.CompanyThreadLocal;
058 import com.liferay.portal.security.permission.ActionKeys;
059 import com.liferay.portal.security.permission.PermissionCacheUtil;
060 import com.liferay.portal.service.ServiceContext;
061 import com.liferay.portal.service.base.RoleLocalServiceBaseImpl;
062 import com.liferay.portal.util.PortalUtil;
063 import com.liferay.portal.util.PortletKeys;
064 import com.liferay.portal.util.PropsUtil;
065 import com.liferay.portal.util.PropsValues;
066 import com.liferay.portlet.admin.util.PortalMyAccountApplicationType;
067 import com.liferay.portlet.exportimport.lar.ExportImportThreadLocal;
068 import com.liferay.portlet.usersadmin.util.UsersAdminUtil;
069
070 import java.util.ArrayList;
071 import java.util.Arrays;
072 import java.util.Collection;
073 import java.util.Collections;
074 import java.util.HashMap;
075 import java.util.LinkedHashMap;
076 import java.util.List;
077 import java.util.Locale;
078 import java.util.Map;
079 import java.util.Set;
080
081
088 public class RoleLocalServiceImpl extends RoleLocalServiceBaseImpl {
089
090
108 @Deprecated
109 @Override
110 public Role addRole(
111 long userId, long companyId, String name,
112 Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
113 int type)
114 throws PortalException {
115
116 return addRole(
117 userId, null, 0, name, titleMap, descriptionMap, type, null, null);
118 }
119
120
143 @Deprecated
144 @Override
145 public Role addRole(
146 long userId, long companyId, String name,
147 Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
148 int type, String className, long classPK)
149 throws PortalException {
150
151 return addRole(
152 userId, className, classPK, name, titleMap, descriptionMap, type,
153 null, null);
154 }
155
156
180 @Override
181 public Role addRole(
182 long userId, String className, long classPK, String name,
183 Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
184 int type, String subtype, ServiceContext serviceContext)
185 throws PortalException {
186
187
188
189 User user = userPersistence.findByPrimaryKey(userId);
190 className = GetterUtil.getString(className);
191 long classNameId = classNameLocalService.getClassNameId(className);
192
193 long roleId = counterLocalService.increment();
194
195 if ((classNameId <= 0) || className.equals(Role.class.getName())) {
196 classNameId = classNameLocalService.getClassNameId(Role.class);
197 classPK = roleId;
198 }
199
200 validate(0, user.getCompanyId(), classNameId, name);
201
202 Role role = rolePersistence.create(roleId);
203
204 if (serviceContext != null) {
205 role.setUuid(serviceContext.getUuid());
206 }
207
208 role.setCompanyId(user.getCompanyId());
209 role.setUserId(user.getUserId());
210 role.setUserName(user.getFullName());
211 role.setClassNameId(classNameId);
212 role.setClassPK(classPK);
213 role.setName(name);
214 role.setTitleMap(titleMap);
215 role.setDescriptionMap(descriptionMap);
216 role.setType(type);
217 role.setSubtype(subtype);
218 role.setExpandoBridgeAttributes(serviceContext);
219
220 rolePersistence.update(role);
221
222
223
224 if (!user.isDefaultUser()) {
225 resourceLocalService.addResources(
226 user.getCompanyId(), 0, userId, Role.class.getName(),
227 role.getRoleId(), false, false, false);
228
229 if (!ExportImportThreadLocal.isImportInProcess()) {
230 reindex(userId);
231 }
232 }
233
234 return role;
235 }
236
237
247 @Override
248 public void addUserRoles(long userId, long[] roleIds)
249 throws PortalException {
250
251 userPersistence.addRoles(userId, roleIds);
252
253 reindex(userId);
254
255 PermissionCacheUtil.clearCache(userId);
256 }
257
258
265 @Override
266 public void checkSystemRoles() throws PortalException {
267 List<Company> companies = companyLocalService.getCompanies();
268
269 for (Company company : companies) {
270 checkSystemRoles(company.getCompanyId());
271 }
272 }
273
274
282 @Override
283 @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
284 public void checkSystemRoles(long companyId) throws PortalException {
285 String companyIdHexString = StringUtil.toHexString(companyId);
286
287 List<Role> roles = null;
288
289 try {
290 roles = roleFinder.findBySystem(companyId);
291 }
292 catch (Exception e) {
293
294
295
296 runSQL("alter table Role_ add uuid_ VARCHAR(75) null");
297 runSQL("alter table Role_ add userId LONG");
298 runSQL("alter table Role_ add userName VARCHAR(75) null");
299 runSQL("alter table Role_ add createDate DATE null");
300 runSQL("alter table Role_ add modifiedDate DATE null");
301
302 roles = roleFinder.findBySystem(companyId);
303 }
304
305 for (Role role : roles) {
306 _systemRolesMap.put(
307 companyIdHexString.concat(role.getName()), role);
308 }
309
310
311
312 String[] systemRoles = PortalUtil.getSystemRoles();
313
314 for (String name : systemRoles) {
315 String key =
316 "system.role." +
317 StringUtil.replace(name, CharPool.SPACE, CharPool.PERIOD) +
318 ".description";
319
320 Map<Locale, String> descriptionMap = new HashMap<>();
321
322 descriptionMap.put(LocaleUtil.getDefault(), PropsUtil.get(key));
323
324 int type = RoleConstants.TYPE_REGULAR;
325
326 checkSystemRole(companyId, name, descriptionMap, type);
327 }
328
329
330
331 String[] systemOrganizationRoles =
332 PortalUtil.getSystemOrganizationRoles();
333
334 for (String name : systemOrganizationRoles) {
335 String key =
336 "system.organization.role." +
337 StringUtil.replace(name, CharPool.SPACE, CharPool.PERIOD) +
338 ".description";
339
340 Map<Locale, String> descriptionMap = new HashMap<>();
341
342 descriptionMap.put(LocaleUtil.getDefault(), PropsUtil.get(key));
343
344 int type = RoleConstants.TYPE_ORGANIZATION;
345
346 checkSystemRole(companyId, name, descriptionMap, type);
347 }
348
349
350
351 String[] systemSiteRoles = PortalUtil.getSystemSiteRoles();
352
353 for (String name : systemSiteRoles) {
354 String key =
355 "system.site.role." +
356 StringUtil.replace(name, CharPool.SPACE, CharPool.PERIOD) +
357 ".description";
358
359 Map<Locale, String> descriptionMap = new HashMap<>();
360
361 descriptionMap.put(LocaleUtil.getDefault(), PropsUtil.get(key));
362
363 int type = RoleConstants.TYPE_SITE;
364
365 checkSystemRole(companyId, name, descriptionMap, type);
366 }
367
368
369
370 Role userRole = getRole(companyId, RoleConstants.USER);
371
372 String[] userViewableRoles = ArrayUtil.append(
373 systemRoles, systemOrganizationRoles, systemSiteRoles);
374
375 for (String roleName : userViewableRoles) {
376 Role role = getRole(companyId, roleName);
377
378 resourcePermissionLocalService.setResourcePermissions(
379 companyId, Role.class.getName(),
380 ResourceConstants.SCOPE_INDIVIDUAL,
381 String.valueOf(role.getRoleId()), userRole.getRoleId(),
382 new String[] {ActionKeys.VIEW});
383 }
384 }
385
386
395 @Override
396 public Role deleteRole(long roleId) throws PortalException {
397 Role role = rolePersistence.findByPrimaryKey(roleId);
398
399 return roleLocalService.deleteRole(role);
400 }
401
402
410 @Override
411 @SystemEvent(
412 action = SystemEventConstants.ACTION_SKIP,
413 type = SystemEventConstants.TYPE_DELETE
414 )
415 public Role deleteRole(Role role) throws PortalException {
416 if (role.isSystem() && !CompanyThreadLocal.isDeleteInProcess()) {
417 throw new RequiredRoleException();
418 }
419
420
421
422 List<ResourceBlockPermission> resourceBlockPermissions =
423 resourceBlockPermissionPersistence.findByRoleId(role.getRoleId());
424
425 for (ResourceBlockPermission resourceBlockPermission :
426 resourceBlockPermissions) {
427
428 resourceBlockPermissionLocalService.deleteResourceBlockPermission(
429 resourceBlockPermission);
430 }
431
432 List<ResourcePermission> resourcePermissions =
433 resourcePermissionPersistence.findByRoleId(role.getRoleId());
434
435 for (ResourcePermission resourcePermission : resourcePermissions) {
436 resourcePermissionLocalService.deleteResourcePermission(
437 resourcePermission);
438 }
439
440 List<ResourceTypePermission> resourceTypePermissions =
441 resourceTypePermissionPersistence.findByRoleId(role.getRoleId());
442
443 for (ResourceTypePermission resourceTypePermission :
444 resourceTypePermissions) {
445
446 resourceTypePermissionLocalService.deleteResourceTypePermission(
447 resourceTypePermission);
448 }
449
450 String className = role.getClassName();
451 long classNameId = role.getClassNameId();
452
453 if ((classNameId <= 0) || className.equals(Role.class.getName())) {
454 resourceLocalService.deleteResource(
455 role.getCompanyId(), Role.class.getName(),
456 ResourceConstants.SCOPE_INDIVIDUAL, role.getRoleId());
457 }
458
459 if ((role.getType() == RoleConstants.TYPE_ORGANIZATION) ||
460 (role.getType() == RoleConstants.TYPE_SITE)) {
461
462 userGroupRoleLocalService.deleteUserGroupRolesByRoleId(
463 role.getRoleId());
464
465 userGroupGroupRoleLocalService.deleteUserGroupGroupRolesByRoleId(
466 role.getRoleId());
467 }
468
469
470
471 rolePersistence.remove(role);
472
473
474
475 expandoRowLocalService.deleteRows(role.getRoleId());
476
477
478
479 PermissionCacheUtil.clearCache();
480
481 return role;
482 }
483
484
497 @Override
498 @Skip
499 public Role fetchRole(long companyId, String name) {
500 String companyIdHexString = StringUtil.toHexString(companyId);
501
502 Role role = _systemRolesMap.get(companyIdHexString.concat(name));
503
504 if (role != null) {
505 return role;
506 }
507
508 return roleLocalService.loadFetchRole(companyId, name);
509 }
510
511
528 @Override
529 public Role getDefaultGroupRole(long groupId) throws PortalException {
530 Group group = groupPersistence.findByPrimaryKey(groupId);
531
532 if (group.isLayout()) {
533 Layout layout = layoutLocalService.getLayout(group.getClassPK());
534
535 group = layout.getGroup();
536 }
537
538 if (group.isStagingGroup()) {
539 group = group.getLiveGroup();
540 }
541
542 Role role = null;
543
544 if (group.isCompany()) {
545 role = getRole(group.getCompanyId(), RoleConstants.USER);
546 }
547 else if (group.isLayoutPrototype() || group.isLayoutSetPrototype() ||
548 group.isRegularSite() || group.isSite()) {
549
550 role = getRole(group.getCompanyId(), RoleConstants.SITE_MEMBER);
551 }
552 else if (group.isOrganization()) {
553 role = getRole(
554 group.getCompanyId(), RoleConstants.ORGANIZATION_USER);
555 }
556 else {
557 role = getRole(group.getCompanyId(), RoleConstants.USER);
558 }
559
560 return role;
561 }
562
563 @Override
564 public List<Role> getGroupRelatedRoles(long groupId)
565 throws PortalException {
566
567 List<Role> roles = new ArrayList<>();
568
569 Group group = groupLocalService.getGroup(groupId);
570
571 if (group.isStagingGroup()) {
572 group = group.getLiveGroup();
573 }
574
575 int[] types = RoleConstants.TYPES_REGULAR;
576
577 if (group.isOrganization()) {
578 types = RoleConstants.TYPES_ORGANIZATION_AND_REGULAR;
579 }
580 else if (group.isLayout() || group.isLayoutSetPrototype() ||
581 group.isSite() || group.isUser()) {
582
583 types = RoleConstants.TYPES_REGULAR_AND_SITE;
584 }
585
586 roles.addAll(getRoles(group.getCompanyId(), types));
587
588 roles.addAll(getTeamRoles(groupId));
589
590 return roles;
591 }
592
593 @Override
594 public List<Role> getResourceBlockRoles(
595 long resourceBlockId, String className, String actionId) {
596
597 return roleFinder.findByR_N_A(resourceBlockId, className, actionId);
598 }
599
600
612 @Override
613 public Map<String, List<String>> getResourceRoles(
614 long companyId, String name, int scope, String primKey) {
615
616 return roleFinder.findByC_N_S_P(companyId, name, scope, primKey);
617 }
618
619
632 @Override
633 public List<Role> getResourceRoles(
634 long companyId, String name, int scope, String primKey,
635 String actionId) {
636
637 return roleFinder.findByC_N_S_P_A(
638 companyId, name, scope, primKey, actionId);
639 }
640
641
655 @Override
656 @Skip
657 public Role getRole(long companyId, String name) throws PortalException {
658 String companyIdHexString = StringUtil.toHexString(companyId);
659
660 Role role = _systemRolesMap.get(companyIdHexString.concat(name));
661
662 if (role != null) {
663 return role;
664 }
665
666 return roleLocalService.loadGetRole(companyId, name);
667 }
668
669
676 @Override
677 public List<Role> getRoles(int type, String subtype) {
678 return rolePersistence.findByT_S(type, subtype);
679 }
680
681
687 @Override
688 public List<Role> getRoles(long companyId) {
689 return rolePersistence.findByCompanyId(companyId);
690 }
691
692
699 @Override
700 public List<Role> getRoles(long companyId, int[] types) {
701 return rolePersistence.findByC_T(companyId, types);
702 }
703
704
712 @Override
713 public List<Role> getRoles(long[] roleIds) throws PortalException {
714 List<Role> roles = new ArrayList<>(roleIds.length);
715
716 for (long roleId : roleIds) {
717 Role role = getRole(roleId);
718
719 roles.add(role);
720 }
721
722 return roles;
723 }
724
725
731 @Override
732 public List<Role> getSubtypeRoles(String subtype) {
733 return rolePersistence.findBySubtype(subtype);
734 }
735
736
742 @Override
743 public int getSubtypeRolesCount(String subtype) {
744 return rolePersistence.countBySubtype(subtype);
745 }
746
747
756 @Override
757 public Role getTeamRole(long companyId, long teamId)
758 throws PortalException {
759
760 long classNameId = classNameLocalService.getClassNameId(Team.class);
761
762 return rolePersistence.findByC_C_C(companyId, classNameId, teamId);
763 }
764
765
774 @Override
775 public Map<Team, Role> getTeamRoleMap(long groupId) throws PortalException {
776 return getTeamRoleMap(groupId, null);
777 }
778
779
788 @Override
789 public List<Role> getTeamRoles(long groupId) throws PortalException {
790 return getTeamRoles(groupId, null);
791 }
792
793
804 @Override
805 public List<Role> getTeamRoles(long groupId, long[] excludedRoleIds)
806 throws PortalException {
807
808 Map<Team, Role> teamRoleMap = getTeamRoleMap(groupId, excludedRoleIds);
809
810 Collection<Role> roles = teamRoleMap.values();
811
812 return ListUtil.fromCollection(roles);
813 }
814
815
821 @Override
822 public List<Role> getTypeRoles(int type) {
823 return rolePersistence.findByType(type);
824 }
825
826
835 @Override
836 public List<Role> getTypeRoles(int type, int start, int end) {
837 return rolePersistence.findByType(type, start, end);
838 }
839
840
846 @Override
847 public int getTypeRolesCount(int type) {
848 return rolePersistence.countByType(type);
849 }
850
851
860 @Override
861 public List<Role> getUserGroupGroupRoles(long userId, long groupId) {
862 return roleFinder.findByUserGroupGroupRole(userId, groupId);
863 }
864
865 @Override
866 public List<Role> getUserGroupGroupRoles(
867 long userId, long groupId, int start, int end) {
868
869 return roleFinder.findByUserGroupGroupRole(userId, groupId, start, end);
870 }
871
872 @Override
873 public int getUserGroupGroupRolesCount(long userId, long groupId) {
874 return roleFinder.countByUserGroupGroupRole(userId, groupId);
875 }
876
877
886 @Override
887 public List<Role> getUserGroupRoles(long userId, long groupId) {
888 return roleFinder.findByUserGroupRole(userId, groupId);
889 }
890
891
900 @Override
901 public List<Role> getUserRelatedRoles(long userId, List<Group> groups) {
902 if ((groups == null) || groups.isEmpty()) {
903 return Collections.emptyList();
904 }
905
906 return roleFinder.findByU_G(userId, groups);
907 }
908
909
918 @Override
919 public List<Role> getUserRelatedRoles(long userId, long groupId) {
920 return roleFinder.findByU_G(userId, groupId);
921 }
922
923
932 @Override
933 public List<Role> getUserRelatedRoles(long userId, long[] groupIds) {
934 return roleFinder.findByU_G(userId, groupIds);
935 }
936
937
951 @Override
952 @ThreadLocalCachable
953 public boolean hasUserRole(
954 long userId, long companyId, String name, boolean inherited)
955 throws PortalException {
956
957 Role role = rolePersistence.fetchByC_N(companyId, name);
958
959 if (role == null) {
960 return false;
961 }
962
963 if (role.getType() != RoleConstants.TYPE_REGULAR) {
964 throw new IllegalArgumentException(name + " is not a regular role");
965 }
966
967 long defaultUserId = userLocalService.getDefaultUserId(companyId);
968
969 if (userId == defaultUserId) {
970 if (name.equals(RoleConstants.GUEST)) {
971 return true;
972 }
973 else {
974 return false;
975 }
976 }
977
978 if (inherited) {
979 if (userPersistence.containsRole(userId, role.getRoleId())) {
980 return true;
981 }
982
983 ThreadLocalCache<Boolean> threadLocalCache =
984 ThreadLocalCacheManager.getThreadLocalCache(
985 Lifecycle.REQUEST, RoleLocalServiceImpl.class.getName());
986
987 String key = String.valueOf(role.getRoleId()).concat(
988 String.valueOf(userId));
989
990 Boolean value = threadLocalCache.get(key);
991
992 if (value != null) {
993 return value;
994 }
995
996 value = PermissionCacheUtil.getUserRole(userId, role);
997
998 if (value == null) {
999 int count = roleFinder.countByR_U(role.getRoleId(), userId);
1000
1001 if (count > 0) {
1002 value = true;
1003 }
1004 else {
1005 value = false;
1006 }
1007
1008 PermissionCacheUtil.putUserRole(userId, role, value);
1009 }
1010
1011 threadLocalCache.put(key, value);
1012
1013 return value;
1014 }
1015 else {
1016 return userPersistence.containsRole(userId, role.getRoleId());
1017 }
1018 }
1019
1020
1035 @Override
1036 public boolean hasUserRoles(
1037 long userId, long companyId, String[] names, boolean inherited)
1038 throws PortalException {
1039
1040 for (String name : names) {
1041 if (hasUserRole(userId, companyId, name, inherited)) {
1042 return true;
1043 }
1044 }
1045
1046 return false;
1047 }
1048
1049
1057 @Override
1058 public Role loadFetchRole(long companyId, String name) {
1059 return rolePersistence.fetchByC_N(companyId, name);
1060 }
1061
1062
1071 @Override
1072 public Role loadGetRole(long companyId, String name)
1073 throws PortalException {
1074
1075 return rolePersistence.findByC_N(companyId, name);
1076 }
1077
1078
1105 @Override
1106 public List<Role> search(
1107 long companyId, String keywords, Integer[] types, int start, int end,
1108 OrderByComparator<Role> obc) {
1109
1110 return search(
1111 companyId, keywords, types, new LinkedHashMap<String, Object>(),
1112 start, end, obc);
1113 }
1114
1115
1145 @Override
1146 public List<Role> search(
1147 long companyId, String keywords, Integer[] types,
1148 LinkedHashMap<String, Object> params, int start, int end,
1149 OrderByComparator<Role> obc) {
1150
1151 return roleFinder.findByKeywords(
1152 companyId, keywords, types, params, start, end, obc);
1153 }
1154
1155
1182 @Override
1183 public List<Role> search(
1184 long companyId, String name, String description, Integer[] types,
1185 int start, int end, OrderByComparator<Role> obc) {
1186
1187 return search(
1188 companyId, name, description, types,
1189 new LinkedHashMap<String, Object>(), start, end, obc);
1190 }
1191
1192
1222 @Override
1223 public List<Role> search(
1224 long companyId, String name, String description, Integer[] types,
1225 LinkedHashMap<String, Object> params, int start, int end,
1226 OrderByComparator<Role> obc) {
1227
1228 return roleFinder.findByC_N_D_T(
1229 companyId, name, description, types, params, true, start, end, obc);
1230 }
1231
1232
1241 @Override
1242 public int searchCount(long companyId, String keywords, Integer[] types) {
1243 return searchCount(
1244 companyId, keywords, types, new LinkedHashMap<String, Object>());
1245 }
1246
1247
1258 @Override
1259 public int searchCount(
1260 long companyId, String keywords, Integer[] types,
1261 LinkedHashMap<String, Object> params) {
1262
1263 return roleFinder.countByKeywords(companyId, keywords, types, params);
1264 }
1265
1266
1275 @Override
1276 public int searchCount(
1277 long companyId, String name, String description, Integer[] types) {
1278
1279 return searchCount(
1280 companyId, name, description, types,
1281 new LinkedHashMap<String, Object>());
1282 }
1283
1284
1297 @Override
1298 public int searchCount(
1299 long companyId, String name, String description, Integer[] types,
1300 LinkedHashMap<String, Object> params) {
1301
1302 return roleFinder.countByC_N_D_T(
1303 companyId, name, description, types, params, true);
1304 }
1305
1306
1315 @Override
1316 public void setUserRoles(long userId, long[] roleIds)
1317 throws PortalException {
1318
1319 roleIds = UsersAdminUtil.addRequiredRoles(userId, roleIds);
1320
1321 userPersistence.setRoles(userId, roleIds);
1322
1323 reindex(userId);
1324
1325 PermissionCacheUtil.clearCache(userId);
1326 }
1327
1328
1337 @Override
1338 public void unsetUserRoles(long userId, long[] roleIds)
1339 throws PortalException {
1340
1341 roleIds = UsersAdminUtil.removeRequiredRoles(userId, roleIds);
1342
1343 userPersistence.removeRoles(userId, roleIds);
1344
1345 reindex(userId);
1346
1347 PermissionCacheUtil.clearCache(userId);
1348 }
1349
1350
1367 @Override
1368 public Role updateRole(
1369 long roleId, String name, Map<Locale, String> titleMap,
1370 Map<Locale, String> descriptionMap, String subtype,
1371 ServiceContext serviceContext)
1372 throws PortalException {
1373
1374 Role role = rolePersistence.findByPrimaryKey(roleId);
1375
1376 validate(roleId, role.getCompanyId(), role.getClassNameId(), name);
1377
1378 if (role.isSystem()) {
1379 name = role.getName();
1380 subtype = null;
1381 }
1382
1383 role.setName(name);
1384 role.setTitleMap(titleMap);
1385 role.setDescriptionMap(descriptionMap);
1386 role.setSubtype(subtype);
1387 role.setExpandoBridgeAttributes(serviceContext);
1388
1389 rolePersistence.update(role);
1390
1391 return role;
1392 }
1393
1394 protected void checkSystemRole(
1395 long companyId, String name, Map<Locale, String> descriptionMap,
1396 int type)
1397 throws PortalException {
1398
1399 String companyIdHexString = StringUtil.toHexString(companyId);
1400
1401 String key = companyIdHexString.concat(name);
1402
1403 Role role = _systemRolesMap.get(key);
1404
1405 try {
1406 if (role == null) {
1407 role = rolePersistence.findByC_N(companyId, name);
1408 }
1409
1410 if (!descriptionMap.equals(role.getDescriptionMap())) {
1411 role.setDescriptionMap(descriptionMap);
1412
1413 roleLocalService.updateRole(role);
1414 }
1415 }
1416 catch (NoSuchRoleException nsre) {
1417 User user = userLocalService.getDefaultUser(companyId);
1418
1419 role = roleLocalService.addRole(
1420 user.getUserId(), null, 0, name, null, descriptionMap, type,
1421 null, null);
1422
1423 if (name.equals(RoleConstants.USER)) {
1424 initPersonalControlPanelPortletsPermissions(role);
1425 }
1426 }
1427
1428 _systemRolesMap.put(key, role);
1429 }
1430
1431 protected String[] getDefaultControlPanelPortlets() {
1432 String myAccountPortletId = PortletProviderUtil.getPortletId(
1433 PortalMyAccountApplicationType.MyAccount.CLASS_NAME,
1434 PortletProvider.Action.VIEW);
1435
1436 return new String[] {
1437 myAccountPortletId, PortletKeys.MY_PAGES,
1438 PortletKeys.MY_WORKFLOW_INSTANCE, PortletKeys.MY_WORKFLOW_TASK
1439 };
1440 }
1441
1442 protected Map<Team, Role> getTeamRoleMap(
1443 long groupId, long[] excludedRoleIds)
1444 throws PortalException {
1445
1446 Group group = groupPersistence.findByPrimaryKey(groupId);
1447
1448 if (group.isLayout()) {
1449 group = group.getParentGroup();
1450 }
1451
1452 List<Team> teams = teamPersistence.findByGroupId(group.getGroupId());
1453
1454 if (teams.isEmpty()) {
1455 return Collections.emptyMap();
1456 }
1457
1458 Set<Long> roleIds = SetUtil.fromArray(excludedRoleIds);
1459
1460 Map<Team, Role> teamRoleMap = new LinkedHashMap<>();
1461
1462 for (Team team : teams) {
1463 Role role = getTeamRole(team.getCompanyId(), team.getTeamId());
1464
1465 if (roleIds.contains(role.getRoleId())) {
1466 continue;
1467 }
1468
1469 teamRoleMap.put(team, role);
1470 }
1471
1472 return teamRoleMap;
1473 }
1474
1475 protected void initPersonalControlPanelPortletsPermissions(Role role)
1476 throws PortalException {
1477
1478 for (String portletId : getDefaultControlPanelPortlets()) {
1479 int count = resourcePermissionPersistence.countByC_N_S_P_R(
1480 role.getCompanyId(), portletId, ResourceConstants.SCOPE_COMPANY,
1481 String.valueOf(role.getCompanyId()), role.getRoleId());
1482
1483 if (count > 0) {
1484 continue;
1485 }
1486
1487 ResourceAction resourceAction =
1488 resourceActionLocalService.fetchResourceAction(
1489 portletId, ActionKeys.ACCESS_IN_CONTROL_PANEL);
1490
1491 if (resourceAction == null) {
1492 continue;
1493 }
1494
1495 setRolePermissions(
1496 role, portletId,
1497 new String[] {ActionKeys.ACCESS_IN_CONTROL_PANEL});
1498 }
1499 }
1500
1501 protected void reindex(long userId) throws SearchException {
1502 Indexer<User> indexer = IndexerRegistryUtil.nullSafeGetIndexer(
1503 User.class);
1504
1505 User user = userLocalService.fetchUser(userId);
1506
1507 indexer.reindex(user);
1508 }
1509
1510 protected void setRolePermissions(
1511 Role role, String name, String[] actionIds)
1512 throws PortalException {
1513
1514 if (resourceBlockLocalService.isSupported(name)) {
1515 resourceBlockLocalService.setCompanyScopePermissions(
1516 role.getCompanyId(), name, role.getRoleId(),
1517 Arrays.asList(actionIds));
1518 }
1519 else {
1520 resourcePermissionLocalService.setResourcePermissions(
1521 role.getCompanyId(), name, ResourceConstants.SCOPE_COMPANY,
1522 String.valueOf(role.getCompanyId()), role.getRoleId(),
1523 actionIds);
1524 }
1525 }
1526
1527 protected void validate(
1528 long roleId, long companyId, long classNameId, String name)
1529 throws PortalException {
1530
1531 if (classNameId == classNameLocalService.getClassNameId(Role.class)) {
1532 if (Validator.isNull(name) ||
1533 (name.indexOf(CharPool.COMMA) != -1) ||
1534 (name.indexOf(CharPool.STAR) != -1)) {
1535
1536 throw new RoleNameException();
1537 }
1538
1539 if (Validator.isNumber(name) &&
1540 !PropsValues.ROLES_NAME_ALLOW_NUMERIC) {
1541
1542 throw new RoleNameException();
1543 }
1544 }
1545
1546 try {
1547 Role role = roleFinder.findByC_N(companyId, name);
1548
1549 if (role.getRoleId() != roleId) {
1550 throw new DuplicateRoleException("{roleId=" + roleId + "}");
1551 }
1552 }
1553 catch (NoSuchRoleException nsre) {
1554 }
1555
1556 if (name.equals(RoleConstants.PLACEHOLDER_DEFAULT_GROUP_ROLE)) {
1557 throw new RoleNameException(
1558 RoleConstants.PLACEHOLDER_DEFAULT_GROUP_ROLE +
1559 " is a temporary placeholder that must not be persisted");
1560 }
1561 }
1562
1563 private final Map<String, Role> _systemRolesMap = new HashMap<>();
1564
1565 }