001    /**
002     * Copyright (c) 2000-2012 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.pacl.checker;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.security.pacl.permission.PortalMessageBusPermission;
020    
021    import java.security.Permission;
022    
023    import java.util.Set;
024    import java.util.TreeSet;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class PortalMessageBusChecker extends BaseChecker {
030    
031            public void afterPropertiesSet() {
032                    initListenDestinationNames();
033                    initSendDestinationNames();
034            }
035    
036            public void checkPermission(Permission permission) {
037                    PortalMessageBusPermission portalMessageBusPermission =
038                            (PortalMessageBusPermission)permission;
039    
040                    String name = portalMessageBusPermission.getName();
041                    String destinationName =
042                            portalMessageBusPermission.getDestinationName();
043    
044                    if (name.equals(PORTAL_MESSAGE_BUS_PERMISSION_LISTEN)) {
045                            if (!_listenDestinationNames.contains(destinationName)) {
046                                    throwSecurityException(
047                                            _log,
048                                            "Attempted to listen on destination " + destinationName);
049                            }
050                    }
051                    else if (name.equals(PORTAL_MESSAGE_BUS_PERMISSION_SEND)) {
052                            if (!_sendDestinationNames.contains(destinationName)) {
053                                    throwSecurityException(
054                                            _log, "Attempted to send to " + destinationName);
055                            }
056                    }
057            }
058    
059            protected void initListenDestinationNames() {
060                    _listenDestinationNames = getPropertySet(
061                            "security-manager-message-bus-listen");
062    
063                    if (_log.isDebugEnabled()) {
064                            Set<String> destinationNames = new TreeSet<String>(
065                                    _listenDestinationNames);
066    
067                            for (String destinationName : destinationNames) {
068                                    _log.debug(
069                                            "Allowing message listeners to listen on destination " +
070                                                    destinationName);
071                            }
072                    }
073            }
074    
075            protected void initSendDestinationNames() {
076                    _sendDestinationNames = getPropertySet(
077                            "security-manager-message-bus-send");
078    
079                    if (_log.isDebugEnabled()) {
080                            Set<String> destinationNames = new TreeSet<String>(
081                                    _sendDestinationNames);
082    
083                            for (String destinationName : destinationNames) {
084                                    _log.debug(
085                                            "Allowing the message bus to send to destination " +
086                                                    destinationName);
087                            }
088                    }
089            }
090    
091            private static Log _log = LogFactoryUtil.getLog(
092                    PortalMessageBusChecker.class);
093    
094            private Set<String> _listenDestinationNames;
095            private Set<String> _sendDestinationNames;
096    
097    }