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();
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 if (group.isUser() || group.isUserGroup()) {
627 role = getRole(group.getCompanyId(), RoleConstants.POWER_USER);
628 }
629 else {
630 role = getRole(group.getCompanyId(), RoleConstants.USER);
631 }
632
633 return role;
634 }
635
636 @Override
637 public List<Role> getGroupRelatedRoles(long groupId)
638 throws PortalException, SystemException {
639
640 List<Role> roles = new ArrayList<Role>();
641
642 Group group = groupLocalService.getGroup(groupId);
643
644 if (group.isStagingGroup()) {
645 group = group.getLiveGroup();
646 }
647
648 int[] types = RoleConstants.TYPES_REGULAR;
649
650 if (group.isOrganization()) {
651 types = RoleConstants.TYPES_ORGANIZATION_AND_REGULAR;
652 }
653 else if (group.isLayout() || group.isLayoutSetPrototype() ||
654 group.isSite()) {
655
656 types = RoleConstants.TYPES_REGULAR_AND_SITE;
657 }
658
659 roles.addAll(getRoles(group.getCompanyId(), types));
660
661 roles.addAll(getTeamRoles(groupId));
662
663 return roles;
664 }
665
666 @Override
667 public List<Role> getResourceBlockRoles(
668 long resourceBlockId, String className, String actionId)
669 throws SystemException {
670
671 return roleFinder.findByR_N_A(resourceBlockId, className, actionId);
672 }
673
674
687 @Override
688 public Map<String, List<String>> getResourceRoles(
689 long companyId, String name, int scope, String primKey)
690 throws SystemException {
691
692 return roleFinder.findByC_N_S_P(companyId, name, scope, primKey);
693 }
694
695
709 @Override
710 public List<Role> getResourceRoles(
711 long companyId, String name, int scope, String primKey,
712 String actionId)
713 throws SystemException {
714
715 return roleFinder.findByC_N_S_P_A(
716 companyId, name, scope, primKey, actionId);
717 }
718
719
734 @Override
735 @Skip
736 public Role getRole(long companyId, String name)
737 throws PortalException, SystemException {
738
739 String companyIdHexString = StringUtil.toHexString(companyId);
740
741 Role role = _systemRolesMap.get(companyIdHexString.concat(name));
742
743 if (role != null) {
744 return role;
745 }
746
747 return roleLocalService.loadGetRole(companyId, name);
748 }
749
750
758 @Override
759 public List<Role> getRoles(int type, String subtype)
760 throws SystemException {
761
762 return rolePersistence.findByT_S(type, subtype);
763 }
764
765
772 @Override
773 public List<Role> getRoles(long companyId) throws SystemException {
774 return rolePersistence.findByCompanyId(companyId);
775 }
776
777
785 @Override
786 public List<Role> getRoles(long companyId, int[] types)
787 throws SystemException {
788
789 return rolePersistence.findByC_T(companyId, types);
790 }
791
792
801 @Override
802 public List<Role> getRoles(long[] roleIds)
803 throws PortalException, SystemException {
804
805 List<Role> roles = new ArrayList<Role>(roleIds.length);
806
807 for (long roleId : roleIds) {
808 Role role = getRole(roleId);
809
810 roles.add(role);
811 }
812
813 return roles;
814 }
815
816
823 @Override
824 public List<Role> getSubtypeRoles(String subtype) throws SystemException {
825 return rolePersistence.findBySubtype(subtype);
826 }
827
828
835 @Override
836 public int getSubtypeRolesCount(String subtype) throws SystemException {
837 return rolePersistence.countBySubtype(subtype);
838 }
839
840
850 @Override
851 public Role getTeamRole(long companyId, long teamId)
852 throws PortalException, SystemException {
853
854 long classNameId = PortalUtil.getClassNameId(Team.class);
855
856 return rolePersistence.findByC_C_C(companyId, classNameId, teamId);
857 }
858
859
869 @Override
870 public Map<Team, Role> getTeamRoleMap(long groupId)
871 throws PortalException, SystemException {
872
873 return getTeamRoleMap(groupId, null);
874 }
875
876
886 @Override
887 public List<Role> getTeamRoles(long groupId)
888 throws PortalException, SystemException {
889
890 return getTeamRoles(groupId, null);
891 }
892
893
905 @Override
906 public List<Role> getTeamRoles(long groupId, long[] excludedRoleIds)
907 throws PortalException, SystemException {
908
909 Map<Team, Role> teamRoleMap = getTeamRoleMap(groupId, excludedRoleIds);
910
911 Collection<Role> roles = teamRoleMap.values();
912
913 return ListUtil.fromCollection(roles);
914 }
915
916
923 @Override
924 public List<Role> getTypeRoles(int type) throws SystemException {
925 return rolePersistence.findByType(type);
926 }
927
928
938 @Override
939 public List<Role> getTypeRoles(int type, int start, int end)
940 throws SystemException {
941
942 return rolePersistence.findByType(type, start, end);
943 }
944
945
952 @Override
953 public int getTypeRolesCount(int type) throws SystemException {
954 return rolePersistence.countByType(type);
955 }
956
957
967 @Override
968 public List<Role> getUserGroupGroupRoles(long userId, long groupId)
969 throws SystemException {
970
971 return roleFinder.findByUserGroupGroupRole(userId, groupId);
972 }
973
974
984 @Override
985 public List<Role> getUserGroupRoles(long userId, long groupId)
986 throws SystemException {
987
988 return roleFinder.findByUserGroupRole(userId, groupId);
989 }
990
991
1001 @Override
1002 public List<Role> getUserRelatedRoles(long userId, List<Group> groups)
1003 throws SystemException {
1004
1005 if ((groups == null) || groups.isEmpty()) {
1006 return Collections.emptyList();
1007 }
1008
1009 return roleFinder.findByU_G(userId, groups);
1010 }
1011
1012
1022 @Override
1023 public List<Role> getUserRelatedRoles(long userId, long groupId)
1024 throws SystemException {
1025
1026 return roleFinder.findByU_G(userId, groupId);
1027 }
1028
1029
1039 @Override
1040 public List<Role> getUserRelatedRoles(long userId, long[] groupIds)
1041 throws SystemException {
1042
1043 return roleFinder.findByU_G(userId, groupIds);
1044 }
1045
1046
1061 @Override
1062 @ThreadLocalCachable
1063 public boolean hasUserRole(
1064 long userId, long companyId, String name, boolean inherited)
1065 throws PortalException, SystemException {
1066
1067 Role role = rolePersistence.fetchByC_N(companyId, name);
1068
1069 if (role == null) {
1070 return false;
1071 }
1072
1073 if (role.getType() != RoleConstants.TYPE_REGULAR) {
1074 throw new IllegalArgumentException(name + " is not a regular role");
1075 }
1076
1077 long defaultUserId = userLocalService.getDefaultUserId(companyId);
1078
1079 if (userId == defaultUserId) {
1080 if (name.equals(RoleConstants.GUEST)) {
1081 return true;
1082 }
1083 else {
1084 return false;
1085 }
1086 }
1087
1088 if (inherited) {
1089 if (userPersistence.containsRole(userId, role.getRoleId())) {
1090 return true;
1091 }
1092
1093 ThreadLocalCache<Integer> threadLocalCache =
1094 ThreadLocalCacheManager.getThreadLocalCache(
1095 Lifecycle.REQUEST, RoleLocalServiceImpl.class.getName());
1096
1097 String key = String.valueOf(role.getRoleId()).concat(
1098 String.valueOf(userId));
1099
1100 Integer value = threadLocalCache.get(key);
1101
1102 if (value == null) {
1103 value = roleFinder.countByR_U(role.getRoleId(), userId);
1104
1105 threadLocalCache.put(key, value);
1106 }
1107
1108 if (value > 0) {
1109 return true;
1110 }
1111 else {
1112 return false;
1113 }
1114 }
1115 else {
1116 return userPersistence.containsRole(userId, role.getRoleId());
1117 }
1118 }
1119
1120
1136 @Override
1137 public boolean hasUserRoles(
1138 long userId, long companyId, String[] names, boolean inherited)
1139 throws PortalException, SystemException {
1140
1141 for (String name : names) {
1142 if (hasUserRole(userId, companyId, name, inherited)) {
1143 return true;
1144 }
1145 }
1146
1147 return false;
1148 }
1149
1150
1159 @Override
1160 public Role loadFetchRole(long companyId, String name)
1161 throws SystemException {
1162
1163 return rolePersistence.fetchByC_N(companyId, name);
1164 }
1165
1166
1176 @Override
1177 public Role loadGetRole(long companyId, String name)
1178 throws PortalException, SystemException {
1179
1180 return rolePersistence.findByC_N(companyId, name);
1181 }
1182
1183
1211 @Override
1212 public List<Role> search(
1213 long companyId, String keywords, Integer[] types, int start,
1214 int end, OrderByComparator obc)
1215 throws SystemException {
1216
1217 return search(
1218 companyId, keywords, types, new LinkedHashMap<String, Object>(),
1219 start, end, obc);
1220 }
1221
1222
1253 @Override
1254 public List<Role> search(
1255 long companyId, String keywords, Integer[] types,
1256 LinkedHashMap<String, Object> params, int start, int end,
1257 OrderByComparator obc)
1258 throws SystemException {
1259
1260 return roleFinder.findByKeywords(
1261 companyId, keywords, types, params, start, end, obc);
1262 }
1263
1264
1292 @Override
1293 public List<Role> search(
1294 long companyId, String name, String description, Integer[] types,
1295 int start, int end, OrderByComparator obc)
1296 throws SystemException {
1297
1298 return search(
1299 companyId, name, description, types,
1300 new LinkedHashMap<String, Object>(), start, end, obc);
1301 }
1302
1303
1334 @Override
1335 public List<Role> search(
1336 long companyId, String name, String description, Integer[] types,
1337 LinkedHashMap<String, Object> params, int start, int end,
1338 OrderByComparator obc)
1339 throws SystemException {
1340
1341 return roleFinder.findByC_N_D_T(
1342 companyId, name, description, types, params, true, start, end, obc);
1343 }
1344
1345
1355 @Override
1356 public int searchCount(long companyId, String keywords, Integer[] types)
1357 throws SystemException {
1358
1359 return searchCount(
1360 companyId, keywords, types, new LinkedHashMap<String, Object>());
1361 }
1362
1363
1375 @Override
1376 public int searchCount(
1377 long companyId, String keywords, Integer[] types,
1378 LinkedHashMap<String, Object> params)
1379 throws SystemException {
1380
1381 return roleFinder.countByKeywords(companyId, keywords, types, params);
1382 }
1383
1384
1394 @Override
1395 public int searchCount(
1396 long companyId, String name, String description, Integer[] types)
1397 throws SystemException {
1398
1399 return searchCount(
1400 companyId, name, description, types,
1401 new LinkedHashMap<String, Object>());
1402 }
1403
1404
1418 @Override
1419 public int searchCount(
1420 long companyId, String name, String description, Integer[] types,
1421 LinkedHashMap<String, Object> params)
1422 throws SystemException {
1423
1424 return roleFinder.countByC_N_D_T(
1425 companyId, name, description, types, params, true);
1426 }
1427
1428
1438 @Override
1439 public void setUserRoles(long userId, long[] roleIds)
1440 throws PortalException, SystemException {
1441
1442 roleIds = UsersAdminUtil.addRequiredRoles(userId, roleIds);
1443
1444 userPersistence.setRoles(userId, roleIds);
1445
1446 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
1447
1448 indexer.reindex(userId);
1449
1450 PermissionCacheUtil.clearCache();
1451 }
1452
1453
1463 @Override
1464 public void unsetUserRoles(long userId, long[] roleIds)
1465 throws PortalException, SystemException {
1466
1467 roleIds = UsersAdminUtil.removeRequiredRoles(userId, roleIds);
1468
1469 userPersistence.removeRoles(userId, roleIds);
1470
1471 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
1472
1473 indexer.reindex(userId);
1474
1475 PermissionCacheUtil.clearCache();
1476 }
1477
1478
1496 @Override
1497 public Role updateRole(
1498 long roleId, String name, Map<Locale, String> titleMap,
1499 Map<Locale, String> descriptionMap, String subtype,
1500 ServiceContext serviceContext)
1501 throws PortalException, SystemException {
1502
1503 Role role = rolePersistence.findByPrimaryKey(roleId);
1504
1505 validate(roleId, role.getCompanyId(), role.getClassNameId(), name);
1506
1507 if (PortalUtil.isSystemRole(role.getName())) {
1508 name = role.getName();
1509 subtype = null;
1510 }
1511
1512 role.setModifiedDate(new Date());
1513 role.setName(name);
1514 role.setTitleMap(titleMap);
1515 role.setDescriptionMap(descriptionMap);
1516 role.setSubtype(subtype);
1517 role.setExpandoBridgeAttributes(serviceContext);
1518
1519 rolePersistence.update(role);
1520
1521 return role;
1522 }
1523
1524 protected void checkSystemRole(
1525 long companyId, String name, Map<Locale, String> descriptionMap,
1526 int type)
1527 throws PortalException, SystemException {
1528
1529 String companyIdHexString = StringUtil.toHexString(companyId);
1530
1531 String key = companyIdHexString.concat(name);
1532
1533 Role role = _systemRolesMap.get(key);
1534
1535 try {
1536 if (role == null) {
1537 role = rolePersistence.findByC_N(companyId, name);
1538 }
1539
1540 if (!descriptionMap.equals(role.getDescriptionMap())) {
1541 role.setDescriptionMap(descriptionMap);
1542
1543 roleLocalService.updateRole(role);
1544 }
1545 }
1546 catch (NoSuchRoleException nsre) {
1547 User user = userLocalService.getDefaultUser(companyId);
1548
1549 role = roleLocalService.addRole(
1550 user.getUserId(), null, 0, name, null, descriptionMap, type,
1551 null, null);
1552
1553 if (name.equals(RoleConstants.USER)) {
1554 initPersonalControlPanelPortletsPermissions(role);
1555 }
1556 }
1557
1558 _systemRolesMap.put(key, role);
1559 }
1560
1561 protected String[] getDefaultControlPanelPortlets() {
1562 return new String[] {
1563 PortletKeys.MY_ACCOUNT, PortletKeys.MY_PAGES,
1564 PortletKeys.MY_WORKFLOW_INSTANCES, PortletKeys.MY_WORKFLOW_TASKS
1565 };
1566 }
1567
1568 protected Map<Team, Role> getTeamRoleMap(
1569 long groupId, long[] excludedRoleIds)
1570 throws PortalException, SystemException {
1571
1572 Group group = groupPersistence.findByPrimaryKey(groupId);
1573
1574 if (group.isLayout()) {
1575 group = group.getParentGroup();
1576 }
1577
1578 List<Team> teams = teamPersistence.findByGroupId(group.getGroupId());
1579
1580 if (teams.isEmpty()) {
1581 return Collections.emptyMap();
1582 }
1583
1584 Set<Long> roleIds = SetUtil.fromArray(excludedRoleIds);
1585
1586 Map<Team, Role> teamRoleMap = new LinkedHashMap<Team, Role>();
1587
1588 for (Team team : teams) {
1589 Role role = getTeamRole(team.getCompanyId(), team.getTeamId());
1590
1591 if (roleIds.contains(role.getRoleId())) {
1592 continue;
1593 }
1594
1595 teamRoleMap.put(team, role);
1596 }
1597
1598 return teamRoleMap;
1599 }
1600
1601 protected void initPersonalControlPanelPortletsPermissions(Role role)
1602 throws PortalException, SystemException {
1603
1604 for (String portletId : getDefaultControlPanelPortlets()) {
1605 int count = resourcePermissionPersistence.countByC_N_S_P_R(
1606 role.getCompanyId(), portletId, ResourceConstants.SCOPE_COMPANY,
1607 String.valueOf(role.getCompanyId()), role.getRoleId());
1608
1609 if (count > 0) {
1610 continue;
1611 }
1612
1613 ResourceAction resourceAction =
1614 resourceActionLocalService.fetchResourceAction(
1615 portletId, ActionKeys.ACCESS_IN_CONTROL_PANEL);
1616
1617 if (resourceAction == null) {
1618 continue;
1619 }
1620
1621 setRolePermissions(
1622 role, portletId,
1623 new String[] {
1624 ActionKeys.ACCESS_IN_CONTROL_PANEL
1625 });
1626 }
1627 }
1628
1629 protected void setRolePermissions(
1630 Role role, String name, String[] actionIds)
1631 throws PortalException, SystemException {
1632
1633 if (resourceBlockLocalService.isSupported(name)) {
1634 resourceBlockLocalService.setCompanyScopePermissions(
1635 role.getCompanyId(), name, role.getRoleId(),
1636 Arrays.asList(actionIds));
1637 }
1638 else {
1639 resourcePermissionLocalService.setResourcePermissions(
1640 role.getCompanyId(), name, ResourceConstants.SCOPE_COMPANY,
1641 String.valueOf(role.getCompanyId()), role.getRoleId(),
1642 actionIds);
1643 }
1644 }
1645
1646 protected void validate(
1647 long roleId, long companyId, long classNameId, String name)
1648 throws PortalException, SystemException {
1649
1650 if (classNameId == PortalUtil.getClassNameId(Role.class)) {
1651 if (Validator.isNull(name) ||
1652 (name.indexOf(CharPool.COMMA) != -1) ||
1653 (name.indexOf(CharPool.STAR) != -1)) {
1654
1655 throw new RoleNameException();
1656 }
1657
1658 if (Validator.isNumber(name) &&
1659 !PropsValues.ROLES_NAME_ALLOW_NUMERIC) {
1660
1661 throw new RoleNameException();
1662 }
1663 }
1664
1665 try {
1666 Role role = roleFinder.findByC_N(companyId, name);
1667
1668 if (role.getRoleId() != roleId) {
1669 throw new DuplicateRoleException("{roleId=" + roleId + "}");
1670 }
1671 }
1672 catch (NoSuchRoleException nsre) {
1673 }
1674 }
1675
1676 private Map<String, Role> _systemRolesMap = new HashMap<String, Role>();
1677
1678 }