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.UserGroup;
020    import com.liferay.portal.service.UserGroupLocalServiceUtil;
021    
022    /**
023     * @author Roberto D??az
024     * @author Sergio Gonz??lez
025     */
026    public abstract class BaseUserGroupMembershipPolicy
027            implements UserGroupMembershipPolicy {
028    
029            @Override
030            @SuppressWarnings("unused")
031            public boolean isMembershipAllowed(long userId, long userGroupId)
032                    throws PortalException {
033    
034                    try {
035                            checkMembership(
036                                    new long[] {userId}, new long[] {userGroupId}, null);
037                    }
038                    catch (Exception e) {
039                            return false;
040                    }
041    
042                    return true;
043            }
044    
045            @Override
046            @SuppressWarnings("unused")
047            public boolean isMembershipRequired(long userId, long userGroupId)
048                    throws PortalException {
049    
050                    try {
051                            checkMembership(
052                                    new long[] {userId}, null, new long[] {userGroupId});
053                    }
054                    catch (Exception e) {
055                            return true;
056                    }
057    
058                    return false;
059            }
060    
061            @Override
062            public void verifyPolicy() throws PortalException {
063                    ActionableDynamicQuery actionableDynamicQuery =
064                            UserGroupLocalServiceUtil.getActionableDynamicQuery();
065    
066                    actionableDynamicQuery.setPerformActionMethod(
067                            new ActionableDynamicQuery.PerformActionMethod<UserGroup>() {
068    
069                                    @Override
070                                    public void performAction(UserGroup userGroup)
071                                            throws PortalException {
072    
073                                            verifyPolicy(userGroup);
074                                    }
075    
076                            });
077    
078                    actionableDynamicQuery.performActions();
079            }
080    
081            @Override
082            public void verifyPolicy(UserGroup userGroup) throws PortalException {
083                    verifyPolicy(userGroup, null, null);
084            }
085    
086    }