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 if (group.isStagingGroup()) {
627 group = group.getLiveGroup();
628 }
629
630 int[] types = RoleConstants.TYPES_REGULAR;
631
632 if (group.isOrganization()) {
633 types = RoleConstants.TYPES_ORGANIZATION_AND_REGULAR;
634 }
635 else if (group.isLayout() || group.isLayoutSetPrototype() ||
636 group.isSite()) {
637
638 types = RoleConstants.TYPES_REGULAR_AND_SITE;
639 }
640
641 roles.addAll(getRoles(group.getCompanyId(), types));
642
643 roles.addAll(getTeamRoles(groupId));
644
645 return roles;
646 }
647
648 @Override
649 public List<Role> getResourceBlockRoles(
650 long resourceBlockId, String className, String actionId)
651 throws SystemException {
652
653 return roleFinder.findByR_N_A(resourceBlockId, className, actionId);
654 }
655
656
669 @Override
670 public Map<String, List<String>> getResourceRoles(
671 long companyId, String name, int scope, String primKey)
672 throws SystemException {
673
674 return roleFinder.findByC_N_S_P(companyId, name, scope, primKey);
675 }
676
677
691 @Override
692 public List<Role> getResourceRoles(
693 long companyId, String name, int scope, String primKey,
694 String actionId)
695 throws SystemException {
696
697 return roleFinder.findByC_N_S_P_A(
698 companyId, name, scope, primKey, actionId);
699 }
700
701
716 @Override
717 @Skip
718 public Role getRole(long companyId, String name)
719 throws PortalException, SystemException {
720
721 String companyIdHexString = StringUtil.toHexString(companyId);
722
723 Role role = _systemRolesMap.get(companyIdHexString.concat(name));
724
725 if (role != null) {
726 return role;
727 }
728
729 return roleLocalService.loadGetRole(companyId, name);
730 }
731
732
740 @Override
741 public List<Role> getRoles(int type, String subtype)
742 throws SystemException {
743
744 return rolePersistence.findByT_S(type, subtype);
745 }
746
747
754 @Override
755 public List<Role> getRoles(long companyId) throws SystemException {
756 return rolePersistence.findByCompanyId(companyId);
757 }
758
759
767 @Override
768 public List<Role> getRoles(long companyId, int[] types)
769 throws SystemException {
770
771 return rolePersistence.findByC_T(companyId, types);
772 }
773
774
783 @Override
784 public List<Role> getRoles(long[] roleIds)
785 throws PortalException, SystemException {
786
787 List<Role> roles = new ArrayList<Role>(roleIds.length);
788
789 for (long roleId : roleIds) {
790 Role role = getRole(roleId);
791
792 roles.add(role);
793 }
794
795 return roles;
796 }
797
798
805 @Override
806 public List<Role> getSubtypeRoles(String subtype) throws SystemException {
807 return rolePersistence.findBySubtype(subtype);
808 }
809
810
817 @Override
818 public int getSubtypeRolesCount(String subtype) throws SystemException {
819 return rolePersistence.countBySubtype(subtype);
820 }
821
822
832 @Override
833 public Role getTeamRole(long companyId, long teamId)
834 throws PortalException, SystemException {
835
836 long classNameId = PortalUtil.getClassNameId(Team.class);
837
838 return rolePersistence.findByC_C_C(companyId, classNameId, teamId);
839 }
840
841
851 @Override
852 public Map<Team, Role> getTeamRoleMap(long groupId)
853 throws PortalException, SystemException {
854
855 return getTeamRoleMap(groupId, null);
856 }
857
858
868 @Override
869 public List<Role> getTeamRoles(long groupId)
870 throws PortalException, SystemException {
871
872 return getTeamRoles(groupId, null);
873 }
874
875
887 @Override
888 public List<Role> getTeamRoles(long groupId, long[] excludedRoleIds)
889 throws PortalException, SystemException {
890
891 Map<Team, Role> teamRoleMap = getTeamRoleMap(groupId, excludedRoleIds);
892
893 Collection<Role> roles = teamRoleMap.values();
894
895 return ListUtil.fromCollection(roles);
896 }
897
898
905 @Override
906 public List<Role> getTypeRoles(int type) throws SystemException {
907 return rolePersistence.findByType(type);
908 }
909
910
920 @Override
921 public List<Role> getTypeRoles(int type, int start, int end)
922 throws SystemException {
923
924 return rolePersistence.findByType(type, start, end);
925 }
926
927
934 @Override
935 public int getTypeRolesCount(int type) throws SystemException {
936 return rolePersistence.countByType(type);
937 }
938
939
949 @Override
950 public List<Role> getUserGroupGroupRoles(long userId, long groupId)
951 throws SystemException {
952
953 return roleFinder.findByUserGroupGroupRole(userId, groupId);
954 }
955
956
966 @Override
967 public List<Role> getUserGroupRoles(long userId, long groupId)
968 throws SystemException {
969
970 return roleFinder.findByUserGroupRole(userId, groupId);
971 }
972
973
983 @Override
984 public List<Role> getUserRelatedRoles(long userId, List<Group> groups)
985 throws SystemException {
986
987 if ((groups == null) || groups.isEmpty()) {
988 return Collections.emptyList();
989 }
990
991 return roleFinder.findByU_G(userId, groups);
992 }
993
994
1004 @Override
1005 public List<Role> getUserRelatedRoles(long userId, long groupId)
1006 throws SystemException {
1007
1008 return roleFinder.findByU_G(userId, groupId);
1009 }
1010
1011
1021 @Override
1022 public List<Role> getUserRelatedRoles(long userId, long[] groupIds)
1023 throws SystemException {
1024
1025 return roleFinder.findByU_G(userId, groupIds);
1026 }
1027
1028
1043 @Override
1044 @ThreadLocalCachable
1045 public boolean hasUserRole(
1046 long userId, long companyId, String name, boolean inherited)
1047 throws PortalException, SystemException {
1048
1049 Role role = rolePersistence.fetchByC_N(companyId, name);
1050
1051 if (role == null) {
1052 return false;
1053 }
1054
1055 if (role.getType() != RoleConstants.TYPE_REGULAR) {
1056 throw new IllegalArgumentException(name + " is not a regular role");
1057 }
1058
1059 long defaultUserId = userLocalService.getDefaultUserId(companyId);
1060
1061 if (userId == defaultUserId) {
1062 if (name.equals(RoleConstants.GUEST)) {
1063 return true;
1064 }
1065 else {
1066 return false;
1067 }
1068 }
1069
1070 if (inherited) {
1071 if (userPersistence.containsRole(userId, role.getRoleId())) {
1072 return true;
1073 }
1074
1075 ThreadLocalCache<Integer> threadLocalCache =
1076 ThreadLocalCacheManager.getThreadLocalCache(
1077 Lifecycle.REQUEST, RoleLocalServiceImpl.class.getName());
1078
1079 String key = String.valueOf(role.getRoleId()).concat(
1080 String.valueOf(userId));
1081
1082 Integer value = threadLocalCache.get(key);
1083
1084 if (value == null) {
1085 value = roleFinder.countByR_U(role.getRoleId(), userId);
1086
1087 threadLocalCache.put(key, value);
1088 }
1089
1090 if (value > 0) {
1091 return true;
1092 }
1093 else {
1094 return false;
1095 }
1096 }
1097 else {
1098 return userPersistence.containsRole(userId, role.getRoleId());
1099 }
1100 }
1101
1102
1118 @Override
1119 public boolean hasUserRoles(
1120 long userId, long companyId, String[] names, boolean inherited)
1121 throws PortalException, SystemException {
1122
1123 for (String name : names) {
1124 if (hasUserRole(userId, companyId, name, inherited)) {
1125 return true;
1126 }
1127 }
1128
1129 return false;
1130 }
1131
1132
1141 @Override
1142 public Role loadFetchRole(long companyId, String name)
1143 throws SystemException {
1144
1145 return rolePersistence.fetchByC_N(companyId, name);
1146 }
1147
1148
1158 @Override
1159 public Role loadGetRole(long companyId, String name)
1160 throws PortalException, SystemException {
1161
1162 return rolePersistence.findByC_N(companyId, name);
1163 }
1164
1165
1193 @Override
1194 public List<Role> search(
1195 long companyId, String keywords, Integer[] types, int start,
1196 int end, OrderByComparator obc)
1197 throws SystemException {
1198
1199 return search(
1200 companyId, keywords, types, new LinkedHashMap<String, Object>(),
1201 start, end, obc);
1202 }
1203
1204
1235 @Override
1236 public List<Role> search(
1237 long companyId, String keywords, Integer[] types,
1238 LinkedHashMap<String, Object> params, int start, int end,
1239 OrderByComparator obc)
1240 throws SystemException {
1241
1242 return roleFinder.findByKeywords(
1243 companyId, keywords, types, params, start, end, obc);
1244 }
1245
1246
1274 @Override
1275 public List<Role> search(
1276 long companyId, String name, String description, Integer[] types,
1277 int start, int end, OrderByComparator obc)
1278 throws SystemException {
1279
1280 return search(
1281 companyId, name, description, types,
1282 new LinkedHashMap<String, Object>(), start, end, obc);
1283 }
1284
1285
1316 @Override
1317 public List<Role> search(
1318 long companyId, String name, String description, Integer[] types,
1319 LinkedHashMap<String, Object> params, int start, int end,
1320 OrderByComparator obc)
1321 throws SystemException {
1322
1323 return roleFinder.findByC_N_D_T(
1324 companyId, name, description, types, params, true, start, end, obc);
1325 }
1326
1327
1337 @Override
1338 public int searchCount(long companyId, String keywords, Integer[] types)
1339 throws SystemException {
1340
1341 return searchCount(
1342 companyId, keywords, types, new LinkedHashMap<String, Object>());
1343 }
1344
1345
1357 @Override
1358 public int searchCount(
1359 long companyId, String keywords, Integer[] types,
1360 LinkedHashMap<String, Object> params)
1361 throws SystemException {
1362
1363 return roleFinder.countByKeywords(companyId, keywords, types, params);
1364 }
1365
1366
1376 @Override
1377 public int searchCount(
1378 long companyId, String name, String description, Integer[] types)
1379 throws SystemException {
1380
1381 return searchCount(
1382 companyId, name, description, types,
1383 new LinkedHashMap<String, Object>());
1384 }
1385
1386
1400 @Override
1401 public int searchCount(
1402 long companyId, String name, String description, Integer[] types,
1403 LinkedHashMap<String, Object> params)
1404 throws SystemException {
1405
1406 return roleFinder.countByC_N_D_T(
1407 companyId, name, description, types, params, true);
1408 }
1409
1410
1420 @Override
1421 public void setUserRoles(long userId, long[] roleIds)
1422 throws PortalException, SystemException {
1423
1424 roleIds = UsersAdminUtil.addRequiredRoles(userId, roleIds);
1425
1426 userPersistence.setRoles(userId, roleIds);
1427
1428 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
1429
1430 indexer.reindex(userId);
1431
1432 PermissionCacheUtil.clearCache();
1433 }
1434
1435
1445 @Override
1446 public void unsetUserRoles(long userId, long[] roleIds)
1447 throws PortalException, SystemException {
1448
1449 roleIds = UsersAdminUtil.removeRequiredRoles(userId, roleIds);
1450
1451 userPersistence.removeRoles(userId, roleIds);
1452
1453 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
1454
1455 indexer.reindex(userId);
1456
1457 PermissionCacheUtil.clearCache();
1458 }
1459
1460
1478 @Override
1479 public Role updateRole(
1480 long roleId, String name, Map<Locale, String> titleMap,
1481 Map<Locale, String> descriptionMap, String subtype,
1482 ServiceContext serviceContext)
1483 throws PortalException, SystemException {
1484
1485 Role role = rolePersistence.findByPrimaryKey(roleId);
1486
1487 validate(roleId, role.getCompanyId(), role.getClassNameId(), name);
1488
1489 if (PortalUtil.isSystemRole(role.getName())) {
1490 name = role.getName();
1491 subtype = null;
1492 }
1493
1494 role.setModifiedDate(new Date());
1495 role.setName(name);
1496 role.setTitleMap(titleMap);
1497 role.setDescriptionMap(descriptionMap);
1498 role.setSubtype(subtype);
1499 role.setExpandoBridgeAttributes(serviceContext);
1500
1501 rolePersistence.update(role);
1502
1503 return role;
1504 }
1505
1506 protected void checkSystemRole(
1507 long companyId, String name, Map<Locale, String> descriptionMap,
1508 int type)
1509 throws PortalException, SystemException {
1510
1511 String companyIdHexString = StringUtil.toHexString(companyId);
1512
1513 String key = companyIdHexString.concat(name);
1514
1515 Role role = _systemRolesMap.get(key);
1516
1517 try {
1518 if (role == null) {
1519 role = rolePersistence.findByC_N(companyId, name);
1520 }
1521
1522 if (!descriptionMap.equals(role.getDescriptionMap())) {
1523 role.setDescriptionMap(descriptionMap);
1524
1525 roleLocalService.updateRole(role);
1526 }
1527 }
1528 catch (NoSuchRoleException nsre) {
1529 User user = userLocalService.getDefaultUser(companyId);
1530
1531 role = roleLocalService.addRole(
1532 user.getUserId(), null, 0, name, null, descriptionMap, type,
1533 null, null);
1534
1535 if (name.equals(RoleConstants.USER)) {
1536 initPersonalControlPanelPortletsPermissions(role);
1537 }
1538 }
1539
1540 _systemRolesMap.put(key, role);
1541 }
1542
1543 protected String[] getDefaultControlPanelPortlets() {
1544 return new String[] {
1545 PortletKeys.MY_ACCOUNT, PortletKeys.MY_PAGES,
1546 PortletKeys.MY_WORKFLOW_INSTANCES, PortletKeys.MY_WORKFLOW_TASKS
1547 };
1548 }
1549
1550 protected Map<Team, Role> getTeamRoleMap(
1551 long groupId, long[] excludedRoleIds)
1552 throws PortalException, SystemException {
1553
1554 Group group = groupPersistence.findByPrimaryKey(groupId);
1555
1556 if (group.isLayout()) {
1557 group = group.getParentGroup();
1558 }
1559
1560 List<Team> teams = teamPersistence.findByGroupId(group.getGroupId());
1561
1562 if (teams.isEmpty()) {
1563 return Collections.emptyMap();
1564 }
1565
1566 Set<Long> roleIds = SetUtil.fromArray(excludedRoleIds);
1567
1568 Map<Team, Role> teamRoleMap = new LinkedHashMap<Team, Role>();
1569
1570 for (Team team : teams) {
1571 Role role = getTeamRole(team.getCompanyId(), team.getTeamId());
1572
1573 if (roleIds.contains(role.getRoleId())) {
1574 continue;
1575 }
1576
1577 teamRoleMap.put(team, role);
1578 }
1579
1580 return teamRoleMap;
1581 }
1582
1583 protected void initPersonalControlPanelPortletsPermissions(Role role)
1584 throws PortalException, SystemException {
1585
1586 for (String portletId : getDefaultControlPanelPortlets()) {
1587 int count = resourcePermissionPersistence.countByC_N_S_P_R(
1588 role.getCompanyId(), portletId, ResourceConstants.SCOPE_COMPANY,
1589 String.valueOf(role.getCompanyId()), role.getRoleId());
1590
1591 if (count > 0) {
1592 continue;
1593 }
1594
1595 ResourceAction resourceAction =
1596 resourceActionLocalService.fetchResourceAction(
1597 portletId, ActionKeys.ACCESS_IN_CONTROL_PANEL);
1598
1599 if (resourceAction == null) {
1600 continue;
1601 }
1602
1603 setRolePermissions(
1604 role, portletId,
1605 new String[] {
1606 ActionKeys.ACCESS_IN_CONTROL_PANEL
1607 });
1608 }
1609 }
1610
1611 protected void setRolePermissions(
1612 Role role, String name, String[] actionIds)
1613 throws PortalException, SystemException {
1614
1615 if (resourceBlockLocalService.isSupported(name)) {
1616 resourceBlockLocalService.setCompanyScopePermissions(
1617 role.getCompanyId(), name, role.getRoleId(),
1618 Arrays.asList(actionIds));
1619 }
1620 else {
1621 resourcePermissionLocalService.setResourcePermissions(
1622 role.getCompanyId(), name, ResourceConstants.SCOPE_COMPANY,
1623 String.valueOf(role.getCompanyId()), role.getRoleId(),
1624 actionIds);
1625 }
1626 }
1627
1628 protected void validate(
1629 long roleId, long companyId, long classNameId, String name)
1630 throws PortalException, SystemException {
1631
1632 if (classNameId == PortalUtil.getClassNameId(Role.class)) {
1633 if (Validator.isNull(name) ||
1634 (name.indexOf(CharPool.COMMA) != -1) ||
1635 (name.indexOf(CharPool.STAR) != -1)) {
1636
1637 throw new RoleNameException();
1638 }
1639
1640 if (Validator.isNumber(name) &&
1641 !PropsValues.ROLES_NAME_ALLOW_NUMERIC) {
1642
1643 throw new RoleNameException();
1644 }
1645 }
1646
1647 try {
1648 Role role = roleFinder.findByC_N(companyId, name);
1649
1650 if (role.getRoleId() != roleId) {
1651 throw new DuplicateRoleException("{roleId=" + roleId + "}");
1652 }
1653 }
1654 catch (NoSuchRoleException nsre) {
1655 }
1656 }
1657
1658 private Map<String, Role> _systemRolesMap = new HashMap<String, Role>();
1659
1660 }