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            @Override
030            @SuppressWarnings("unused")
031            public boolean isRoleAllowed(long userId, long roleId)
032                    throws PortalException, SystemException {
033    
034                    try {
035                            checkRoles(new long[] {userId}, new long[] {roleId}, null);
036                    }
037                    catch (Exception e) {
038                            return false;
039                    }
040    
041                    return true;
042            }
043    
044            @Override
045            @SuppressWarnings("unused")
046            public boolean isRoleRequired(long userId, long roleId)
047                    throws PortalException, SystemException {
048    
049                    try {
050                            checkRoles(new long[] {userId}, null, new long[] {roleId});
051                    }
052                    catch (Exception e) {
053                            return true;
054                    }
055    
056                    return false;
057            }
058    
059            @Override
060            public void verifyPolicy() throws PortalException, SystemException {
061                    ActionableDynamicQuery actionableDynamicQuery =
062                            new RoleActionableDynamicQuery() {
063    
064                            @Override
065                            protected void performAction(Object object)
066                                    throws PortalException, SystemException {
067    
068                                    Role role = (Role)object;
069    
070                                    verifyPolicy(role);
071                            }
072    
073                    };
074    
075                    actionableDynamicQuery.performActions();
076            }
077    
078            @Override
079            public void verifyPolicy(Role role)
080                    throws PortalException, SystemException {
081    
082                    verifyPolicy(role, null, null);
083            }
084    
085    }