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.CharPool;
039 import com.liferay.portal.kernel.util.GetterUtil;
040 import com.liferay.portal.kernel.util.ListUtil;
041 import com.liferay.portal.kernel.util.LocaleUtil;
042 import com.liferay.portal.kernel.util.OrderByComparator;
043 import com.liferay.portal.kernel.util.SetUtil;
044 import com.liferay.portal.kernel.util.StringUtil;
045 import com.liferay.portal.kernel.util.Validator;
046 import com.liferay.portal.model.Company;
047 import com.liferay.portal.model.Group;
048 import com.liferay.portal.model.Layout;
049 import com.liferay.portal.model.ResourceAction;
050 import com.liferay.portal.model.ResourceBlockPermission;
051 import com.liferay.portal.model.ResourceConstants;
052 import com.liferay.portal.model.ResourcePermission;
053 import com.liferay.portal.model.ResourceTypePermission;
054 import com.liferay.portal.model.Role;
055 import com.liferay.portal.model.RoleConstants;
056 import com.liferay.portal.model.Shard;
057 import com.liferay.portal.model.SystemEventConstants;
058 import com.liferay.portal.model.Team;
059 import com.liferay.portal.model.User;
060 import com.liferay.portal.security.auth.CompanyThreadLocal;
061 import com.liferay.portal.security.permission.ActionKeys;
062 import com.liferay.portal.security.permission.PermissionCacheUtil;
063 import com.liferay.portal.service.ServiceContext;
064 import com.liferay.portal.service.base.RoleLocalServiceBaseImpl;
065 import com.liferay.portal.util.PortalUtil;
066 import com.liferay.portal.util.PortletKeys;
067 import com.liferay.portal.util.PropsUtil;
068 import com.liferay.portal.util.PropsValues;
069 import com.liferay.portlet.usersadmin.util.UsersAdminUtil;
070
071 import java.io.IOException;
072
073 import java.util.ArrayList;
074 import java.util.Arrays;
075 import java.util.Collection;
076 import java.util.Collections;
077 import java.util.Date;
078 import java.util.HashMap;
079 import java.util.LinkedHashMap;
080 import java.util.List;
081 import java.util.Locale;
082 import java.util.Map;
083 import java.util.Set;
084
085
092 public class RoleLocalServiceImpl extends RoleLocalServiceBaseImpl {
093
094
113 @Override
114 public Role addRole(
115 long userId, long companyId, String name,
116 Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
117 int type)
118 throws PortalException, SystemException {
119
120 return addRole(
121 userId, null, 0, name, titleMap, descriptionMap, type, null, null);
122 }
123
124
148 @Override
149 public Role addRole(
150 long userId, long companyId, String name,
151 Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
152 int type, String className, long classPK)
153 throws PortalException, SystemException {
154
155 return addRole(
156 userId, className, classPK, name, titleMap, descriptionMap, type,
157 null, null);
158 }
159
160
185 @Override
186 public Role addRole(
187 long userId, String className, long classPK, String name,
188 Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
189 int type, String subtype, ServiceContext serviceContext)
190 throws PortalException, SystemException {
191
192
193
194 User user = userPersistence.findByPrimaryKey(userId);
195 className = GetterUtil.getString(className);
196 long classNameId = PortalUtil.getClassNameId(className);
197
198 long roleId = counterLocalService.increment();
199
200 if ((classNameId <= 0) || className.equals(Role.class.getName())) {
201 classNameId = PortalUtil.getClassNameId(Role.class);
202 classPK = roleId;
203 }
204
205 Date now = new Date();
206
207 validate(0, user.getCompanyId(), classNameId, name);
208
209 Role role = rolePersistence.create(roleId);
210
211 if (serviceContext != null) {
212 role.setUuid(serviceContext.getUuid());
213 }
214
215 role.setCompanyId(user.getCompanyId());
216 role.setUserId(user.getUserId());
217 role.setUserName(user.getFullName());
218
219 if (serviceContext != null) {
220 role.setCreateDate(serviceContext.getCreateDate(now));
221 role.setModifiedDate(serviceContext.getModifiedDate(now));
222 }
223 else {
224 role.setCreateDate(now);
225 role.setModifiedDate(now);
226 }
227
228 role.setClassNameId(classNameId);
229 role.setClassPK(classPK);
230 role.setName(name);
231 role.setTitleMap(titleMap);
232 role.setDescriptionMap(descriptionMap);
233 role.setType(type);
234 role.setSubtype(subtype);
235 role.setExpandoBridgeAttributes(serviceContext);
236
237 rolePersistence.update(role);
238
239
240
241 if (!user.isDefaultUser()) {
242 resourceLocalService.addResources(
243 user.getCompanyId(), 0, userId, Role.class.getName(),
244 role.getRoleId(), false, false, false);
245
246 if (!ExportImportThreadLocal.isImportInProcess()) {
247 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
248 User.class);
249
250 indexer.reindex(userId);
251 }
252 }
253
254 return role;
255 }
256
257
268 @Override
269 public void addUserRoles(long userId, long[] roleIds)
270 throws PortalException, SystemException {
271
272 userPersistence.addRoles(userId, roleIds);
273
274 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
275
276 indexer.reindex(userId);
277
278 PermissionCacheUtil.clearCache();
279 }
280
281
289 @Override
290 public void checkSystemRoles() throws PortalException, SystemException {
291 List<Company> companies = companyLocalService.getCompanies();
292
293 String currentShardName = ShardUtil.getCurrentShardName();
294
295 for (Company company : companies) {
296 String shardName = null;
297
298 try {
299 shardName = company.getShardName();
300 }
301 catch (NoSuchShardException nsse) {
302 Shard shard = shardLocalService.addShard(
303 Company.class.getName(), company.getCompanyId(),
304 PropsValues.SHARD_DEFAULT_NAME);
305
306 shardName = shard.getName();
307 }
308
309 if (!ShardUtil.isEnabled() || shardName.equals(currentShardName)) {
310 checkSystemRoles(company.getCompanyId());
311 }
312 }
313 }
314
315
324 @Override
325 @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
326 public void checkSystemRoles(long companyId)
327 throws PortalException, SystemException {
328
329 String companyIdHexString = StringUtil.toHexString(companyId);
330
331 List<Role> roles = null;
332
333 try {
334 roles = roleFinder.findBySystem(companyId);
335 }
336 catch (Exception e) {
337
338
339
340 DB db = DBFactoryUtil.getDB();
341
342 try {
343 runSQL(
344 db.buildSQL(
345 "alter table Role_ add uuid_ VARCHAR(75) null"));
346 runSQL(db.buildSQL("alter table Role_ add userId LONG"));
347 runSQL(
348 db.buildSQL(
349 "alter table Role_ add userName VARCHAR(75) null"));
350 runSQL(
351 db.buildSQL("alter table Role_ add createDate DATE null"));
352 runSQL(
353 db.buildSQL(
354 "alter table Role_ add modifiedDate DATE null"));
355 }
356 catch (IOException ioe) {
357 throw new SystemException(ioe);
358 }
359
360 roles = roleFinder.findBySystem(companyId);
361 }
362
363 for (Role role : roles) {
364 _systemRolesMap.put(
365 companyIdHexString.concat(role.getName()), role);
366 }
367
368
369
370 String[] systemRoles = PortalUtil.getSystemRoles();
371
372 for (String name : systemRoles) {
373 String key =
374 "system.role." +
375 StringUtil.replace(name, CharPool.SPACE, CharPool.PERIOD) +
376 ".description";
377
378 Map<Locale, String> descriptionMap = new HashMap<Locale, String>();
379
380 descriptionMap.put(LocaleUtil.getDefault(), PropsUtil.get(key));
381
382 int type = RoleConstants.TYPE_REGULAR;
383
384 checkSystemRole(companyId, name, descriptionMap, type);
385 }
386
387
388
389 String[] systemOrganizationRoles =
390 PortalUtil.getSystemOrganizationRoles();
391
392 for (String name : systemOrganizationRoles) {
393 String key =
394 "system.organization.role." +
395 StringUtil.replace(name, CharPool.SPACE, CharPool.PERIOD) +
396 ".description";
397
398 Map<Locale, String> descriptionMap = new HashMap<Locale, String>();
399
400 descriptionMap.put(LocaleUtil.getDefault(), PropsUtil.get(key));
401
402 int type = RoleConstants.TYPE_ORGANIZATION;
403
404 checkSystemRole(companyId, name, descriptionMap, type);
405 }
406
407
408
409 String[] systemSiteRoles = PortalUtil.getSystemSiteRoles();
410
411 for (String name : systemSiteRoles) {
412 String key =
413 "system.site.role." +
414 StringUtil.replace(name, CharPool.SPACE, CharPool.PERIOD) +
415 ".description";
416
417 Map<Locale, String> descriptionMap = new HashMap<Locale, String>();
418
419 descriptionMap.put(LocaleUtil.getDefault(), PropsUtil.get(key));
420
421 int type = RoleConstants.TYPE_SITE;
422
423 checkSystemRole(companyId, name, descriptionMap, type);
424 }
425 }
426
427
437 @Override
438 public Role deleteRole(long roleId)
439 throws PortalException, SystemException {
440
441 Role role = rolePersistence.findByPrimaryKey(roleId);
442
443 return roleLocalService.deleteRole(role);
444 }
445
446
455 @Override
456 @SystemEvent(
457 action = SystemEventConstants.ACTION_SKIP,
458 type = SystemEventConstants.TYPE_DELETE)
459 public Role deleteRole(Role role) throws PortalException, SystemException {
460 if (PortalUtil.isSystemRole(role.getName()) &&
461 !CompanyThreadLocal.isDeleteInProcess()) {
462
463 throw new RequiredRoleException();
464 }
465
466
467
468 List<ResourceBlockPermission> resourceBlockPermissions =
469 resourceBlockPermissionPersistence.findByRoleId(role.getRoleId());
470
471 for (ResourceBlockPermission resourceBlockPermission :
472 resourceBlockPermissions) {
473
474 resourceBlockPermissionLocalService.deleteResourceBlockPermission(
475 resourceBlockPermission);
476 }
477
478 List<ResourcePermission> resourcePermissions =
479 resourcePermissionPersistence.findByRoleId(role.getRoleId());
480
481 for (ResourcePermission resourcePermission : resourcePermissions) {
482 resourcePermissionLocalService.deleteResourcePermission(
483 resourcePermission);
484 }
485
486 List<ResourceTypePermission> resourceTypePermissions =
487 resourceTypePermissionPersistence.findByRoleId(role.getRoleId());
488
489 for (ResourceTypePermission resourceTypePermission :
490 resourceTypePermissions) {
491
492 resourceTypePermissionLocalService.deleteResourceTypePermission(
493 resourceTypePermission);
494 }
495
496 String className = role.getClassName();
497 long classNameId = role.getClassNameId();
498
499 if ((classNameId <= 0) || className.equals(Role.class.getName())) {
500 resourceLocalService.deleteResource(
501 role.getCompanyId(), Role.class.getName(),
502 ResourceConstants.SCOPE_INDIVIDUAL, role.getRoleId());
503 }
504
505 if ((role.getType() == RoleConstants.TYPE_ORGANIZATION) ||
506 (role.getType() == RoleConstants.TYPE_SITE)) {
507
508 userGroupRoleLocalService.deleteUserGroupRolesByRoleId(
509 role.getRoleId());
510
511 userGroupGroupRoleLocalService.deleteUserGroupGroupRolesByRoleId(
512 role.getRoleId());
513 }
514
515
516
517 rolePersistence.remove(role);
518
519
520
521 expandoRowLocalService.deleteRows(role.getRoleId());
522
523
524
525 PermissionCacheUtil.clearCache();
526
527 return role;
528 }
529
530
544 @Override
545 @Skip
546 public Role fetchRole(long companyId, String name) throws SystemException {
547 String companyIdHexString = StringUtil.toHexString(companyId);
548
549 Role role = _systemRolesMap.get(companyIdHexString.concat(name));
550
551 if (role != null) {
552 return role;
553 }
554
555 return roleLocalService.loadFetchRole(companyId, name);
556 }
557
558
578 @Override
579 public Role getDefaultGroupRole(long groupId)
580 throws PortalException, SystemException {
581
582 Group group = groupPersistence.findByPrimaryKey(groupId);
583
584 if (group.isLayout()) {
585 Layout layout = layoutLocalService.getLayout(group.getClassPK());
586
587 group = layout.getGroup();
588 }
589
590 if (group.isStagingGroup()) {
591 group = group.getLiveGroup();
592 }
593
594 Role role = null;
595
596 if (group.isCompany()) {
597 role = getRole(group.getCompanyId(), RoleConstants.USER);
598 }
599 else if (group.isLayoutPrototype() || group.isLayoutSetPrototype() ||
600 group.isRegularSite() || group.isSite()) {
601
602 role = getRole(group.getCompanyId(), RoleConstants.SITE_MEMBER);
603 }
604 else if (group.isOrganization()) {
605 role = getRole(
606 group.getCompanyId(), RoleConstants.ORGANIZATION_USER);
607 }
608 else if (group.isUser() || group.isUserGroup()) {
609 role = getRole(group.getCompanyId(), RoleConstants.POWER_USER);
610 }
611 else {
612 role = getRole(group.getCompanyId(), RoleConstants.USER);
613 }
614
615 return role;
616 }
617
618 @Override
619 public List<Role> getGroupRelatedRoles(long groupId)
620 throws PortalException, SystemException {
621
622 List<Role> roles = new ArrayList<Role>();
623
624 Group group = groupLocalService.getGroup(groupId);
625
626 int[] types = RoleConstants.TYPES_REGULAR;
627
628 if (group.isOrganization()) {
629 types = RoleConstants.TYPES_ORGANIZATION_AND_REGULAR;
630 }
631 else if (group.isLayout() || group.isLayoutSetPrototype() ||
632 group.isSite()) {
633
634 types = RoleConstants.TYPES_REGULAR_AND_SITE;
635 }
636
637 roles.addAll(getRoles(group.getCompanyId(), types));
638
639 roles.addAll(getTeamRoles(groupId));
640
641 return roles;
642 }
643
644 @Override
645 public List<Role> getResourceBlockRoles(
646 long resourceBlockId, String className, String actionId)
647 throws SystemException {
648
649 return roleFinder.findByR_N_A(resourceBlockId, className, actionId);
650 }
651
652
665 @Override
666 public Map<String, List<String>> getResourceRoles(
667 long companyId, String name, int scope, String primKey)
668 throws SystemException {
669
670 return roleFinder.findByC_N_S_P(companyId, name, scope, primKey);
671 }
672
673
687 @Override
688 public List<Role> getResourceRoles(
689 long companyId, String name, int scope, String primKey,
690 String actionId)
691 throws SystemException {
692
693 return roleFinder.findByC_N_S_P_A(
694 companyId, name, scope, primKey, actionId);
695 }
696
697
712 @Override
713 @Skip
714 public Role getRole(long companyId, String name)
715 throws PortalException, SystemException {
716
717 String companyIdHexString = StringUtil.toHexString(companyId);
718
719 Role role = _systemRolesMap.get(companyIdHexString.concat(name));
720
721 if (role != null) {
722 return role;
723 }
724
725 return roleLocalService.loadGetRole(companyId, name);
726 }
727
728
736 @Override
737 public List<Role> getRoles(int type, String subtype)
738 throws SystemException {
739
740 return rolePersistence.findByT_S(type, subtype);
741 }
742
743
750 @Override
751 public List<Role> getRoles(long companyId) throws SystemException {
752 return rolePersistence.findByCompanyId(companyId);
753 }
754
755
763 @Override
764 public List<Role> getRoles(long companyId, int[] types)
765 throws SystemException {
766
767 return rolePersistence.findByC_T(companyId, types);
768 }
769
770
779 @Override
780 public List<Role> getRoles(long[] roleIds)
781 throws PortalException, SystemException {
782
783 List<Role> roles = new ArrayList<Role>(roleIds.length);
784
785 for (long roleId : roleIds) {
786 Role role = getRole(roleId);
787
788 roles.add(role);
789 }
790
791 return roles;
792 }
793
794
801 @Override
802 public List<Role> getSubtypeRoles(String subtype) throws SystemException {
803 return rolePersistence.findBySubtype(subtype);
804 }
805
806
813 @Override
814 public int getSubtypeRolesCount(String subtype) throws SystemException {
815 return rolePersistence.countBySubtype(subtype);
816 }
817
818
828 @Override
829 public Role getTeamRole(long companyId, long teamId)
830 throws PortalException, SystemException {
831
832 long classNameId = PortalUtil.getClassNameId(Team.class);
833
834 return rolePersistence.findByC_C_C(companyId, classNameId, teamId);
835 }
836
837
847 @Override
848 public Map<Team, Role> getTeamRoleMap(long groupId)
849 throws PortalException, SystemException {
850
851 return getTeamRoleMap(groupId, null);
852 }
853
854
864 @Override
865 public List<Role> getTeamRoles(long groupId)
866 throws PortalException, SystemException {
867
868 return getTeamRoles(groupId, null);
869 }
870
871
883 @Override
884 public List<Role> getTeamRoles(long groupId, long[] excludedRoleIds)
885 throws PortalException, SystemException {
886
887 Map<Team, Role> teamRoleMap = getTeamRoleMap(groupId, excludedRoleIds);
888
889 Collection<Role> roles = teamRoleMap.values();
890
891 return ListUtil.fromCollection(roles);
892 }
893
894
901 @Override
902 public List<Role> getTypeRoles(int type) throws SystemException {
903 return rolePersistence.findByType(type);
904 }
905
906
916 @Override
917 public List<Role> getTypeRoles(int type, int start, int end)
918 throws SystemException {
919
920 return rolePersistence.findByType(type, start, end);
921 }
922
923
930 @Override
931 public int getTypeRolesCount(int type) throws SystemException {
932 return rolePersistence.countByType(type);
933 }
934
935
945 @Override
946 public List<Role> getUserGroupGroupRoles(long userId, long groupId)
947 throws SystemException {
948
949 return roleFinder.findByUserGroupGroupRole(userId, groupId);
950 }
951
952
962 @Override
963 public List<Role> getUserGroupRoles(long userId, long groupId)
964 throws SystemException {
965
966 return roleFinder.findByUserGroupRole(userId, groupId);
967 }
968
969
979 @Override
980 public List<Role> getUserRelatedRoles(long userId, List<Group> groups)
981 throws SystemException {
982
983 if ((groups == null) || groups.isEmpty()) {
984 return Collections.emptyList();
985 }
986
987 return roleFinder.findByU_G(userId, groups);
988 }
989
990
1000 @Override
1001 public List<Role> getUserRelatedRoles(long userId, long groupId)
1002 throws SystemException {
1003
1004 return roleFinder.findByU_G(userId, groupId);
1005 }
1006
1007
1017 @Override
1018 public List<Role> getUserRelatedRoles(long userId, long[] groupIds)
1019 throws SystemException {
1020
1021 return roleFinder.findByU_G(userId, groupIds);
1022 }
1023
1024
1039 @Override
1040 @ThreadLocalCachable
1041 public boolean hasUserRole(
1042 long userId, long companyId, String name, boolean inherited)
1043 throws PortalException, SystemException {
1044
1045 Role role = rolePersistence.fetchByC_N(companyId, name);
1046
1047 if (role == null) {
1048 return false;
1049 }
1050
1051 if (role.getType() != RoleConstants.TYPE_REGULAR) {
1052 throw new IllegalArgumentException(name + " is not a regular role");
1053 }
1054
1055 long defaultUserId = userLocalService.getDefaultUserId(companyId);
1056
1057 if (userId == defaultUserId) {
1058 if (name.equals(RoleConstants.GUEST)) {
1059 return true;
1060 }
1061 else {
1062 return false;
1063 }
1064 }
1065
1066 if (inherited) {
1067 if (userPersistence.containsRole(userId, role.getRoleId())) {
1068 return true;
1069 }
1070
1071 ThreadLocalCache<Integer> threadLocalCache =
1072 ThreadLocalCacheManager.getThreadLocalCache(
1073 Lifecycle.REQUEST, RoleLocalServiceImpl.class.getName());
1074
1075 String key = String.valueOf(role.getRoleId()).concat(
1076 String.valueOf(userId));
1077
1078 Integer value = threadLocalCache.get(key);
1079
1080 if (value == null) {
1081 value = roleFinder.countByR_U(role.getRoleId(), userId);
1082
1083 threadLocalCache.put(key, value);
1084 }
1085
1086 if (value > 0) {
1087 return true;
1088 }
1089 else {
1090 return false;
1091 }
1092 }
1093 else {
1094 return userPersistence.containsRole(userId, role.getRoleId());
1095 }
1096 }
1097
1098
1114 @Override
1115 public boolean hasUserRoles(
1116 long userId, long companyId, String[] names, boolean inherited)
1117 throws PortalException, SystemException {
1118
1119 for (String name : names) {
1120 if (hasUserRole(userId, companyId, name, inherited)) {
1121 return true;
1122 }
1123 }
1124
1125 return false;
1126 }
1127
1128
1137 @Override
1138 public Role loadFetchRole(long companyId, String name)
1139 throws SystemException {
1140
1141 return rolePersistence.fetchByC_N(companyId, name);
1142 }
1143
1144
1154 @Override
1155 public Role loadGetRole(long companyId, String name)
1156 throws PortalException, SystemException {
1157
1158 return rolePersistence.findByC_N(companyId, name);
1159 }
1160
1161
1189 @Override
1190 public List<Role> search(
1191 long companyId, String keywords, Integer[] types, int start,
1192 int end, OrderByComparator obc)
1193 throws SystemException {
1194
1195 return search(
1196 companyId, keywords, types, new LinkedHashMap<String, Object>(),
1197 start, end, obc);
1198 }
1199
1200
1231 @Override
1232 public List<Role> search(
1233 long companyId, String keywords, Integer[] types,
1234 LinkedHashMap<String, Object> params, int start, int end,
1235 OrderByComparator obc)
1236 throws SystemException {
1237
1238 return roleFinder.findByKeywords(
1239 companyId, keywords, types, params, start, end, obc);
1240 }
1241
1242
1270 @Override
1271 public List<Role> search(
1272 long companyId, String name, String description, Integer[] types,
1273 int start, int end, OrderByComparator obc)
1274 throws SystemException {
1275
1276 return search(
1277 companyId, name, description, types,
1278 new LinkedHashMap<String, Object>(), start, end, obc);
1279 }
1280
1281
1312 @Override
1313 public List<Role> search(
1314 long companyId, String name, String description, Integer[] types,
1315 LinkedHashMap<String, Object> params, int start, int end,
1316 OrderByComparator obc)
1317 throws SystemException {
1318
1319 return roleFinder.findByC_N_D_T(
1320 companyId, name, description, types, params, true, start, end, obc);
1321 }
1322
1323
1333 @Override
1334 public int searchCount(long companyId, String keywords, Integer[] types)
1335 throws SystemException {
1336
1337 return searchCount(
1338 companyId, keywords, types, new LinkedHashMap<String, Object>());
1339 }
1340
1341
1353 @Override
1354 public int searchCount(
1355 long companyId, String keywords, Integer[] types,
1356 LinkedHashMap<String, Object> params)
1357 throws SystemException {
1358
1359 return roleFinder.countByKeywords(companyId, keywords, types, params);
1360 }
1361
1362
1372 @Override
1373 public int searchCount(
1374 long companyId, String name, String description, Integer[] types)
1375 throws SystemException {
1376
1377 return searchCount(
1378 companyId, name, description, types,
1379 new LinkedHashMap<String, Object>());
1380 }
1381
1382
1396 @Override
1397 public int searchCount(
1398 long companyId, String name, String description, Integer[] types,
1399 LinkedHashMap<String, Object> params)
1400 throws SystemException {
1401
1402 return roleFinder.countByC_N_D_T(
1403 companyId, name, description, types, params, true);
1404 }
1405
1406
1416 @Override
1417 public void setUserRoles(long userId, long[] roleIds)
1418 throws PortalException, SystemException {
1419
1420 roleIds = UsersAdminUtil.addRequiredRoles(userId, roleIds);
1421
1422 userPersistence.setRoles(userId, roleIds);
1423
1424 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
1425
1426 indexer.reindex(userId);
1427
1428 PermissionCacheUtil.clearCache();
1429 }
1430
1431
1441 @Override
1442 public void unsetUserRoles(long userId, long[] roleIds)
1443 throws PortalException, SystemException {
1444
1445 roleIds = UsersAdminUtil.removeRequiredRoles(userId, roleIds);
1446
1447 userPersistence.removeRoles(userId, roleIds);
1448
1449 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
1450
1451 indexer.reindex(userId);
1452
1453 PermissionCacheUtil.clearCache();
1454 }
1455
1456
1474 @Override
1475 public Role updateRole(
1476 long roleId, String name, Map<Locale, String> titleMap,
1477 Map<Locale, String> descriptionMap, String subtype,
1478 ServiceContext serviceContext)
1479 throws PortalException, SystemException {
1480
1481 Role role = rolePersistence.findByPrimaryKey(roleId);
1482
1483 validate(roleId, role.getCompanyId(), role.getClassNameId(), name);
1484
1485 if (PortalUtil.isSystemRole(role.getName())) {
1486 name = role.getName();
1487 subtype = null;
1488 }
1489
1490 role.setModifiedDate(new Date());
1491 role.setName(name);
1492 role.setTitleMap(titleMap);
1493 role.setDescriptionMap(descriptionMap);
1494 role.setSubtype(subtype);
1495 role.setExpandoBridgeAttributes(serviceContext);
1496
1497 rolePersistence.update(role);
1498
1499 return role;
1500 }
1501
1502 protected void checkSystemRole(
1503 long companyId, String name, Map<Locale, String> descriptionMap,
1504 int type)
1505 throws PortalException, SystemException {
1506
1507 String companyIdHexString = StringUtil.toHexString(companyId);
1508
1509 String key = companyIdHexString.concat(name);
1510
1511 Role role = _systemRolesMap.get(key);
1512
1513 try {
1514 if (role == null) {
1515 role = rolePersistence.findByC_N(companyId, name);
1516 }
1517
1518 if (!descriptionMap.equals(role.getDescriptionMap())) {
1519 role.setDescriptionMap(descriptionMap);
1520
1521 roleLocalService.updateRole(role);
1522 }
1523 }
1524 catch (NoSuchRoleException nsre) {
1525 User user = userLocalService.getDefaultUser(companyId);
1526
1527 role = roleLocalService.addRole(
1528 user.getUserId(), null, 0, name, null, descriptionMap, type,
1529 null, null);
1530
1531 if (name.equals(RoleConstants.USER)) {
1532 initPersonalControlPanelPortletsPermissions(role);
1533 }
1534 }
1535
1536 _systemRolesMap.put(key, role);
1537 }
1538
1539 protected String[] getDefaultControlPanelPortlets() {
1540 return new String[] {
1541 PortletKeys.MY_ACCOUNT, PortletKeys.MY_PAGES,
1542 PortletKeys.MY_WORKFLOW_INSTANCES, PortletKeys.MY_WORKFLOW_TASKS
1543 };
1544 }
1545
1546 protected Map<Team, Role> getTeamRoleMap(
1547 long groupId, long[] excludedRoleIds)
1548 throws PortalException, SystemException {
1549
1550 Group group = groupPersistence.findByPrimaryKey(groupId);
1551
1552 if (group.isLayout()) {
1553 group = group.getParentGroup();
1554 }
1555
1556 List<Team> teams = teamPersistence.findByGroupId(group.getGroupId());
1557
1558 if (teams.isEmpty()) {
1559 return Collections.emptyMap();
1560 }
1561
1562 Set<Long> roleIds = SetUtil.fromArray(excludedRoleIds);
1563
1564 Map<Team, Role> teamRoleMap = new LinkedHashMap<Team, Role>();
1565
1566 for (Team team : teams) {
1567 Role role = getTeamRole(team.getCompanyId(), team.getTeamId());
1568
1569 if (roleIds.contains(role.getRoleId())) {
1570 continue;
1571 }
1572
1573 teamRoleMap.put(team, role);
1574 }
1575
1576 return teamRoleMap;
1577 }
1578
1579 protected void initPersonalControlPanelPortletsPermissions(Role role)
1580 throws PortalException, SystemException {
1581
1582 for (String portletId : getDefaultControlPanelPortlets()) {
1583 int count = resourcePermissionPersistence.countByC_N_S_P_R(
1584 role.getCompanyId(), portletId, ResourceConstants.SCOPE_COMPANY,
1585 String.valueOf(role.getCompanyId()), role.getRoleId());
1586
1587 if (count > 0) {
1588 continue;
1589 }
1590
1591 ResourceAction resourceAction =
1592 resourceActionLocalService.fetchResourceAction(
1593 portletId, ActionKeys.ACCESS_IN_CONTROL_PANEL);
1594
1595 if (resourceAction == null) {
1596 continue;
1597 }
1598
1599 setRolePermissions(
1600 role, portletId,
1601 new String[] {
1602 ActionKeys.ACCESS_IN_CONTROL_PANEL
1603 });
1604 }
1605 }
1606
1607 protected void setRolePermissions(
1608 Role role, String name, String[] actionIds)
1609 throws PortalException, SystemException {
1610
1611 if (resourceBlockLocalService.isSupported(name)) {
1612 resourceBlockLocalService.setCompanyScopePermissions(
1613 role.getCompanyId(), name, role.getRoleId(),
1614 Arrays.asList(actionIds));
1615 }
1616 else {
1617 resourcePermissionLocalService.setResourcePermissions(
1618 role.getCompanyId(), name, ResourceConstants.SCOPE_COMPANY,
1619 String.valueOf(role.getCompanyId()), role.getRoleId(),
1620 actionIds);
1621 }
1622 }
1623
1624 protected void validate(
1625 long roleId, long companyId, long classNameId, String name)
1626 throws PortalException, SystemException {
1627
1628 if (classNameId == PortalUtil.getClassNameId(Role.class)) {
1629 if (Validator.isNull(name) ||
1630 (name.indexOf(CharPool.COMMA) != -1) ||
1631 (name.indexOf(CharPool.STAR) != -1)) {
1632
1633 throw new RoleNameException();
1634 }
1635
1636 if (Validator.isNumber(name) &&
1637 !PropsValues.ROLES_NAME_ALLOW_NUMERIC) {
1638
1639 throw new RoleNameException();
1640 }
1641 }
1642
1643 try {
1644 Role role = roleFinder.findByC_N(companyId, name);
1645
1646 if (role.getRoleId() != roleId) {
1647 throw new DuplicateRoleException();
1648 }
1649 }
1650 catch (NoSuchRoleException nsre) {
1651 }
1652 }
1653
1654 private Map<String, Role> _systemRolesMap = new HashMap<String, Role>();
1655
1656 }