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