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