001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.impl;
016    
017    import com.liferay.portal.NoSuchResourceException;
018    import com.liferay.portal.ResourceActionsException;
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.exception.SystemException;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.model.Group;
024    import com.liferay.portal.model.GroupConstants;
025    import com.liferay.portal.model.Permission;
026    import com.liferay.portal.model.Resource;
027    import com.liferay.portal.model.ResourceCode;
028    import com.liferay.portal.model.ResourceConstants;
029    import com.liferay.portal.model.Role;
030    import com.liferay.portal.model.RoleConstants;
031    import com.liferay.portal.model.impl.ResourceImpl;
032    import com.liferay.portal.security.permission.PermissionThreadLocal;
033    import com.liferay.portal.security.permission.PermissionsListFilter;
034    import com.liferay.portal.security.permission.PermissionsListFilterFactory;
035    import com.liferay.portal.security.permission.ResourceActionsUtil;
036    import com.liferay.portal.service.base.ResourceLocalServiceBaseImpl;
037    import com.liferay.portal.util.PropsValues;
038    import com.liferay.portal.util.comparator.ResourceComparator;
039    
040    import java.util.Iterator;
041    import java.util.List;
042    
043    /**
044     * @author Brian Wing Shun Chan
045     * @author Wilson S. Man
046     * @author Raymond Augé
047     * @author Julio Camarero
048     */
049    public class ResourceLocalServiceImpl extends ResourceLocalServiceBaseImpl {
050    
051            public void addModelResources(
052                            long companyId, long groupId, long userId, String name,
053                            long primKey, String[] communityPermissions,
054                            String[] guestPermissions)
055                    throws PortalException, SystemException {
056    
057                    addModelResources(
058                            companyId, groupId, userId, name, String.valueOf(primKey),
059                            communityPermissions, guestPermissions);
060            }
061    
062            public void addModelResources(
063                            long companyId, long groupId, long userId, String name,
064                            String primKey, String[] communityPermissions,
065                            String[] guestPermissions)
066                    throws PortalException, SystemException {
067    
068                    if (!PermissionThreadLocal.isAddResource()) {
069                            return;
070                    }
071    
072                    validate(name, false);
073    
074                    // Company
075    
076                    addResource(
077                            companyId, name, ResourceConstants.SCOPE_COMPANY,
078                            String.valueOf(companyId));
079    
080                    // Guest
081    
082                    Group guestGroup = groupLocalService.getGroup(
083                            companyId, GroupConstants.GUEST);
084    
085                    addResource(
086                            companyId, name, ResourceConstants.SCOPE_GROUP,
087                            String.valueOf(guestGroup.getGroupId()));
088    
089                    // Group
090    
091                    if ((groupId > 0) && (guestGroup.getGroupId() != groupId)) {
092                            addResource(
093                                    companyId, name, ResourceConstants.SCOPE_GROUP,
094                                    String.valueOf(groupId));
095                    }
096    
097                    if (primKey == null) {
098                            return;
099                    }
100    
101                    // Individual
102    
103                    Resource resource = addResource(
104                            companyId, name, ResourceConstants.SCOPE_INDIVIDUAL, primKey);
105    
106                    // Permissions
107    
108                    if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
109                            addModelResources_6(
110                                    companyId, groupId, resource, communityPermissions,
111                                    guestPermissions);
112                    }
113                    else {
114                            addModelResources_1to5(
115                                    companyId, groupId, userId, resource, communityPermissions,
116                                    guestPermissions);
117                    }
118            }
119    
120            public Resource addResource(
121                            long companyId, String name, int scope, String primKey)
122                    throws SystemException {
123    
124                    if (!PermissionThreadLocal.isAddResource()) {
125                            return null;
126                    }
127    
128                    if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
129                            return addResource_6(companyId, name, scope, primKey);
130                    }
131                    else {
132                            return addResource_1to5(companyId, name, scope, primKey);
133                    }
134            }
135    
136            public void addResources(
137                            long companyId, long groupId, String name, boolean portletActions)
138                    throws PortalException, SystemException {
139    
140                    addResources(
141                            companyId, groupId, 0, name, null, portletActions, false, false);
142            }
143    
144            public void addResources(
145                            long companyId, long groupId, long userId, String name,
146                            long primKey, boolean portletActions,
147                            boolean addCommunityPermissions, boolean addGuestPermissions)
148                    throws PortalException, SystemException {
149    
150                    addResources(
151                            companyId, groupId, userId, name, String.valueOf(primKey),
152                            portletActions, addCommunityPermissions, addGuestPermissions);
153            }
154    
155            public void addResources(
156                            long companyId, long groupId, long userId, String name,
157                            String primKey, boolean portletActions,
158                            boolean addCommunityPermissions, boolean addGuestPermissions)
159                    throws PortalException, SystemException {
160    
161                    if (!PermissionThreadLocal.isAddResource()) {
162                            return;
163                    }
164    
165                    validate(name, portletActions);
166    
167                    // Company
168    
169                    addResource(
170                            companyId, name, ResourceConstants.SCOPE_COMPANY,
171                            String.valueOf(companyId));
172    
173                    if (groupId > 0) {
174                            addResource(
175                                    companyId, name, ResourceConstants.SCOPE_GROUP,
176                                    String.valueOf(groupId));
177                    }
178    
179                    if (primKey == null) {
180                            return;
181                    }
182    
183                    // Individual
184    
185                    Resource resource = addResource(
186                            companyId, name, ResourceConstants.SCOPE_INDIVIDUAL, primKey);
187    
188                    // Permissions
189    
190                    if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
191                            addResources_6(
192                                    companyId, groupId, userId, resource, portletActions);
193                    }
194                    else {
195                            addResources_1to5(
196                                    companyId, groupId, userId, resource, portletActions);
197                    }
198    
199                    // Community permissions
200    
201                    if ((groupId > 0) && addCommunityPermissions) {
202                            addCommunityPermissions(
203                                    companyId, groupId, userId, name, resource, portletActions);
204                    }
205    
206                    // Guest permissions
207    
208                    if (addGuestPermissions) {
209    
210                            // Don't add guest permissions when you've already added community
211                            // permissions and the given community is the guest community.
212    
213                            addGuestPermissions(
214                                    companyId, groupId, userId, name, resource, portletActions);
215                    }
216            }
217    
218            public void deleteResource(long resourceId) throws SystemException {
219                    try {
220                            Resource resource = resourcePersistence.findByPrimaryKey(
221                                    resourceId);
222    
223                            deleteResource(resource);
224                    }
225                    catch (NoSuchResourceException nsre) {
226                            if (_log.isWarnEnabled()) {
227                                    _log.warn(nsre);
228                            }
229                    }
230            }
231    
232            public void deleteResource(Resource resource) throws SystemException {
233    
234                    // Permissions
235    
236                    List<Permission> permissions = permissionPersistence.findByResourceId(
237                            resource.getResourceId());
238    
239                    for (Permission permission : permissions) {
240                            orgGroupPermissionPersistence.removeByPermissionId(
241                                    permission.getPermissionId());
242                    }
243    
244                    permissionPersistence.removeByResourceId(resource.getResourceId());
245    
246                    // Resource
247    
248                    resourcePersistence.remove(resource);
249            }
250    
251            public void deleteResource(
252                            long companyId, String name, int scope, long primKey)
253                    throws PortalException, SystemException {
254    
255                    deleteResource(companyId, name, scope, String.valueOf(primKey));
256            }
257    
258            public void deleteResource(
259                            long companyId, String name, int scope, String primKey)
260                    throws PortalException, SystemException {
261    
262                    if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
263                            return;
264                    }
265    
266                    try {
267                            Resource resource = getResource(companyId, name, scope, primKey);
268    
269                            deleteResource(resource.getResourceId());
270                    }
271                    catch (NoSuchResourceException nsre) {
272                            if (_log.isWarnEnabled()) {
273                                    _log.warn(nsre);
274                            }
275                    }
276            }
277    
278            public void deleteResources(String name) throws SystemException {
279                    List<Resource> resources = resourceFinder.findByName(name);
280    
281                    for (Resource resource : resources) {
282                            deleteResource(resource);
283                    }
284            }
285    
286            public long getLatestResourceId() throws SystemException {
287                    List<Resource> resources = resourcePersistence.findAll(
288                            0, 1, new ResourceComparator());
289    
290                    if (resources.size() == 0) {
291                            return 0;
292                    }
293                    else {
294                            Resource resource = resources.get(0);
295    
296                            return resource.getResourceId();
297                    }
298            }
299    
300            public Resource getResource(long resourceId)
301                    throws PortalException, SystemException {
302    
303                    return resourcePersistence.findByPrimaryKey(resourceId);
304            }
305    
306            public Resource getResource(
307                            long companyId, String name, int scope, String primKey)
308                    throws PortalException, SystemException {
309    
310                    if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
311                            return getResource_6(companyId, name, scope, primKey);
312                    }
313                    else {
314                            return getResource_1to5(companyId, name, scope, primKey);
315                    }
316            }
317    
318            public List<Resource> getResources() throws SystemException {
319                    return resourcePersistence.findAll();
320            }
321    
322            public void updateResources(
323                            long companyId, long groupId, String name, long primKey,
324                            String[] communityPermissions, String[] guestPermissions)
325                    throws PortalException, SystemException {
326    
327                    updateResources(
328                            companyId, groupId, name, String.valueOf(primKey),
329                            communityPermissions, guestPermissions);
330            }
331    
332            public void updateResources(
333                            long companyId, long groupId, String name, String primKey,
334                            String[] communityPermissions, String[] guestPermissions)
335                    throws PortalException, SystemException {
336    
337                    Resource resource = getResource(
338                            companyId, name, ResourceConstants.SCOPE_INDIVIDUAL, primKey);
339    
340                    if (communityPermissions == null) {
341                            communityPermissions = new String[0];
342                    }
343    
344                    if (guestPermissions == null) {
345                            guestPermissions = new String[0];
346                    }
347    
348                    if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
349                            updateResources_6(
350                                    companyId, groupId, resource, communityPermissions,
351                                    guestPermissions);
352                    }
353                    else {
354                            updateResources_1to5(
355                                    companyId, groupId, resource, communityPermissions,
356                                    guestPermissions);
357                    }
358            }
359    
360            protected void addCommunityPermissions(
361                            long companyId, long groupId, long userId, String name,
362                            Resource resource, boolean portletActions)
363                    throws PortalException, SystemException {
364    
365                    List<String> actions = null;
366    
367                    if (portletActions) {
368                            actions =
369                                    ResourceActionsUtil.getPortletResourceCommunityDefaultActions(
370                                            name);
371                    }
372                    else {
373                            actions =
374                                    ResourceActionsUtil.getModelResourceCommunityDefaultActions(
375                                            name);
376                    }
377    
378                    String[] actionIds = actions.toArray(new String[actions.size()]);
379    
380                    if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
381                            addCommunityPermissions_6(groupId, resource, actionIds);
382                    }
383                    else {
384                            addCommunityPermissions_1to5(
385                                    companyId, groupId, userId, name, resource, portletActions,
386                                    actionIds);
387                    }
388            }
389    
390            protected void addCommunityPermissions_1to5(
391                            long companyId, long groupId, long userId, String name,
392                            Resource resource, boolean portletActions, String[] actionIds)
393                    throws PortalException, SystemException {
394    
395                    long resourceId = resource.getResourceId();
396                    String primKey = resource.getPrimKey();
397    
398                    List<Permission> communityPermissionsList =
399                            permissionLocalService.getPermissions(
400                                    companyId, actionIds, resourceId);
401    
402                    PermissionsListFilter permissionsListFilter =
403                            PermissionsListFilterFactory.getInstance();
404    
405                    communityPermissionsList =
406                            permissionsListFilter.filterCommunityPermissions(
407                                    companyId, groupId, userId, name, primKey, portletActions,
408                                    communityPermissionsList);
409    
410                    if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 5) {
411                            Role role = roleLocalService.getDefaultGroupRole(groupId);
412    
413                            rolePersistence.addPermissions(
414                                    role.getRoleId(), communityPermissionsList);
415                    }
416                    else {
417                            groupPersistence.addPermissions(groupId, communityPermissionsList);
418                    }
419            }
420    
421            protected void addCommunityPermissions_6(
422                            long groupId, Resource resource, String[] actionIds)
423                    throws PortalException, SystemException {
424    
425                    Role role = roleLocalService.getDefaultGroupRole(groupId);
426    
427                    resourcePermissionLocalService.setResourcePermissions(
428                            resource.getCompanyId(), resource.getName(), resource.getScope(),
429                            resource.getPrimKey(), role.getRoleId(), actionIds);
430            }
431    
432            protected void addGuestPermissions(
433                            long companyId, long groupId, long userId, String name,
434                            Resource resource, boolean portletActions)
435                    throws PortalException, SystemException {
436    
437                    List<String> actions = null;
438    
439                    if (portletActions) {
440                            actions = ResourceActionsUtil.getPortletResourceGuestDefaultActions(
441                                    name);
442                    }
443                    else {
444                            actions = ResourceActionsUtil.getModelResourceGuestDefaultActions(
445                                    name);
446                    }
447    
448                    String[] actionIds = actions.toArray(new String[actions.size()]);
449    
450                    if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
451                            addGuestPermissions_6(companyId, resource, actionIds);
452                    }
453                    else {
454                            addGuestPermissions_1to5(
455                                    companyId, groupId, userId, name, resource, portletActions,
456                                    actionIds);
457                    }
458            }
459    
460            protected void addGuestPermissions_1to5(
461                            long companyId, long groupId, long userId, String name,
462                            Resource resource, boolean portletActions, String[] actionIds)
463                    throws PortalException, SystemException {
464    
465                    List<Permission> guestPermissionsList =
466                            permissionLocalService.getPermissions(
467                                    companyId, actionIds, resource.getResourceId());
468    
469                    PermissionsListFilter permissionsListFilter =
470                            PermissionsListFilterFactory.getInstance();
471    
472                    guestPermissionsList =
473                            permissionsListFilter.filterGuestPermissions(
474                                    companyId, groupId, userId, name, resource.getPrimKey(),
475                                    portletActions, guestPermissionsList);
476    
477                    if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 5) {
478                            Role guestRole = roleLocalService.getRole(
479                                    companyId, RoleConstants.GUEST);
480    
481                            rolePersistence.addPermissions(
482                                    guestRole.getRoleId(), guestPermissionsList);
483                    }
484                    else {
485                            long defaultUserId = userLocalService.getDefaultUserId(companyId);
486    
487                            userPersistence.addPermissions(defaultUserId, guestPermissionsList);
488                    }
489            }
490    
491            protected void addGuestPermissions_6(
492                            long companyId, Resource resource, String[] actionIds)
493                    throws PortalException, SystemException {
494    
495                    Role guestRole = roleLocalService.getRole(
496                            companyId, RoleConstants.GUEST);
497    
498                    resourcePermissionLocalService.setResourcePermissions(
499                            resource.getCompanyId(), resource.getName(), resource.getScope(),
500                            resource.getPrimKey(), guestRole.getRoleId(), actionIds);
501            }
502    
503            protected void addModelResources_1to5(
504                            long companyId, long groupId, long userId, Resource resource,
505                            String[] communityPermissions, String[] guestPermissions)
506                    throws PortalException, SystemException {
507    
508                    long defaultUserId = userLocalService.getDefaultUserId(companyId);
509    
510                    PermissionsListFilter permissionsListFilter =
511                            PermissionsListFilterFactory.getInstance();
512    
513                    List<Permission> permissionsList =
514                            permissionLocalService.addPermissions(
515                                    companyId, resource.getName(), resource.getResourceId(), false);
516    
517                    List<Permission> userPermissionsList =
518                            permissionsListFilter.filterUserPermissions(
519                                    companyId, groupId, userId, resource.getName(),
520                                    resource.getPrimKey(), false, permissionsList);
521    
522                    filterOwnerPermissions(resource.getName(), userPermissionsList);
523    
524                    if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 5) {
525    
526                            // Owner permissions
527    
528                            Role ownerRole = roleLocalService.getRole(
529                                    companyId, RoleConstants.OWNER);
530    
531                            rolePersistence.addPermissions(
532                                    ownerRole.getRoleId(), userPermissionsList);
533                    }
534                    else {
535    
536                            // User permissions
537    
538                            if ((userId > 0) && (userId != defaultUserId)) {
539                                    userPersistence.addPermissions(userId, userPermissionsList);
540                            }
541                    }
542    
543                    // Community permissions
544    
545                    if (groupId > 0) {
546                            if (communityPermissions == null) {
547                                    communityPermissions = new String[0];
548                            }
549    
550                            List<Permission> communityPermissionsList =
551                                    permissionLocalService.getPermissions(
552                                            companyId, communityPermissions, resource.getResourceId());
553    
554                            communityPermissionsList =
555                                    permissionsListFilter.filterCommunityPermissions(
556                                            companyId, groupId, userId, resource.getName(),
557                                            resource.getPrimKey(), false, communityPermissionsList);
558    
559                            if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 5) {
560                                    Role role = roleLocalService.getDefaultGroupRole(groupId);
561    
562                                    rolePersistence.addPermissions(
563                                            role.getRoleId(), communityPermissionsList);
564                            }
565                            else {
566                                    groupPersistence.addPermissions(
567                                            groupId, communityPermissionsList);
568                            }
569                    }
570    
571                    // Guest permissions
572    
573                    if (guestPermissions == null) {
574                            guestPermissions = new String[0];
575                    }
576    
577                    List<Permission> guestPermissionsList =
578                            permissionLocalService.getPermissions(
579                                    companyId, guestPermissions, resource.getResourceId());
580    
581                    guestPermissionsList = permissionsListFilter.filterGuestPermissions(
582                            companyId, groupId, userId, resource.getName(),
583                            resource.getPrimKey(), false, guestPermissionsList);
584    
585                    if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 5) {
586                            Role guestRole = roleLocalService.getRole(
587                                    companyId, RoleConstants.GUEST);
588    
589                            rolePersistence.addPermissions(
590                                    guestRole.getRoleId(), guestPermissionsList);
591                    }
592                    else {
593                            userPersistence.addPermissions(defaultUserId, guestPermissionsList);
594                    }
595            }
596    
597            protected void addModelResources_6(
598                            long companyId, long groupId, Resource resource,
599                            String[] communityPermissions, String[] guestPermissions)
600                    throws PortalException, SystemException {
601    
602                    // Owner permissions
603    
604                    Role ownerRole = roleLocalService.getRole(
605                            companyId, RoleConstants.OWNER);
606    
607                    List<String> actionIds = ResourceActionsUtil.getModelResourceActions(
608                            resource.getName());
609    
610                    filterOwnerActions(resource.getName(), actionIds);
611    
612                    resourcePermissionLocalService.setResourcePermissions(
613                            resource.getCompanyId(), resource.getName(), resource.getScope(),
614                            resource.getPrimKey(), ownerRole.getRoleId(),
615                            actionIds.toArray(new String[actionIds.size()]));
616    
617                    // Community permissions
618    
619                    if (groupId > 0) {
620                            Role role = roleLocalService.getDefaultGroupRole(groupId);
621    
622                            if (communityPermissions == null) {
623                                    communityPermissions = new String[0];
624                            }
625    
626                            resourcePermissionLocalService.setResourcePermissions(
627                                    resource.getCompanyId(), resource.getName(),
628                                    resource.getScope(), resource.getPrimKey(), role.getRoleId(),
629                                    communityPermissions);
630                    }
631    
632                    // Guest permissions
633    
634                    Role guestRole = roleLocalService.getRole(
635                            companyId, RoleConstants.GUEST);
636    
637                    if (guestPermissions == null) {
638                            guestPermissions = new String[0];
639                    }
640    
641                    resourcePermissionLocalService.setResourcePermissions(
642                            resource.getCompanyId(), resource.getName(), resource.getScope(),
643                            resource.getPrimKey(), guestRole.getRoleId(), guestPermissions);
644            }
645    
646            protected Resource addResource_1to5(
647                            long companyId, String name, int scope, String primKey)
648                    throws SystemException {
649    
650                    ResourceCode resourceCode = resourceCodeLocalService.getResourceCode(
651                            companyId, name, scope);
652    
653                    long codeId = resourceCode.getCodeId();
654    
655                    Resource resource = resourcePersistence.fetchByC_P(codeId, primKey);
656    
657                    if (resource == null) {
658                            long resourceId = counterLocalService.increment(
659                                    Resource.class.getName());
660    
661                            resource = resourcePersistence.create(resourceId);
662    
663                            resource.setCodeId(codeId);
664                            resource.setPrimKey(primKey);
665    
666                            try {
667                                    resourcePersistence.update(resource, false);
668                            }
669                            catch (SystemException se) {
670                                    if (_log.isWarnEnabled()) {
671                                            _log.warn(
672                                                    "Add failed, fetch {codeId=" + codeId + ", primKey=" +
673                                                            primKey + "}");
674                                    }
675    
676                                    resource = resourcePersistence.fetchByC_P(
677                                            codeId, primKey, false);
678    
679                                    if (resource == null) {
680                                            throw se;
681                                    }
682                            }
683                    }
684    
685                    return resource;
686            }
687    
688            protected Resource addResource_6(
689                    long companyId, String name, int scope, String primKey) {
690    
691                    Resource resource = new ResourceImpl();
692    
693                    resource.setCompanyId(companyId);
694                    resource.setName(name);
695                    resource.setScope(scope);
696                    resource.setPrimKey(primKey);
697    
698                    return resource;
699            }
700    
701            protected void addResources_1to5(
702                            long companyId, long groupId, long userId, Resource resource,
703                            boolean portletActions)
704                    throws PortalException, SystemException {
705    
706                    List<Permission> permissionsList =
707                            permissionLocalService.addPermissions(
708                                    companyId, resource.getName(), resource.getResourceId(),
709                                    portletActions);
710    
711                    PermissionsListFilter permissionsListFilter =
712                            PermissionsListFilterFactory.getInstance();
713    
714                    List<Permission> userPermissionsList =
715                            permissionsListFilter.filterUserPermissions(
716                                    companyId, groupId, userId, resource.getName(),
717                                    resource.getPrimKey(), portletActions, permissionsList);
718    
719                    filterOwnerPermissions(resource.getName(), userPermissionsList);
720    
721                    if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 5) {
722    
723                            // Owner permissions
724    
725                            Role ownerRole = roleLocalService.getRole(
726                                    companyId, RoleConstants.OWNER);
727    
728                            rolePersistence.addPermissions(
729                                    ownerRole.getRoleId(), userPermissionsList);
730                    }
731                    else {
732    
733                            // User permissions
734    
735                            long defaultUserId = userLocalService.getDefaultUserId(companyId);
736    
737                            if ((userId > 0) && (userId != defaultUserId)) {
738                                    userPersistence.addPermissions(userId, userPermissionsList);
739                            }
740                    }
741            }
742    
743            protected void addResources_6(
744                            long companyId, long groupId, long userId, Resource resource,
745                            boolean portletActions)
746                    throws PortalException, SystemException {
747    
748                    List<String> actionIds = null;
749    
750                    if (portletActions) {
751                            actionIds = ResourceActionsUtil.getPortletResourceActions(
752                                    resource.getName());
753                    }
754                    else {
755                            actionIds = ResourceActionsUtil.getModelResourceActions(
756                                    resource.getName());
757    
758                            filterOwnerActions(resource.getName(), actionIds);
759                    }
760    
761                    Role role = roleLocalService.getRole(companyId, RoleConstants.OWNER);
762    
763                    resourcePermissionLocalService.setResourcePermissions(
764                            resource.getCompanyId(), resource.getName(), resource.getScope(),
765                            resource.getPrimKey(), role.getRoleId(),
766                            actionIds.toArray(new String[actionIds.size()]));
767            }
768    
769            protected void filterOwnerActions(String name, List<String> actionIds) {
770                    List<String> defaultOwnerActions =
771                            ResourceActionsUtil.getModelResourceOwnerDefaultActions(name);
772    
773                    if (defaultOwnerActions.isEmpty()) {
774                            return;
775                    }
776    
777                    Iterator<String> itr = actionIds.iterator();
778    
779                    while (itr.hasNext()) {
780                            String actionId = itr.next();
781    
782                            if (!defaultOwnerActions.contains(actionId)) {
783                                    itr.remove();
784                            }
785                    }
786            }
787    
788            protected void filterOwnerPermissions(
789                    String name, List<Permission> permissions) {
790    
791                    List<String> defaultOwnerActions =
792                            ResourceActionsUtil.getModelResourceOwnerDefaultActions(name);
793    
794                    if (defaultOwnerActions.isEmpty()) {
795                            return;
796                    }
797    
798                    Iterator<Permission> itr = permissions.iterator();
799    
800                    while (itr.hasNext()) {
801                            Permission permission = itr.next();
802    
803                            String actionId = permission.getActionId();
804    
805                            if (!defaultOwnerActions.contains(actionId)) {
806                                    itr.remove();
807                            }
808                    }
809            }
810    
811            protected Resource getResource_1to5(
812                            long companyId, String name, int scope, String primKey)
813                    throws PortalException, SystemException {
814    
815                    ResourceCode resourceCode = resourceCodeLocalService.getResourceCode(
816                            companyId, name, scope);
817    
818                    return resourcePersistence.findByC_P(resourceCode.getCodeId(), primKey);
819            }
820    
821            protected Resource getResource_6(
822                    long companyId, String name, int scope, String primKey) {
823    
824                    Resource resource = new ResourceImpl();
825    
826                    resource.setCompanyId(companyId);
827                    resource.setName(name);
828                    resource.setScope(scope);
829                    resource.setPrimKey(primKey);
830    
831                    return resource;
832            }
833    
834            protected void updateResources_1to5(
835                            long companyId, long groupId, Resource resource,
836                            String[] communityPermissions, String[] guestPermissions)
837                    throws PortalException, SystemException {
838    
839                    Role role = roleLocalService.getDefaultGroupRole(groupId);
840    
841                    permissionLocalService.setRolePermissions(
842                            role.getRoleId(), communityPermissions, resource.getResourceId());
843    
844                    role = roleLocalService.getRole(companyId, RoleConstants.GUEST);
845    
846                    permissionLocalService.setRolePermissions(
847                            role.getRoleId(), guestPermissions, resource.getResourceId());
848            }
849    
850            protected void updateResources_6(
851                            long companyId, long groupId, Resource resource,
852                            String[] communityPermissions, String[] guestPermissions)
853                    throws PortalException, SystemException {
854    
855                    Role role = roleLocalService.getDefaultGroupRole(groupId);
856    
857                    resourcePermissionLocalService.setResourcePermissions(
858                            resource.getCompanyId(), resource.getName(), resource.getScope(),
859                            resource.getPrimKey(), role.getRoleId(), communityPermissions);
860    
861                    role = roleLocalService.getRole(companyId, RoleConstants.GUEST);
862    
863                    resourcePermissionLocalService.setResourcePermissions(
864                            resource.getCompanyId(), resource.getName(), resource.getScope(),
865                            resource.getPrimKey(), role.getRoleId(), guestPermissions);
866            }
867    
868            protected void validate(String name, boolean portletActions)
869                    throws PortalException {
870    
871                    List<String> actions = null;
872    
873                    if (portletActions) {
874                            actions = ResourceActionsUtil.getPortletResourceActions(name);
875                    }
876                    else {
877                            actions = ResourceActionsUtil.getModelResourceActions(name);
878                    }
879    
880                    if (actions.size() == 0) {
881                            throw new ResourceActionsException(
882                                    "There are no actions associated with the resource " + name);
883                    }
884            }
885    
886            private static Log _log = LogFactoryUtil.getLog(
887                    ResourceLocalServiceImpl.class);
888    
889    }