001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }