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.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.kernel.exception.SystemException;
020    import com.liferay.portal.model.Role;
021    import com.liferay.portal.service.persistence.RoleActionableDynamicQuery;
022    
023    /**
024     * @author Roberto Díaz
025     * @author Sergio González
026     */
027    public abstract class BaseRoleMembershipPolicy implements RoleMembershipPolicy {
028    
029            @SuppressWarnings("unused")
030            public boolean isRoleAllowed(long userId, long roleId)
031                    throws PortalException, SystemException {
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            @SuppressWarnings("unused")
044            public boolean isRoleRequired(long userId, long roleId)
045                    throws PortalException, SystemException {
046    
047                    try {
048                            checkRoles(new long[] {userId}, null, new long[] {roleId});
049                    }
050                    catch (Exception e) {
051                            return true;
052                    }
053    
054                    return false;
055            }
056    
057            public void verifyPolicy() throws PortalException, SystemException {
058                    ActionableDynamicQuery actionableDynamicQuery =
059                            new RoleActionableDynamicQuery() {
060    
061                            @Override
062                            protected void performAction(Object object)
063                                    throws PortalException, SystemException {
064    
065                                    Role role = (Role)object;
066    
067                                    verifyPolicy(role);
068                            }
069    
070                    };
071    
072                    actionableDynamicQuery.performActions();
073            }
074    
075            public void verifyPolicy(Role role)
076                    throws PortalException, SystemException {
077    
078                    verifyPolicy(role, null, null);
079            }
080    
081    }