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            @SuppressWarnings("unused")
031            public boolean isMembershipAllowed(long userId, long userGroupId)
032                    throws PortalException, SystemException {
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            @SuppressWarnings("unused")
046            public boolean isMembershipRequired(long userId, long userGroupId)
047                    throws PortalException, SystemException {
048    
049                    try {
050                            checkMembership(
051                                    new long[] {userId}, null, new long[] {userGroupId});
052                    }
053                    catch (Exception e) {
054                            return true;
055                    }
056    
057                    return false;
058            }
059    
060            public void verifyPolicy() throws PortalException, SystemException {
061                    ActionableDynamicQuery actionableDynamicQuery =
062                            new UserGroupActionableDynamicQuery() {
063    
064                            @Override
065                            protected void performAction(Object object)
066                                    throws PortalException, SystemException {
067    
068                                    UserGroup userGroup = (UserGroup)object;
069    
070                                    verifyPolicy(userGroup);
071                            }
072    
073                    };
074    
075                    actionableDynamicQuery.performActions();
076            }
077    
078            public void verifyPolicy(UserGroup userGroup)
079                    throws PortalException, SystemException {
080    
081                    verifyPolicy(userGroup, null, null);
082            }
083    
084    }