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