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