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