001    /**
002     * Copyright (c) 2000-present 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.security.membershippolicy;
016    
017    import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.model.Role;
020    import com.liferay.portal.service.RoleLocalServiceUtil;
021    
022    /**
023     * @author Roberto D??az
024     * @author Sergio Gonz??lez
025     */
026    public abstract class BaseRoleMembershipPolicy implements RoleMembershipPolicy {
027    
028            @Override
029            @SuppressWarnings("unused")
030            public boolean isRoleAllowed(long userId, long roleId)
031                    throws PortalException {
032    
033                    try {
034                            checkRoles(new long[] {userId}, new long[] {roleId}, null);
035                    }
036                    catch (Exception e) {
037                            return false;
038                    }
039    
040                    return true;
041            }
042    
043            @Override
044            @SuppressWarnings("unused")
045            public boolean isRoleRequired(long userId, long roleId)
046                    throws PortalException {
047    
048                    try {
049                            checkRoles(new long[] {userId}, null, new long[] {roleId});
050                    }
051                    catch (Exception e) {
052                            return true;
053                    }
054    
055                    return false;
056            }
057    
058            @Override
059            public void verifyPolicy() throws PortalException {
060                    ActionableDynamicQuery actionableDynamicQuery =
061                            RoleLocalServiceUtil.getActionableDynamicQuery();
062    
063                    actionableDynamicQuery.setPerformActionMethod(
064                            new ActionableDynamicQuery.PerformActionMethod() {
065    
066                                    @Override
067                                    public void performAction(Object object)
068                                            throws PortalException {
069    
070                                            Role role = (Role)object;
071    
072                                            verifyPolicy(role);
073                                    }
074    
075                            });
076    
077                    actionableDynamicQuery.performActions();
078            }
079    
080            @Override
081            public void verifyPolicy(Role role) throws PortalException {
082                    verifyPolicy(role, null, null);
083            }
084    
085    }