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.shard.ShardUtil;
027 import com.liferay.portal.kernel.exception.PortalException;
028 import com.liferay.portal.kernel.lar.ExportImportThreadLocal;
029 import com.liferay.portal.kernel.search.Indexer;
030 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
031 import com.liferay.portal.kernel.spring.aop.Skip;
032 import com.liferay.portal.kernel.systemevent.SystemEvent;
033 import com.liferay.portal.kernel.transaction.Propagation;
034 import com.liferay.portal.kernel.transaction.Transactional;
035 import com.liferay.portal.kernel.util.ArrayUtil;
036 import com.liferay.portal.kernel.util.CharPool;
037 import com.liferay.portal.kernel.util.GetterUtil;
038 import com.liferay.portal.kernel.util.ListUtil;
039 import com.liferay.portal.kernel.util.LocaleUtil;
040 import com.liferay.portal.kernel.util.OrderByComparator;
041 import com.liferay.portal.kernel.util.SetUtil;
042 import com.liferay.portal.kernel.util.StringUtil;
043 import com.liferay.portal.kernel.util.Validator;
044 import com.liferay.portal.model.Company;
045 import com.liferay.portal.model.Group;
046 import com.liferay.portal.model.Layout;
047 import com.liferay.portal.model.ResourceAction;
048 import com.liferay.portal.model.ResourceBlockPermission;
049 import com.liferay.portal.model.ResourceConstants;
050 import com.liferay.portal.model.ResourcePermission;
051 import com.liferay.portal.model.ResourceTypePermission;
052 import com.liferay.portal.model.Role;
053 import com.liferay.portal.model.RoleConstants;
054 import com.liferay.portal.model.Shard;
055 import com.liferay.portal.model.SystemEventConstants;
056 import com.liferay.portal.model.Team;
057 import com.liferay.portal.model.User;
058 import com.liferay.portal.security.auth.CompanyThreadLocal;
059 import com.liferay.portal.security.permission.ActionKeys;
060 import com.liferay.portal.security.permission.PermissionCacheUtil;
061 import com.liferay.portal.service.ServiceContext;
062 import com.liferay.portal.service.base.RoleLocalServiceBaseImpl;
063 import com.liferay.portal.util.PortalUtil;
064 import com.liferay.portal.util.PortletKeys;
065 import com.liferay.portal.util.PropsUtil;
066 import com.liferay.portal.util.PropsValues;
067 import com.liferay.portlet.usersadmin.util.UsersAdminUtil;
068
069 import java.util.ArrayList;
070 import java.util.Arrays;
071 import java.util.Collection;
072 import java.util.Collections;
073 import java.util.Date;
074 import java.util.HashMap;
075 import java.util.LinkedHashMap;
076 import java.util.List;
077 import java.util.Locale;
078 import java.util.Map;
079 import java.util.Set;
080
081
088 public class RoleLocalServiceImpl extends RoleLocalServiceBaseImpl {
089
090
108 @Deprecated
109 @Override
110 public Role addRole(
111 long userId, long companyId, String name,
112 Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
113 int type)
114 throws PortalException {
115
116 return addRole(
117 userId, null, 0, name, titleMap, descriptionMap, type, null, null);
118 }
119
120
143 @Deprecated
144 @Override
145 public Role addRole(
146 long userId, long companyId, String name,
147 Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
148 int type, String className, long classPK)
149 throws PortalException {
150
151 return addRole(
152 userId, className, classPK, name, titleMap, descriptionMap, type,
153 null, null);
154 }
155
156
180 @Override
181 public Role addRole(
182 long userId, String className, long classPK, String name,
183 Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
184 int type, String subtype, ServiceContext serviceContext)
185 throws PortalException {
186
187
188
189 User user = userPersistence.findByPrimaryKey(userId);
190 className = GetterUtil.getString(className);
191 long classNameId = classNameLocalService.getClassNameId(className);
192
193 long roleId = counterLocalService.increment();
194
195 if ((classNameId <= 0) || className.equals(Role.class.getName())) {
196 classNameId = classNameLocalService.getClassNameId(Role.class);
197 classPK = roleId;
198 }
199
200 Date now = new Date();
201
202 validate(0, user.getCompanyId(), classNameId, name);
203
204 Role role = rolePersistence.create(roleId);
205
206 if (serviceContext != null) {
207 role.setUuid(serviceContext.getUuid());
208 }
209
210 role.setCompanyId(user.getCompanyId());
211 role.setUserId(user.getUserId());
212 role.setUserName(user.getFullName());
213
214 if (serviceContext != null) {
215 role.setCreateDate(serviceContext.getCreateDate(now));
216 role.setModifiedDate(serviceContext.getModifiedDate(now));
217 }
218 else {
219 role.setCreateDate(now);
220 role.setModifiedDate(now);
221 }
222
223 role.setClassNameId(classNameId);
224 role.setClassPK(classPK);
225 role.setName(name);
226 role.setTitleMap(titleMap);
227 role.setDescriptionMap(descriptionMap);
228 role.setType(type);
229 role.setSubtype(subtype);
230 role.setExpandoBridgeAttributes(serviceContext);
231
232 rolePersistence.update(role);
233
234
235
236 if (!user.isDefaultUser()) {
237 resourceLocalService.addResources(
238 user.getCompanyId(), 0, userId, Role.class.getName(),
239 role.getRoleId(), false, false, false);
240
241 if (!ExportImportThreadLocal.isImportInProcess()) {
242 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
243 User.class);
244
245 indexer.reindex(userId);
246 }
247 }
248
249 return role;
250 }
251
252
262 @Override
263 public void addUserRoles(long userId, long[] roleIds)
264 throws PortalException {
265
266 userPersistence.addRoles(userId, roleIds);
267
268 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
269
270 indexer.reindex(userId);
271
272 PermissionCacheUtil.clearCache();
273 }
274
275
282 @Override
283 public void checkSystemRoles() throws PortalException {
284 List<Company> companies = companyLocalService.getCompanies();
285
286 String currentShardName = ShardUtil.getCurrentShardName();
287
288 for (Company company : companies) {
289 String shardName = null;
290
291 try {
292 shardName = company.getShardName();
293 }
294 catch (NoSuchShardException nsse) {
295 Shard shard = shardLocalService.addShard(
296 Company.class.getName(), company.getCompanyId(),
297 PropsValues.SHARD_DEFAULT_NAME);
298
299 shardName = shard.getName();
300 }
301
302 if (!ShardUtil.isEnabled() || shardName.equals(currentShardName)) {
303 checkSystemRoles(company.getCompanyId());
304 }
305 }
306 }
307
308
316 @Override
317 @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
318 public void checkSystemRoles(long companyId) throws PortalException {
319 String companyIdHexString = StringUtil.toHexString(companyId);
320
321 List<Role> roles = null;
322
323 try {
324 roles = roleFinder.findBySystem(companyId);
325 }
326 catch (Exception e) {
327
328
329
330 runSQL("alter table Role_ add uuid_ VARCHAR(75) null");
331 runSQL("alter table Role_ add userId LONG");
332 runSQL("alter table Role_ add userName VARCHAR(75) null");
333 runSQL("alter table Role_ add createDate DATE null");
334 runSQL("alter table Role_ add modifiedDate DATE null");
335
336 roles = roleFinder.findBySystem(companyId);
337 }
338
339 for (Role role : roles) {
340 _systemRolesMap.put(
341 companyIdHexString.concat(role.getName()), role);
342 }
343
344
345
346 String[] systemRoles = PortalUtil.getSystemRoles();
347
348 for (String name : systemRoles) {
349 String key =
350 "system.role." +
351 StringUtil.replace(name, CharPool.SPACE, CharPool.PERIOD) +
352 ".description";
353
354 Map<Locale, String> descriptionMap = new HashMap<Locale, String>();
355
356 descriptionMap.put(LocaleUtil.getDefault(), PropsUtil.get(key));
357
358 int type = RoleConstants.TYPE_REGULAR;
359
360 checkSystemRole(companyId, name, descriptionMap, type);
361 }
362
363
364
365 String[] systemOrganizationRoles =
366 PortalUtil.getSystemOrganizationRoles();
367
368 for (String name : systemOrganizationRoles) {
369 String key =
370 "system.organization.role." +
371 StringUtil.replace(name, CharPool.SPACE, CharPool.PERIOD) +
372 ".description";
373
374 Map<Locale, String> descriptionMap = new HashMap<Locale, String>();
375
376 descriptionMap.put(LocaleUtil.getDefault(), PropsUtil.get(key));
377
378 int type = RoleConstants.TYPE_ORGANIZATION;
379
380 checkSystemRole(companyId, name, descriptionMap, type);
381 }
382
383
384
385 String[] systemSiteRoles = PortalUtil.getSystemSiteRoles();
386
387 for (String name : systemSiteRoles) {
388 String key =
389 "system.site.role." +
390 StringUtil.replace(name, CharPool.SPACE, CharPool.PERIOD) +
391 ".description";
392
393 Map<Locale, String> descriptionMap = new HashMap<Locale, String>();
394
395 descriptionMap.put(LocaleUtil.getDefault(), PropsUtil.get(key));
396
397 int type = RoleConstants.TYPE_SITE;
398
399 checkSystemRole(companyId, name, descriptionMap, type);
400 }
401
402
403
404 Role userRole = getRole(companyId, RoleConstants.USER);
405
406 String[] userViewableRoles = ArrayUtil.append(
407 systemRoles, systemOrganizationRoles, systemSiteRoles);
408
409 for (String roleName : userViewableRoles) {
410 Role role = getRole(companyId, roleName);
411
412 resourcePermissionLocalService.setResourcePermissions(
413 companyId, Role.class.getName(),
414 ResourceConstants.SCOPE_INDIVIDUAL,
415 String.valueOf(role.getRoleId()), userRole.getRoleId(),
416 new String[] {ActionKeys.VIEW});
417 }
418 }
419
420
429 @Override
430 public Role deleteRole(long roleId) throws PortalException {
431 Role role = rolePersistence.findByPrimaryKey(roleId);
432
433 return roleLocalService.deleteRole(role);
434 }
435
436
444 @Override
445 @SystemEvent(
446 action = SystemEventConstants.ACTION_SKIP,
447 type = SystemEventConstants.TYPE_DELETE)
448 public Role deleteRole(Role role) throws PortalException {
449 if (PortalUtil.isSystemRole(role.getName()) &&
450 !CompanyThreadLocal.isDeleteInProcess()) {
451
452 throw new RequiredRoleException();
453 }
454
455
456
457 List<ResourceBlockPermission> resourceBlockPermissions =
458 resourceBlockPermissionPersistence.findByRoleId(role.getRoleId());
459
460 for (ResourceBlockPermission resourceBlockPermission :
461 resourceBlockPermissions) {
462
463 resourceBlockPermissionLocalService.deleteResourceBlockPermission(
464 resourceBlockPermission);
465 }
466
467 List<ResourcePermission> resourcePermissions =
468 resourcePermissionPersistence.findByRoleId(role.getRoleId());
469
470 for (ResourcePermission resourcePermission : resourcePermissions) {
471 resourcePermissionLocalService.deleteResourcePermission(
472 resourcePermission);
473 }
474
475 List<ResourceTypePermission> resourceTypePermissions =
476 resourceTypePermissionPersistence.findByRoleId(role.getRoleId());
477
478 for (ResourceTypePermission resourceTypePermission :
479 resourceTypePermissions) {
480
481 resourceTypePermissionLocalService.deleteResourceTypePermission(
482 resourceTypePermission);
483 }
484
485 String className = role.getClassName();
486 long classNameId = role.getClassNameId();
487
488 if ((classNameId <= 0) || className.equals(Role.class.getName())) {
489 resourceLocalService.deleteResource(
490 role.getCompanyId(), Role.class.getName(),
491 ResourceConstants.SCOPE_INDIVIDUAL, role.getRoleId());
492 }
493
494 if ((role.getType() == RoleConstants.TYPE_ORGANIZATION) ||
495 (role.getType() == RoleConstants.TYPE_SITE)) {
496
497 userGroupRoleLocalService.deleteUserGroupRolesByRoleId(
498 role.getRoleId());
499
500 userGroupGroupRoleLocalService.deleteUserGroupGroupRolesByRoleId(
501 role.getRoleId());
502 }
503
504
505
506 rolePersistence.remove(role);
507
508
509
510 expandoRowLocalService.deleteRows(role.getRoleId());
511
512
513
514 PermissionCacheUtil.clearCache();
515
516 return role;
517 }
518
519
532 @Override
533 @Skip
534 public Role fetchRole(long companyId, String name) {
535 String companyIdHexString = StringUtil.toHexString(companyId);
536
537 Role role = _systemRolesMap.get(companyIdHexString.concat(name));
538
539 if (role != null) {
540 return role;
541 }
542
543 return roleLocalService.loadFetchRole(companyId, name);
544 }
545
546
565 @Override
566 public Role getDefaultGroupRole(long groupId) throws PortalException {
567 Group group = groupPersistence.findByPrimaryKey(groupId);
568
569 if (group.isLayout()) {
570 Layout layout = layoutLocalService.getLayout(group.getClassPK());
571
572 group = layout.getGroup();
573 }
574
575 if (group.isStagingGroup()) {
576 group = group.getLiveGroup();
577 }
578
579 Role role = null;
580
581 if (group.isCompany()) {
582 role = getRole(group.getCompanyId(), RoleConstants.USER);
583 }
584 else if (group.isLayoutPrototype() || group.isLayoutSetPrototype() ||
585 group.isRegularSite() || group.isSite()) {
586
587 role = getRole(group.getCompanyId(), RoleConstants.SITE_MEMBER);
588 }
589 else if (group.isOrganization()) {
590 role = getRole(
591 group.getCompanyId(), RoleConstants.ORGANIZATION_USER);
592 }
593 else if (group.isUser() || group.isUserGroup()) {
594 role = getRole(group.getCompanyId(), RoleConstants.POWER_USER);
595 }
596 else {
597 role = getRole(group.getCompanyId(), RoleConstants.USER);
598 }
599
600 return role;
601 }
602
603 @Override
604 public List<Role> getGroupRelatedRoles(long groupId)
605 throws PortalException {
606
607 List<Role> roles = new ArrayList<Role>();
608
609 Group group = groupLocalService.getGroup(groupId);
610
611 if (group.isStagingGroup()) {
612 group = group.getLiveGroup();
613 }
614
615 int[] types = RoleConstants.TYPES_REGULAR;
616
617 if (group.isOrganization()) {
618 types = RoleConstants.TYPES_ORGANIZATION_AND_REGULAR;
619 }
620 else if (group.isLayout() || group.isLayoutSetPrototype() ||
621 group.isSite()) {
622
623 types = RoleConstants.TYPES_REGULAR_AND_SITE;
624 }
625
626 roles.addAll(getRoles(group.getCompanyId(), types));
627
628 roles.addAll(getTeamRoles(groupId));
629
630 return roles;
631 }
632
633 @Override
634 public List<Role> getResourceBlockRoles(
635 long resourceBlockId, String className, String actionId) {
636
637 return roleFinder.findByR_N_A(resourceBlockId, className, actionId);
638 }
639
640
652 @Override
653 public Map<String, List<String>> getResourceRoles(
654 long companyId, String name, int scope, String primKey) {
655
656 return roleFinder.findByC_N_S_P(companyId, name, scope, primKey);
657 }
658
659
672 @Override
673 public List<Role> getResourceRoles(
674 long companyId, String name, int scope, String primKey,
675 String actionId) {
676
677 return roleFinder.findByC_N_S_P_A(
678 companyId, name, scope, primKey, actionId);
679 }
680
681
695 @Override
696 @Skip
697 public Role getRole(long companyId, String name) throws PortalException {
698 String companyIdHexString = StringUtil.toHexString(companyId);
699
700 Role role = _systemRolesMap.get(companyIdHexString.concat(name));
701
702 if (role != null) {
703 return role;
704 }
705
706 return roleLocalService.loadGetRole(companyId, name);
707 }
708
709
716 @Override
717 public List<Role> getRoles(int type, String subtype) {
718 return rolePersistence.findByT_S(type, subtype);
719 }
720
721
727 @Override
728 public List<Role> getRoles(long companyId) {
729 return rolePersistence.findByCompanyId(companyId);
730 }
731
732
739 @Override
740 public List<Role> getRoles(long companyId, int[] types) {
741 return rolePersistence.findByC_T(companyId, types);
742 }
743
744
752 @Override
753 public List<Role> getRoles(long[] roleIds) throws PortalException {
754 List<Role> roles = new ArrayList<Role>(roleIds.length);
755
756 for (long roleId : roleIds) {
757 Role role = getRole(roleId);
758
759 roles.add(role);
760 }
761
762 return roles;
763 }
764
765
771 @Override
772 public List<Role> getSubtypeRoles(String subtype) {
773 return rolePersistence.findBySubtype(subtype);
774 }
775
776
782 @Override
783 public int getSubtypeRolesCount(String subtype) {
784 return rolePersistence.countBySubtype(subtype);
785 }
786
787
796 @Override
797 public Role getTeamRole(long companyId, long teamId)
798 throws PortalException {
799
800 long classNameId = classNameLocalService.getClassNameId(Team.class);
801
802 return rolePersistence.findByC_C_C(companyId, classNameId, teamId);
803 }
804
805
814 @Override
815 public Map<Team, Role> getTeamRoleMap(long groupId) throws PortalException {
816 return getTeamRoleMap(groupId, null);
817 }
818
819
828 @Override
829 public List<Role> getTeamRoles(long groupId) throws PortalException {
830 return getTeamRoles(groupId, null);
831 }
832
833
844 @Override
845 public List<Role> getTeamRoles(long groupId, long[] excludedRoleIds)
846 throws PortalException {
847
848 Map<Team, Role> teamRoleMap = getTeamRoleMap(groupId, excludedRoleIds);
849
850 Collection<Role> roles = teamRoleMap.values();
851
852 return ListUtil.fromCollection(roles);
853 }
854
855
861 @Override
862 public List<Role> getTypeRoles(int type) {
863 return rolePersistence.findByType(type);
864 }
865
866
875 @Override
876 public List<Role> getTypeRoles(int type, int start, int end) {
877 return rolePersistence.findByType(type, start, end);
878 }
879
880
886 @Override
887 public int getTypeRolesCount(int type) {
888 return rolePersistence.countByType(type);
889 }
890
891
900 @Override
901 public List<Role> getUserGroupGroupRoles(long userId, long groupId) {
902 return roleFinder.findByUserGroupGroupRole(userId, groupId);
903 }
904
905
914 @Override
915 public List<Role> getUserGroupRoles(long userId, long groupId) {
916 return roleFinder.findByUserGroupRole(userId, groupId);
917 }
918
919
928 @Override
929 public List<Role> getUserRelatedRoles(long userId, List<Group> groups) {
930 if ((groups == null) || groups.isEmpty()) {
931 return Collections.emptyList();
932 }
933
934 return roleFinder.findByU_G(userId, groups);
935 }
936
937
946 @Override
947 public List<Role> getUserRelatedRoles(long userId, long groupId) {
948 return roleFinder.findByU_G(userId, groupId);
949 }
950
951
960 @Override
961 public List<Role> getUserRelatedRoles(long userId, long[] groupIds) {
962 return roleFinder.findByU_G(userId, groupIds);
963 }
964
965
979 @Override
980 @ThreadLocalCachable
981 public boolean hasUserRole(
982 long userId, long companyId, String name, boolean inherited)
983 throws PortalException {
984
985 Role role = rolePersistence.fetchByC_N(companyId, name);
986
987 if (role == null) {
988 return false;
989 }
990
991 if (role.getType() != RoleConstants.TYPE_REGULAR) {
992 throw new IllegalArgumentException(name + " is not a regular role");
993 }
994
995 long defaultUserId = userLocalService.getDefaultUserId(companyId);
996
997 if (userId == defaultUserId) {
998 if (name.equals(RoleConstants.GUEST)) {
999 return true;
1000 }
1001 else {
1002 return false;
1003 }
1004 }
1005
1006 if (inherited) {
1007 if (userPersistence.containsRole(userId, role.getRoleId())) {
1008 return true;
1009 }
1010
1011 ThreadLocalCache<Integer> threadLocalCache =
1012 ThreadLocalCacheManager.getThreadLocalCache(
1013 Lifecycle.REQUEST, RoleLocalServiceImpl.class.getName());
1014
1015 String key = String.valueOf(role.getRoleId()).concat(
1016 String.valueOf(userId));
1017
1018 Integer value = threadLocalCache.get(key);
1019
1020 if (value == null) {
1021 value = roleFinder.countByR_U(role.getRoleId(), userId);
1022
1023 threadLocalCache.put(key, value);
1024 }
1025
1026 if (value > 0) {
1027 return true;
1028 }
1029 else {
1030 return false;
1031 }
1032 }
1033 else {
1034 return userPersistence.containsRole(userId, role.getRoleId());
1035 }
1036 }
1037
1038
1053 @Override
1054 public boolean hasUserRoles(
1055 long userId, long companyId, String[] names, boolean inherited)
1056 throws PortalException {
1057
1058 for (String name : names) {
1059 if (hasUserRole(userId, companyId, name, inherited)) {
1060 return true;
1061 }
1062 }
1063
1064 return false;
1065 }
1066
1067
1075 @Override
1076 public Role loadFetchRole(long companyId, String name) {
1077 return rolePersistence.fetchByC_N(companyId, name);
1078 }
1079
1080
1089 @Override
1090 public Role loadGetRole(long companyId, String name)
1091 throws PortalException {
1092
1093 return rolePersistence.findByC_N(companyId, name);
1094 }
1095
1096
1123 @Override
1124 public List<Role> search(
1125 long companyId, String keywords, Integer[] types, int start, int end,
1126 OrderByComparator<Role> obc) {
1127
1128 return search(
1129 companyId, keywords, types, new LinkedHashMap<String, Object>(),
1130 start, end, obc);
1131 }
1132
1133
1163 @Override
1164 public List<Role> search(
1165 long companyId, String keywords, Integer[] types,
1166 LinkedHashMap<String, Object> params, int start, int end,
1167 OrderByComparator<Role> obc) {
1168
1169 return roleFinder.findByKeywords(
1170 companyId, keywords, types, params, start, end, obc);
1171 }
1172
1173
1200 @Override
1201 public List<Role> search(
1202 long companyId, String name, String description, Integer[] types,
1203 int start, int end, OrderByComparator<Role> obc) {
1204
1205 return search(
1206 companyId, name, description, types,
1207 new LinkedHashMap<String, Object>(), start, end, obc);
1208 }
1209
1210
1240 @Override
1241 public List<Role> search(
1242 long companyId, String name, String description, Integer[] types,
1243 LinkedHashMap<String, Object> params, int start, int end,
1244 OrderByComparator<Role> obc) {
1245
1246 return roleFinder.findByC_N_D_T(
1247 companyId, name, description, types, params, true, start, end, obc);
1248 }
1249
1250
1259 @Override
1260 public int searchCount(long companyId, String keywords, Integer[] types) {
1261 return searchCount(
1262 companyId, keywords, types, new LinkedHashMap<String, Object>());
1263 }
1264
1265
1276 @Override
1277 public int searchCount(
1278 long companyId, String keywords, Integer[] types,
1279 LinkedHashMap<String, Object> params) {
1280
1281 return roleFinder.countByKeywords(companyId, keywords, types, params);
1282 }
1283
1284
1293 @Override
1294 public int searchCount(
1295 long companyId, String name, String description, Integer[] types) {
1296
1297 return searchCount(
1298 companyId, name, description, types,
1299 new LinkedHashMap<String, Object>());
1300 }
1301
1302
1315 @Override
1316 public int searchCount(
1317 long companyId, String name, String description, Integer[] types,
1318 LinkedHashMap<String, Object> params) {
1319
1320 return roleFinder.countByC_N_D_T(
1321 companyId, name, description, types, params, true);
1322 }
1323
1324
1333 @Override
1334 public void setUserRoles(long userId, long[] roleIds)
1335 throws PortalException {
1336
1337 roleIds = UsersAdminUtil.addRequiredRoles(userId, roleIds);
1338
1339 userPersistence.setRoles(userId, roleIds);
1340
1341 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
1342
1343 indexer.reindex(userId);
1344
1345 PermissionCacheUtil.clearCache();
1346 }
1347
1348
1357 @Override
1358 public void unsetUserRoles(long userId, long[] roleIds)
1359 throws PortalException {
1360
1361 roleIds = UsersAdminUtil.removeRequiredRoles(userId, roleIds);
1362
1363 userPersistence.removeRoles(userId, roleIds);
1364
1365 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
1366
1367 indexer.reindex(userId);
1368
1369 PermissionCacheUtil.clearCache();
1370 }
1371
1372
1389 @Override
1390 public Role updateRole(
1391 long roleId, String name, Map<Locale, String> titleMap,
1392 Map<Locale, String> descriptionMap, String subtype,
1393 ServiceContext serviceContext)
1394 throws PortalException {
1395
1396 Role role = rolePersistence.findByPrimaryKey(roleId);
1397
1398 validate(roleId, role.getCompanyId(), role.getClassNameId(), name);
1399
1400 if (PortalUtil.isSystemRole(role.getName())) {
1401 name = role.getName();
1402 subtype = null;
1403 }
1404
1405 role.setModifiedDate(new Date());
1406 role.setName(name);
1407 role.setTitleMap(titleMap);
1408 role.setDescriptionMap(descriptionMap);
1409 role.setSubtype(subtype);
1410 role.setExpandoBridgeAttributes(serviceContext);
1411
1412 rolePersistence.update(role);
1413
1414 return role;
1415 }
1416
1417 protected void checkSystemRole(
1418 long companyId, String name, Map<Locale, String> descriptionMap,
1419 int type)
1420 throws PortalException {
1421
1422 String companyIdHexString = StringUtil.toHexString(companyId);
1423
1424 String key = companyIdHexString.concat(name);
1425
1426 Role role = _systemRolesMap.get(key);
1427
1428 try {
1429 if (role == null) {
1430 role = rolePersistence.findByC_N(companyId, name);
1431 }
1432
1433 if (!descriptionMap.equals(role.getDescriptionMap())) {
1434 role.setDescriptionMap(descriptionMap);
1435
1436 roleLocalService.updateRole(role);
1437 }
1438 }
1439 catch (NoSuchRoleException nsre) {
1440 User user = userLocalService.getDefaultUser(companyId);
1441
1442 role = roleLocalService.addRole(
1443 user.getUserId(), null, 0, name, null, descriptionMap, type,
1444 null, null);
1445
1446 if (name.equals(RoleConstants.USER)) {
1447 initPersonalControlPanelPortletsPermissions(role);
1448 }
1449 }
1450
1451 _systemRolesMap.put(key, role);
1452 }
1453
1454 protected String[] getDefaultControlPanelPortlets() {
1455 return new String[] {
1456 PortletKeys.MY_ACCOUNT, PortletKeys.MY_PAGES,
1457 PortletKeys.MY_WORKFLOW_INSTANCES, PortletKeys.MY_WORKFLOW_TASKS
1458 };
1459 }
1460
1461 protected Map<Team, Role> getTeamRoleMap(
1462 long groupId, long[] excludedRoleIds)
1463 throws PortalException {
1464
1465 Group group = groupPersistence.findByPrimaryKey(groupId);
1466
1467 if (group.isLayout()) {
1468 group = group.getParentGroup();
1469 }
1470
1471 List<Team> teams = teamPersistence.findByGroupId(group.getGroupId());
1472
1473 if (teams.isEmpty()) {
1474 return Collections.emptyMap();
1475 }
1476
1477 Set<Long> roleIds = SetUtil.fromArray(excludedRoleIds);
1478
1479 Map<Team, Role> teamRoleMap = new LinkedHashMap<Team, Role>();
1480
1481 for (Team team : teams) {
1482 Role role = getTeamRole(team.getCompanyId(), team.getTeamId());
1483
1484 if (roleIds.contains(role.getRoleId())) {
1485 continue;
1486 }
1487
1488 teamRoleMap.put(team, role);
1489 }
1490
1491 return teamRoleMap;
1492 }
1493
1494 protected void initPersonalControlPanelPortletsPermissions(Role role)
1495 throws PortalException {
1496
1497 for (String portletId : getDefaultControlPanelPortlets()) {
1498 int count = resourcePermissionPersistence.countByC_N_S_P_R(
1499 role.getCompanyId(), portletId, ResourceConstants.SCOPE_COMPANY,
1500 String.valueOf(role.getCompanyId()), role.getRoleId());
1501
1502 if (count > 0) {
1503 continue;
1504 }
1505
1506 ResourceAction resourceAction =
1507 resourceActionLocalService.fetchResourceAction(
1508 portletId, ActionKeys.ACCESS_IN_CONTROL_PANEL);
1509
1510 if (resourceAction == null) {
1511 continue;
1512 }
1513
1514 setRolePermissions(
1515 role, portletId,
1516 new String[] {
1517 ActionKeys.ACCESS_IN_CONTROL_PANEL
1518 });
1519 }
1520 }
1521
1522 protected void setRolePermissions(
1523 Role role, String name, String[] actionIds)
1524 throws PortalException {
1525
1526 if (resourceBlockLocalService.isSupported(name)) {
1527 resourceBlockLocalService.setCompanyScopePermissions(
1528 role.getCompanyId(), name, role.getRoleId(),
1529 Arrays.asList(actionIds));
1530 }
1531 else {
1532 resourcePermissionLocalService.setResourcePermissions(
1533 role.getCompanyId(), name, ResourceConstants.SCOPE_COMPANY,
1534 String.valueOf(role.getCompanyId()), role.getRoleId(),
1535 actionIds);
1536 }
1537 }
1538
1539 protected void validate(
1540 long roleId, long companyId, long classNameId, String name)
1541 throws PortalException {
1542
1543 if (classNameId == classNameLocalService.getClassNameId(Role.class)) {
1544 if (Validator.isNull(name) ||
1545 (name.indexOf(CharPool.COMMA) != -1) ||
1546 (name.indexOf(CharPool.STAR) != -1)) {
1547
1548 throw new RoleNameException();
1549 }
1550
1551 if (Validator.isNumber(name) &&
1552 !PropsValues.ROLES_NAME_ALLOW_NUMERIC) {
1553
1554 throw new RoleNameException();
1555 }
1556 }
1557
1558 try {
1559 Role role = roleFinder.findByC_N(companyId, name);
1560
1561 if (role.getRoleId() != roleId) {
1562 throw new DuplicateRoleException("{roleId=" + roleId + "}");
1563 }
1564 }
1565 catch (NoSuchRoleException nsre) {
1566 }
1567 }
1568
1569 private final Map<String, Role> _systemRolesMap =
1570 new HashMap<String, Role>();
1571
1572 }