001
014
015 package com.liferay.portal.service.impl;
016
017 import com.liferay.admin.kernel.util.PortalMyAccountApplicationType;
018 import com.liferay.exportimport.kernel.lar.ExportImportThreadLocal;
019 import com.liferay.portal.kernel.cache.thread.local.Lifecycle;
020 import com.liferay.portal.kernel.cache.thread.local.ThreadLocalCachable;
021 import com.liferay.portal.kernel.cache.thread.local.ThreadLocalCache;
022 import com.liferay.portal.kernel.cache.thread.local.ThreadLocalCacheManager;
023 import com.liferay.portal.kernel.exception.DuplicateRoleException;
024 import com.liferay.portal.kernel.exception.NoSuchRoleException;
025 import com.liferay.portal.kernel.exception.PortalException;
026 import com.liferay.portal.kernel.exception.RequiredRoleException;
027 import com.liferay.portal.kernel.exception.RoleNameException;
028 import com.liferay.portal.kernel.model.Company;
029 import com.liferay.portal.kernel.model.Group;
030 import com.liferay.portal.kernel.model.Layout;
031 import com.liferay.portal.kernel.model.ResourceAction;
032 import com.liferay.portal.kernel.model.ResourceBlockPermission;
033 import com.liferay.portal.kernel.model.ResourceConstants;
034 import com.liferay.portal.kernel.model.ResourcePermission;
035 import com.liferay.portal.kernel.model.ResourceTypePermission;
036 import com.liferay.portal.kernel.model.Role;
037 import com.liferay.portal.kernel.model.RoleConstants;
038 import com.liferay.portal.kernel.model.SystemEventConstants;
039 import com.liferay.portal.kernel.model.Team;
040 import com.liferay.portal.kernel.model.User;
041 import com.liferay.portal.kernel.portlet.PortletProvider;
042 import com.liferay.portal.kernel.portlet.PortletProviderUtil;
043 import com.liferay.portal.kernel.search.Indexer;
044 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
045 import com.liferay.portal.kernel.search.SearchException;
046 import com.liferay.portal.kernel.security.auth.CompanyThreadLocal;
047 import com.liferay.portal.kernel.security.permission.ActionKeys;
048 import com.liferay.portal.kernel.service.ServiceContext;
049 import com.liferay.portal.kernel.spring.aop.Skip;
050 import com.liferay.portal.kernel.systemevent.SystemEvent;
051 import com.liferay.portal.kernel.transaction.Propagation;
052 import com.liferay.portal.kernel.transaction.Transactional;
053 import com.liferay.portal.kernel.util.ArrayUtil;
054 import com.liferay.portal.kernel.util.CharPool;
055 import com.liferay.portal.kernel.util.GetterUtil;
056 import com.liferay.portal.kernel.util.ListUtil;
057 import com.liferay.portal.kernel.util.LocaleUtil;
058 import com.liferay.portal.kernel.util.OrderByComparator;
059 import com.liferay.portal.kernel.util.PortalUtil;
060 import com.liferay.portal.kernel.util.PortletKeys;
061 import com.liferay.portal.kernel.util.SetUtil;
062 import com.liferay.portal.kernel.util.StringUtil;
063 import com.liferay.portal.kernel.util.Validator;
064 import com.liferay.portal.security.permission.PermissionCacheUtil;
065 import com.liferay.portal.service.base.RoleLocalServiceBaseImpl;
066 import com.liferay.portal.util.PropsUtil;
067 import com.liferay.portal.util.PropsValues;
068 import com.liferay.users.admin.kernel.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
111 @Override
112 public Role addRole(
113 long userId, String className, long classPK, String name,
114 Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
115 int type, String subtype, ServiceContext serviceContext)
116 throws PortalException {
117
118
119
120 User user = userPersistence.findByPrimaryKey(userId);
121 className = GetterUtil.getString(className);
122 long classNameId = classNameLocalService.getClassNameId(className);
123
124 long roleId = counterLocalService.increment();
125
126 if ((classNameId <= 0) || className.equals(Role.class.getName())) {
127 classNameId = classNameLocalService.getClassNameId(Role.class);
128 classPK = roleId;
129 }
130
131 validate(0, user.getCompanyId(), classNameId, name);
132
133 Role role = rolePersistence.create(roleId);
134
135 if (serviceContext != null) {
136 role.setUuid(serviceContext.getUuid());
137 }
138
139 role.setCompanyId(user.getCompanyId());
140 role.setUserId(user.getUserId());
141 role.setUserName(user.getFullName());
142 role.setClassNameId(classNameId);
143 role.setClassPK(classPK);
144 role.setName(name);
145 role.setTitleMap(titleMap);
146 role.setDescriptionMap(descriptionMap);
147 role.setType(type);
148 role.setSubtype(subtype);
149 role.setExpandoBridgeAttributes(serviceContext);
150
151 rolePersistence.update(role);
152
153
154
155 if (!user.isDefaultUser()) {
156 resourceLocalService.addResources(
157 user.getCompanyId(), 0, userId, Role.class.getName(),
158 role.getRoleId(), false, false, false);
159
160 if (!ExportImportThreadLocal.isImportInProcess()) {
161 reindex(userId);
162 }
163 }
164
165 return role;
166 }
167
168
177 @Override
178 public void addUserRoles(long userId, long[] roleIds)
179 throws PortalException {
180
181 userPersistence.addRoles(userId, roleIds);
182
183 reindex(userId);
184
185 PermissionCacheUtil.clearCache(userId);
186 }
187
188
192 @Override
193 public void checkSystemRoles() throws PortalException {
194 List<Company> companies = companyLocalService.getCompanies();
195
196 for (Company company : companies) {
197 checkSystemRoles(company.getCompanyId());
198 }
199 }
200
201
207 @Override
208 @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
209 public void checkSystemRoles(long companyId) throws PortalException {
210 String companyIdHexString = StringUtil.toHexString(companyId);
211
212 List<Role> roles = null;
213
214 try {
215 roles = roleFinder.findBySystem(companyId);
216 }
217 catch (Exception e) {
218
219
220
221 runSQL("alter table Role_ add uuid_ VARCHAR(75) null");
222 runSQL("alter table Role_ add userId LONG");
223 runSQL("alter table Role_ add userName VARCHAR(75) null");
224 runSQL("alter table Role_ add createDate DATE null");
225 runSQL("alter table Role_ add modifiedDate DATE null");
226
227 roles = roleFinder.findBySystem(companyId);
228 }
229
230 for (Role role : roles) {
231 _systemRolesMap.put(
232 companyIdHexString.concat(role.getName()), role);
233 }
234
235
236
237 String[] systemRoles = PortalUtil.getSystemRoles();
238
239 for (String name : systemRoles) {
240 String key =
241 "system.role." +
242 StringUtil.replace(name, CharPool.SPACE, CharPool.PERIOD) +
243 ".description";
244
245 Map<Locale, String> descriptionMap = new HashMap<>();
246
247 descriptionMap.put(LocaleUtil.getDefault(), PropsUtil.get(key));
248
249 int type = RoleConstants.TYPE_REGULAR;
250
251 checkSystemRole(companyId, name, descriptionMap, type);
252 }
253
254
255
256 String[] systemOrganizationRoles =
257 PortalUtil.getSystemOrganizationRoles();
258
259 for (String name : systemOrganizationRoles) {
260 String key =
261 "system.organization.role." +
262 StringUtil.replace(name, CharPool.SPACE, CharPool.PERIOD) +
263 ".description";
264
265 Map<Locale, String> descriptionMap = new HashMap<>();
266
267 descriptionMap.put(LocaleUtil.getDefault(), PropsUtil.get(key));
268
269 int type = RoleConstants.TYPE_ORGANIZATION;
270
271 checkSystemRole(companyId, name, descriptionMap, type);
272 }
273
274
275
276 String[] systemSiteRoles = PortalUtil.getSystemSiteRoles();
277
278 for (String name : systemSiteRoles) {
279 String key =
280 "system.site.role." +
281 StringUtil.replace(name, CharPool.SPACE, CharPool.PERIOD) +
282 ".description";
283
284 Map<Locale, String> descriptionMap = new HashMap<>();
285
286 descriptionMap.put(LocaleUtil.getDefault(), PropsUtil.get(key));
287
288 int type = RoleConstants.TYPE_SITE;
289
290 checkSystemRole(companyId, name, descriptionMap, type);
291 }
292
293
294
295 Role userRole = getRole(companyId, RoleConstants.USER);
296
297 String[] userViewableRoles = ArrayUtil.append(
298 systemRoles, systemOrganizationRoles, systemSiteRoles);
299
300 for (String roleName : userViewableRoles) {
301 Role role = getRole(companyId, roleName);
302
303 resourcePermissionLocalService.setResourcePermissions(
304 companyId, Role.class.getName(),
305 ResourceConstants.SCOPE_INDIVIDUAL,
306 String.valueOf(role.getRoleId()), userRole.getRoleId(),
307 new String[] {ActionKeys.VIEW});
308 }
309 }
310
311
317 @Override
318 public Role deleteRole(long roleId) throws PortalException {
319 Role role = rolePersistence.findByPrimaryKey(roleId);
320
321 return roleLocalService.deleteRole(role);
322 }
323
324
330 @Override
331 @SystemEvent(
332 action = SystemEventConstants.ACTION_SKIP,
333 type = SystemEventConstants.TYPE_DELETE
334 )
335 public Role deleteRole(Role role) throws PortalException {
336 if (role.isSystem() && !CompanyThreadLocal.isDeleteInProcess()) {
337 throw new RequiredRoleException();
338 }
339
340
341
342 List<ResourceBlockPermission> resourceBlockPermissions =
343 resourceBlockPermissionPersistence.findByRoleId(role.getRoleId());
344
345 for (ResourceBlockPermission resourceBlockPermission :
346 resourceBlockPermissions) {
347
348 resourceBlockPermissionLocalService.deleteResourceBlockPermission(
349 resourceBlockPermission);
350 }
351
352 List<ResourcePermission> resourcePermissions =
353 resourcePermissionPersistence.findByRoleId(role.getRoleId());
354
355 for (ResourcePermission resourcePermission : resourcePermissions) {
356 resourcePermissionLocalService.deleteResourcePermission(
357 resourcePermission);
358 }
359
360 List<ResourceTypePermission> resourceTypePermissions =
361 resourceTypePermissionPersistence.findByRoleId(role.getRoleId());
362
363 for (ResourceTypePermission resourceTypePermission :
364 resourceTypePermissions) {
365
366 resourceTypePermissionLocalService.deleteResourceTypePermission(
367 resourceTypePermission);
368 }
369
370 String className = role.getClassName();
371 long classNameId = role.getClassNameId();
372
373 if ((classNameId <= 0) || className.equals(Role.class.getName())) {
374 resourceLocalService.deleteResource(
375 role.getCompanyId(), Role.class.getName(),
376 ResourceConstants.SCOPE_INDIVIDUAL, role.getRoleId());
377 }
378
379 if ((role.getType() == RoleConstants.TYPE_ORGANIZATION) ||
380 (role.getType() == RoleConstants.TYPE_SITE)) {
381
382 userGroupRoleLocalService.deleteUserGroupRolesByRoleId(
383 role.getRoleId());
384
385 userGroupGroupRoleLocalService.deleteUserGroupGroupRolesByRoleId(
386 role.getRoleId());
387 }
388
389
390
391 rolePersistence.remove(role);
392
393
394
395 expandoRowLocalService.deleteRows(role.getRoleId());
396
397
398
399 PermissionCacheUtil.clearCache();
400
401 return role;
402 }
403
404
417 @Override
418 @Skip
419 public Role fetchRole(long companyId, String name) {
420 String companyIdHexString = StringUtil.toHexString(companyId);
421
422 Role role = _systemRolesMap.get(companyIdHexString.concat(name));
423
424 if (role != null) {
425 return role;
426 }
427
428 return roleLocalService.loadFetchRole(companyId, name);
429 }
430
431
446 @Override
447 public Role getDefaultGroupRole(long groupId) throws PortalException {
448 Group group = groupPersistence.findByPrimaryKey(groupId);
449
450 if (group.isLayout()) {
451 Layout layout = layoutLocalService.getLayout(group.getClassPK());
452
453 group = layout.getGroup();
454 }
455
456 if (group.isStagingGroup()) {
457 group = group.getLiveGroup();
458 }
459
460 Role role = null;
461
462 if (group.isCompany()) {
463 role = getRole(group.getCompanyId(), RoleConstants.USER);
464 }
465 else if (group.isLayoutPrototype() || group.isLayoutSetPrototype() ||
466 group.isRegularSite() || group.isSite()) {
467
468 role = getRole(group.getCompanyId(), RoleConstants.SITE_MEMBER);
469 }
470 else if (group.isOrganization()) {
471 role = getRole(
472 group.getCompanyId(), RoleConstants.ORGANIZATION_USER);
473 }
474 else {
475 role = getRole(group.getCompanyId(), RoleConstants.USER);
476 }
477
478 return role;
479 }
480
481 @Override
482 public List<Role> getGroupRelatedRoles(long groupId)
483 throws PortalException {
484
485 List<Role> roles = new ArrayList<>();
486
487 Group group = groupLocalService.getGroup(groupId);
488
489 if (group.isStagingGroup()) {
490 group = group.getLiveGroup();
491 }
492
493 int[] types = RoleConstants.TYPES_REGULAR;
494
495 if (group.isOrganization()) {
496 if (group.isSite()) {
497 types = RoleConstants.TYPES_ORGANIZATION_AND_REGULAR_AND_SITE;
498 }
499 else {
500 types = RoleConstants.TYPES_ORGANIZATION_AND_REGULAR;
501 }
502 }
503 else if (group.isLayout() || group.isLayoutSetPrototype() ||
504 group.isSite() || group.isUser()) {
505
506 types = RoleConstants.TYPES_REGULAR_AND_SITE;
507 }
508
509 roles.addAll(getRoles(group.getCompanyId(), types));
510
511 roles.addAll(getTeamRoles(groupId));
512
513 return roles;
514 }
515
516 @Override
517 public List<Role> getResourceBlockRoles(
518 long resourceBlockId, String className, String actionId) {
519
520 return roleFinder.findByR_N_A(resourceBlockId, className, actionId);
521 }
522
523
535 @Override
536 public Map<String, List<String>> getResourceRoles(
537 long companyId, String name, int scope, String primKey) {
538
539 return roleFinder.findByC_N_S_P(companyId, name, scope, primKey);
540 }
541
542
555 @Override
556 public List<Role> getResourceRoles(
557 long companyId, String name, int scope, String primKey,
558 String actionId) {
559
560 return roleFinder.findByC_N_S_P_A(
561 companyId, name, scope, primKey, actionId);
562 }
563
564
576 @Override
577 @Skip
578 public Role getRole(long companyId, String name) throws PortalException {
579 String companyIdHexString = StringUtil.toHexString(companyId);
580
581 Role role = _systemRolesMap.get(companyIdHexString.concat(name));
582
583 if (role != null) {
584 return role;
585 }
586
587 return roleLocalService.loadGetRole(companyId, name);
588 }
589
590
597 @Override
598 public List<Role> getRoles(int type, String subtype) {
599 return rolePersistence.findByT_S(type, subtype);
600 }
601
602
608 @Override
609 public List<Role> getRoles(long companyId) {
610 return rolePersistence.findByCompanyId(companyId);
611 }
612
613
620 @Override
621 public List<Role> getRoles(long companyId, int[] types) {
622 return rolePersistence.findByC_T(companyId, types);
623 }
624
625
631 @Override
632 public List<Role> getRoles(long[] roleIds) throws PortalException {
633 List<Role> roles = new ArrayList<>(roleIds.length);
634
635 for (long roleId : roleIds) {
636 Role role = getRole(roleId);
637
638 roles.add(role);
639 }
640
641 return roles;
642 }
643
644
650 @Override
651 public List<Role> getSubtypeRoles(String subtype) {
652 return rolePersistence.findBySubtype(subtype);
653 }
654
655
661 @Override
662 public int getSubtypeRolesCount(String subtype) {
663 return rolePersistence.countBySubtype(subtype);
664 }
665
666
673 @Override
674 public Role getTeamRole(long companyId, long teamId)
675 throws PortalException {
676
677 long classNameId = classNameLocalService.getClassNameId(Team.class);
678
679 return rolePersistence.findByC_C_C(companyId, classNameId, teamId);
680 }
681
682
688 @Override
689 public Map<Team, Role> getTeamRoleMap(long groupId) throws PortalException {
690 return getTeamRoleMap(groupId, null);
691 }
692
693
699 @Override
700 public List<Role> getTeamRoles(long groupId) throws PortalException {
701 return getTeamRoles(groupId, null);
702 }
703
704
712 @Override
713 public List<Role> getTeamRoles(long groupId, long[] excludedRoleIds)
714 throws PortalException {
715
716 Map<Team, Role> teamRoleMap = getTeamRoleMap(groupId, excludedRoleIds);
717
718 Collection<Role> roles = teamRoleMap.values();
719
720 return ListUtil.fromCollection(roles);
721 }
722
723
729 @Override
730 public List<Role> getTypeRoles(int type) {
731 return rolePersistence.findByType(type);
732 }
733
734
743 @Override
744 public List<Role> getTypeRoles(int type, int start, int end) {
745 return rolePersistence.findByType(type, start, end);
746 }
747
748
754 @Override
755 public int getTypeRolesCount(int type) {
756 return rolePersistence.countByType(type);
757 }
758
759
768 @Override
769 public List<Role> getUserGroupGroupRoles(long userId, long groupId) {
770 return roleFinder.findByUserGroupGroupRole(userId, groupId);
771 }
772
773 @Override
774 public List<Role> getUserGroupGroupRoles(
775 long userId, long groupId, int start, int end) {
776
777 return roleFinder.findByUserGroupGroupRole(userId, groupId, start, end);
778 }
779
780 @Override
781 public int getUserGroupGroupRolesCount(long userId, long groupId) {
782 return roleFinder.countByUserGroupGroupRole(userId, groupId);
783 }
784
785
794 @Override
795 public List<Role> getUserGroupRoles(long userId, long groupId) {
796 return roleFinder.findByUserGroupRole(userId, groupId);
797 }
798
799
808 @Override
809 public List<Role> getUserRelatedRoles(long userId, List<Group> groups) {
810 if ((groups == null) || groups.isEmpty()) {
811 return Collections.emptyList();
812 }
813
814 return roleFinder.findByU_G(userId, groups);
815 }
816
817
826 @Override
827 public List<Role> getUserRelatedRoles(long userId, long groupId) {
828 return roleFinder.findByU_G(userId, groupId);
829 }
830
831
840 @Override
841 public List<Role> getUserRelatedRoles(long userId, long[] groupIds) {
842 return roleFinder.findByU_G(userId, groupIds);
843 }
844
845
857 @Override
858 @ThreadLocalCachable
859 public boolean hasUserRole(
860 long userId, long companyId, String name, boolean inherited)
861 throws PortalException {
862
863 Role role = rolePersistence.fetchByC_N(companyId, name);
864
865 if (role == null) {
866 return false;
867 }
868
869 if (role.getType() != RoleConstants.TYPE_REGULAR) {
870 throw new IllegalArgumentException(name + " is not a regular role");
871 }
872
873 long defaultUserId = userLocalService.getDefaultUserId(companyId);
874
875 if (userId == defaultUserId) {
876 if (name.equals(RoleConstants.GUEST)) {
877 return true;
878 }
879 else {
880 return false;
881 }
882 }
883
884 if (inherited) {
885 if (userPersistence.containsRole(userId, role.getRoleId())) {
886 return true;
887 }
888
889 ThreadLocalCache<Boolean> threadLocalCache =
890 ThreadLocalCacheManager.getThreadLocalCache(
891 Lifecycle.REQUEST, RoleLocalServiceImpl.class.getName());
892
893 String key = String.valueOf(role.getRoleId()).concat(
894 String.valueOf(userId));
895
896 Boolean value = threadLocalCache.get(key);
897
898 if (value != null) {
899 return value;
900 }
901
902 value = PermissionCacheUtil.getUserRole(userId, role);
903
904 if (value == null) {
905 int count = roleFinder.countByR_U(role.getRoleId(), userId);
906
907 if (count > 0) {
908 value = true;
909 }
910 else {
911 value = false;
912 }
913
914 PermissionCacheUtil.putUserRole(userId, role, value);
915 }
916
917 threadLocalCache.put(key, value);
918
919 return value;
920 }
921 else {
922 return userPersistence.containsRole(userId, role.getRoleId());
923 }
924 }
925
926
938 @Override
939 public boolean hasUserRoles(
940 long userId, long companyId, String[] names, boolean inherited)
941 throws PortalException {
942
943 for (String name : names) {
944 if (hasUserRole(userId, companyId, name, inherited)) {
945 return true;
946 }
947 }
948
949 return false;
950 }
951
952
960 @Override
961 public Role loadFetchRole(long companyId, String name) {
962 return rolePersistence.fetchByC_N(companyId, name);
963 }
964
965
972 @Override
973 public Role loadGetRole(long companyId, String name)
974 throws PortalException {
975
976 return rolePersistence.findByC_N(companyId, name);
977 }
978
979
1006 @Override
1007 public List<Role> search(
1008 long companyId, String keywords, Integer[] types, int start, int end,
1009 OrderByComparator<Role> obc) {
1010
1011 return search(
1012 companyId, keywords, types, new LinkedHashMap<String, Object>(),
1013 start, end, obc);
1014 }
1015
1016
1046 @Override
1047 public List<Role> search(
1048 long companyId, String keywords, Integer[] types,
1049 LinkedHashMap<String, Object> params, int start, int end,
1050 OrderByComparator<Role> obc) {
1051
1052 return roleFinder.findByKeywords(
1053 companyId, keywords, types, params, start, end, obc);
1054 }
1055
1056
1083 @Override
1084 public List<Role> search(
1085 long companyId, String name, String description, Integer[] types,
1086 int start, int end, OrderByComparator<Role> obc) {
1087
1088 return search(
1089 companyId, name, description, types,
1090 new LinkedHashMap<String, Object>(), start, end, obc);
1091 }
1092
1093
1123 @Override
1124 public List<Role> search(
1125 long companyId, String name, String description, Integer[] types,
1126 LinkedHashMap<String, Object> params, int start, int end,
1127 OrderByComparator<Role> obc) {
1128
1129 return roleFinder.findByC_N_D_T(
1130 companyId, name, description, types, params, true, start, end, obc);
1131 }
1132
1133
1142 @Override
1143 public int searchCount(long companyId, String keywords, Integer[] types) {
1144 return searchCount(
1145 companyId, keywords, types, new LinkedHashMap<String, Object>());
1146 }
1147
1148
1159 @Override
1160 public int searchCount(
1161 long companyId, String keywords, Integer[] types,
1162 LinkedHashMap<String, Object> params) {
1163
1164 return roleFinder.countByKeywords(companyId, keywords, types, params);
1165 }
1166
1167
1176 @Override
1177 public int searchCount(
1178 long companyId, String name, String description, Integer[] types) {
1179
1180 return searchCount(
1181 companyId, name, description, types,
1182 new LinkedHashMap<String, Object>());
1183 }
1184
1185
1198 @Override
1199 public int searchCount(
1200 long companyId, String name, String description, Integer[] types,
1201 LinkedHashMap<String, Object> params) {
1202
1203 return roleFinder.countByC_N_D_T(
1204 companyId, name, description, types, params, true);
1205 }
1206
1207
1214 @Override
1215 public void setUserRoles(long userId, long[] roleIds)
1216 throws PortalException {
1217
1218 roleIds = UsersAdminUtil.addRequiredRoles(userId, roleIds);
1219
1220 userPersistence.setRoles(userId, roleIds);
1221
1222 reindex(userId);
1223
1224 PermissionCacheUtil.clearCache(userId);
1225 }
1226
1227
1234 @Override
1235 public void unsetUserRoles(long userId, long[] roleIds)
1236 throws PortalException {
1237
1238 roleIds = UsersAdminUtil.removeRequiredRoles(userId, roleIds);
1239
1240 userPersistence.removeRoles(userId, roleIds);
1241
1242 reindex(userId);
1243
1244 PermissionCacheUtil.clearCache(userId);
1245 }
1246
1247
1262 @Override
1263 public Role updateRole(
1264 long roleId, String name, Map<Locale, String> titleMap,
1265 Map<Locale, String> descriptionMap, String subtype,
1266 ServiceContext serviceContext)
1267 throws PortalException {
1268
1269 Role role = rolePersistence.findByPrimaryKey(roleId);
1270
1271 validate(roleId, role.getCompanyId(), role.getClassNameId(), name);
1272
1273 if (role.isSystem()) {
1274 name = role.getName();
1275 subtype = null;
1276 }
1277
1278 role.setName(name);
1279 role.setTitleMap(titleMap);
1280 role.setDescriptionMap(descriptionMap);
1281 role.setSubtype(subtype);
1282 role.setExpandoBridgeAttributes(serviceContext);
1283
1284 rolePersistence.update(role);
1285
1286 return role;
1287 }
1288
1289 protected void checkSystemRole(
1290 long companyId, String name, Map<Locale, String> descriptionMap,
1291 int type)
1292 throws PortalException {
1293
1294 String companyIdHexString = StringUtil.toHexString(companyId);
1295
1296 String key = companyIdHexString.concat(name);
1297
1298 Role role = _systemRolesMap.get(key);
1299
1300 try {
1301 if (role == null) {
1302 role = rolePersistence.findByC_N(companyId, name);
1303 }
1304
1305 if (!descriptionMap.equals(role.getDescriptionMap())) {
1306 role.setDescriptionMap(descriptionMap);
1307
1308 roleLocalService.updateRole(role);
1309 }
1310 }
1311 catch (NoSuchRoleException nsre) {
1312 User user = userLocalService.getDefaultUser(companyId);
1313
1314 role = roleLocalService.addRole(
1315 user.getUserId(), null, 0, name, null, descriptionMap, type,
1316 null, null);
1317
1318 if (name.equals(RoleConstants.USER)) {
1319 initPersonalControlPanelPortletsPermissions(role);
1320 }
1321 }
1322
1323 _systemRolesMap.put(key, role);
1324 }
1325
1326 protected String[] getDefaultControlPanelPortlets() {
1327 String myAccountPortletId = PortletProviderUtil.getPortletId(
1328 PortalMyAccountApplicationType.MyAccount.CLASS_NAME,
1329 PortletProvider.Action.VIEW);
1330
1331 return new String[] {
1332 myAccountPortletId, PortletKeys.MY_PAGES,
1333 PortletKeys.MY_WORKFLOW_INSTANCE, PortletKeys.MY_WORKFLOW_TASK
1334 };
1335 }
1336
1337 protected Map<Team, Role> getTeamRoleMap(
1338 long groupId, long[] excludedRoleIds)
1339 throws PortalException {
1340
1341 Group group = groupPersistence.findByPrimaryKey(groupId);
1342
1343 if (group.isLayout()) {
1344 group = group.getParentGroup();
1345 }
1346
1347 List<Team> teams = teamPersistence.findByGroupId(group.getGroupId());
1348
1349 if (teams.isEmpty()) {
1350 return Collections.emptyMap();
1351 }
1352
1353 Set<Long> roleIds = SetUtil.fromArray(excludedRoleIds);
1354
1355 Map<Team, Role> teamRoleMap = new LinkedHashMap<>();
1356
1357 for (Team team : teams) {
1358 Role role = getTeamRole(team.getCompanyId(), team.getTeamId());
1359
1360 if (roleIds.contains(role.getRoleId())) {
1361 continue;
1362 }
1363
1364 teamRoleMap.put(team, role);
1365 }
1366
1367 return teamRoleMap;
1368 }
1369
1370 protected void initPersonalControlPanelPortletsPermissions(Role role)
1371 throws PortalException {
1372
1373 for (String portletId : getDefaultControlPanelPortlets()) {
1374 int count = resourcePermissionPersistence.countByC_N_S_P_R(
1375 role.getCompanyId(), portletId, ResourceConstants.SCOPE_COMPANY,
1376 String.valueOf(role.getCompanyId()), role.getRoleId());
1377
1378 if (count > 0) {
1379 continue;
1380 }
1381
1382 ResourceAction resourceAction =
1383 resourceActionLocalService.fetchResourceAction(
1384 portletId, ActionKeys.ACCESS_IN_CONTROL_PANEL);
1385
1386 if (resourceAction == null) {
1387 continue;
1388 }
1389
1390 setRolePermissions(
1391 role, portletId,
1392 new String[] {ActionKeys.ACCESS_IN_CONTROL_PANEL});
1393 }
1394 }
1395
1396 protected void reindex(long userId) throws SearchException {
1397 Indexer<User> indexer = IndexerRegistryUtil.nullSafeGetIndexer(
1398 User.class);
1399
1400 User user = userLocalService.fetchUser(userId);
1401
1402 indexer.reindex(user);
1403 }
1404
1405 protected void setRolePermissions(
1406 Role role, String name, String[] actionIds)
1407 throws PortalException {
1408
1409 if (resourceBlockLocalService.isSupported(name)) {
1410 resourceBlockLocalService.setCompanyScopePermissions(
1411 role.getCompanyId(), name, role.getRoleId(),
1412 Arrays.asList(actionIds));
1413 }
1414 else {
1415 resourcePermissionLocalService.setResourcePermissions(
1416 role.getCompanyId(), name, ResourceConstants.SCOPE_COMPANY,
1417 String.valueOf(role.getCompanyId()), role.getRoleId(),
1418 actionIds);
1419 }
1420 }
1421
1422 protected void validate(
1423 long roleId, long companyId, long classNameId, String name)
1424 throws PortalException {
1425
1426 if (classNameId == classNameLocalService.getClassNameId(Role.class)) {
1427 if (Validator.isNull(name) ||
1428 (name.indexOf(CharPool.COMMA) != -1) ||
1429 (name.indexOf(CharPool.STAR) != -1)) {
1430
1431 throw new RoleNameException();
1432 }
1433
1434 if (Validator.isNumber(name) &&
1435 !PropsValues.ROLES_NAME_ALLOW_NUMERIC) {
1436
1437 throw new RoleNameException();
1438 }
1439 }
1440
1441 try {
1442 Role role = roleFinder.findByC_N(companyId, name);
1443
1444 if (role.getRoleId() != roleId) {
1445 throw new DuplicateRoleException("{roleId=" + roleId + "}");
1446 }
1447 }
1448 catch (NoSuchRoleException nsre) {
1449 }
1450
1451 if (name.equals(RoleConstants.PLACEHOLDER_DEFAULT_GROUP_ROLE)) {
1452 throw new RoleNameException(
1453 RoleConstants.PLACEHOLDER_DEFAULT_GROUP_ROLE +
1454 " is a temporary placeholder that must not be persisted");
1455 }
1456 }
1457
1458 private final Map<String, Role> _systemRolesMap = new HashMap<>();
1459
1460 }