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