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