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.NoSuchShardException;
020 import com.liferay.portal.RequiredRoleException;
021 import com.liferay.portal.RoleNameException;
022 import com.liferay.portal.kernel.cache.Lifecycle;
023 import com.liferay.portal.kernel.cache.ThreadLocalCachable;
024 import com.liferay.portal.kernel.cache.ThreadLocalCache;
025 import com.liferay.portal.kernel.cache.ThreadLocalCacheManager;
026 import com.liferay.portal.kernel.dao.db.DB;
027 import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
028 import com.liferay.portal.kernel.dao.shard.ShardUtil;
029 import com.liferay.portal.kernel.exception.PortalException;
030 import com.liferay.portal.kernel.exception.SystemException;
031 import com.liferay.portal.kernel.lar.ExportImportThreadLocal;
032 import com.liferay.portal.kernel.search.Indexer;
033 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
034 import com.liferay.portal.kernel.spring.aop.Skip;
035 import com.liferay.portal.kernel.systemevent.SystemEvent;
036 import com.liferay.portal.kernel.transaction.Propagation;
037 import com.liferay.portal.kernel.transaction.Transactional;
038 import com.liferay.portal.kernel.util.ArrayUtil;
039 import com.liferay.portal.kernel.util.CharPool;
040 import com.liferay.portal.kernel.util.GetterUtil;
041 import com.liferay.portal.kernel.util.ListUtil;
042 import com.liferay.portal.kernel.util.LocaleUtil;
043 import com.liferay.portal.kernel.util.OrderByComparator;
044 import com.liferay.portal.kernel.util.SetUtil;
045 import com.liferay.portal.kernel.util.StringUtil;
046 import com.liferay.portal.kernel.util.Validator;
047 import com.liferay.portal.model.Company;
048 import com.liferay.portal.model.Group;
049 import com.liferay.portal.model.Layout;
050 import com.liferay.portal.model.ResourceAction;
051 import com.liferay.portal.model.ResourceBlockPermission;
052 import com.liferay.portal.model.ResourceConstants;
053 import com.liferay.portal.model.ResourcePermission;
054 import com.liferay.portal.model.ResourceTypePermission;
055 import com.liferay.portal.model.Role;
056 import com.liferay.portal.model.RoleConstants;
057 import com.liferay.portal.model.Shard;
058 import com.liferay.portal.model.SystemEventConstants;
059 import com.liferay.portal.model.Team;
060 import com.liferay.portal.model.User;
061 import com.liferay.portal.security.auth.CompanyThreadLocal;
062 import com.liferay.portal.security.permission.ActionKeys;
063 import com.liferay.portal.security.permission.PermissionCacheUtil;
064 import com.liferay.portal.security.permission.PermissionThreadLocal;
065 import com.liferay.portal.service.ServiceContext;
066 import com.liferay.portal.service.base.RoleLocalServiceBaseImpl;
067 import com.liferay.portal.util.PortalUtil;
068 import com.liferay.portal.util.PortletKeys;
069 import com.liferay.portal.util.PropsUtil;
070 import com.liferay.portal.util.PropsValues;
071 import com.liferay.portlet.usersadmin.util.UsersAdminUtil;
072
073 import java.io.IOException;
074
075 import java.util.ArrayList;
076 import java.util.Arrays;
077 import java.util.Collection;
078 import java.util.Collections;
079 import java.util.Date;
080 import java.util.HashMap;
081 import java.util.LinkedHashMap;
082 import java.util.List;
083 import java.util.Locale;
084 import java.util.Map;
085 import java.util.Set;
086
087
094 public class RoleLocalServiceImpl extends RoleLocalServiceBaseImpl {
095
096
115 @Override
116 public Role addRole(
117 long userId, long companyId, String name,
118 Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
119 int type)
120 throws PortalException, SystemException {
121
122 return addRole(
123 userId, null, 0, name, titleMap, descriptionMap, type, null, null);
124 }
125
126
150 @Override
151 public Role addRole(
152 long userId, long companyId, String name,
153 Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
154 int type, String className, long classPK)
155 throws PortalException, SystemException {
156
157 return addRole(
158 userId, className, classPK, name, titleMap, descriptionMap, type,
159 null, null);
160 }
161
162
187 @Override
188 public Role addRole(
189 long userId, String className, long classPK, String name,
190 Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
191 int type, String subtype, ServiceContext serviceContext)
192 throws PortalException, SystemException {
193
194
195
196 User user = userPersistence.findByPrimaryKey(userId);
197 className = GetterUtil.getString(className);
198 long classNameId = PortalUtil.getClassNameId(className);
199
200 long roleId = counterLocalService.increment();
201
202 if ((classNameId <= 0) || className.equals(Role.class.getName())) {
203 classNameId = PortalUtil.getClassNameId(Role.class);
204 classPK = roleId;
205 }
206
207 Date now = new Date();
208
209 validate(0, user.getCompanyId(), classNameId, name);
210
211 Role role = rolePersistence.create(roleId);
212
213 if (serviceContext != null) {
214 role.setUuid(serviceContext.getUuid());
215 }
216
217 role.setCompanyId(user.getCompanyId());
218 role.setUserId(user.getUserId());
219 role.setUserName(user.getFullName());
220
221 if (serviceContext != null) {
222 role.setCreateDate(serviceContext.getCreateDate(now));
223 role.setModifiedDate(serviceContext.getModifiedDate(now));
224 }
225 else {
226 role.setCreateDate(now);
227 role.setModifiedDate(now);
228 }
229
230 role.setClassNameId(classNameId);
231 role.setClassPK(classPK);
232 role.setName(name);
233 role.setTitleMap(titleMap);
234 role.setDescriptionMap(descriptionMap);
235 role.setType(type);
236 role.setSubtype(subtype);
237 role.setExpandoBridgeAttributes(serviceContext);
238
239 rolePersistence.update(role);
240
241
242
243 long ownerId = userId;
244
245 if (user.isDefaultUser()) {
246 ownerId = 0;
247 }
248
249 resourceLocalService.addResources(
250 user.getCompanyId(), 0, ownerId, Role.class.getName(),
251 role.getRoleId(), false, false, false);
252
253 if (!user.isDefaultUser()) {
254 resourceLocalService.addResources(
255 user.getCompanyId(), 0, userId, Role.class.getName(),
256 role.getRoleId(), false, false, false);
257
258 if (!ExportImportThreadLocal.isImportInProcess()) {
259 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
260 User.class);
261
262 indexer.reindex(userId);
263 }
264 }
265
266 return role;
267 }
268
269
279 @Override
280 public void addUserRole(long userId, long roleId)
281 throws PortalException, SystemException {
282
283 userPersistence.addRole(userId, roleId);
284
285 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
286
287 indexer.reindex(userId);
288
289 PermissionCacheUtil.clearCache(userId);
290 }
291
292
302 @Override
303 public void addUserRole(long userId, Role role)
304 throws PortalException, SystemException {
305
306 userPersistence.addRole(userId, role);
307
308 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
309
310 indexer.reindex(userId);
311
312 PermissionCacheUtil.clearCache(userId);
313 }
314
315
326 @Override
327 public void addUserRoles(long userId, List<Role> roles)
328 throws PortalException, SystemException {
329
330 userPersistence.addRoles(userId, roles);
331
332 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
333
334 indexer.reindex(userId);
335
336 PermissionCacheUtil.clearCache(userId);
337 }
338
339
350 @Override
351 public void addUserRoles(long userId, long[] roleIds)
352 throws PortalException, SystemException {
353
354 userPersistence.addRoles(userId, roleIds);
355
356 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
357
358 indexer.reindex(userId);
359
360 PermissionCacheUtil.clearCache(userId);
361 }
362
363
371 @Override
372 public void checkSystemRoles() throws PortalException, SystemException {
373 List<Company> companies = companyLocalService.getCompanies();
374
375 String currentShardName = ShardUtil.getCurrentShardName();
376
377 for (Company company : companies) {
378 String shardName = null;
379
380 try {
381 shardName = company.getShardName();
382 }
383 catch (NoSuchShardException nsse) {
384 Shard shard = shardLocalService.addShard(
385 Company.class.getName(), company.getCompanyId(),
386 PropsValues.SHARD_DEFAULT_NAME);
387
388 shardName = shard.getName();
389 }
390
391 if (!ShardUtil.isEnabled() || shardName.equals(currentShardName)) {
392 checkSystemRoles(company.getCompanyId());
393 }
394 }
395 }
396
397
406 @Override
407 @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
408 public void checkSystemRoles(long companyId)
409 throws PortalException, SystemException {
410
411 String companyIdHexString = StringUtil.toHexString(companyId);
412
413 List<Role> roles = null;
414
415 try {
416 roles = roleFinder.findBySystem(companyId);
417 }
418 catch (Exception e) {
419
420
421
422 DB db = DBFactoryUtil.getDB();
423
424 try {
425 runSQL(
426 db.buildSQL(
427 "alter table Role_ add uuid_ VARCHAR(75) null"));
428 runSQL(db.buildSQL("alter table Role_ add userId LONG"));
429 runSQL(
430 db.buildSQL(
431 "alter table Role_ add userName VARCHAR(75) null"));
432 runSQL(
433 db.buildSQL("alter table Role_ add createDate DATE null"));
434 runSQL(
435 db.buildSQL(
436 "alter table Role_ add modifiedDate DATE null"));
437 }
438 catch (IOException ioe) {
439 throw new SystemException(ioe);
440 }
441
442 roles = roleFinder.findBySystem(companyId);
443 }
444
445 for (Role role : roles) {
446 _systemRolesMap.put(
447 companyIdHexString.concat(role.getName()), role);
448 }
449
450
451
452 String[] systemRoles = PortalUtil.getSystemRoles();
453
454 for (String name : systemRoles) {
455 String key =
456 "system.role." +
457 StringUtil.replace(name, CharPool.SPACE, CharPool.PERIOD) +
458 ".description";
459
460 Map<Locale, String> descriptionMap = new HashMap<Locale, String>();
461
462 descriptionMap.put(LocaleUtil.getDefault(), PropsUtil.get(key));
463
464 int type = RoleConstants.TYPE_REGULAR;
465
466 checkSystemRole(companyId, name, descriptionMap, type);
467 }
468
469
470
471 String[] systemOrganizationRoles =
472 PortalUtil.getSystemOrganizationRoles();
473
474 for (String name : systemOrganizationRoles) {
475 String key =
476 "system.organization.role." +
477 StringUtil.replace(name, CharPool.SPACE, CharPool.PERIOD) +
478 ".description";
479
480 Map<Locale, String> descriptionMap = new HashMap<Locale, String>();
481
482 descriptionMap.put(LocaleUtil.getDefault(), PropsUtil.get(key));
483
484 int type = RoleConstants.TYPE_ORGANIZATION;
485
486 checkSystemRole(companyId, name, descriptionMap, type);
487 }
488
489
490
491 String[] systemSiteRoles = PortalUtil.getSystemSiteRoles();
492
493 for (String name : systemSiteRoles) {
494 String key =
495 "system.site.role." +
496 StringUtil.replace(name, CharPool.SPACE, CharPool.PERIOD) +
497 ".description";
498
499 Map<Locale, String> descriptionMap = new HashMap<Locale, String>();
500
501 descriptionMap.put(LocaleUtil.getDefault(), PropsUtil.get(key));
502
503 int type = RoleConstants.TYPE_SITE;
504
505 checkSystemRole(companyId, name, descriptionMap, type);
506 }
507
508 String[] allSystemRoles = ArrayUtil.append(
509 systemRoles, systemOrganizationRoles, systemSiteRoles);
510
511 for (String roleName : allSystemRoles) {
512 Role role = getRole(companyId, roleName);
513
514 resourceLocalService.addResources(
515 companyId, 0, 0, Role.class.getName(), role.getRoleId(), false,
516 false, false);
517 }
518
519
520
521 Role userRole = getRole(companyId, RoleConstants.USER);
522
523 for (String roleName : allSystemRoles) {
524 Role role = getRole(companyId, roleName);
525
526 resourcePermissionLocalService.setResourcePermissions(
527 companyId, Role.class.getName(),
528 ResourceConstants.SCOPE_INDIVIDUAL,
529 String.valueOf(role.getRoleId()), userRole.getRoleId(),
530 new String[] {ActionKeys.VIEW});
531 }
532 }
533
534
544 @Override
545 public void clearUserRoles(long userId)
546 throws PortalException, SystemException {
547
548 userPersistence.clearRoles(userId);
549
550 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
551
552 indexer.reindex(userId);
553
554 PermissionCacheUtil.clearCache(userId);
555 }
556
557
567 @Override
568 public Role deleteRole(long roleId)
569 throws PortalException, SystemException {
570
571 Role role = rolePersistence.findByPrimaryKey(roleId);
572
573 return roleLocalService.deleteRole(role);
574 }
575
576
585 @Override
586 @SystemEvent(
587 action = SystemEventConstants.ACTION_SKIP,
588 type = SystemEventConstants.TYPE_DELETE)
589 public Role deleteRole(Role role) throws PortalException, SystemException {
590 if (PortalUtil.isSystemRole(role.getName()) &&
591 !CompanyThreadLocal.isDeleteInProcess()) {
592
593 throw new RequiredRoleException();
594 }
595
596
597
598 List<ResourceBlockPermission> resourceBlockPermissions =
599 resourceBlockPermissionPersistence.findByRoleId(role.getRoleId());
600
601 for (ResourceBlockPermission resourceBlockPermission :
602 resourceBlockPermissions) {
603
604 resourceBlockPermissionLocalService.deleteResourceBlockPermission(
605 resourceBlockPermission);
606 }
607
608 List<ResourcePermission> resourcePermissions =
609 resourcePermissionPersistence.findByRoleId(role.getRoleId());
610
611 for (ResourcePermission resourcePermission : resourcePermissions) {
612 resourcePermissionLocalService.deleteResourcePermission(
613 resourcePermission);
614 }
615
616 List<ResourceTypePermission> resourceTypePermissions =
617 resourceTypePermissionPersistence.findByRoleId(role.getRoleId());
618
619 for (ResourceTypePermission resourceTypePermission :
620 resourceTypePermissions) {
621
622 resourceTypePermissionLocalService.deleteResourceTypePermission(
623 resourceTypePermission);
624 }
625
626 String className = role.getClassName();
627 long classNameId = role.getClassNameId();
628
629 if ((classNameId <= 0) || className.equals(Role.class.getName())) {
630 resourceLocalService.deleteResource(
631 role.getCompanyId(), Role.class.getName(),
632 ResourceConstants.SCOPE_INDIVIDUAL, role.getRoleId());
633 }
634
635 if ((role.getType() == RoleConstants.TYPE_ORGANIZATION) ||
636 (role.getType() == RoleConstants.TYPE_SITE)) {
637
638 userGroupRoleLocalService.deleteUserGroupRolesByRoleId(
639 role.getRoleId());
640
641 userGroupGroupRoleLocalService.deleteUserGroupGroupRolesByRoleId(
642 role.getRoleId());
643 }
644
645
646
647 rolePersistence.remove(role);
648
649
650
651 expandoRowLocalService.deleteRows(role.getRoleId());
652
653
654
655 PermissionCacheUtil.clearCache();
656
657 return role;
658 }
659
660
671 @Override
672 public void deleteUserRole(long userId, long roleId)
673 throws PortalException, SystemException {
674
675 userPersistence.removeRole(userId, roleId);
676
677 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
678
679 indexer.reindex(userId);
680
681 PermissionCacheUtil.clearCache(userId);
682 }
683
684
695 @Override
696 public void deleteUserRole(long userId, Role role)
697 throws PortalException, SystemException {
698
699 userPersistence.removeRole(userId, role);
700
701 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
702
703 indexer.reindex(userId);
704
705 PermissionCacheUtil.clearCache(userId);
706 }
707
708
719 @Override
720 public void deleteUserRoles(long userId, List<Role> roles)
721 throws PortalException, SystemException {
722
723 userPersistence.removeRoles(userId, roles);
724
725 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
726
727 indexer.reindex(userId);
728
729 PermissionCacheUtil.clearCache(userId);
730 }
731
732
743 @Override
744 public void deleteUserRoles(long userId, long[] roleIds)
745 throws PortalException, SystemException {
746
747 userPersistence.removeRoles(userId, roleIds);
748
749 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
750
751 indexer.reindex(userId);
752
753 PermissionCacheUtil.clearCache(userId);
754 }
755
756
770 @Override
771 @Skip
772 public Role fetchRole(long companyId, String name) throws SystemException {
773 String companyIdHexString = StringUtil.toHexString(companyId);
774
775 Role role = _systemRolesMap.get(companyIdHexString.concat(name));
776
777 if (role != null) {
778 return role;
779 }
780
781 return roleLocalService.loadFetchRole(companyId, name);
782 }
783
784
804 @Override
805 public Role getDefaultGroupRole(long groupId)
806 throws PortalException, SystemException {
807
808 Group group = groupPersistence.findByPrimaryKey(groupId);
809
810 if (group.isLayout()) {
811 Layout layout = layoutLocalService.getLayout(group.getClassPK());
812
813 group = layout.getGroup();
814 }
815
816 if (group.isStagingGroup()) {
817 group = group.getLiveGroup();
818 }
819
820 Role role = null;
821
822 if (group.isCompany()) {
823 role = getRole(group.getCompanyId(), RoleConstants.USER);
824 }
825 else if (group.isLayoutPrototype() || group.isLayoutSetPrototype() ||
826 group.isRegularSite() || group.isSite()) {
827
828 role = getRole(group.getCompanyId(), RoleConstants.SITE_MEMBER);
829 }
830 else if (group.isOrganization()) {
831 role = getRole(
832 group.getCompanyId(), RoleConstants.ORGANIZATION_USER);
833 }
834 else {
835 role = getRole(group.getCompanyId(), RoleConstants.USER);
836 }
837
838 return role;
839 }
840
841 @Override
842 public List<Role> getGroupRelatedRoles(long groupId)
843 throws PortalException, SystemException {
844
845 List<Role> roles = new ArrayList<Role>();
846
847 Group group = groupLocalService.getGroup(groupId);
848
849 if (group.isStagingGroup()) {
850 group = group.getLiveGroup();
851 }
852
853 int[] types = RoleConstants.TYPES_REGULAR;
854
855 if (group.isOrganization()) {
856 types = RoleConstants.TYPES_ORGANIZATION_AND_REGULAR;
857 }
858 else if (group.isLayout() || group.isLayoutSetPrototype() ||
859 group.isSite() || group.isUser()) {
860
861 types = RoleConstants.TYPES_REGULAR_AND_SITE;
862 }
863
864 roles.addAll(getRoles(group.getCompanyId(), types));
865
866 roles.addAll(getTeamRoles(groupId));
867
868 return roles;
869 }
870
871 @Override
872 public List<Role> getResourceBlockRoles(
873 long resourceBlockId, String className, String actionId)
874 throws SystemException {
875
876 return roleFinder.findByR_N_A(resourceBlockId, className, actionId);
877 }
878
879
892 @Override
893 public Map<String, List<String>> getResourceRoles(
894 long companyId, String name, int scope, String primKey)
895 throws SystemException {
896
897 return roleFinder.findByC_N_S_P(companyId, name, scope, primKey);
898 }
899
900
914 @Override
915 public List<Role> getResourceRoles(
916 long companyId, String name, int scope, String primKey,
917 String actionId)
918 throws SystemException {
919
920 return roleFinder.findByC_N_S_P_A(
921 companyId, name, scope, primKey, actionId);
922 }
923
924
939 @Override
940 @Skip
941 public Role getRole(long companyId, String name)
942 throws PortalException, SystemException {
943
944 String companyIdHexString = StringUtil.toHexString(companyId);
945
946 Role role = _systemRolesMap.get(companyIdHexString.concat(name));
947
948 if (role != null) {
949 return role;
950 }
951
952 return roleLocalService.loadGetRole(companyId, name);
953 }
954
955
963 @Override
964 public List<Role> getRoles(int type, String subtype)
965 throws SystemException {
966
967 return rolePersistence.findByT_S(type, subtype);
968 }
969
970
977 @Override
978 public List<Role> getRoles(long companyId) throws SystemException {
979 return rolePersistence.findByCompanyId(companyId);
980 }
981
982
990 @Override
991 public List<Role> getRoles(long companyId, int[] types)
992 throws SystemException {
993
994 return rolePersistence.findByC_T(companyId, types);
995 }
996
997
1006 @Override
1007 public List<Role> getRoles(long[] roleIds)
1008 throws PortalException, SystemException {
1009
1010 List<Role> roles = new ArrayList<Role>(roleIds.length);
1011
1012 for (long roleId : roleIds) {
1013 Role role = getRole(roleId);
1014
1015 roles.add(role);
1016 }
1017
1018 return roles;
1019 }
1020
1021
1028 @Override
1029 public List<Role> getSubtypeRoles(String subtype) throws SystemException {
1030 return rolePersistence.findBySubtype(subtype);
1031 }
1032
1033
1040 @Override
1041 public int getSubtypeRolesCount(String subtype) throws SystemException {
1042 return rolePersistence.countBySubtype(subtype);
1043 }
1044
1045
1055 @Override
1056 public Role getTeamRole(long companyId, long teamId)
1057 throws PortalException, SystemException {
1058
1059 long classNameId = PortalUtil.getClassNameId(Team.class);
1060
1061 return rolePersistence.findByC_C_C(companyId, classNameId, teamId);
1062 }
1063
1064
1074 @Override
1075 public Map<Team, Role> getTeamRoleMap(long groupId)
1076 throws PortalException, SystemException {
1077
1078 return getTeamRoleMap(groupId, null);
1079 }
1080
1081
1091 @Override
1092 public List<Role> getTeamRoles(long groupId)
1093 throws PortalException, SystemException {
1094
1095 return getTeamRoles(groupId, null);
1096 }
1097
1098
1110 @Override
1111 public List<Role> getTeamRoles(long groupId, long[] excludedRoleIds)
1112 throws PortalException, SystemException {
1113
1114 Map<Team, Role> teamRoleMap = getTeamRoleMap(groupId, excludedRoleIds);
1115
1116 Collection<Role> roles = teamRoleMap.values();
1117
1118 return ListUtil.fromCollection(roles);
1119 }
1120
1121
1128 @Override
1129 public List<Role> getTypeRoles(int type) throws SystemException {
1130 return rolePersistence.findByType(type);
1131 }
1132
1133
1143 @Override
1144 public List<Role> getTypeRoles(int type, int start, int end)
1145 throws SystemException {
1146
1147 return rolePersistence.findByType(type, start, end);
1148 }
1149
1150
1157 @Override
1158 public int getTypeRolesCount(int type) throws SystemException {
1159 return rolePersistence.countByType(type);
1160 }
1161
1162
1172 @Override
1173 public List<Role> getUserGroupGroupRoles(long userId, long groupId)
1174 throws SystemException {
1175
1176 return roleFinder.findByUserGroupGroupRole(userId, groupId);
1177 }
1178
1179 @Override
1180 public List<Role> getUserGroupGroupRoles(
1181 long userId, long groupId, int start, int end)
1182 throws SystemException {
1183
1184 return roleFinder.findByUserGroupGroupRole(userId, groupId, start, end);
1185 }
1186
1187 @Override
1188 public int getUserGroupGroupRolesCount(long userId, long groupId)
1189 throws SystemException {
1190
1191 return roleFinder.countByUserGroupGroupRole(userId, groupId);
1192 }
1193
1194
1204 @Override
1205 public List<Role> getUserGroupRoles(long userId, long groupId)
1206 throws SystemException {
1207
1208 return roleFinder.findByUserGroupRole(userId, groupId);
1209 }
1210
1211
1221 @Override
1222 public List<Role> getUserRelatedRoles(long userId, List<Group> groups)
1223 throws SystemException {
1224
1225 if ((groups == null) || groups.isEmpty()) {
1226 return Collections.emptyList();
1227 }
1228
1229 return roleFinder.findByU_G(userId, groups);
1230 }
1231
1232
1242 @Override
1243 public List<Role> getUserRelatedRoles(long userId, long groupId)
1244 throws SystemException {
1245
1246 return roleFinder.findByU_G(userId, groupId);
1247 }
1248
1249
1259 @Override
1260 public List<Role> getUserRelatedRoles(long userId, long[] groupIds)
1261 throws SystemException {
1262
1263 return roleFinder.findByU_G(userId, groupIds);
1264 }
1265
1266
1281 @Override
1282 @ThreadLocalCachable
1283 public boolean hasUserRole(
1284 long userId, long companyId, String name, boolean inherited)
1285 throws PortalException, SystemException {
1286
1287 Role role = rolePersistence.fetchByC_N(companyId, name);
1288
1289 if (role == null) {
1290 return false;
1291 }
1292
1293 if (role.getType() != RoleConstants.TYPE_REGULAR) {
1294 throw new IllegalArgumentException(name + " is not a regular role");
1295 }
1296
1297 long defaultUserId = userLocalService.getDefaultUserId(companyId);
1298
1299 if (userId == defaultUserId) {
1300 if (name.equals(RoleConstants.GUEST)) {
1301 return true;
1302 }
1303 else {
1304 return false;
1305 }
1306 }
1307
1308 if (inherited) {
1309 if (userPersistence.containsRole(userId, role.getRoleId())) {
1310 return true;
1311 }
1312
1313 ThreadLocalCache<Boolean> threadLocalCache =
1314 ThreadLocalCacheManager.getThreadLocalCache(
1315 Lifecycle.REQUEST, RoleLocalServiceImpl.class.getName());
1316
1317 String key = String.valueOf(role.getRoleId()).concat(
1318 String.valueOf(userId));
1319
1320 Boolean value = threadLocalCache.get(key);
1321
1322 if (value != null) {
1323 return value;
1324 }
1325
1326 value = PermissionCacheUtil.getUserRole(userId, role);
1327
1328 if (value == null) {
1329 int count = roleFinder.countByR_U(role.getRoleId(), userId);
1330
1331 if (count > 0) {
1332 value = true;
1333 }
1334 else {
1335 value = false;
1336 }
1337
1338 PermissionCacheUtil.putUserRole(userId, role, value);
1339 }
1340
1341 threadLocalCache.put(key, value);
1342
1343 return value;
1344 }
1345 else {
1346 return userPersistence.containsRole(userId, role.getRoleId());
1347 }
1348 }
1349
1350
1366 @Override
1367 public boolean hasUserRoles(
1368 long userId, long companyId, String[] names, boolean inherited)
1369 throws PortalException, SystemException {
1370
1371 for (String name : names) {
1372 if (hasUserRole(userId, companyId, name, inherited)) {
1373 return true;
1374 }
1375 }
1376
1377 return false;
1378 }
1379
1380
1389 @Override
1390 public Role loadFetchRole(long companyId, String name)
1391 throws SystemException {
1392
1393 return rolePersistence.fetchByC_N(companyId, name);
1394 }
1395
1396
1406 @Override
1407 public Role loadGetRole(long companyId, String name)
1408 throws PortalException, SystemException {
1409
1410 return rolePersistence.findByC_N(companyId, name);
1411 }
1412
1413
1441 @Override
1442 public List<Role> search(
1443 long companyId, String keywords, Integer[] types, int start,
1444 int end, OrderByComparator obc)
1445 throws SystemException {
1446
1447 return search(
1448 companyId, keywords, types, new LinkedHashMap<String, Object>(),
1449 start, end, obc);
1450 }
1451
1452
1483 @Override
1484 public List<Role> search(
1485 long companyId, String keywords, Integer[] types,
1486 LinkedHashMap<String, Object> params, int start, int end,
1487 OrderByComparator obc)
1488 throws SystemException {
1489
1490 return roleFinder.findByKeywords(
1491 companyId, keywords, types, params, start, end, obc);
1492 }
1493
1494
1522 @Override
1523 public List<Role> search(
1524 long companyId, String name, String description, Integer[] types,
1525 int start, int end, OrderByComparator obc)
1526 throws SystemException {
1527
1528 return search(
1529 companyId, name, description, types,
1530 new LinkedHashMap<String, Object>(), start, end, obc);
1531 }
1532
1533
1564 @Override
1565 public List<Role> search(
1566 long companyId, String name, String description, Integer[] types,
1567 LinkedHashMap<String, Object> params, int start, int end,
1568 OrderByComparator obc)
1569 throws SystemException {
1570
1571 return roleFinder.findByC_N_D_T(
1572 companyId, name, description, types, params, true, start, end, obc);
1573 }
1574
1575
1585 @Override
1586 public int searchCount(long companyId, String keywords, Integer[] types)
1587 throws SystemException {
1588
1589 return searchCount(
1590 companyId, keywords, types, new LinkedHashMap<String, Object>());
1591 }
1592
1593
1605 @Override
1606 public int searchCount(
1607 long companyId, String keywords, Integer[] types,
1608 LinkedHashMap<String, Object> params)
1609 throws SystemException {
1610
1611 return roleFinder.countByKeywords(companyId, keywords, types, params);
1612 }
1613
1614
1624 @Override
1625 public int searchCount(
1626 long companyId, String name, String description, Integer[] types)
1627 throws SystemException {
1628
1629 return searchCount(
1630 companyId, name, description, types,
1631 new LinkedHashMap<String, Object>());
1632 }
1633
1634
1648 @Override
1649 public int searchCount(
1650 long companyId, String name, String description, Integer[] types,
1651 LinkedHashMap<String, Object> params)
1652 throws SystemException {
1653
1654 return roleFinder.countByC_N_D_T(
1655 companyId, name, description, types, params, true);
1656 }
1657
1658
1668 @Override
1669 public void setUserRoles(long userId, long[] roleIds)
1670 throws PortalException, SystemException {
1671
1672 roleIds = UsersAdminUtil.addRequiredRoles(userId, roleIds);
1673
1674 userPersistence.setRoles(userId, roleIds);
1675
1676 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
1677
1678 indexer.reindex(userId);
1679
1680 PermissionCacheUtil.clearCache(userId);
1681 }
1682
1683
1693 @Override
1694 public void unsetUserRoles(long userId, long[] roleIds)
1695 throws PortalException, SystemException {
1696
1697 roleIds = UsersAdminUtil.removeRequiredRoles(userId, roleIds);
1698
1699 userPersistence.removeRoles(userId, roleIds);
1700
1701 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
1702
1703 indexer.reindex(userId);
1704
1705 PermissionCacheUtil.clearCache(userId);
1706 }
1707
1708
1726 @Override
1727 public Role updateRole(
1728 long roleId, String name, Map<Locale, String> titleMap,
1729 Map<Locale, String> descriptionMap, String subtype,
1730 ServiceContext serviceContext)
1731 throws PortalException, SystemException {
1732
1733 Role role = rolePersistence.findByPrimaryKey(roleId);
1734
1735 validate(roleId, role.getCompanyId(), role.getClassNameId(), name);
1736
1737 if (PortalUtil.isSystemRole(role.getName())) {
1738 name = role.getName();
1739 subtype = null;
1740 }
1741
1742 role.setModifiedDate(new Date());
1743 role.setName(name);
1744 role.setTitleMap(titleMap);
1745 role.setDescriptionMap(descriptionMap);
1746 role.setSubtype(subtype);
1747 role.setExpandoBridgeAttributes(serviceContext);
1748
1749 rolePersistence.update(role);
1750
1751 return role;
1752 }
1753
1754 protected void checkSystemRole(
1755 long companyId, String name, Map<Locale, String> descriptionMap,
1756 int type)
1757 throws PortalException, SystemException {
1758
1759 String companyIdHexString = StringUtil.toHexString(companyId);
1760
1761 String key = companyIdHexString.concat(name);
1762
1763 Role role = _systemRolesMap.get(key);
1764
1765 try {
1766 if (role == null) {
1767 role = rolePersistence.findByC_N(companyId, name);
1768 }
1769
1770 if (!descriptionMap.equals(role.getDescriptionMap())) {
1771 role.setDescriptionMap(descriptionMap);
1772
1773 roleLocalService.updateRole(role);
1774 }
1775 }
1776 catch (NoSuchRoleException nsre) {
1777 User user = userLocalService.getDefaultUser(companyId);
1778
1779 PermissionThreadLocal.setAddResource(false);
1780
1781 try {
1782 role = roleLocalService.addRole(
1783 user.getUserId(), null, 0, name, null, descriptionMap, type,
1784 null, null);
1785 }
1786 finally {
1787 PermissionThreadLocal.setAddResource(true);
1788 }
1789
1790 if (name.equals(RoleConstants.USER)) {
1791 initPersonalControlPanelPortletsPermissions(role);
1792 }
1793 }
1794
1795 _systemRolesMap.put(key, role);
1796 }
1797
1798 protected String[] getDefaultControlPanelPortlets() {
1799 return new String[] {
1800 PortletKeys.MY_ACCOUNT, PortletKeys.MY_PAGES,
1801 PortletKeys.MY_WORKFLOW_INSTANCES, PortletKeys.MY_WORKFLOW_TASKS
1802 };
1803 }
1804
1805 protected Map<Team, Role> getTeamRoleMap(
1806 long groupId, long[] excludedRoleIds)
1807 throws PortalException, SystemException {
1808
1809 Group group = groupPersistence.findByPrimaryKey(groupId);
1810
1811 if (group.isLayout()) {
1812 group = group.getParentGroup();
1813 }
1814
1815 List<Team> teams = teamPersistence.findByGroupId(group.getGroupId());
1816
1817 if (teams.isEmpty()) {
1818 return Collections.emptyMap();
1819 }
1820
1821 Set<Long> roleIds = SetUtil.fromArray(excludedRoleIds);
1822
1823 Map<Team, Role> teamRoleMap = new LinkedHashMap<Team, Role>();
1824
1825 for (Team team : teams) {
1826 Role role = getTeamRole(team.getCompanyId(), team.getTeamId());
1827
1828 if (roleIds.contains(role.getRoleId())) {
1829 continue;
1830 }
1831
1832 teamRoleMap.put(team, role);
1833 }
1834
1835 return teamRoleMap;
1836 }
1837
1838 protected void initPersonalControlPanelPortletsPermissions(Role role)
1839 throws PortalException, SystemException {
1840
1841 for (String portletId : getDefaultControlPanelPortlets()) {
1842 int count = resourcePermissionPersistence.countByC_N_S_P_R(
1843 role.getCompanyId(), portletId, ResourceConstants.SCOPE_COMPANY,
1844 String.valueOf(role.getCompanyId()), role.getRoleId());
1845
1846 if (count > 0) {
1847 continue;
1848 }
1849
1850 ResourceAction resourceAction =
1851 resourceActionLocalService.fetchResourceAction(
1852 portletId, ActionKeys.ACCESS_IN_CONTROL_PANEL);
1853
1854 if (resourceAction == null) {
1855 continue;
1856 }
1857
1858 setRolePermissions(
1859 role, portletId,
1860 new String[] {
1861 ActionKeys.ACCESS_IN_CONTROL_PANEL
1862 });
1863 }
1864 }
1865
1866 protected void setRolePermissions(
1867 Role role, String name, String[] actionIds)
1868 throws PortalException, SystemException {
1869
1870 if (resourceBlockLocalService.isSupported(name)) {
1871 resourceBlockLocalService.setCompanyScopePermissions(
1872 role.getCompanyId(), name, role.getRoleId(),
1873 Arrays.asList(actionIds));
1874 }
1875 else {
1876 resourcePermissionLocalService.setResourcePermissions(
1877 role.getCompanyId(), name, ResourceConstants.SCOPE_COMPANY,
1878 String.valueOf(role.getCompanyId()), role.getRoleId(),
1879 actionIds);
1880 }
1881 }
1882
1883 protected void validate(
1884 long roleId, long companyId, long classNameId, String name)
1885 throws PortalException, SystemException {
1886
1887 if (classNameId == PortalUtil.getClassNameId(Role.class)) {
1888 if (Validator.isNull(name) ||
1889 (name.indexOf(CharPool.COMMA) != -1) ||
1890 (name.indexOf(CharPool.STAR) != -1)) {
1891
1892 throw new RoleNameException();
1893 }
1894
1895 if (Validator.isNumber(name) &&
1896 !PropsValues.ROLES_NAME_ALLOW_NUMERIC) {
1897
1898 throw new RoleNameException();
1899 }
1900 }
1901
1902 try {
1903 Role role = roleFinder.findByC_N(companyId, name);
1904
1905 if (role.getRoleId() != roleId) {
1906 throw new DuplicateRoleException("{roleId=" + roleId + "}");
1907 }
1908 }
1909 catch (NoSuchRoleException nsre) {
1910 }
1911 }
1912
1913 private Map<String, Role> _systemRolesMap = new HashMap<String, Role>();
1914
1915 }