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.service.ServiceContext;
065 import com.liferay.portal.service.base.RoleLocalServiceBaseImpl;
066 import com.liferay.portal.util.PortalUtil;
067 import com.liferay.portal.util.PortletKeys;
068 import com.liferay.portal.util.PropsUtil;
069 import com.liferay.portal.util.PropsValues;
070 import com.liferay.portlet.usersadmin.util.UsersAdminUtil;
071
072 import java.io.IOException;
073
074 import java.util.ArrayList;
075 import java.util.Arrays;
076 import java.util.Collection;
077 import java.util.Collections;
078 import java.util.Date;
079 import java.util.HashMap;
080 import java.util.LinkedHashMap;
081 import java.util.List;
082 import java.util.Locale;
083 import java.util.Map;
084 import java.util.Set;
085
086
093 public class RoleLocalServiceImpl extends RoleLocalServiceBaseImpl {
094
095
114 @Override
115 public Role addRole(
116 long userId, long companyId, String name,
117 Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
118 int type)
119 throws PortalException, SystemException {
120
121 return addRole(
122 userId, null, 0, name, titleMap, descriptionMap, type, null, null);
123 }
124
125
149 @Override
150 public Role addRole(
151 long userId, long companyId, String name,
152 Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
153 int type, String className, long classPK)
154 throws PortalException, SystemException {
155
156 return addRole(
157 userId, className, classPK, name, titleMap, descriptionMap, type,
158 null, null);
159 }
160
161
186 @Override
187 public Role addRole(
188 long userId, String className, long classPK, String name,
189 Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
190 int type, String subtype, ServiceContext serviceContext)
191 throws PortalException, SystemException {
192
193
194
195 User user = userPersistence.findByPrimaryKey(userId);
196 className = GetterUtil.getString(className);
197 long classNameId = PortalUtil.getClassNameId(className);
198
199 long roleId = counterLocalService.increment();
200
201 if ((classNameId <= 0) || className.equals(Role.class.getName())) {
202 classNameId = PortalUtil.getClassNameId(Role.class);
203 classPK = roleId;
204 }
205
206 Date now = new Date();
207
208 validate(0, user.getCompanyId(), classNameId, name);
209
210 Role role = rolePersistence.create(roleId);
211
212 if (serviceContext != null) {
213 role.setUuid(serviceContext.getUuid());
214 }
215
216 role.setCompanyId(user.getCompanyId());
217 role.setUserId(user.getUserId());
218 role.setUserName(user.getFullName());
219
220 if (serviceContext != null) {
221 role.setCreateDate(serviceContext.getCreateDate(now));
222 role.setModifiedDate(serviceContext.getModifiedDate(now));
223 }
224 else {
225 role.setCreateDate(now);
226 role.setModifiedDate(now);
227 }
228
229 role.setClassNameId(classNameId);
230 role.setClassPK(classPK);
231 role.setName(name);
232 role.setTitleMap(titleMap);
233 role.setDescriptionMap(descriptionMap);
234 role.setType(type);
235 role.setSubtype(subtype);
236 role.setExpandoBridgeAttributes(serviceContext);
237
238 rolePersistence.update(role);
239
240
241
242 if (!user.isDefaultUser()) {
243 resourceLocalService.addResources(
244 user.getCompanyId(), 0, userId, Role.class.getName(),
245 role.getRoleId(), false, false, false);
246
247 if (!ExportImportThreadLocal.isImportInProcess()) {
248 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
249 User.class);
250
251 indexer.reindex(userId);
252 }
253 }
254
255 return role;
256 }
257
258
269 @Override
270 public void addUserRoles(long userId, long[] roleIds)
271 throws PortalException, SystemException {
272
273 userPersistence.addRoles(userId, roleIds);
274
275 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
276
277 indexer.reindex(userId);
278
279 PermissionCacheUtil.clearCache(userId);
280 }
281
282
290 @Override
291 public void checkSystemRoles() throws PortalException, SystemException {
292 List<Company> companies = companyLocalService.getCompanies();
293
294 String currentShardName = ShardUtil.getCurrentShardName();
295
296 for (Company company : companies) {
297 String shardName = null;
298
299 try {
300 shardName = company.getShardName();
301 }
302 catch (NoSuchShardException nsse) {
303 Shard shard = shardLocalService.addShard(
304 Company.class.getName(), company.getCompanyId(),
305 PropsValues.SHARD_DEFAULT_NAME);
306
307 shardName = shard.getName();
308 }
309
310 if (!ShardUtil.isEnabled() || shardName.equals(currentShardName)) {
311 checkSystemRoles(company.getCompanyId());
312 }
313 }
314 }
315
316
325 @Override
326 @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
327 public void checkSystemRoles(long companyId)
328 throws PortalException, SystemException {
329
330 String companyIdHexString = StringUtil.toHexString(companyId);
331
332 List<Role> roles = null;
333
334 try {
335 roles = roleFinder.findBySystem(companyId);
336 }
337 catch (Exception e) {
338
339
340
341 DB db = DBFactoryUtil.getDB();
342
343 try {
344 runSQL(
345 db.buildSQL(
346 "alter table Role_ add uuid_ VARCHAR(75) null"));
347 runSQL(db.buildSQL("alter table Role_ add userId LONG"));
348 runSQL(
349 db.buildSQL(
350 "alter table Role_ add userName VARCHAR(75) null"));
351 runSQL(
352 db.buildSQL("alter table Role_ add createDate DATE null"));
353 runSQL(
354 db.buildSQL(
355 "alter table Role_ add modifiedDate DATE null"));
356 }
357 catch (IOException ioe) {
358 throw new SystemException(ioe);
359 }
360
361 roles = roleFinder.findBySystem(companyId);
362 }
363
364 for (Role role : roles) {
365 _systemRolesMap.put(
366 companyIdHexString.concat(role.getName()), role);
367 }
368
369
370
371 String[] systemRoles = PortalUtil.getSystemRoles();
372
373 for (String name : systemRoles) {
374 String key =
375 "system.role." +
376 StringUtil.replace(name, CharPool.SPACE, CharPool.PERIOD) +
377 ".description";
378
379 Map<Locale, String> descriptionMap = new HashMap<Locale, String>();
380
381 descriptionMap.put(LocaleUtil.getDefault(), PropsUtil.get(key));
382
383 int type = RoleConstants.TYPE_REGULAR;
384
385 checkSystemRole(companyId, name, descriptionMap, type);
386 }
387
388
389
390 String[] systemOrganizationRoles =
391 PortalUtil.getSystemOrganizationRoles();
392
393 for (String name : systemOrganizationRoles) {
394 String key =
395 "system.organization.role." +
396 StringUtil.replace(name, CharPool.SPACE, CharPool.PERIOD) +
397 ".description";
398
399 Map<Locale, String> descriptionMap = new HashMap<Locale, String>();
400
401 descriptionMap.put(LocaleUtil.getDefault(), PropsUtil.get(key));
402
403 int type = RoleConstants.TYPE_ORGANIZATION;
404
405 checkSystemRole(companyId, name, descriptionMap, type);
406 }
407
408
409
410 String[] systemSiteRoles = PortalUtil.getSystemSiteRoles();
411
412 for (String name : systemSiteRoles) {
413 String key =
414 "system.site.role." +
415 StringUtil.replace(name, CharPool.SPACE, CharPool.PERIOD) +
416 ".description";
417
418 Map<Locale, String> descriptionMap = new HashMap<Locale, String>();
419
420 descriptionMap.put(LocaleUtil.getDefault(), PropsUtil.get(key));
421
422 int type = RoleConstants.TYPE_SITE;
423
424 checkSystemRole(companyId, name, descriptionMap, type);
425 }
426
427
428
429 Role userRole = getRole(companyId, RoleConstants.USER);
430
431 String[] userViewableRoles = ArrayUtil.append(
432 systemRoles, systemOrganizationRoles, systemSiteRoles);
433
434 for (String roleName : userViewableRoles) {
435 Role role = getRole(companyId, roleName);
436
437 resourcePermissionLocalService.setResourcePermissions(
438 companyId, Role.class.getName(),
439 ResourceConstants.SCOPE_INDIVIDUAL,
440 String.valueOf(role.getRoleId()), userRole.getRoleId(),
441 new String[] {ActionKeys.VIEW});
442 }
443 }
444
445
455 @Override
456 public Role deleteRole(long roleId)
457 throws PortalException, SystemException {
458
459 Role role = rolePersistence.findByPrimaryKey(roleId);
460
461 return roleLocalService.deleteRole(role);
462 }
463
464
473 @Override
474 @SystemEvent(
475 action = SystemEventConstants.ACTION_SKIP,
476 type = SystemEventConstants.TYPE_DELETE)
477 public Role deleteRole(Role role) throws PortalException, SystemException {
478 if (PortalUtil.isSystemRole(role.getName()) &&
479 !CompanyThreadLocal.isDeleteInProcess()) {
480
481 throw new RequiredRoleException();
482 }
483
484
485
486 List<ResourceBlockPermission> resourceBlockPermissions =
487 resourceBlockPermissionPersistence.findByRoleId(role.getRoleId());
488
489 for (ResourceBlockPermission resourceBlockPermission :
490 resourceBlockPermissions) {
491
492 resourceBlockPermissionLocalService.deleteResourceBlockPermission(
493 resourceBlockPermission);
494 }
495
496 List<ResourcePermission> resourcePermissions =
497 resourcePermissionPersistence.findByRoleId(role.getRoleId());
498
499 for (ResourcePermission resourcePermission : resourcePermissions) {
500 resourcePermissionLocalService.deleteResourcePermission(
501 resourcePermission);
502 }
503
504 List<ResourceTypePermission> resourceTypePermissions =
505 resourceTypePermissionPersistence.findByRoleId(role.getRoleId());
506
507 for (ResourceTypePermission resourceTypePermission :
508 resourceTypePermissions) {
509
510 resourceTypePermissionLocalService.deleteResourceTypePermission(
511 resourceTypePermission);
512 }
513
514 String className = role.getClassName();
515 long classNameId = role.getClassNameId();
516
517 if ((classNameId <= 0) || className.equals(Role.class.getName())) {
518 resourceLocalService.deleteResource(
519 role.getCompanyId(), Role.class.getName(),
520 ResourceConstants.SCOPE_INDIVIDUAL, role.getRoleId());
521 }
522
523 if ((role.getType() == RoleConstants.TYPE_ORGANIZATION) ||
524 (role.getType() == RoleConstants.TYPE_SITE)) {
525
526 userGroupRoleLocalService.deleteUserGroupRolesByRoleId(
527 role.getRoleId());
528
529 userGroupGroupRoleLocalService.deleteUserGroupGroupRolesByRoleId(
530 role.getRoleId());
531 }
532
533
534
535 rolePersistence.remove(role);
536
537
538
539 expandoRowLocalService.deleteRows(role.getRoleId());
540
541
542
543 PermissionCacheUtil.clearCache();
544
545 return role;
546 }
547
548
562 @Override
563 @Skip
564 public Role fetchRole(long companyId, String name) throws SystemException {
565 String companyIdHexString = StringUtil.toHexString(companyId);
566
567 Role role = _systemRolesMap.get(companyIdHexString.concat(name));
568
569 if (role != null) {
570 return role;
571 }
572
573 return roleLocalService.loadFetchRole(companyId, name);
574 }
575
576
596 @Override
597 public Role getDefaultGroupRole(long groupId)
598 throws PortalException, SystemException {
599
600 Group group = groupPersistence.findByPrimaryKey(groupId);
601
602 if (group.isLayout()) {
603 Layout layout = layoutLocalService.getLayout(group.getClassPK());
604
605 group = layout.getGroup();
606 }
607
608 if (group.isStagingGroup()) {
609 group = group.getLiveGroup();
610 }
611
612 Role role = null;
613
614 if (group.isCompany()) {
615 role = getRole(group.getCompanyId(), RoleConstants.USER);
616 }
617 else if (group.isLayoutPrototype() || group.isLayoutSetPrototype() ||
618 group.isRegularSite() || group.isSite()) {
619
620 role = getRole(group.getCompanyId(), RoleConstants.SITE_MEMBER);
621 }
622 else if (group.isOrganization()) {
623 role = getRole(
624 group.getCompanyId(), RoleConstants.ORGANIZATION_USER);
625 }
626 else {
627 role = getRole(group.getCompanyId(), RoleConstants.USER);
628 }
629
630 return role;
631 }
632
633 @Override
634 public List<Role> getGroupRelatedRoles(long groupId)
635 throws PortalException, SystemException {
636
637 List<Role> roles = new ArrayList<Role>();
638
639 Group group = groupLocalService.getGroup(groupId);
640
641 if (group.isStagingGroup()) {
642 group = group.getLiveGroup();
643 }
644
645 int[] types = RoleConstants.TYPES_REGULAR;
646
647 if (group.isOrganization()) {
648 types = RoleConstants.TYPES_ORGANIZATION_AND_REGULAR;
649 }
650 else if (group.isLayout() || group.isLayoutSetPrototype() ||
651 group.isSite() || group.isUser()) {
652
653 types = RoleConstants.TYPES_REGULAR_AND_SITE;
654 }
655
656 roles.addAll(getRoles(group.getCompanyId(), types));
657
658 roles.addAll(getTeamRoles(groupId));
659
660 return roles;
661 }
662
663 @Override
664 public List<Role> getResourceBlockRoles(
665 long resourceBlockId, String className, String actionId)
666 throws SystemException {
667
668 return roleFinder.findByR_N_A(resourceBlockId, className, actionId);
669 }
670
671
684 @Override
685 public Map<String, List<String>> getResourceRoles(
686 long companyId, String name, int scope, String primKey)
687 throws SystemException {
688
689 return roleFinder.findByC_N_S_P(companyId, name, scope, primKey);
690 }
691
692
706 @Override
707 public List<Role> getResourceRoles(
708 long companyId, String name, int scope, String primKey,
709 String actionId)
710 throws SystemException {
711
712 return roleFinder.findByC_N_S_P_A(
713 companyId, name, scope, primKey, actionId);
714 }
715
716
731 @Override
732 @Skip
733 public Role getRole(long companyId, String name)
734 throws PortalException, SystemException {
735
736 String companyIdHexString = StringUtil.toHexString(companyId);
737
738 Role role = _systemRolesMap.get(companyIdHexString.concat(name));
739
740 if (role != null) {
741 return role;
742 }
743
744 return roleLocalService.loadGetRole(companyId, name);
745 }
746
747
755 @Override
756 public List<Role> getRoles(int type, String subtype)
757 throws SystemException {
758
759 return rolePersistence.findByT_S(type, subtype);
760 }
761
762
769 @Override
770 public List<Role> getRoles(long companyId) throws SystemException {
771 return rolePersistence.findByCompanyId(companyId);
772 }
773
774
782 @Override
783 public List<Role> getRoles(long companyId, int[] types)
784 throws SystemException {
785
786 return rolePersistence.findByC_T(companyId, types);
787 }
788
789
798 @Override
799 public List<Role> getRoles(long[] roleIds)
800 throws PortalException, SystemException {
801
802 List<Role> roles = new ArrayList<Role>(roleIds.length);
803
804 for (long roleId : roleIds) {
805 Role role = getRole(roleId);
806
807 roles.add(role);
808 }
809
810 return roles;
811 }
812
813
820 @Override
821 public List<Role> getSubtypeRoles(String subtype) throws SystemException {
822 return rolePersistence.findBySubtype(subtype);
823 }
824
825
832 @Override
833 public int getSubtypeRolesCount(String subtype) throws SystemException {
834 return rolePersistence.countBySubtype(subtype);
835 }
836
837
847 @Override
848 public Role getTeamRole(long companyId, long teamId)
849 throws PortalException, SystemException {
850
851 long classNameId = PortalUtil.getClassNameId(Team.class);
852
853 return rolePersistence.findByC_C_C(companyId, classNameId, teamId);
854 }
855
856
866 @Override
867 public Map<Team, Role> getTeamRoleMap(long groupId)
868 throws PortalException, SystemException {
869
870 return getTeamRoleMap(groupId, null);
871 }
872
873
883 @Override
884 public List<Role> getTeamRoles(long groupId)
885 throws PortalException, SystemException {
886
887 return getTeamRoles(groupId, null);
888 }
889
890
902 @Override
903 public List<Role> getTeamRoles(long groupId, long[] excludedRoleIds)
904 throws PortalException, SystemException {
905
906 Map<Team, Role> teamRoleMap = getTeamRoleMap(groupId, excludedRoleIds);
907
908 Collection<Role> roles = teamRoleMap.values();
909
910 return ListUtil.fromCollection(roles);
911 }
912
913
920 @Override
921 public List<Role> getTypeRoles(int type) throws SystemException {
922 return rolePersistence.findByType(type);
923 }
924
925
935 @Override
936 public List<Role> getTypeRoles(int type, int start, int end)
937 throws SystemException {
938
939 return rolePersistence.findByType(type, start, end);
940 }
941
942
949 @Override
950 public int getTypeRolesCount(int type) throws SystemException {
951 return rolePersistence.countByType(type);
952 }
953
954
964 @Override
965 public List<Role> getUserGroupGroupRoles(long userId, long groupId)
966 throws SystemException {
967
968 return roleFinder.findByUserGroupGroupRole(userId, groupId);
969 }
970
971
981 @Override
982 public List<Role> getUserGroupRoles(long userId, long groupId)
983 throws SystemException {
984
985 return roleFinder.findByUserGroupRole(userId, groupId);
986 }
987
988
998 @Override
999 public List<Role> getUserRelatedRoles(long userId, List<Group> groups)
1000 throws SystemException {
1001
1002 if ((groups == null) || groups.isEmpty()) {
1003 return Collections.emptyList();
1004 }
1005
1006 return roleFinder.findByU_G(userId, groups);
1007 }
1008
1009
1019 @Override
1020 public List<Role> getUserRelatedRoles(long userId, long groupId)
1021 throws SystemException {
1022
1023 return roleFinder.findByU_G(userId, groupId);
1024 }
1025
1026
1036 @Override
1037 public List<Role> getUserRelatedRoles(long userId, long[] groupIds)
1038 throws SystemException {
1039
1040 return roleFinder.findByU_G(userId, groupIds);
1041 }
1042
1043
1058 @Override
1059 @ThreadLocalCachable
1060 public boolean hasUserRole(
1061 long userId, long companyId, String name, boolean inherited)
1062 throws PortalException, SystemException {
1063
1064 Role role = rolePersistence.fetchByC_N(companyId, name);
1065
1066 if (role == null) {
1067 return false;
1068 }
1069
1070 if (role.getType() != RoleConstants.TYPE_REGULAR) {
1071 throw new IllegalArgumentException(name + " is not a regular role");
1072 }
1073
1074 long defaultUserId = userLocalService.getDefaultUserId(companyId);
1075
1076 if (userId == defaultUserId) {
1077 if (name.equals(RoleConstants.GUEST)) {
1078 return true;
1079 }
1080 else {
1081 return false;
1082 }
1083 }
1084
1085 if (inherited) {
1086 if (userPersistence.containsRole(userId, role.getRoleId())) {
1087 return true;
1088 }
1089
1090 ThreadLocalCache<Boolean> threadLocalCache =
1091 ThreadLocalCacheManager.getThreadLocalCache(
1092 Lifecycle.REQUEST, RoleLocalServiceImpl.class.getName());
1093
1094 String key = String.valueOf(role.getRoleId()).concat(
1095 String.valueOf(userId));
1096
1097 Boolean value = threadLocalCache.get(key);
1098
1099 if (value != null) {
1100 return value;
1101 }
1102
1103 value = PermissionCacheUtil.getUserRole(userId, role);
1104
1105 if (value == null) {
1106 int count = roleFinder.countByR_U(role.getRoleId(), userId);
1107
1108 if (count > 0) {
1109 value = true;
1110 }
1111 else {
1112 value = false;
1113 }
1114
1115 PermissionCacheUtil.putUserRole(userId, role, value);
1116 }
1117
1118 threadLocalCache.put(key, value);
1119
1120 return value;
1121 }
1122 else {
1123 return userPersistence.containsRole(userId, role.getRoleId());
1124 }
1125 }
1126
1127
1143 @Override
1144 public boolean hasUserRoles(
1145 long userId, long companyId, String[] names, boolean inherited)
1146 throws PortalException, SystemException {
1147
1148 for (String name : names) {
1149 if (hasUserRole(userId, companyId, name, inherited)) {
1150 return true;
1151 }
1152 }
1153
1154 return false;
1155 }
1156
1157
1166 @Override
1167 public Role loadFetchRole(long companyId, String name)
1168 throws SystemException {
1169
1170 return rolePersistence.fetchByC_N(companyId, name);
1171 }
1172
1173
1183 @Override
1184 public Role loadGetRole(long companyId, String name)
1185 throws PortalException, SystemException {
1186
1187 return rolePersistence.findByC_N(companyId, name);
1188 }
1189
1190
1218 @Override
1219 public List<Role> search(
1220 long companyId, String keywords, Integer[] types, int start,
1221 int end, OrderByComparator obc)
1222 throws SystemException {
1223
1224 return search(
1225 companyId, keywords, types, new LinkedHashMap<String, Object>(),
1226 start, end, obc);
1227 }
1228
1229
1260 @Override
1261 public List<Role> search(
1262 long companyId, String keywords, Integer[] types,
1263 LinkedHashMap<String, Object> params, int start, int end,
1264 OrderByComparator obc)
1265 throws SystemException {
1266
1267 return roleFinder.findByKeywords(
1268 companyId, keywords, types, params, start, end, obc);
1269 }
1270
1271
1299 @Override
1300 public List<Role> search(
1301 long companyId, String name, String description, Integer[] types,
1302 int start, int end, OrderByComparator obc)
1303 throws SystemException {
1304
1305 return search(
1306 companyId, name, description, types,
1307 new LinkedHashMap<String, Object>(), start, end, obc);
1308 }
1309
1310
1341 @Override
1342 public List<Role> search(
1343 long companyId, String name, String description, Integer[] types,
1344 LinkedHashMap<String, Object> params, int start, int end,
1345 OrderByComparator obc)
1346 throws SystemException {
1347
1348 return roleFinder.findByC_N_D_T(
1349 companyId, name, description, types, params, true, start, end, obc);
1350 }
1351
1352
1362 @Override
1363 public int searchCount(long companyId, String keywords, Integer[] types)
1364 throws SystemException {
1365
1366 return searchCount(
1367 companyId, keywords, types, new LinkedHashMap<String, Object>());
1368 }
1369
1370
1382 @Override
1383 public int searchCount(
1384 long companyId, String keywords, Integer[] types,
1385 LinkedHashMap<String, Object> params)
1386 throws SystemException {
1387
1388 return roleFinder.countByKeywords(companyId, keywords, types, params);
1389 }
1390
1391
1401 @Override
1402 public int searchCount(
1403 long companyId, String name, String description, Integer[] types)
1404 throws SystemException {
1405
1406 return searchCount(
1407 companyId, name, description, types,
1408 new LinkedHashMap<String, Object>());
1409 }
1410
1411
1425 @Override
1426 public int searchCount(
1427 long companyId, String name, String description, Integer[] types,
1428 LinkedHashMap<String, Object> params)
1429 throws SystemException {
1430
1431 return roleFinder.countByC_N_D_T(
1432 companyId, name, description, types, params, true);
1433 }
1434
1435
1445 @Override
1446 public void setUserRoles(long userId, long[] roleIds)
1447 throws PortalException, SystemException {
1448
1449 roleIds = UsersAdminUtil.addRequiredRoles(userId, roleIds);
1450
1451 userPersistence.setRoles(userId, roleIds);
1452
1453 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
1454
1455 indexer.reindex(userId);
1456
1457 PermissionCacheUtil.clearCache(userId);
1458 }
1459
1460
1470 @Override
1471 public void unsetUserRoles(long userId, long[] roleIds)
1472 throws PortalException, SystemException {
1473
1474 roleIds = UsersAdminUtil.removeRequiredRoles(userId, roleIds);
1475
1476 userPersistence.removeRoles(userId, roleIds);
1477
1478 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
1479
1480 indexer.reindex(userId);
1481
1482 PermissionCacheUtil.clearCache(userId);
1483 }
1484
1485
1503 @Override
1504 public Role updateRole(
1505 long roleId, String name, Map<Locale, String> titleMap,
1506 Map<Locale, String> descriptionMap, String subtype,
1507 ServiceContext serviceContext)
1508 throws PortalException, SystemException {
1509
1510 Role role = rolePersistence.findByPrimaryKey(roleId);
1511
1512 validate(roleId, role.getCompanyId(), role.getClassNameId(), name);
1513
1514 if (PortalUtil.isSystemRole(role.getName())) {
1515 name = role.getName();
1516 subtype = null;
1517 }
1518
1519 role.setModifiedDate(new Date());
1520 role.setName(name);
1521 role.setTitleMap(titleMap);
1522 role.setDescriptionMap(descriptionMap);
1523 role.setSubtype(subtype);
1524 role.setExpandoBridgeAttributes(serviceContext);
1525
1526 rolePersistence.update(role);
1527
1528 return role;
1529 }
1530
1531 protected void checkSystemRole(
1532 long companyId, String name, Map<Locale, String> descriptionMap,
1533 int type)
1534 throws PortalException, SystemException {
1535
1536 String companyIdHexString = StringUtil.toHexString(companyId);
1537
1538 String key = companyIdHexString.concat(name);
1539
1540 Role role = _systemRolesMap.get(key);
1541
1542 try {
1543 if (role == null) {
1544 role = rolePersistence.findByC_N(companyId, name);
1545 }
1546
1547 if (!descriptionMap.equals(role.getDescriptionMap())) {
1548 role.setDescriptionMap(descriptionMap);
1549
1550 roleLocalService.updateRole(role);
1551 }
1552 }
1553 catch (NoSuchRoleException nsre) {
1554 User user = userLocalService.getDefaultUser(companyId);
1555
1556 role = roleLocalService.addRole(
1557 user.getUserId(), null, 0, name, null, descriptionMap, type,
1558 null, null);
1559
1560 if (name.equals(RoleConstants.USER)) {
1561 initPersonalControlPanelPortletsPermissions(role);
1562 }
1563 }
1564
1565 _systemRolesMap.put(key, role);
1566 }
1567
1568 protected String[] getDefaultControlPanelPortlets() {
1569 return new String[] {
1570 PortletKeys.MY_ACCOUNT, PortletKeys.MY_PAGES,
1571 PortletKeys.MY_WORKFLOW_INSTANCES, PortletKeys.MY_WORKFLOW_TASKS
1572 };
1573 }
1574
1575 protected Map<Team, Role> getTeamRoleMap(
1576 long groupId, long[] excludedRoleIds)
1577 throws PortalException, SystemException {
1578
1579 Group group = groupPersistence.findByPrimaryKey(groupId);
1580
1581 if (group.isLayout()) {
1582 group = group.getParentGroup();
1583 }
1584
1585 List<Team> teams = teamPersistence.findByGroupId(group.getGroupId());
1586
1587 if (teams.isEmpty()) {
1588 return Collections.emptyMap();
1589 }
1590
1591 Set<Long> roleIds = SetUtil.fromArray(excludedRoleIds);
1592
1593 Map<Team, Role> teamRoleMap = new LinkedHashMap<Team, Role>();
1594
1595 for (Team team : teams) {
1596 Role role = getTeamRole(team.getCompanyId(), team.getTeamId());
1597
1598 if (roleIds.contains(role.getRoleId())) {
1599 continue;
1600 }
1601
1602 teamRoleMap.put(team, role);
1603 }
1604
1605 return teamRoleMap;
1606 }
1607
1608 protected void initPersonalControlPanelPortletsPermissions(Role role)
1609 throws PortalException, SystemException {
1610
1611 for (String portletId : getDefaultControlPanelPortlets()) {
1612 int count = resourcePermissionPersistence.countByC_N_S_P_R(
1613 role.getCompanyId(), portletId, ResourceConstants.SCOPE_COMPANY,
1614 String.valueOf(role.getCompanyId()), role.getRoleId());
1615
1616 if (count > 0) {
1617 continue;
1618 }
1619
1620 ResourceAction resourceAction =
1621 resourceActionLocalService.fetchResourceAction(
1622 portletId, ActionKeys.ACCESS_IN_CONTROL_PANEL);
1623
1624 if (resourceAction == null) {
1625 continue;
1626 }
1627
1628 setRolePermissions(
1629 role, portletId,
1630 new String[] {
1631 ActionKeys.ACCESS_IN_CONTROL_PANEL
1632 });
1633 }
1634 }
1635
1636 protected void setRolePermissions(
1637 Role role, String name, String[] actionIds)
1638 throws PortalException, SystemException {
1639
1640 if (resourceBlockLocalService.isSupported(name)) {
1641 resourceBlockLocalService.setCompanyScopePermissions(
1642 role.getCompanyId(), name, role.getRoleId(),
1643 Arrays.asList(actionIds));
1644 }
1645 else {
1646 resourcePermissionLocalService.setResourcePermissions(
1647 role.getCompanyId(), name, ResourceConstants.SCOPE_COMPANY,
1648 String.valueOf(role.getCompanyId()), role.getRoleId(),
1649 actionIds);
1650 }
1651 }
1652
1653 protected void validate(
1654 long roleId, long companyId, long classNameId, String name)
1655 throws PortalException, SystemException {
1656
1657 if (classNameId == PortalUtil.getClassNameId(Role.class)) {
1658 if (Validator.isNull(name) ||
1659 (name.indexOf(CharPool.COMMA) != -1) ||
1660 (name.indexOf(CharPool.STAR) != -1)) {
1661
1662 throw new RoleNameException();
1663 }
1664
1665 if (Validator.isNumber(name) &&
1666 !PropsValues.ROLES_NAME_ALLOW_NUMERIC) {
1667
1668 throw new RoleNameException();
1669 }
1670 }
1671
1672 try {
1673 Role role = roleFinder.findByC_N(companyId, name);
1674
1675 if (role.getRoleId() != roleId) {
1676 throw new DuplicateRoleException("{roleId=" + roleId + "}");
1677 }
1678 }
1679 catch (NoSuchRoleException nsre) {
1680 }
1681 }
1682
1683 private Map<String, Role> _systemRolesMap = new HashMap<String, Role>();
1684
1685 }