001    /**
002     * Copyright (c) 2000-2013 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.NoSuchResourcePermissionException;
018    import com.liferay.portal.kernel.concurrent.LockRegistry;
019    import com.liferay.portal.kernel.dao.db.DB;
020    import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
021    import com.liferay.portal.kernel.dao.orm.QueryPos;
022    import com.liferay.portal.kernel.dao.orm.SQLQuery;
023    import com.liferay.portal.kernel.dao.orm.Session;
024    import com.liferay.portal.kernel.dao.orm.Type;
025    import com.liferay.portal.kernel.exception.PortalException;
026    import com.liferay.portal.kernel.exception.SystemException;
027    import com.liferay.portal.kernel.search.SearchEngineUtil;
028    import com.liferay.portal.kernel.util.ArrayUtil;
029    import com.liferay.portal.kernel.util.ListUtil;
030    import com.liferay.portal.kernel.util.StringBundler;
031    import com.liferay.portal.kernel.util.StringPool;
032    import com.liferay.portal.kernel.util.StringUtil;
033    import com.liferay.portal.model.Resource;
034    import com.liferay.portal.model.ResourceAction;
035    import com.liferay.portal.model.ResourceConstants;
036    import com.liferay.portal.model.ResourcePermission;
037    import com.liferay.portal.model.ResourcePermissionConstants;
038    import com.liferay.portal.model.Role;
039    import com.liferay.portal.model.RoleConstants;
040    import com.liferay.portal.security.auth.PrincipalException;
041    import com.liferay.portal.security.permission.PermissionCacheUtil;
042    import com.liferay.portal.security.permission.PermissionThreadLocal;
043    import com.liferay.portal.security.permission.ResourceActionsUtil;
044    import com.liferay.portal.service.base.ResourcePermissionLocalServiceBaseImpl;
045    import com.liferay.portal.util.PortalUtil;
046    import com.liferay.portal.util.PropsValues;
047    import com.liferay.portal.util.ResourcePermissionsThreadLocal;
048    import com.liferay.util.dao.orm.CustomSQLUtil;
049    
050    import java.util.ArrayList;
051    import java.util.Collection;
052    import java.util.Collections;
053    import java.util.HashMap;
054    import java.util.HashSet;
055    import java.util.List;
056    import java.util.Map;
057    import java.util.Set;
058    import java.util.concurrent.locks.Lock;
059    
060    /**
061     * Provides the local service for accessing, adding, checking, deleting,
062     * granting, and revoking resource permissions.
063     *
064     * <p>
065     * Before attempting to read any of the documentation for this class, first read
066     * {@link com.liferay.portal.model.impl.ResourcePermissionImpl} for an
067     * explanation of scoping.
068     * </p>
069     *
070     * @author Brian Wing Shun Chan
071     * @author Raymond Aug??
072     * @author Connor McKay
073     */
074    public class ResourcePermissionLocalServiceImpl
075            extends ResourcePermissionLocalServiceBaseImpl {
076    
077            /**
078             * @see com.liferay.portal.verify.VerifyPermission#fixOrganizationRolePermissions
079             */
080            public static final String[] EMPTY_ACTION_IDS = {null};
081    
082            /**
083             * Grants the role permission at the scope to perform the action on
084             * resources of the type. Existing actions are retained.
085             *
086             * <p>
087             * This method cannot be used to grant individual scope permissions, but is
088             * only intended for adding permissions at the company, group, and
089             * group-template scopes. For example, this method could be used to grant a
090             * company scope permission to edit message board posts.
091             * </p>
092             *
093             * <p>
094             * If a company scope permission is granted to resources that the role
095             * already had group scope permissions to, the group scope permissions are
096             * deleted. Likewise, if a group scope permission is granted to resources
097             * that the role already had company scope permissions to, the company scope
098             * permissions are deleted. Be aware that this latter behavior can result in
099             * an overall reduction in permissions for the role.
100             * </p>
101             *
102             * <p>
103             * Depending on the scope, the value of <code>primKey</code> will have
104             * different meanings. For more information, see {@link
105             * com.liferay.portal.model.impl.ResourcePermissionImpl}.
106             * </p>
107             *
108             * @param  companyId the primary key of the company
109             * @param  name the resource's name, which can be either a class name or a
110             *         portlet ID
111             * @param  scope the scope. This method only supports company, group, and
112             *         group-template scope.
113             * @param  primKey the primary key
114             * @param  roleId the primary key of the role
115             * @param  actionId the action ID
116             * @throws PortalException if scope was set to individual scope or if a role
117             *         with the primary key or a resource action with the name and
118             *         action ID could not be found
119             * @throws SystemException if a system exception occurred
120             */
121            @Override
122            public void addResourcePermission(
123                            long companyId, String name, int scope, String primKey, long roleId,
124                            String actionId)
125                    throws PortalException, SystemException {
126    
127                    if (scope == ResourceConstants.SCOPE_COMPANY) {
128    
129                            // Remove group permission
130    
131                            removeResourcePermissions(
132                                    companyId, name, ResourceConstants.SCOPE_GROUP, roleId,
133                                    actionId);
134                    }
135                    else if (scope == ResourceConstants.SCOPE_GROUP) {
136    
137                            // Remove company permission
138    
139                            removeResourcePermissions(
140                                    companyId, name, ResourceConstants.SCOPE_COMPANY, roleId,
141                                    actionId);
142                    }
143                    else if (scope == ResourceConstants.SCOPE_INDIVIDUAL) {
144                            throw new NoSuchResourcePermissionException();
145                    }
146    
147                    updateResourcePermission(
148                            companyId, name, scope, primKey, roleId, 0, new String[] {actionId},
149                            ResourcePermissionConstants.OPERATOR_ADD);
150    
151                    PermissionCacheUtil.clearCache();
152            }
153    
154            /**
155             * Grants the role permissions at the scope to perform the actions on all
156             * resources of the type. Existing actions are retained.
157             *
158             * <p>
159             * This method should only be used to add default permissions to existing
160             * resources en masse during upgrades or while verifying permissions. For
161             * example, this method could be used to grant site members individual scope
162             * permissions to view all blog posts.
163             * </p>
164             *
165             * @param  resourceName the resource's name, which can be either a class
166             *         name or a portlet ID
167             * @param  roleName the role's name
168             * @param  scope the scope
169             * @param  resourceActionBitwiseValue the bitwise IDs of the actions
170             * @throws SystemException if a system exception occurred
171             */
172            @Override
173            public void addResourcePermissions(
174                            String resourceName, String roleName, int scope,
175                            long resourceActionBitwiseValue)
176                    throws SystemException {
177    
178                    List<Role> roles = rolePersistence.findByName(roleName);
179    
180                    if (roles.isEmpty()) {
181                            return;
182                    }
183    
184                    Session session = resourcePermissionPersistence.openSession();
185    
186                    try {
187    
188                            // Update existing resource permissions
189    
190                            String sql = CustomSQLUtil.get(_UPDATE_ACTION_IDS);
191    
192                            sql = StringUtil.replace(
193                                    sql, "[$ROLE_ID$]",
194                                    ListUtil.toString(roles, Role.ROLE_ID_ACCESSOR));
195    
196                            SQLQuery sqlQuery = session.createSQLQuery(sql);
197    
198                            QueryPos qPos = QueryPos.getInstance(sqlQuery);
199    
200                            qPos.add(resourceActionBitwiseValue);
201                            qPos.add(resourceActionBitwiseValue);
202                            qPos.add(resourceName);
203                            qPos.add(scope);
204    
205                            sqlQuery.executeUpdate();
206    
207                            // Add missing resource permissions
208    
209                            sql = CustomSQLUtil.get(_FIND_MISSING_RESOURCE_PERMISSIONS);
210    
211                            sqlQuery = session.createSQLQuery(sql);
212    
213                            sqlQuery.addScalar("companyId", Type.LONG);
214                            sqlQuery.addScalar("name", Type.STRING);
215                            sqlQuery.addScalar("scope", Type.INTEGER);
216                            sqlQuery.addScalar("primKey", Type.STRING);
217                            sqlQuery.addScalar("roleId", Type.LONG);
218    
219                            qPos = QueryPos.getInstance(sqlQuery);
220    
221                            qPos.add(resourceName);
222                            qPos.add(scope);
223                            qPos.add(roleName);
224    
225                            List<Object[]> resourcePermissionArrays = sqlQuery.list(true);
226    
227                            if (resourcePermissionArrays.isEmpty()) {
228                                    return;
229                            }
230    
231                            for (Object[] resourcePermissionArray : resourcePermissionArrays) {
232                                    long resourcePermissionId = counterLocalService.increment(
233                                            ResourcePermission.class.getName());
234    
235                                    ResourcePermission resourcePermission =
236                                            resourcePermissionPersistence.create(resourcePermissionId);
237    
238                                    resourcePermission.setCompanyId(
239                                            (Long)resourcePermissionArray[0]);
240                                    resourcePermission.setName((String)resourcePermissionArray[1]);
241                                    resourcePermission.setScope(
242                                            (Integer)resourcePermissionArray[2]);
243                                    resourcePermission.setPrimKey(
244                                            (String)resourcePermissionArray[3]);
245                                    resourcePermission.setRoleId((Long)resourcePermissionArray[4]);
246                                    resourcePermission.setActionIds(resourceActionBitwiseValue);
247    
248                                    session.save(resourcePermission);
249                            }
250                    }
251                    catch (Exception e) {
252                            throw new SystemException(e);
253                    }
254                    finally {
255                            resourcePermissionPersistence.closeSession(session);
256    
257                            resourcePermissionPersistence.clearCache();
258                    }
259            }
260    
261            /**
262             * Deletes all resource permissions at the scope to resources of the type.
263             * This method should not be confused with any of the
264             * <code>removeResourcePermission</code> methods, as its purpose is very
265             * different. This method should only be used for deleting resource
266             * permissions that refer to a resource when that resource is deleted. For
267             * example this method could be used to delete all individual scope
268             * permissions to a blog post when it is deleted.
269             *
270             * <p>
271             * Depending on the scope, the value of <code>primKey</code> will have
272             * different meanings. For more information, see {@link
273             * com.liferay.portal.model.impl.ResourcePermissionImpl}.
274             * </p>
275             *
276             * @param  companyId the primary key of the company
277             * @param  name the resource's name, which can be either a class name or a
278             *         portlet ID
279             * @param  scope the scope
280             * @param  primKey the primary key
281             * @throws PortalException if a portal exception occurred
282             * @throws SystemException if a system exception occurred
283             */
284            @Override
285            public void deleteResourcePermissions(
286                            long companyId, String name, int scope, long primKey)
287                    throws PortalException, SystemException {
288    
289                    deleteResourcePermissions(
290                            companyId, name, scope, String.valueOf(primKey));
291            }
292    
293            /**
294             * Deletes all resource permissions at the scope to resources of the type.
295             * This method should not be confused with any of the
296             * <code>removeResourcePermission</code> methods, as its purpose is very
297             * different. This method should only be used for deleting resource
298             * permissions that refer to a resource when that resource is deleted. For
299             * example this method could be used to delete all individual scope
300             * permissions to a blog post when it is deleted.
301             *
302             * <p>
303             * Depending on the scope, the value of <code>primKey</code> will have
304             * different meanings. For more information, see {@link
305             * com.liferay.portal.model.impl.ResourcePermissionImpl}.
306             * </p>
307             *
308             * @param  companyId the primary key of the company
309             * @param  name the resource's name, which can be either a class name or a
310             *         portlet ID
311             * @param  scope the scope
312             * @param  primKey the primary key
313             * @throws PortalException if a portal exception occurred
314             * @throws SystemException if a system exception occurred
315             */
316            @Override
317            public void deleteResourcePermissions(
318                            long companyId, String name, int scope, String primKey)
319                    throws PortalException, SystemException {
320    
321                    List<ResourcePermission> resourcePermissions =
322                            resourcePermissionPersistence.findByC_N_S_P(
323                                    companyId, name, scope, primKey);
324    
325                    for (ResourcePermission resourcePermission : resourcePermissions) {
326                            deleteResourcePermission(
327                                    resourcePermission.getResourcePermissionId());
328                    }
329            }
330    
331            /**
332             * Returns the intersection of action IDs the role has permission at the
333             * scope to perform on resources of the type.
334             *
335             * @param  companyId he primary key of the company
336             * @param  name the resource's name, which can be either a class name or a
337             *         portlet ID
338             * @param  scope the scope
339             * @param  primKey the primary key
340             * @param  roleId the primary key of the role
341             * @param  actionIds the action IDs
342             * @return the intersection of action IDs the role has permission at the
343             *         scope to perform on resources of the type
344             * @throws PortalException if a resouce action could not be found for any
345             *         one of the actions on the resource
346             * @throws SystemException if a system exception occurred
347             */
348            @Override
349            public List<String> getAvailableResourcePermissionActionIds(
350                            long companyId, String name, int scope, String primKey, long roleId,
351                            Collection<String> actionIds)
352                    throws PortalException, SystemException {
353    
354                    ResourcePermission resourcePermission =
355                            resourcePermissionPersistence.fetchByC_N_S_P_R(
356                                    companyId, name, scope, primKey, roleId);
357    
358                    if (resourcePermission == null) {
359                            return Collections.emptyList();
360                    }
361    
362                    List<String> availableActionIds = new ArrayList<String>(
363                            actionIds.size());
364    
365                    for (String actionId : actionIds) {
366                            ResourceAction resourceAction =
367                                    resourceActionLocalService.getResourceAction(name, actionId);
368    
369                            if (hasActionId(resourcePermission, resourceAction)) {
370                                    availableActionIds.add(actionId);
371                            }
372                    }
373    
374                    return availableActionIds;
375            }
376    
377            @Override
378            public Map<Long, Set<String>> getAvailableResourcePermissionActionIds(
379                            long companyId, String name, int scope, String primKey,
380                            long[] roleIds, Collection<String> actionIds)
381                    throws PortalException, SystemException {
382    
383                    List<ResourcePermission> resourcePermissions =
384                            resourcePermissionPersistence.findByC_N_S_P_R(
385                                    companyId, name, scope, primKey, roleIds);
386    
387                    if (resourcePermissions.isEmpty()) {
388                            return Collections.emptyMap();
389                    }
390    
391                    Map<Long, Set<String>> roleIdsToActionIds =
392                            new HashMap<Long, Set<String>>();
393    
394                    for (ResourcePermission resourcePermission : resourcePermissions) {
395                            long roleId = resourcePermission.getRoleId();
396    
397                            Set<String> availableActionIds = roleIdsToActionIds.get(roleId);
398    
399                            if (availableActionIds != null) {
400                                    continue;
401                            }
402    
403                            availableActionIds = new HashSet<String>();
404    
405                            roleIdsToActionIds.put(roleId, availableActionIds);
406    
407                            for (String actionId : actionIds) {
408                                    ResourceAction resourceAction =
409                                            resourceActionLocalService.getResourceAction(
410                                                    name, actionId);
411    
412                                    if (hasActionId(resourcePermission, resourceAction)) {
413                                            availableActionIds.add(actionId);
414                                    }
415                            }
416                    }
417    
418                    return roleIdsToActionIds;
419            }
420    
421            /**
422             * Returns the resource permission for the role at the scope to perform the
423             * actions on resources of the type.
424             *
425             * @param  companyId the primary key of the company
426             * @param  name the resource's name, which can be either a class name or a
427             *         portlet ID
428             * @param  scope the scope
429             * @param  primKey the primary key
430             * @param  roleId the primary key of the role
431             * @return the resource permission for the role at the scope to perform the
432             *         actions on resources of the type
433             * @throws PortalException if no matching resources could be found
434             * @throws SystemException if a system exception occurred
435             */
436            @Override
437            public ResourcePermission getResourcePermission(
438                            long companyId, String name, int scope, String primKey, long roleId)
439                    throws PortalException, SystemException {
440    
441                    return resourcePermissionPersistence.findByC_N_S_P_R(
442                            companyId, name, scope, primKey, roleId);
443            }
444    
445            /**
446             * Returns all the resource permissions at the scope of the type.
447             *
448             * @param  companyId the primary key of the company
449             * @param  name the resource's name, which can be either a class name or a
450             *         portlet ID
451             * @param  scope the scope
452             * @param  primKey the primary key
453             * @return the resource permissions at the scope of the type
454             * @throws SystemException if a system exception occurred
455             */
456            @Override
457            public List<ResourcePermission> getResourcePermissions(
458                            long companyId, String name, int scope, String primKey)
459                    throws SystemException {
460    
461                    return resourcePermissionPersistence.findByC_N_S_P(
462                            companyId, name, scope, primKey);
463            }
464    
465            /**
466             * Returns the number of resource permissions at the scope of the type.
467             *
468             * @param  companyId the primary key of the company
469             * @param  name the resource's name, which can be either a class name or a
470             *         portlet ID
471             * @param  scope the scope
472             * @param  primKey the primary key
473             * @return the number of resource permissions at the scope of the type
474             * @throws SystemException if a system exception occurred
475             */
476            @Override
477            public int getResourcePermissionsCount(
478                            long companyId, String name, int scope, String primKey)
479                    throws SystemException {
480    
481                    return resourcePermissionPersistence.countByC_N_S_P(
482                            companyId, name, scope, primKey);
483            }
484    
485            /**
486             * Returns the resource permissions that apply to the resource.
487             *
488             * @param  companyId the primary key of the resource's company
489             * @param  groupId the primary key of the resource's group
490             * @param  name the resource's name, which can be either a class name or a
491             *         portlet ID
492             * @param  primKey the primary key of the resource
493             * @return the resource permissions associated with the resource
494             * @throws SystemException if a system exception occurred
495             */
496            @Override
497            public List<ResourcePermission> getResourceResourcePermissions(
498                            long companyId, long groupId, String name, String primKey)
499                    throws SystemException {
500    
501                    return resourcePermissionFinder.findByResource(
502                            companyId, groupId, name, primKey);
503            }
504    
505            /**
506             * Returns all the resource permissions for the role.
507             *
508             * @param  roleId the primary key of the role
509             * @return the resource permissions for the role
510             * @throws SystemException if a system exception occurred
511             */
512            @Override
513            public List<ResourcePermission> getRoleResourcePermissions(long roleId)
514                    throws SystemException {
515    
516                    return resourcePermissionPersistence.findByRoleId(roleId);
517            }
518    
519            /**
520             * Returns a range of all the resource permissions for the role at the
521             * scopes.
522             *
523             * <p>
524             * Useful when paginating results. Returns a maximum of <code>end -
525             * start</code> instances. <code>start</code> and <code>end</code> are not
526             * primary keys, they are indexes in the result set. Thus, <code>0</code>
527             * refers to the first result in the set. Setting both <code>start</code>
528             * and <code>end</code> to {@link
529             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
530             * result set.
531             * </p>
532             *
533             * @param  roleId the primary key of the role
534             * @param  scopes the scopes
535             * @param  start the lower bound of the range of results
536             * @param  end the upper bound of the range of results (not inclusive)
537             * @return the range of resource permissions for the role at the scopes
538             * @throws SystemException if a system exception occurred
539             */
540            @Override
541            public List<ResourcePermission> getRoleResourcePermissions(
542                            long roleId, int[] scopes, int start, int end)
543                    throws SystemException {
544    
545                    return resourcePermissionFinder.findByR_S(roleId, scopes, start, end);
546            }
547    
548            /**
549             * Returns all the resource permissions where scope = any &#63;.
550             *
551             * <p>
552             * Useful when paginating results. Returns a maximum of <code>end -
553             * start</code> instances. <code>start</code> and <code>end</code> are not
554             * primary keys, they are indexes in the result set. Thus, <code>0</code>
555             * refers to the first result in the set. Setting both <code>start</code>
556             * and <code>end</code> to {@link
557             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
558             * result set.
559             * </p>
560             *
561             * @param  scopes the scopes
562             * @return the resource permissions where scope = any &#63;
563             * @throws SystemException if a system exception occurred
564             */
565            @Override
566            public List<ResourcePermission> getScopeResourcePermissions(int[] scopes)
567                    throws SystemException {
568    
569                    return resourcePermissionPersistence.findByScope(scopes);
570            }
571    
572            /**
573             * Returns <code>true</code> if the resource permission grants permission to
574             * perform the resource action. Note that this method does not ensure that
575             * the resource permission refers to the same type of resource as the
576             * resource action.
577             *
578             * @param  resourcePermission the resource permission
579             * @param  resourceAction the resource action
580             * @return <code>true</code> if the resource permission grants permission to
581             *         perform the resource action
582             */
583            @Override
584            public boolean hasActionId(
585                    ResourcePermission resourcePermission, ResourceAction resourceAction) {
586    
587                    long actionIds = resourcePermission.getActionIds();
588                    long bitwiseValue = resourceAction.getBitwiseValue();
589    
590                    if ((actionIds & bitwiseValue) == bitwiseValue) {
591                            return true;
592                    }
593                    else {
594                            return false;
595                    }
596            }
597    
598            /**
599             * Returns <code>true</code> if the roles have permission at the scope to
600             * perform the action on the resources.
601             *
602             * <p>
603             * Depending on the scope, the value of <code>primKey</code> will have
604             * different meanings. For more information, see {@link
605             * com.liferay.portal.model.impl.ResourcePermissionImpl}.
606             * </p>
607             *
608             * @param  resources the resources
609             * @param  roleIds the primary keys of the roles
610             * @param  actionId the action ID
611             * @return <code>true</code> if any one of the roles has permission to
612             *         perform the action on any one of the resources;
613             *         <code>false</code> otherwise
614             * @throws PortalException if any one of the roles with the primary keys
615             *         could not be found or if a resource action with the name and
616             *         action ID could not be found
617             * @throws SystemException if a system exception occurred
618             */
619            @Override
620            public boolean hasResourcePermission(
621                            List<Resource> resources, long[] roleIds, String actionId)
622                    throws PortalException, SystemException {
623    
624                    // Iterate the list of resources in reverse order to test permissions
625                    // from company scope to individual scope because it is more likely that
626                    // a permission is assigned at a higher scope. Optimizing this method to
627                    // one SQL call may actually slow things down since most of the calls
628                    // will pull from the cache after the first request.
629    
630                    for (int i = resources.size() - 1; i >= 0; i--) {
631                            Resource resource = resources.get(i);
632    
633                            if (hasResourcePermission(
634                                            resource.getCompanyId(), resource.getName(),
635                                            resource.getScope(), resource.getPrimKey(), roleIds,
636                                            actionId)) {
637    
638                                    return true;
639                            }
640                    }
641    
642                    return false;
643            }
644    
645            /**
646             * Returns <code>true</code> if the role has permission at the scope to
647             * perform the action on resources of the type.
648             *
649             * <p>
650             * Depending on the scope, the value of <code>primKey</code> will have
651             * different meanings. For more information, see {@link
652             * com.liferay.portal.model.impl.ResourcePermissionImpl}.
653             * </p>
654             *
655             * @param  companyId the primary key of the company
656             * @param  name the resource's name, which can be either a class name or a
657             *         portlet ID
658             * @param  scope the scope
659             * @param  primKey the primary key
660             * @param  roleId the primary key of the role
661             * @param  actionId the action ID
662             * @return <code>true</code> if the role has permission to perform the
663             *         action on the resource; <code>false</code> otherwise
664             * @throws PortalException if a role with the primary key or a resource
665             *         action with the name and action ID could not be found
666             * @throws SystemException if a system exception occurred
667             */
668            @Override
669            public boolean hasResourcePermission(
670                            long companyId, String name, int scope, String primKey, long roleId,
671                            String actionId)
672                    throws PortalException, SystemException {
673    
674                    ResourcePermission resourcePermission =
675                            resourcePermissionPersistence.fetchByC_N_S_P_R(
676                                    companyId, name, scope, primKey, roleId);
677    
678                    if (resourcePermission == null) {
679                            return false;
680                    }
681    
682                    ResourceAction resourceAction =
683                            resourceActionLocalService.getResourceAction(name, actionId);
684    
685                    if (hasActionId(resourcePermission, resourceAction)) {
686                            return true;
687                    }
688    
689                    return false;
690            }
691    
692            /**
693             * Returns <code>true</code> if the roles have permission at the scope to
694             * perform the action on resources of the type.
695             *
696             * <p>
697             * Depending on the scope, the value of <code>primKey</code> will have
698             * different meanings. For more information, see {@link
699             * com.liferay.portal.model.impl.ResourcePermissionImpl}.
700             * </p>
701             *
702             * @param  companyId the primary key of the company
703             * @param  name the resource's name, which can be either a class name or a
704             *         portlet ID
705             * @param  scope the scope
706             * @param  primKey the primary key
707             * @param  roleIds the primary keys of the roles
708             * @param  actionId the action ID
709             * @return <code>true</code> if any one of the roles has permission to
710             *         perform the action on the resource; <code>false</code> otherwise
711             * @throws PortalException if any one of the roles with the primary keys
712             *         could not be found or if a resource action with the name and
713             *         action ID could not be found
714             * @throws SystemException if a system exception occurred
715             */
716            @Override
717            public boolean hasResourcePermission(
718                            long companyId, String name, int scope, String primKey,
719                            long[] roleIds, String actionId)
720                    throws PortalException, SystemException {
721    
722                    ResourceAction resourceAction =
723                            resourceActionLocalService.getResourceAction(name, actionId);
724    
725                    DB db = DBFactoryUtil.getDB();
726    
727                    String dbType = db.getType();
728    
729                    if ((roleIds.length >
730                                    PropsValues.
731                                            PERMISSIONS_ROLE_RESOURCE_PERMISSION_QUERY_THRESHOLD) &&
732                            !dbType.equals(DB.TYPE_DERBY) &&
733                            !dbType.equals(DB.TYPE_JDATASTORE) &&
734                            !dbType.equals(DB.TYPE_SAP)) {
735    
736                            int count = resourcePermissionFinder.countByC_N_S_P_R_A(
737                                    companyId, name, scope, primKey, roleIds,
738                                    resourceAction.getBitwiseValue());
739    
740                            if (count > 0) {
741                                    return true;
742                            }
743                    }
744                    else {
745                            List<ResourcePermission> resourcePermissions =
746                                    resourcePermissionPersistence.findByC_N_S_P_R(
747                                            companyId, name, scope, primKey, roleIds);
748    
749                            if (resourcePermissions.isEmpty()) {
750                                    return false;
751                            }
752    
753                            for (ResourcePermission resourcePermission : resourcePermissions) {
754                                    if (hasActionId(resourcePermission, resourceAction)) {
755                                            return true;
756                                    }
757                            }
758                    }
759    
760                    return false;
761            }
762    
763            @Override
764            public boolean[] hasResourcePermissions(
765                            long companyId, String name, int scope, String primKey,
766                            long[] roleIds, String actionId)
767                    throws PortalException, SystemException {
768    
769                    ResourceAction resourceAction =
770                            resourceActionLocalService.getResourceAction(name, actionId);
771    
772                    List<ResourcePermission> resourcePermissions =
773                            resourcePermissionPersistence.findByC_N_S_P_R(
774                                    companyId, name, scope, primKey, roleIds);
775    
776                    boolean[] hasResourcePermissions = new boolean[roleIds.length];
777    
778                    if (resourcePermissions.isEmpty()) {
779                            return hasResourcePermissions;
780                    }
781    
782                    for (ResourcePermission resourcePermission : resourcePermissions) {
783                            if (hasActionId(resourcePermission, resourceAction)) {
784                                    long roleId = resourcePermission.getRoleId();
785    
786                                    for (int i = 0; i < roleIds.length; i++) {
787                                            if (roleIds[i] == roleId) {
788                                                    hasResourcePermissions[i] = true;
789                                            }
790                                    }
791                            }
792                    }
793    
794                    return hasResourcePermissions;
795            }
796    
797            /**
798             * Returns <code>true</code> if the role has permission at the scope to
799             * perform the action on the resource.
800             *
801             * <p>
802             * Depending on the scope, the value of <code>primKey</code> will have
803             * different meanings. For more information, see {@link
804             * com.liferay.portal.model.impl.ResourcePermissionImpl}.
805             * </p>
806             *
807             * @param  companyId the primary key of the company
808             * @param  name the resource's name, which can be either a class name or a
809             *         portlet ID
810             * @param  scope the scope
811             * @param  roleId the primary key of the role
812             * @param  actionId the action ID
813             * @return <code>true</code> if the role has permission to perform the
814             *         action on the resource; <code>false</code> otherwise
815             * @throws PortalException if a role with the primary key or a resource
816             *         action with the name and action ID could not be found
817             * @throws SystemException if a system exception occurred
818             */
819            @Override
820            public boolean hasScopeResourcePermission(
821                            long companyId, String name, int scope, long roleId,
822                            String actionId)
823                    throws PortalException, SystemException {
824    
825                    List<ResourcePermission> resourcePermissions =
826                            resourcePermissionPersistence.findByC_N_S(companyId, name, scope);
827    
828                    for (ResourcePermission resourcePermission : resourcePermissions) {
829                            if (hasResourcePermission(
830                                            companyId, name, scope, resourcePermission.getPrimKey(),
831                                            roleId, actionId)) {
832    
833                                    return true;
834                            }
835                    }
836    
837                    return false;
838            }
839    
840            /**
841             * Reassigns all the resource permissions from the source role to the
842             * destination role, and deletes the source role.
843             *
844             * @param  fromRoleId the primary key of the source role
845             * @param  toRoleId the primary key of the destination role
846             * @throws PortalException if a role with the primary key could not be found
847             * @throws SystemException if a system exception occurred
848             */
849            @Override
850            public void mergePermissions(long fromRoleId, long toRoleId)
851                    throws PortalException, SystemException {
852    
853                    Role fromRole = rolePersistence.findByPrimaryKey(fromRoleId);
854                    Role toRole = rolePersistence.findByPrimaryKey(toRoleId);
855    
856                    if (fromRole.getType() != toRole.getType()) {
857                            throw new PortalException("Role types are mismatched");
858                    }
859                    else if (PortalUtil.isSystemRole(toRole.getName())) {
860                            throw new PortalException("Cannot move permissions to system role");
861                    }
862                    else if (PortalUtil.isSystemRole(fromRole.getName())) {
863                            throw new PortalException(
864                                    "Cannot move permissions from system role");
865                    }
866    
867                    List<ResourcePermission> resourcePermissions =
868                            getRoleResourcePermissions(fromRoleId);
869    
870                    for (ResourcePermission resourcePermission : resourcePermissions) {
871                            resourcePermission.setRoleId(toRoleId);
872    
873                            resourcePermissionPersistence.update(resourcePermission);
874                    }
875    
876                    roleLocalService.deleteRole(fromRoleId);
877    
878                    PermissionCacheUtil.clearCache();
879            }
880    
881            /**
882             * Grants the role default permissions to all the resources of the type and
883             * at the scope stored in the resource permission, deletes the resource
884             * permission, and deletes the resource permission's role if it has no
885             * permissions remaining.
886             *
887             * @param  resourcePermissionId the primary key of the resource permission
888             * @param  toRoleId the primary key of the role
889             * @throws PortalException if a resource permission or role with the primary
890             *         key could not be found
891             * @throws SystemException if a system exception occurred
892             */
893            @Override
894            public void reassignPermissions(long resourcePermissionId, long toRoleId)
895                    throws PortalException, SystemException {
896    
897                    ResourcePermission resourcePermission = getResourcePermission(
898                            resourcePermissionId);
899    
900                    long companyId = resourcePermission.getCompanyId();
901                    String name = resourcePermission.getName();
902                    int scope = resourcePermission.getScope();
903                    String primKey = resourcePermission.getPrimKey();
904                    long fromRoleId = resourcePermission.getRoleId();
905    
906                    Role toRole = roleLocalService.getRole(toRoleId);
907    
908                    List<String> actionIds = null;
909    
910                    if (toRole.getType() == RoleConstants.TYPE_REGULAR) {
911                            actionIds = ResourceActionsUtil.getModelResourceActions(name);
912                    }
913                    else {
914                            actionIds = ResourceActionsUtil.getModelResourceGroupDefaultActions(
915                                    name);
916                    }
917    
918                    setResourcePermissions(
919                            companyId, name, scope, primKey, toRoleId,
920                            actionIds.toArray(new String[actionIds.size()]));
921    
922                    resourcePermissionPersistence.remove(resourcePermissionId);
923    
924                    List<ResourcePermission> resourcePermissions =
925                            getRoleResourcePermissions(fromRoleId);
926    
927                    if (resourcePermissions.isEmpty()) {
928                            roleLocalService.deleteRole(fromRoleId);
929                    }
930            }
931    
932            /**
933             * Revokes permission at the scope from the role to perform the action on
934             * resources of the type. For example, this method could be used to revoke a
935             * group scope permission to edit blog posts.
936             *
937             * <p>
938             * Depending on the scope, the value of <code>primKey</code> will have
939             * different meanings. For more information, see {@link
940             * com.liferay.portal.model.impl.ResourcePermissionImpl}.
941             * </p>
942             *
943             * @param  companyId the primary key of the company
944             * @param  name the resource's name, which can be either a class name or a
945             *         portlet ID
946             * @param  scope the scope
947             * @param  primKey the primary key
948             * @param  roleId the primary key of the role
949             * @param  actionId the action ID
950             * @throws PortalException if a role with the primary key or a resource
951             *         action with the name and action ID could not be found
952             * @throws SystemException if a system exception occurred
953             */
954            @Override
955            public void removeResourcePermission(
956                            long companyId, String name, int scope, String primKey, long roleId,
957                            String actionId)
958                    throws PortalException, SystemException {
959    
960                    updateResourcePermission(
961                            companyId, name, scope, primKey, roleId, 0, new String[] {actionId},
962                            ResourcePermissionConstants.OPERATOR_REMOVE);
963    
964                    PermissionCacheUtil.clearCache();
965            }
966    
967            /**
968             * Revokes all permissions at the scope from the role to perform the action
969             * on resources of the type. For example, this method could be used to
970             * revoke all individual scope permissions to edit blog posts from site
971             * members.
972             *
973             * @param  companyId the primary key of the company
974             * @param  name the resource's name, which can be either a class name or a
975             *         portlet ID
976             * @param  scope the scope
977             * @param  roleId the primary key of the role
978             * @param  actionId the action ID
979             * @throws PortalException if a role with the primary key or a resource
980             *         action with the name and action ID could not be found
981             * @throws SystemException if a system exception occurred
982             */
983            @Override
984            public void removeResourcePermissions(
985                            long companyId, String name, int scope, long roleId,
986                            String actionId)
987                    throws PortalException, SystemException {
988    
989                    List<ResourcePermission> resourcePermissions =
990                            resourcePermissionPersistence.findByC_N_S(companyId, name, scope);
991    
992                    for (ResourcePermission resourcePermission : resourcePermissions) {
993                            updateResourcePermission(
994                                    companyId, name, scope, resourcePermission.getPrimKey(), roleId,
995                                    0, new String[] {actionId},
996                                    ResourcePermissionConstants.OPERATOR_REMOVE);
997                    }
998    
999                    PermissionCacheUtil.clearCache();
1000            }
1001    
1002            /**
1003             * Updates the role's permissions at the scope, setting the actions that can
1004             * be performed on resources of the type, also setting the owner of any
1005             * newly created resource permissions. Existing actions are replaced.
1006             *
1007             * <p>
1008             * This method can be used to set permissions at any scope, but it is
1009             * generally only used at the individual scope. For example, it could be
1010             * used to set the guest permissions on a blog post.
1011             * </p>
1012             *
1013             * <p>
1014             * Depending on the scope, the value of <code>primKey</code> will have
1015             * different meanings. For more information, see {@link
1016             * com.liferay.portal.model.impl.ResourcePermissionImpl}.
1017             * </p>
1018             *
1019             * @param  companyId the primary key of the company
1020             * @param  name the resource's name, which can be either a class name or a
1021             *         portlet ID
1022             * @param  scope the scope
1023             * @param  primKey the primary key
1024             * @param  roleId the primary key of the role
1025             * @param  ownerId the primary key of the owner (generally the user that
1026             *         created the resource)
1027             * @param  actionIds the action IDs of the actions
1028             * @throws PortalException if a role with the primary key or a resource
1029             *         action with the name and action ID could not be found
1030             * @throws SystemException if a system exception occurred
1031             */
1032            @Override
1033            public void setOwnerResourcePermissions(
1034                            long companyId, String name, int scope, String primKey, long roleId,
1035                            long ownerId, String[] actionIds)
1036                    throws PortalException, SystemException {
1037    
1038                    updateResourcePermission(
1039                            companyId, name, scope, primKey, roleId, ownerId, actionIds,
1040                            ResourcePermissionConstants.OPERATOR_SET);
1041            }
1042    
1043            /**
1044             * Updates the role's permissions at the scope, setting the actions that can
1045             * be performed on resources of the type. Existing actions are replaced.
1046             *
1047             * <p>
1048             * This method can be used to set permissions at any scope, but it is
1049             * generally only used at the individual scope. For example, it could be
1050             * used to set the guest permissions on a blog post.
1051             * </p>
1052             *
1053             * <p>
1054             * Depending on the scope, the value of <code>primKey</code> will have
1055             * different meanings. For more information, see {@link
1056             * com.liferay.portal.model.impl.ResourcePermissionImpl}.
1057             * </p>
1058             *
1059             * @param  companyId the primary key of the company
1060             * @param  name the resource's name, which can be either a class name or a
1061             *         portlet ID
1062             * @param  scope the scope
1063             * @param  primKey the primary key
1064             * @param  roleId the primary key of the role
1065             * @param  actionIds the action IDs of the actions
1066             * @throws PortalException if a role with the primary key or a resource
1067             *         action with the name and action ID could not be found
1068             * @throws SystemException if a system exception occurred
1069             */
1070            @Override
1071            public void setResourcePermissions(
1072                            long companyId, String name, int scope, String primKey, long roleId,
1073                            String[] actionIds)
1074                    throws PortalException, SystemException {
1075    
1076                    updateResourcePermission(
1077                            companyId, name, scope, primKey, roleId, 0, actionIds,
1078                            ResourcePermissionConstants.OPERATOR_SET);
1079            }
1080    
1081            /**
1082             * Updates the role's permissions at the scope, setting the actions that can
1083             * be performed on resources of the type. Existing actions are replaced.
1084             *
1085             * <p>
1086             * This method can be used to set permissions at any scope, but it is
1087             * generally only used at the individual scope. For example, it could be
1088             * used to set the guest permissions on a blog post.
1089             * </p>
1090             *
1091             * <p>
1092             * Depending on the scope, the value of <code>primKey</code> will have
1093             * different meanings. For more information, see {@link
1094             * com.liferay.portal.model.impl.ResourcePermissionImpl}.
1095             * </p>
1096             *
1097             * @param  companyId the primary key of the company
1098             * @param  name the resource's name, which can be either a class name or a
1099             *         portlet ID
1100             * @param  scope the scope
1101             * @param  primKey the primary key
1102             * @param  roleIdsToActionIds a map of role IDs to action IDs of the actions
1103             * @throws PortalException if a role with the primary key or a resource
1104             *         action with the name and action ID could not be found
1105             * @throws SystemException if a system exception occurred
1106             */
1107            @Override
1108            public void setResourcePermissions(
1109                            long companyId, String name, int scope, String primKey,
1110                            Map<Long, String[]> roleIdsToActionIds)
1111                    throws PortalException, SystemException {
1112    
1113                    updateResourcePermission(
1114                            companyId, name, scope, primKey, 0, roleIdsToActionIds);
1115            }
1116    
1117            protected void doUpdateResourcePermission(
1118                            long companyId, String name, int scope, String primKey,
1119                            long ownerId, long roleId, String[] actionIds, int operator,
1120                            boolean fetch)
1121                    throws PortalException, SystemException {
1122    
1123                    ResourcePermission resourcePermission = null;
1124    
1125                    Map<Long, ResourcePermission> resourcePermissionsMap =
1126                            ResourcePermissionsThreadLocal.getResourcePermissions();
1127    
1128                    if (resourcePermissionsMap != null) {
1129                            resourcePermission = resourcePermissionsMap.get(roleId);
1130                    }
1131                    else if (fetch) {
1132                            resourcePermission = resourcePermissionPersistence.fetchByC_N_S_P_R(
1133                                    companyId, name, scope, primKey, roleId);
1134                    }
1135    
1136                    if (resourcePermission == null) {
1137                            if (((operator == ResourcePermissionConstants.OPERATOR_ADD) ||
1138                                     (operator == ResourcePermissionConstants.OPERATOR_SET)) &&
1139                                    (actionIds.length == 0)) {
1140    
1141                                    return;
1142                            }
1143    
1144                            if (operator == ResourcePermissionConstants.OPERATOR_REMOVE) {
1145                                    return;
1146                            }
1147    
1148                            long resourcePermissionId = counterLocalService.increment(
1149                                    ResourcePermission.class.getName());
1150    
1151                            resourcePermission = resourcePermissionPersistence.create(
1152                                    resourcePermissionId);
1153    
1154                            resourcePermission.setCompanyId(companyId);
1155                            resourcePermission.setName(name);
1156                            resourcePermission.setScope(scope);
1157                            resourcePermission.setPrimKey(primKey);
1158                            resourcePermission.setRoleId(roleId);
1159                            resourcePermission.setOwnerId(ownerId);
1160                    }
1161    
1162                    List<String> unsupportedActionIds = Collections.emptyList();
1163    
1164                    if (((operator == ResourcePermissionConstants.OPERATOR_ADD) ||
1165                             (operator == ResourcePermissionConstants.OPERATOR_SET)) &&
1166                            isGuestRoleId(companyId, roleId)) {
1167    
1168                            unsupportedActionIds =
1169                                    ResourceActionsUtil.getResourceGuestUnsupportedActions(
1170                                            name, name);
1171                    }
1172    
1173                    long actionIdsLong = resourcePermission.getActionIds();
1174    
1175                    if (operator == ResourcePermissionConstants.OPERATOR_SET) {
1176                            actionIdsLong = 0;
1177                    }
1178    
1179                    for (String actionId : actionIds) {
1180                            if (actionId == null) {
1181                                    break;
1182                            }
1183    
1184                            if (unsupportedActionIds.contains(actionId)) {
1185                                    throw new PrincipalException(
1186                                            actionId + "is not supported by role " + roleId);
1187                            }
1188    
1189                            ResourceAction resourceAction =
1190                                    resourceActionLocalService.getResourceAction(name, actionId);
1191    
1192                            if ((operator == ResourcePermissionConstants.OPERATOR_ADD) ||
1193                                    (operator == ResourcePermissionConstants.OPERATOR_SET)) {
1194    
1195                                    actionIdsLong |= resourceAction.getBitwiseValue();
1196                            }
1197                            else {
1198                                    actionIdsLong =
1199                                            actionIdsLong & (~resourceAction.getBitwiseValue());
1200                            }
1201                    }
1202    
1203                    resourcePermission.setActionIds(actionIdsLong);
1204    
1205                    resourcePermissionPersistence.update(resourcePermission);
1206    
1207                    PermissionCacheUtil.clearCache();
1208    
1209                    SearchEngineUtil.updatePermissionFields(name, primKey);
1210            }
1211    
1212            protected void doUpdateResourcePermission(
1213                            long companyId, String name, int scope, String primKey,
1214                            long ownerId, Map<Long, String[]> roleIdsToActionIds)
1215                    throws PortalException, SystemException {
1216    
1217                    boolean flushEnabled = PermissionThreadLocal.isFlushEnabled();
1218    
1219                    PermissionThreadLocal.setIndexEnabled(false);
1220    
1221                    try {
1222                            long[] roleIds = ArrayUtil.toLongArray(roleIdsToActionIds.keySet());
1223    
1224                            List<ResourcePermission> resourcePermissions =
1225                                    resourcePermissionPersistence.findByC_N_S_P_R(
1226                                            companyId, name, scope, primKey, roleIds);
1227    
1228                            for (ResourcePermission resourcePermission : resourcePermissions) {
1229                                    long roleId = resourcePermission.getRoleId();
1230                                    String[] actionIds = roleIdsToActionIds.remove(roleId);
1231    
1232                                    doUpdateResourcePermission(
1233                                            companyId, name, scope, primKey, ownerId, roleId, actionIds,
1234                                            ResourcePermissionConstants.OPERATOR_SET, true);
1235                            }
1236    
1237                            if (roleIdsToActionIds.isEmpty()) {
1238                                    return;
1239                            }
1240    
1241                            for (Map.Entry<Long, String[]> entry :
1242                                            roleIdsToActionIds.entrySet()) {
1243    
1244                                    long roleId = entry.getKey();
1245                                    String[] actionIds = entry.getValue();
1246    
1247                                    doUpdateResourcePermission(
1248                                            companyId, name, scope, primKey, ownerId, roleId, actionIds,
1249                                            ResourcePermissionConstants.OPERATOR_SET, false);
1250                            }
1251                    }
1252                    finally {
1253                            PermissionThreadLocal.setIndexEnabled(flushEnabled);
1254    
1255                            PermissionCacheUtil.clearCache();
1256    
1257                            SearchEngineUtil.updatePermissionFields(name, primKey);
1258                    }
1259            }
1260    
1261            protected boolean isGuestRoleId(long companyId, long roleId)
1262                    throws PortalException, SystemException {
1263    
1264                    Role guestRole = roleLocalService.getRole(
1265                            companyId, RoleConstants.GUEST);
1266    
1267                    if (roleId == guestRole.getRoleId()) {
1268                            return true;
1269                    }
1270    
1271                    return false;
1272            }
1273    
1274            /**
1275             * Updates the role's permissions at the scope, either adding to, removing
1276             * from, or setting the actions that can be performed on resources of the
1277             * type. Automatically creates a new resource permission if none exists, or
1278             * deletes the existing resource permission if it no longer grants
1279             * permissions to perform any action.
1280             *
1281             * <p>
1282             * Depending on the scope, the value of <code>primKey</code> will have
1283             * different meanings. For more information, see {@link
1284             * com.liferay.portal.model.impl.ResourcePermissionImpl}.
1285             * </p>
1286             *
1287             * @param  companyId the primary key of the company
1288             * @param  name the resource's name, which can be either a class name or a
1289             *         portlet ID
1290             * @param  scope the scope
1291             * @param  primKey the primary key
1292             * @param  roleId the primary key of the role
1293             * @param  ownerId the primary key of the owner
1294             * @param  actionIds the action IDs of the actions
1295             * @param  operator whether to add to, remove from, or set/replace the
1296             *         existing actions. Possible values can be found in {@link
1297             *         ResourcePermissionConstants}.
1298             * @throws PortalException if a role with the primary key or a resource
1299             *         action with the name and action ID could not be found
1300             * @throws SystemException if a system exception occurred
1301             */
1302            protected void updateResourcePermission(
1303                            long companyId, String name, int scope, String primKey, long roleId,
1304                            long ownerId, String[] actionIds, int operator)
1305                    throws PortalException, SystemException {
1306    
1307                    DB db = DBFactoryUtil.getDB();
1308    
1309                    String dbType = db.getType();
1310    
1311                    if (!dbType.equals(DB.TYPE_HYPERSONIC)) {
1312                            doUpdateResourcePermission(
1313                                    companyId, name, scope, primKey, ownerId, roleId, actionIds,
1314                                    operator, true);
1315    
1316                            return;
1317                    }
1318    
1319                    StringBundler sb = new StringBundler(9);
1320    
1321                    sb.append(companyId);
1322                    sb.append(StringPool.POUND);
1323                    sb.append(name);
1324                    sb.append(StringPool.POUND);
1325                    sb.append(scope);
1326                    sb.append(StringPool.POUND);
1327                    sb.append(primKey);
1328                    sb.append(StringPool.POUND);
1329                    sb.append(roleId);
1330    
1331                    Class<?> clazz = getClass();
1332    
1333                    String groupName = clazz.getName();
1334    
1335                    String key = sb.toString();
1336    
1337                    Lock lock = LockRegistry.allocateLock(groupName, key);
1338    
1339                    lock.lock();
1340    
1341                    try {
1342                            doUpdateResourcePermission(
1343                                    companyId, name, scope, primKey, ownerId, roleId, actionIds,
1344                                    operator, true);
1345                    }
1346                    finally {
1347                            lock.unlock();
1348    
1349                            LockRegistry.freeLock(groupName, key);
1350                    }
1351            }
1352    
1353            /**
1354             * Updates the role's permissions at the scope, either adding to, removing
1355             * from, or setting the actions that can be performed on resources of the
1356             * type. Automatically creates a new resource permission if none exists, or
1357             * deletes the existing resource permission if it no longer grants
1358             * permissions to perform any action.
1359             *
1360             * <p>
1361             * Depending on the scope, the value of <code>primKey</code> will have
1362             * different meanings. For more information, see {@link
1363             * com.liferay.portal.model.impl.ResourcePermissionImpl}.
1364             * </p>
1365             *
1366             * @param  companyId the primary key of the company
1367             * @param  name the resource's name, which can be either a class name or a
1368             *         portlet ID
1369             * @param  scope the scope
1370             * @param  primKey the primary key
1371             * @param  ownerId the primary key of the owner
1372             * @throws PortalException if a role with the primary key or a resource
1373             *         action with the name and action ID could not be found
1374             * @throws SystemException if a system exception occurred
1375             */
1376            protected void updateResourcePermission(
1377                            long companyId, String name, int scope, String primKey,
1378                            long ownerId, Map<Long, String[]> roleIdsToActionIds)
1379                    throws PortalException, SystemException {
1380    
1381                    DB db = DBFactoryUtil.getDB();
1382    
1383                    String dbType = db.getType();
1384    
1385                    if (!dbType.equals(DB.TYPE_HYPERSONIC)) {
1386                            doUpdateResourcePermission(
1387                                    companyId, name, scope, primKey, ownerId, roleIdsToActionIds);
1388    
1389                            return;
1390                    }
1391    
1392                    StringBundler sb = new StringBundler(9);
1393    
1394                    sb.append(companyId);
1395                    sb.append(StringPool.POUND);
1396                    sb.append(name);
1397                    sb.append(StringPool.POUND);
1398                    sb.append(scope);
1399                    sb.append(StringPool.POUND);
1400                    sb.append(primKey);
1401                    sb.append(StringPool.POUND);
1402                    sb.append(StringUtil.merge(roleIdsToActionIds.keySet()));
1403    
1404                    Class<?> clazz = getClass();
1405    
1406                    String groupName = clazz.getName();
1407    
1408                    String key = sb.toString();
1409    
1410                    Lock lock = LockRegistry.allocateLock(groupName, key);
1411    
1412                    lock.lock();
1413    
1414                    try {
1415                            doUpdateResourcePermission(
1416                                    companyId, name, scope, primKey, ownerId, roleIdsToActionIds);
1417                    }
1418                    finally {
1419                            lock.unlock();
1420    
1421                            LockRegistry.freeLock(groupName, key);
1422                    }
1423            }
1424    
1425            private static final String _FIND_MISSING_RESOURCE_PERMISSIONS =
1426                    ResourcePermissionLocalServiceImpl.class.getName() +
1427                            ".findMissingResourcePermissions";
1428    
1429            private static final String _UPDATE_ACTION_IDS =
1430                    ResourcePermissionLocalServiceImpl.class.getName() + ".updateActionIds";
1431    
1432    }