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