001    /**
002     * Copyright (c) 2000-2011 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.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                            resourcePermissionLocalService.deleteResourcePermissions(
265                                    companyId, name, scope, primKey);
266    
267                            return;
268                    }
269    
270                    try {
271                            Resource resource = getResource(companyId, name, scope, primKey);
272    
273                            deleteResource(resource.getResourceId());
274                    }
275                    catch (NoSuchResourceException nsre) {
276                            if (_log.isWarnEnabled()) {
277                                    _log.warn(nsre);
278                            }
279                    }
280            }
281    
282            public void deleteResources(String name) throws SystemException {
283                    List<Resource> resources = resourceFinder.findByName(name);
284    
285                    for (Resource resource : resources) {
286                            deleteResource(resource);
287                    }
288            }
289    
290            public long getLatestResourceId() throws SystemException {
291                    List<Resource> resources = resourcePersistence.findAll(
292                            0, 1, new ResourceComparator());
293    
294                    if (resources.size() == 0) {
295                            return 0;
296                    }
297                    else {
298                            Resource resource = resources.get(0);
299    
300                            return resource.getResourceId();
301                    }
302            }
303    
304            public Resource getResource(long resourceId)
305                    throws PortalException, SystemException {
306    
307                    return resourcePersistence.findByPrimaryKey(resourceId);
308            }
309    
310            public Resource getResource(
311                            long companyId, String name, int scope, String primKey)
312                    throws PortalException, SystemException {
313    
314                    if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
315                            return getResource_6(companyId, name, scope, primKey);
316                    }
317                    else {
318                            return getResource_1to5(companyId, name, scope, primKey);
319                    }
320            }
321    
322            public List<Resource> getResources() throws SystemException {
323                    return resourcePersistence.findAll();
324            }
325    
326            public void updateResources(
327                            long companyId, String name, int scope, String primKey,
328                            String newPrimKey)
329                    throws PortalException, SystemException {
330    
331                    if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
332                            updateResources_6(companyId, name, scope, primKey, newPrimKey);
333                    }
334                    else {
335                            updateResources_1to5(
336                                    companyId, name, scope, primKey, newPrimKey);
337                    }
338            }
339    
340            public void updateResources(
341                            long companyId, long groupId, String name, long primKey,
342                            String[] communityPermissions, String[] guestPermissions)
343                    throws PortalException, SystemException {
344    
345                    updateResources(
346                            companyId, groupId, name, String.valueOf(primKey),
347                            communityPermissions, guestPermissions);
348            }
349    
350            public void updateResources(
351                            long companyId, long groupId, String name, String primKey,
352                            String[] communityPermissions, String[] guestPermissions)
353                    throws PortalException, SystemException {
354    
355                    Resource resource = getResource(
356                            companyId, name, ResourceConstants.SCOPE_INDIVIDUAL, primKey);
357    
358                    if (communityPermissions == null) {
359                            communityPermissions = new String[0];
360                    }
361    
362                    if (guestPermissions == null) {
363                            guestPermissions = new String[0];
364                    }
365    
366                    if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
367                            updateResources_6(
368                                    companyId, groupId, resource, communityPermissions,
369                                    guestPermissions);
370                    }
371                    else {
372                            updateResources_1to5(
373                                    companyId, groupId, resource, communityPermissions,
374                                    guestPermissions);
375                    }
376            }
377    
378            protected void addCommunityPermissions(
379                            long companyId, long groupId, long userId, String name,
380                            Resource resource, boolean portletActions)
381                    throws PortalException, SystemException {
382    
383                    List<String> actions = null;
384    
385                    if (portletActions) {
386                            actions =
387                                    ResourceActionsUtil.getPortletResourceCommunityDefaultActions(
388                                            name);
389                    }
390                    else {
391                            actions =
392                                    ResourceActionsUtil.getModelResourceCommunityDefaultActions(
393                                            name);
394                    }
395    
396                    String[] actionIds = actions.toArray(new String[actions.size()]);
397    
398                    if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
399                            addCommunityPermissions_6(groupId, resource, actionIds);
400                    }
401                    else {
402                            addCommunityPermissions_1to5(
403                                    companyId, groupId, userId, name, resource, portletActions,
404                                    actionIds);
405                    }
406            }
407    
408            protected void addCommunityPermissions_1to5(
409                            long companyId, long groupId, long userId, String name,
410                            Resource resource, boolean portletActions, String[] actionIds)
411                    throws PortalException, SystemException {
412    
413                    long resourceId = resource.getResourceId();
414                    String primKey = resource.getPrimKey();
415    
416                    List<Permission> communityPermissionsList =
417                            permissionLocalService.getPermissions(
418                                    companyId, actionIds, resourceId);
419    
420                    PermissionsListFilter permissionsListFilter =
421                            PermissionsListFilterFactory.getInstance();
422    
423                    communityPermissionsList =
424                            permissionsListFilter.filterCommunityPermissions(
425                                    companyId, groupId, userId, name, primKey, portletActions,
426                                    communityPermissionsList);
427    
428                    if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 5) {
429                            Role role = roleLocalService.getDefaultGroupRole(groupId);
430    
431                            rolePersistence.addPermissions(
432                                    role.getRoleId(), communityPermissionsList);
433                    }
434                    else {
435                            groupPersistence.addPermissions(groupId, communityPermissionsList);
436                    }
437            }
438    
439            protected void addCommunityPermissions_6(
440                            long groupId, Resource resource, String[] actionIds)
441                    throws PortalException, SystemException {
442    
443                    Role role = roleLocalService.getDefaultGroupRole(groupId);
444    
445                    resourcePermissionLocalService.setResourcePermissions(
446                            resource.getCompanyId(), resource.getName(), resource.getScope(),
447                            resource.getPrimKey(), role.getRoleId(), actionIds);
448            }
449    
450            protected void addGuestPermissions(
451                            long companyId, long groupId, long userId, String name,
452                            Resource resource, boolean portletActions)
453                    throws PortalException, SystemException {
454    
455                    List<String> actions = null;
456    
457                    if (portletActions) {
458                            actions = ResourceActionsUtil.getPortletResourceGuestDefaultActions(
459                                    name);
460                    }
461                    else {
462                            actions = ResourceActionsUtil.getModelResourceGuestDefaultActions(
463                                    name);
464                    }
465    
466                    String[] actionIds = actions.toArray(new String[actions.size()]);
467    
468                    if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
469                            addGuestPermissions_6(companyId, resource, actionIds);
470                    }
471                    else {
472                            addGuestPermissions_1to5(
473                                    companyId, groupId, userId, name, resource, portletActions,
474                                    actionIds);
475                    }
476            }
477    
478            protected void addGuestPermissions_1to5(
479                            long companyId, long groupId, long userId, String name,
480                            Resource resource, boolean portletActions, String[] actionIds)
481                    throws PortalException, SystemException {
482    
483                    List<Permission> guestPermissionsList =
484                            permissionLocalService.getPermissions(
485                                    companyId, actionIds, resource.getResourceId());
486    
487                    PermissionsListFilter permissionsListFilter =
488                            PermissionsListFilterFactory.getInstance();
489    
490                    guestPermissionsList =
491                            permissionsListFilter.filterGuestPermissions(
492                                    companyId, groupId, userId, name, resource.getPrimKey(),
493                                    portletActions, guestPermissionsList);
494    
495                    if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 5) {
496                            Role guestRole = roleLocalService.getRole(
497                                    companyId, RoleConstants.GUEST);
498    
499                            rolePersistence.addPermissions(
500                                    guestRole.getRoleId(), guestPermissionsList);
501                    }
502                    else {
503                            long defaultUserId = userLocalService.getDefaultUserId(companyId);
504    
505                            userPersistence.addPermissions(defaultUserId, guestPermissionsList);
506                    }
507            }
508    
509            protected void addGuestPermissions_6(
510                            long companyId, Resource resource, String[] actionIds)
511                    throws PortalException, SystemException {
512    
513                    Role guestRole = roleLocalService.getRole(
514                            companyId, RoleConstants.GUEST);
515    
516                    resourcePermissionLocalService.setResourcePermissions(
517                            resource.getCompanyId(), resource.getName(), resource.getScope(),
518                            resource.getPrimKey(), guestRole.getRoleId(), actionIds);
519            }
520    
521            protected void addModelResources_1to5(
522                            long companyId, long groupId, long userId, Resource resource,
523                            String[] communityPermissions, String[] guestPermissions)
524                    throws PortalException, SystemException {
525    
526                    long defaultUserId = userLocalService.getDefaultUserId(companyId);
527    
528                    PermissionsListFilter permissionsListFilter =
529                            PermissionsListFilterFactory.getInstance();
530    
531                    List<Permission> permissionsList =
532                            permissionLocalService.addPermissions(
533                                    companyId, resource.getName(), resource.getResourceId(), false);
534    
535                    List<Permission> userPermissionsList =
536                            permissionsListFilter.filterUserPermissions(
537                                    companyId, groupId, userId, resource.getName(),
538                                    resource.getPrimKey(), false, permissionsList);
539    
540                    filterOwnerPermissions(resource.getName(), userPermissionsList);
541    
542                    if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 5) {
543    
544                            // Owner permissions
545    
546                            Role ownerRole = roleLocalService.getRole(
547                                    companyId, RoleConstants.OWNER);
548    
549                            rolePersistence.addPermissions(
550                                    ownerRole.getRoleId(), userPermissionsList);
551                    }
552                    else {
553    
554                            // User permissions
555    
556                            if ((userId > 0) && (userId != defaultUserId)) {
557                                    userPersistence.addPermissions(userId, userPermissionsList);
558                            }
559                    }
560    
561                    // Community permissions
562    
563                    if (groupId > 0) {
564                            if (communityPermissions == null) {
565                                    communityPermissions = new String[0];
566                            }
567    
568                            List<Permission> communityPermissionsList =
569                                    permissionLocalService.getPermissions(
570                                            companyId, communityPermissions, resource.getResourceId());
571    
572                            communityPermissionsList =
573                                    permissionsListFilter.filterCommunityPermissions(
574                                            companyId, groupId, userId, resource.getName(),
575                                            resource.getPrimKey(), false, communityPermissionsList);
576    
577                            if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 5) {
578                                    Role role = roleLocalService.getDefaultGroupRole(groupId);
579    
580                                    rolePersistence.addPermissions(
581                                            role.getRoleId(), communityPermissionsList);
582                            }
583                            else {
584                                    groupPersistence.addPermissions(
585                                            groupId, communityPermissionsList);
586                            }
587                    }
588    
589                    // Guest permissions
590    
591                    if (guestPermissions == null) {
592                            guestPermissions = new String[0];
593                    }
594    
595                    List<Permission> guestPermissionsList =
596                            permissionLocalService.getPermissions(
597                                    companyId, guestPermissions, resource.getResourceId());
598    
599                    guestPermissionsList = permissionsListFilter.filterGuestPermissions(
600                            companyId, groupId, userId, resource.getName(),
601                            resource.getPrimKey(), false, guestPermissionsList);
602    
603                    if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 5) {
604                            Role guestRole = roleLocalService.getRole(
605                                    companyId, RoleConstants.GUEST);
606    
607                            rolePersistence.addPermissions(
608                                    guestRole.getRoleId(), guestPermissionsList);
609                    }
610                    else {
611                            userPersistence.addPermissions(defaultUserId, guestPermissionsList);
612                    }
613            }
614    
615            protected void addModelResources_6(
616                            long companyId, long groupId, Resource resource,
617                            String[] communityPermissions, String[] guestPermissions)
618                    throws PortalException, SystemException {
619    
620                    // Owner permissions
621    
622                    Role ownerRole = roleLocalService.getRole(
623                            companyId, RoleConstants.OWNER);
624    
625                    List<String> actionIds = ResourceActionsUtil.getModelResourceActions(
626                            resource.getName());
627    
628                    filterOwnerActions(resource.getName(), actionIds);
629    
630                    resourcePermissionLocalService.setResourcePermissions(
631                            resource.getCompanyId(), resource.getName(), resource.getScope(),
632                            resource.getPrimKey(), ownerRole.getRoleId(),
633                            actionIds.toArray(new String[actionIds.size()]));
634    
635                    // Community permissions
636    
637                    if (groupId > 0) {
638                            Role role = roleLocalService.getDefaultGroupRole(groupId);
639    
640                            if (communityPermissions == null) {
641                                    communityPermissions = new String[0];
642                            }
643    
644                            resourcePermissionLocalService.setResourcePermissions(
645                                    resource.getCompanyId(), resource.getName(),
646                                    resource.getScope(), resource.getPrimKey(), role.getRoleId(),
647                                    communityPermissions);
648                    }
649    
650                    // Guest permissions
651    
652                    Role guestRole = roleLocalService.getRole(
653                            companyId, RoleConstants.GUEST);
654    
655                    if (guestPermissions == null) {
656                            guestPermissions = new String[0];
657                    }
658    
659                    resourcePermissionLocalService.setResourcePermissions(
660                            resource.getCompanyId(), resource.getName(), resource.getScope(),
661                            resource.getPrimKey(), guestRole.getRoleId(), guestPermissions);
662            }
663    
664            protected Resource addResource_1to5(
665                            long companyId, String name, int scope, String primKey)
666                    throws SystemException {
667    
668                    ResourceCode resourceCode = resourceCodeLocalService.getResourceCode(
669                            companyId, name, scope);
670    
671                    long codeId = resourceCode.getCodeId();
672    
673                    Resource resource = resourcePersistence.fetchByC_P(codeId, primKey);
674    
675                    if (resource == null) {
676                            long resourceId = counterLocalService.increment(
677                                    Resource.class.getName());
678    
679                            resource = resourcePersistence.create(resourceId);
680    
681                            resource.setCodeId(codeId);
682                            resource.setPrimKey(primKey);
683    
684                            try {
685                                    resourcePersistence.update(resource, false);
686                            }
687                            catch (SystemException se) {
688                                    if (_log.isWarnEnabled()) {
689                                            _log.warn(
690                                                    "Add failed, fetch {codeId=" + codeId + ", primKey=" +
691                                                            primKey + "}");
692                                    }
693    
694                                    resource = resourcePersistence.fetchByC_P(
695                                            codeId, primKey, false);
696    
697                                    if (resource == null) {
698                                            throw se;
699                                    }
700                            }
701                    }
702    
703                    return resource;
704            }
705    
706            protected Resource addResource_6(
707                    long companyId, String name, int scope, String primKey) {
708    
709                    Resource resource = new ResourceImpl();
710    
711                    resource.setCompanyId(companyId);
712                    resource.setName(name);
713                    resource.setScope(scope);
714                    resource.setPrimKey(primKey);
715    
716                    return resource;
717            }
718    
719            protected void addResources_1to5(
720                            long companyId, long groupId, long userId, Resource resource,
721                            boolean portletActions)
722                    throws PortalException, SystemException {
723    
724                    List<Permission> permissionsList =
725                            permissionLocalService.addPermissions(
726                                    companyId, resource.getName(), resource.getResourceId(),
727                                    portletActions);
728    
729                    PermissionsListFilter permissionsListFilter =
730                            PermissionsListFilterFactory.getInstance();
731    
732                    List<Permission> userPermissionsList =
733                            permissionsListFilter.filterUserPermissions(
734                                    companyId, groupId, userId, resource.getName(),
735                                    resource.getPrimKey(), portletActions, permissionsList);
736    
737                    filterOwnerPermissions(resource.getName(), userPermissionsList);
738    
739                    if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 5) {
740    
741                            // Owner permissions
742    
743                            Role ownerRole = roleLocalService.getRole(
744                                    companyId, RoleConstants.OWNER);
745    
746                            rolePersistence.addPermissions(
747                                    ownerRole.getRoleId(), userPermissionsList);
748                    }
749                    else {
750    
751                            // User permissions
752    
753                            long defaultUserId = userLocalService.getDefaultUserId(companyId);
754    
755                            if ((userId > 0) && (userId != defaultUserId)) {
756                                    userPersistence.addPermissions(userId, userPermissionsList);
757                            }
758                    }
759            }
760    
761            protected void addResources_6(
762                            long companyId, long groupId, long userId, Resource resource,
763                            boolean portletActions)
764                    throws PortalException, SystemException {
765    
766                    List<String> actionIds = null;
767    
768                    if (portletActions) {
769                            actionIds = ResourceActionsUtil.getPortletResourceActions(
770                                    resource.getName());
771                    }
772                    else {
773                            actionIds = ResourceActionsUtil.getModelResourceActions(
774                                    resource.getName());
775    
776                            filterOwnerActions(resource.getName(), actionIds);
777                    }
778    
779                    Role role = roleLocalService.getRole(companyId, RoleConstants.OWNER);
780    
781                    resourcePermissionLocalService.setResourcePermissions(
782                            resource.getCompanyId(), resource.getName(), resource.getScope(),
783                            resource.getPrimKey(), role.getRoleId(),
784                            actionIds.toArray(new String[actionIds.size()]));
785            }
786    
787            protected void filterOwnerActions(String name, List<String> actionIds) {
788                    List<String> defaultOwnerActions =
789                            ResourceActionsUtil.getModelResourceOwnerDefaultActions(name);
790    
791                    if (defaultOwnerActions.isEmpty()) {
792                            return;
793                    }
794    
795                    Iterator<String> itr = actionIds.iterator();
796    
797                    while (itr.hasNext()) {
798                            String actionId = itr.next();
799    
800                            if (!defaultOwnerActions.contains(actionId)) {
801                                    itr.remove();
802                            }
803                    }
804            }
805    
806            protected void filterOwnerPermissions(
807                    String name, List<Permission> permissions) {
808    
809                    List<String> defaultOwnerActions =
810                            ResourceActionsUtil.getModelResourceOwnerDefaultActions(name);
811    
812                    if (defaultOwnerActions.isEmpty()) {
813                            return;
814                    }
815    
816                    Iterator<Permission> itr = permissions.iterator();
817    
818                    while (itr.hasNext()) {
819                            Permission permission = itr.next();
820    
821                            String actionId = permission.getActionId();
822    
823                            if (!defaultOwnerActions.contains(actionId)) {
824                                    itr.remove();
825                            }
826                    }
827            }
828    
829            protected Resource getResource_1to5(
830                            long companyId, String name, int scope, String primKey)
831                    throws PortalException, SystemException {
832    
833                    ResourceCode resourceCode = resourceCodeLocalService.getResourceCode(
834                            companyId, name, scope);
835    
836                    return resourcePersistence.findByC_P(resourceCode.getCodeId(), primKey);
837            }
838    
839            protected Resource getResource_6(
840                    long companyId, String name, int scope, String primKey) {
841    
842                    Resource resource = new ResourceImpl();
843    
844                    resource.setCompanyId(companyId);
845                    resource.setName(name);
846                    resource.setScope(scope);
847                    resource.setPrimKey(primKey);
848    
849                    return resource;
850            }
851    
852            protected void updateResources_1to5(
853                            long companyId, String name, int scope, String primKey,
854                            String newPrimKey)
855                    throws PortalException, SystemException {
856    
857                    Resource resource = getResource(companyId, name, scope, primKey);
858    
859                    resource.setPrimKey(newPrimKey);
860    
861                    resourcePersistence.update(resource, false);
862            }
863    
864            protected void updateResources_1to5(
865                            long companyId, long groupId, Resource resource,
866                            String[] communityPermissions, String[] guestPermissions)
867                    throws PortalException, SystemException {
868    
869                    Role role = roleLocalService.getDefaultGroupRole(groupId);
870    
871                    permissionLocalService.setRolePermissions(
872                            role.getRoleId(), communityPermissions, resource.getResourceId());
873    
874                    role = roleLocalService.getRole(companyId, RoleConstants.GUEST);
875    
876                    permissionLocalService.setRolePermissions(
877                            role.getRoleId(), guestPermissions, resource.getResourceId());
878            }
879    
880            protected void updateResources_6(
881                            long companyId, String name, int scope, String primKey,
882                            String newPrimKey)
883                    throws SystemException {
884    
885                    List<ResourcePermission> resourcePermissions =
886                            resourcePermissionLocalService.getResourcePermissions(
887                                    companyId, name, scope, primKey);
888    
889                    for (ResourcePermission resourcePermission : resourcePermissions) {
890                            resourcePermission.setPrimKey(newPrimKey);
891    
892                            resourcePermissionPersistence.update(resourcePermission, false);
893                    }
894            }
895    
896            protected void updateResources_6(
897                            long companyId, long groupId, Resource resource,
898                            String[] communityPermissions, String[] guestPermissions)
899                    throws PortalException, SystemException {
900    
901                    Role role = roleLocalService.getDefaultGroupRole(groupId);
902    
903                    resourcePermissionLocalService.setResourcePermissions(
904                            resource.getCompanyId(), resource.getName(), resource.getScope(),
905                            resource.getPrimKey(), role.getRoleId(), communityPermissions);
906    
907                    role = roleLocalService.getRole(companyId, RoleConstants.GUEST);
908    
909                    resourcePermissionLocalService.setResourcePermissions(
910                            resource.getCompanyId(), resource.getName(), resource.getScope(),
911                            resource.getPrimKey(), role.getRoleId(), guestPermissions);
912            }
913    
914            protected void validate(String name, boolean portletActions)
915                    throws PortalException {
916    
917                    List<String> actions = null;
918    
919                    if (portletActions) {
920                            actions = ResourceActionsUtil.getPortletResourceActions(name);
921                    }
922                    else {
923                            actions = ResourceActionsUtil.getModelResourceActions(name);
924                    }
925    
926                    if (actions.size() == 0) {
927                            throw new ResourceActionsException(
928                                    "There are no actions associated with the resource " + name);
929                    }
930            }
931    
932            private static Log _log = LogFactoryUtil.getLog(
933                    ResourceLocalServiceImpl.class);
934    
935    }