1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.service.impl;
16  
17  import com.liferay.portal.NoSuchResourceException;
18  import com.liferay.portal.PortalException;
19  import com.liferay.portal.ResourceActionsException;
20  import com.liferay.portal.SystemException;
21  import com.liferay.portal.kernel.log.Log;
22  import com.liferay.portal.kernel.log.LogFactoryUtil;
23  import com.liferay.portal.model.Group;
24  import com.liferay.portal.model.GroupConstants;
25  import com.liferay.portal.model.Layout;
26  import com.liferay.portal.model.Permission;
27  import com.liferay.portal.model.Resource;
28  import com.liferay.portal.model.ResourceCode;
29  import com.liferay.portal.model.ResourceConstants;
30  import com.liferay.portal.model.ResourcePermission;
31  import com.liferay.portal.model.Role;
32  import com.liferay.portal.model.RoleConstants;
33  import com.liferay.portal.model.impl.ResourceImpl;
34  import com.liferay.portal.security.permission.PermissionThreadLocal;
35  import com.liferay.portal.security.permission.PermissionsListFilter;
36  import com.liferay.portal.security.permission.PermissionsListFilterFactory;
37  import com.liferay.portal.security.permission.ResourceActionsUtil;
38  import com.liferay.portal.service.base.ResourceLocalServiceBaseImpl;
39  import com.liferay.portal.util.PropsValues;
40  import com.liferay.portal.util.ResourcePermissionsThreadLocal;
41  import com.liferay.portal.util.comparator.ResourceComparator;
42  
43  import java.util.Iterator;
44  import java.util.List;
45  
46  /**
47   * <a href="ResourceLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   * @author Wilson S. Man
51   * @author Raymond Augé
52   * @author Julio Camarero
53   */
54  public class ResourceLocalServiceImpl extends ResourceLocalServiceBaseImpl {
55  
56      public void addModelResources(
57              long companyId, long groupId, long userId, String name,
58              long primKey, String[] communityPermissions,
59              String[] guestPermissions)
60          throws PortalException, SystemException {
61  
62          addModelResources(
63              companyId, groupId, userId, name, String.valueOf(primKey),
64              communityPermissions, guestPermissions);
65      }
66  
67      public void addModelResources(
68              long companyId, long groupId, long userId, String name,
69              String primKey, String[] communityPermissions,
70              String[] guestPermissions)
71          throws PortalException, SystemException {
72  
73          if (!PermissionThreadLocal.isAddResource()) {
74              return;
75          }
76  
77          validate(name, false);
78  
79          // Company
80  
81          addResource(
82              companyId, name, ResourceConstants.SCOPE_COMPANY,
83              String.valueOf(companyId));
84  
85          // Guest
86  
87          Group guestGroup = groupLocalService.getGroup(
88              companyId, GroupConstants.GUEST);
89  
90          addResource(
91              companyId, name, ResourceConstants.SCOPE_GROUP,
92              String.valueOf(guestGroup.getGroupId()));
93  
94          // Group
95  
96          if ((groupId > 0) && (guestGroup.getGroupId() != groupId)) {
97              addResource(
98                  companyId, name, ResourceConstants.SCOPE_GROUP,
99                  String.valueOf(groupId));
100         }
101 
102         if (primKey == null) {
103             return;
104         }
105 
106         // Individual
107 
108         Resource resource = addResource(
109             companyId, name, ResourceConstants.SCOPE_INDIVIDUAL, primKey);
110 
111         // Permissions
112 
113         if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
114             addModelResources_6(
115                 companyId, groupId, resource, communityPermissions,
116                 guestPermissions);
117         }
118         else {
119             addModelResources_1to5(
120                 companyId, groupId, userId, resource, communityPermissions,
121                 guestPermissions);
122         }
123     }
124 
125     public Resource addResource(
126             long companyId, String name, int scope, String primKey)
127         throws SystemException {
128 
129         if (!PermissionThreadLocal.isAddResource()) {
130             return null;
131         }
132 
133         if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
134             return addResource_6(companyId, name, scope, primKey);
135         }
136         else {
137             return addResource_1to5(companyId, name, scope, primKey);
138         }
139     }
140 
141     public void addResources(
142             long companyId, long groupId, String name, boolean portletActions)
143         throws PortalException, SystemException {
144 
145         addResources(
146             companyId, groupId, 0, name, null, portletActions, false, false);
147     }
148 
149     public void addResources(
150             long companyId, long groupId, long userId, String name,
151             long primKey, boolean portletActions,
152             boolean addCommunityPermissions, boolean addGuestPermissions)
153         throws PortalException, SystemException {
154 
155         addResources(
156             companyId, groupId, userId, name, String.valueOf(primKey),
157             portletActions, addCommunityPermissions, addGuestPermissions);
158     }
159 
160     public void addResources(
161             long companyId, long groupId, long userId, String name,
162             String primKey, boolean portletActions,
163             boolean addCommunityPermissions, boolean addGuestPermissions)
164         throws PortalException, SystemException {
165 
166         if (!PermissionThreadLocal.isAddResource()) {
167             return;
168         }
169 
170         validate(name, portletActions);
171 
172         // Company
173 
174         addResource(
175             companyId, name, ResourceConstants.SCOPE_COMPANY,
176             String.valueOf(companyId));
177 
178         if (groupId > 0) {
179             addResource(
180                 companyId, name, ResourceConstants.SCOPE_GROUP,
181                 String.valueOf(groupId));
182         }
183 
184         if (primKey == null) {
185             return;
186         }
187 
188         // Individual
189 
190         Resource resource = addResource(
191             companyId, name, ResourceConstants.SCOPE_INDIVIDUAL, primKey);
192 
193         // Permissions
194 
195         List<ResourcePermission> resourcePermissions =
196             resourcePermissionPersistence.findByC_N_S_P(
197                 companyId, name, ResourceConstants.SCOPE_INDIVIDUAL, primKey);
198 
199         ResourcePermissionsThreadLocal.setResourcePermissions(
200             resourcePermissions);
201 
202         try {
203             if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
204                 addResources_6(
205                     companyId, groupId, userId, resource, portletActions);
206             }
207             else {
208                 addResources_1to5(
209                     companyId, groupId, userId, resource, portletActions);
210             }
211 
212             // Community permissions
213 
214             if ((groupId > 0) && addCommunityPermissions) {
215                 addCommunityPermissions(
216                     companyId, groupId, userId, name, resource, portletActions);
217             }
218 
219             // Guest permissions
220 
221             if (addGuestPermissions) {
222 
223                 // Don't add guest permissions when you've already added
224                 // community permissions and the given community is the guest
225                 // community.
226 
227                 addGuestPermissions(
228                     companyId, groupId, userId, name, resource, portletActions);
229             }
230         }
231         finally {
232             ResourcePermissionsThreadLocal.setResourcePermissions(null);
233         }
234     }
235 
236     public void deleteResource(long resourceId) throws SystemException {
237         try {
238             Resource resource = resourcePersistence.findByPrimaryKey(
239                 resourceId);
240 
241             deleteResource(resource);
242         }
243         catch (NoSuchResourceException nsre) {
244             if (_log.isWarnEnabled()) {
245                 _log.warn(nsre);
246             }
247         }
248     }
249 
250     public void deleteResource(Resource resource) throws SystemException {
251 
252         // Permissions
253 
254         List<Permission> permissions = permissionPersistence.findByResourceId(
255             resource.getResourceId());
256 
257         for (Permission permission : permissions) {
258             orgGroupPermissionPersistence.removeByPermissionId(
259                 permission.getPermissionId());
260         }
261 
262         permissionPersistence.removeByResourceId(resource.getResourceId());
263 
264         // Resource
265 
266         resourcePersistence.remove(resource);
267     }
268 
269     public void deleteResource(
270             long companyId, String name, int scope, long primKey)
271         throws PortalException, SystemException {
272 
273         deleteResource(companyId, name, scope, String.valueOf(primKey));
274     }
275 
276     public void deleteResource(
277             long companyId, String name, int scope, String primKey)
278         throws PortalException, SystemException {
279 
280         if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
281             return;
282         }
283 
284         try {
285             Resource resource = getResource(companyId, name, scope, primKey);
286 
287             deleteResource(resource.getResourceId());
288         }
289         catch (NoSuchResourceException nsre) {
290             if (_log.isWarnEnabled()) {
291                 _log.warn(nsre);
292             }
293         }
294     }
295 
296     public void deleteResources(String name) throws SystemException {
297         List<Resource> resources = resourceFinder.findByName(name);
298 
299         for (Resource resource : resources) {
300             deleteResource(resource);
301         }
302     }
303 
304     public long getLatestResourceId() throws SystemException {
305         List<Resource> resources = resourcePersistence.findAll(
306             0, 1, new ResourceComparator());
307 
308         if (resources.size() == 0) {
309             return 0;
310         }
311         else {
312             Resource resource = resources.get(0);
313 
314             return resource.getResourceId();
315         }
316     }
317 
318     public Resource getResource(long resourceId)
319         throws PortalException, SystemException {
320 
321         return resourcePersistence.findByPrimaryKey(resourceId);
322     }
323 
324     public Resource getResource(
325             long companyId, String name, int scope, String primKey)
326         throws PortalException, SystemException {
327 
328         if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
329             return getResource_6(companyId, name, scope, primKey);
330         }
331         else {
332             return getResource_1to5(companyId, name, scope, primKey);
333         }
334     }
335 
336     public List<Resource> getResources() throws SystemException {
337         return resourcePersistence.findAll();
338     }
339 
340     public void updateResources(
341             long companyId, long groupId, String name, long primKey,
342             String[] communityPermissions, String[] guestPermissions)
343         throws PortalException, SystemException {
344 
345         updateResources(
346             companyId, groupId, name, String.valueOf(primKey),
347             communityPermissions, guestPermissions);
348     }
349 
350     public void updateResources(
351             long companyId, long groupId, String name, String primKey,
352             String[] communityPermissions, String[] guestPermissions)
353         throws PortalException, SystemException {
354 
355         Resource resource = getResource(
356             companyId, name, ResourceConstants.SCOPE_INDIVIDUAL, primKey);
357 
358         if (communityPermissions == null) {
359             communityPermissions = new String[0];
360         }
361 
362         if (guestPermissions == null) {
363             guestPermissions = new String[0];
364         }
365 
366         if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
367             updateResources_6(
368                 companyId, groupId, resource, communityPermissions,
369                 guestPermissions);
370         }
371         else {
372             updateResources_1to5(
373                 companyId, groupId, resource, communityPermissions,
374                 guestPermissions);
375         }
376     }
377 
378     protected void addCommunityPermissions(
379             long companyId, long groupId, long userId, String name,
380             Resource resource, boolean portletActions)
381         throws PortalException, SystemException {
382 
383         List<String> actions = null;
384 
385         if (portletActions) {
386             actions =
387                 ResourceActionsUtil.getPortletResourceCommunityDefaultActions(
388                     name);
389         }
390         else {
391             actions =
392                 ResourceActionsUtil.getModelResourceCommunityDefaultActions(
393                     name);
394         }
395 
396         String[] actionIds = actions.toArray(new String[actions.size()]);
397 
398         if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
399             addCommunityPermissions_6(groupId, resource, actionIds);
400         }
401         else {
402             addCommunityPermissions_1to5(
403                 companyId, groupId, userId, name, resource, portletActions,
404                 actionIds);
405         }
406     }
407 
408     protected void addCommunityPermissions_1to5(
409             long companyId, long groupId, long userId, String name,
410             Resource resource, boolean portletActions, String[] actionIds)
411         throws PortalException, SystemException {
412 
413         long resourceId = resource.getResourceId();
414         String primKey = resource.getPrimKey();
415 
416         List<Permission> communityPermissionsList =
417             permissionLocalService.getPermissions(
418                 companyId, actionIds, resourceId);
419 
420         PermissionsListFilter permissionsListFilter =
421             PermissionsListFilterFactory.getInstance();
422 
423         communityPermissionsList =
424             permissionsListFilter.filterCommunityPermissions(
425                 companyId, groupId, userId, name, primKey, portletActions,
426                 communityPermissionsList);
427 
428         if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 5) {
429             Role role = getRole(groupId);
430 
431             rolePersistence.addPermissions(
432                 role.getRoleId(), communityPermissionsList);
433         }
434         else {
435             groupPersistence.addPermissions(groupId, communityPermissionsList);
436         }
437     }
438 
439     protected void addCommunityPermissions_6(
440             long groupId, Resource resource, String[] actionIds)
441         throws PortalException, SystemException {
442 
443         Role role = getRole(groupId);
444 
445         resourcePermissionLocalService.setResourcePermissions(
446             resource.getCompanyId(), resource.getName(), resource.getScope(),
447             resource.getPrimKey(), role.getRoleId(), actionIds);
448     }
449 
450     protected void addGuestPermissions(
451             long companyId, long groupId, long userId, String name,
452             Resource resource, boolean portletActions)
453         throws PortalException, SystemException {
454 
455         List<String> actions = null;
456 
457         if (portletActions) {
458             actions = ResourceActionsUtil.getPortletResourceGuestDefaultActions(
459                 name);
460         }
461         else {
462             actions = ResourceActionsUtil.getModelResourceGuestDefaultActions(
463                 name);
464         }
465 
466         String[] actionIds = actions.toArray(new String[actions.size()]);
467 
468         if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
469             addGuestPermissions_6(companyId, resource, actionIds);
470         }
471         else {
472             addGuestPermissions_1to5(
473                 companyId, groupId, userId, name, resource, portletActions,
474                 actionIds);
475         }
476     }
477 
478     protected void addGuestPermissions_1to5(
479             long companyId, long groupId, long userId, String name,
480             Resource resource, boolean portletActions, String[] actionIds)
481         throws PortalException, SystemException {
482 
483         List<Permission> guestPermissionsList =
484             permissionLocalService.getPermissions(
485                 companyId, actionIds, resource.getResourceId());
486 
487         PermissionsListFilter permissionsListFilter =
488             PermissionsListFilterFactory.getInstance();
489 
490         guestPermissionsList =
491             permissionsListFilter.filterGuestPermissions(
492                 companyId, groupId, userId, name, resource.getPrimKey(),
493                 portletActions, guestPermissionsList);
494 
495         if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 5) {
496             Role guestRole = roleLocalService.getRole(
497                 companyId, RoleConstants.GUEST);
498 
499             rolePersistence.addPermissions(
500                 guestRole.getRoleId(), guestPermissionsList);
501         }
502         else {
503             long defaultUserId = userLocalService.getDefaultUserId(companyId);
504 
505             userPersistence.addPermissions(defaultUserId, guestPermissionsList);
506         }
507     }
508 
509     protected void addGuestPermissions_6(
510             long companyId, Resource resource, String[] actionIds)
511         throws PortalException, SystemException {
512 
513         Role guestRole = roleLocalService.getRole(
514             companyId, RoleConstants.GUEST);
515 
516         resourcePermissionLocalService.setResourcePermissions(
517             resource.getCompanyId(), resource.getName(), resource.getScope(),
518             resource.getPrimKey(), guestRole.getRoleId(), actionIds);
519     }
520 
521     protected void addModelResources_1to5(
522             long companyId, long groupId, long userId, Resource resource,
523             String[] communityPermissions, String[] guestPermissions)
524         throws PortalException, SystemException {
525 
526         long defaultUserId = userLocalService.getDefaultUserId(companyId);
527 
528         PermissionsListFilter permissionsListFilter =
529             PermissionsListFilterFactory.getInstance();
530 
531         List<Permission> permissionsList =
532             permissionLocalService.addPermissions(
533                 companyId, resource.getName(), resource.getResourceId(), false);
534 
535         List<Permission> userPermissionsList =
536             permissionsListFilter.filterUserPermissions(
537                 companyId, groupId, userId, resource.getName(),
538                 resource.getPrimKey(), false, permissionsList);
539 
540         filterOwnerPermissions(resource.getName(), userPermissionsList);
541 
542         if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 5) {
543 
544             // Owner permissions
545 
546             Role ownerRole = roleLocalService.getRole(
547                 companyId, RoleConstants.OWNER);
548 
549             rolePersistence.addPermissions(
550                 ownerRole.getRoleId(), userPermissionsList);
551         }
552         else {
553 
554             // User permissions
555 
556             if ((userId > 0) && (userId != defaultUserId)) {
557                 userPersistence.addPermissions(userId, userPermissionsList);
558             }
559         }
560 
561         // Community permissions
562 
563         if (groupId > 0) {
564             if (communityPermissions == null) {
565                 communityPermissions = new String[0];
566             }
567 
568             List<Permission> communityPermissionsList =
569                 permissionLocalService.getPermissions(
570                     companyId, communityPermissions, resource.getResourceId());
571 
572             communityPermissionsList =
573                 permissionsListFilter.filterCommunityPermissions(
574                     companyId, groupId, userId, resource.getName(),
575                     resource.getPrimKey(), false, communityPermissionsList);
576 
577             if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 5) {
578                 Role role = getRole(groupId);
579 
580                 rolePersistence.addPermissions(
581                     role.getRoleId(), communityPermissionsList);
582             }
583             else {
584                 groupPersistence.addPermissions(
585                     groupId, communityPermissionsList);
586             }
587         }
588 
589         // Guest permissions
590 
591         if (guestPermissions == null) {
592             guestPermissions = new String[0];
593         }
594 
595         List<Permission> guestPermissionsList =
596             permissionLocalService.getPermissions(
597                 companyId, guestPermissions, resource.getResourceId());
598 
599         guestPermissionsList = permissionsListFilter.filterGuestPermissions(
600             companyId, groupId, userId, resource.getName(),
601             resource.getPrimKey(), false, guestPermissionsList);
602 
603         if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 5) {
604             Role guestRole = roleLocalService.getRole(
605                 companyId, RoleConstants.GUEST);
606 
607             rolePersistence.addPermissions(
608                 guestRole.getRoleId(), guestPermissionsList);
609         }
610         else {
611             userPersistence.addPermissions(defaultUserId, guestPermissionsList);
612         }
613     }
614 
615     protected void addModelResources_6(
616             long companyId, long groupId, Resource resource,
617             String[] communityPermissions, String[] guestPermissions)
618         throws PortalException, SystemException {
619 
620         // Owner permissions
621 
622         Role ownerRole = roleLocalService.getRole(
623             companyId, RoleConstants.OWNER);
624 
625         List<String> actionIds = ResourceActionsUtil.getModelResourceActions(
626             resource.getName());
627 
628         filterOwnerActions(resource.getName(), actionIds);
629 
630         resourcePermissionLocalService.setResourcePermissions(
631             resource.getCompanyId(), resource.getName(), resource.getScope(),
632             resource.getPrimKey(), ownerRole.getRoleId(),
633             actionIds.toArray(new String[actionIds.size()]));
634 
635         // Community permissions
636 
637         if (groupId > 0) {
638             Role role = getRole(groupId);
639 
640             if (communityPermissions == null) {
641                 communityPermissions = new String[0];
642             }
643 
644             resourcePermissionLocalService.setResourcePermissions(
645                 resource.getCompanyId(), resource.getName(),
646                 resource.getScope(), resource.getPrimKey(), role.getRoleId(),
647                 communityPermissions);
648         }
649 
650         // Guest permissions
651 
652         Role guestRole = roleLocalService.getRole(
653             companyId, RoleConstants.GUEST);
654 
655         if (guestPermissions == null) {
656             guestPermissions = new String[0];
657         }
658 
659         resourcePermissionLocalService.setResourcePermissions(
660             resource.getCompanyId(), resource.getName(), resource.getScope(),
661             resource.getPrimKey(), guestRole.getRoleId(), guestPermissions);
662     }
663 
664     protected Resource addResource_1to5(
665             long companyId, String name, int scope, String primKey)
666         throws SystemException {
667 
668         ResourceCode resourceCode = resourceCodeLocalService.getResourceCode(
669             companyId, name, scope);
670 
671         long codeId = resourceCode.getCodeId();
672 
673         Resource resource = resourcePersistence.fetchByC_P(codeId, primKey);
674 
675         if (resource == null) {
676             long resourceId = counterLocalService.increment(
677                 Resource.class.getName());
678 
679             resource = resourcePersistence.create(resourceId);
680 
681             resource.setCodeId(codeId);
682             resource.setPrimKey(primKey);
683 
684             try {
685                 resourcePersistence.update(resource, false);
686             }
687             catch (SystemException se) {
688                 if (_log.isWarnEnabled()) {
689                     _log.warn(
690                         "Add failed, fetch {codeId=" + codeId + ", primKey=" +
691                             primKey + "}");
692                 }
693 
694                 resource = resourcePersistence.fetchByC_P(
695                     codeId, primKey, false);
696 
697                 if (resource == null) {
698                     throw se;
699                 }
700             }
701         }
702 
703         return resource;
704     }
705 
706     protected Resource addResource_6(
707         long companyId, String name, int scope, String primKey) {
708 
709         Resource resource = new ResourceImpl();
710 
711         resource.setCompanyId(companyId);
712         resource.setName(name);
713         resource.setScope(scope);
714         resource.setPrimKey(primKey);
715 
716         return resource;
717     }
718 
719     protected void addResources_1to5(
720             long companyId, long groupId, long userId, Resource resource,
721             boolean portletActions)
722         throws PortalException, SystemException {
723 
724         List<Permission> permissionsList =
725             permissionLocalService.addPermissions(
726                 companyId, resource.getName(), resource.getResourceId(),
727                 portletActions);
728 
729         PermissionsListFilter permissionsListFilter =
730             PermissionsListFilterFactory.getInstance();
731 
732         List<Permission> userPermissionsList =
733             permissionsListFilter.filterUserPermissions(
734                 companyId, groupId, userId, resource.getName(),
735                 resource.getPrimKey(), portletActions, permissionsList);
736 
737         filterOwnerPermissions(resource.getName(), userPermissionsList);
738 
739         if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 5) {
740 
741             // Owner permissions
742 
743             Role ownerRole = roleLocalService.getRole(
744                 companyId, RoleConstants.OWNER);
745 
746             rolePersistence.addPermissions(
747                 ownerRole.getRoleId(), userPermissionsList);
748         }
749         else {
750 
751             // User permissions
752 
753             long defaultUserId = userLocalService.getDefaultUserId(companyId);
754 
755             if ((userId > 0) && (userId != defaultUserId)) {
756                 userPersistence.addPermissions(userId, userPermissionsList);
757             }
758         }
759     }
760 
761     protected void addResources_6(
762             long companyId, long groupId, long userId, Resource resource,
763             boolean portletActions)
764         throws PortalException, SystemException {
765 
766         List<String> actionIds = null;
767 
768         if (portletActions) {
769             actionIds = ResourceActionsUtil.getPortletResourceActions(
770                 resource.getName());
771         }
772         else {
773             actionIds = ResourceActionsUtil.getModelResourceActions(
774                 resource.getName());
775 
776             filterOwnerActions(resource.getName(), actionIds);
777         }
778 
779         Role role = roleLocalService.getRole(companyId, RoleConstants.OWNER);
780 
781         resourcePermissionLocalService.setResourcePermissions(
782             resource.getCompanyId(), resource.getName(), resource.getScope(),
783             resource.getPrimKey(), role.getRoleId(),
784             actionIds.toArray(new String[actionIds.size()]));
785     }
786 
787     protected void filterOwnerActions(String name, List<String> actionIds) {
788         List<String> defaultOwnerActions =
789             ResourceActionsUtil.getModelResourceOwnerDefaultActions(name);
790 
791         if (defaultOwnerActions.isEmpty()) {
792             return;
793         }
794 
795         Iterator<String> itr = actionIds.iterator();
796 
797         while (itr.hasNext()) {
798             String actionId = itr.next();
799 
800             if (!defaultOwnerActions.contains(actionId)) {
801                 itr.remove();
802             }
803         }
804     }
805 
806     protected void filterOwnerPermissions(
807         String name, List<Permission> permissions) {
808 
809         List<String> defaultOwnerActions =
810             ResourceActionsUtil.getModelResourceOwnerDefaultActions(name);
811 
812         if (defaultOwnerActions.isEmpty()) {
813             return;
814         }
815 
816         Iterator<Permission> itr = permissions.iterator();
817 
818         while (itr.hasNext()) {
819             Permission permission = itr.next();
820 
821             String actionId = permission.getActionId();
822 
823             if (!defaultOwnerActions.contains(actionId)) {
824                 itr.remove();
825             }
826         }
827     }
828 
829     protected Resource getResource_1to5(
830             long companyId, String name, int scope, String primKey)
831         throws PortalException, SystemException {
832 
833         ResourceCode resourceCode = resourceCodeLocalService.getResourceCode(
834             companyId, name, scope);
835 
836         return resourcePersistence.findByC_P(resourceCode.getCodeId(), primKey);
837     }
838 
839     protected Resource getResource_6(
840         long companyId, String name, int scope, String primKey) {
841 
842         Resource resource = new ResourceImpl();
843 
844         resource.setCompanyId(companyId);
845         resource.setName(name);
846         resource.setScope(scope);
847         resource.setPrimKey(primKey);
848 
849         return resource;
850     }
851 
852     protected Role getRole(long groupId)
853         throws PortalException, SystemException {
854 
855         Group group = groupPersistence.findByPrimaryKey(groupId);
856 
857         if (group.isLayout()) {
858             Layout layout = layoutLocalService.getLayout(
859                 group.getClassPK());
860 
861             group = layout.getGroup();
862         }
863 
864         Role role = null;
865 
866         if (group.isCommunity()) {
867             role = roleLocalService.getRole(
868                 group.getCompanyId(), RoleConstants.COMMUNITY_MEMBER);
869         }
870         else if (group.isOrganization()) {
871             role = roleLocalService.getRole(
872                 group.getCompanyId(), RoleConstants.ORGANIZATION_MEMBER);
873         }
874         else if (group.isUser() || group.isUserGroup()) {
875             role = roleLocalService.getRole(
876                 group.getCompanyId(), RoleConstants.POWER_USER);
877         }
878 
879         return role;
880     }
881 
882     protected void updateResources_1to5(
883             long companyId, long groupId, Resource resource,
884             String[] communityPermissions, String[] guestPermissions)
885         throws PortalException, SystemException {
886 
887         Role role = getRole(groupId);
888 
889         permissionLocalService.setRolePermissions(
890             role.getRoleId(), communityPermissions, resource.getResourceId());
891 
892         role = roleLocalService.getRole(companyId, RoleConstants.GUEST);
893 
894         permissionLocalService.setRolePermissions(
895             role.getRoleId(), guestPermissions, resource.getResourceId());
896     }
897 
898     protected void updateResources_6(
899             long companyId, long groupId, Resource resource,
900             String[] communityPermissions, String[] guestPermissions)
901         throws PortalException, SystemException {
902 
903         Role role = getRole(groupId);
904 
905         resourcePermissionLocalService.setResourcePermissions(
906             resource.getCompanyId(), resource.getName(), resource.getScope(),
907             resource.getPrimKey(), role.getRoleId(), communityPermissions);
908 
909         role = roleLocalService.getRole(companyId, RoleConstants.GUEST);
910 
911         resourcePermissionLocalService.setResourcePermissions(
912             resource.getCompanyId(), resource.getName(), resource.getScope(),
913             resource.getPrimKey(), role.getRoleId(), guestPermissions);
914     }
915 
916     protected void validate(String name, boolean portletActions)
917         throws PortalException {
918 
919         List<String> actions = null;
920 
921         if (portletActions) {
922             actions = ResourceActionsUtil.getPortletResourceActions(name);
923         }
924         else {
925             actions = ResourceActionsUtil.getModelResourceActions(name);
926         }
927 
928         if (actions.size() == 0) {
929             throw new ResourceActionsException(
930                 "There are no actions associated with the resource " + name);
931         }
932     }
933 
934     private static Log _log = LogFactoryUtil.getLog(
935         ResourceLocalServiceImpl.class);
936 
937 }