001    /**
002     * Copyright (c) 2000-2012 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.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    }