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