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