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.lang;
016    
017    import com.liferay.portal.kernel.util.ServerDetector;
018    import com.liferay.portal.security.pacl.PortalSecurityManagerImpl;
019    import com.liferay.portal.util.PropsValues;
020    
021    /**
022     * @author Brian Wing Shun Chan
023     * @author Raymond Augé
024     * @author Zsolt Berentey
025     */
026    public class SecurityManagerUtil {
027    
028            public static PortalSecurityManager getPortalSecurityManager() {
029                    return _portalSecurityManager;
030            }
031    
032            public static void init() {
033                    if (_portalSecurityManagerStrategy != null) {
034                            return;
035                    }
036    
037                    _portalSecurityManagerStrategy = PortalSecurityManagerStrategy.parse(
038                            PropsValues.PORTAL_SECURITY_MANAGER_STRATEGY);
039    
040                    if ((_portalSecurityManagerStrategy ==
041                                    PortalSecurityManagerStrategy.LIFERAY) ||
042                            (_portalSecurityManagerStrategy ==
043                                    PortalSecurityManagerStrategy.SMART)) {
044    
045                            _portalSecurityManager = new PortalSecurityManagerImpl();
046                    }
047    
048                    if (_portalSecurityManagerStrategy ==
049                                    PortalSecurityManagerStrategy.LIFERAY) {
050    
051                            System.setSecurityManager((SecurityManager)_portalSecurityManager);
052                    }
053                    else if (_portalSecurityManagerStrategy ==
054                                            PortalSecurityManagerStrategy.NONE) {
055    
056                            System.setSecurityManager(null);
057                    }
058            }
059    
060            public static boolean isDefault() {
061                    init();
062    
063                    if (_portalSecurityManagerStrategy ==
064                                    PortalSecurityManagerStrategy.DEFAULT) {
065    
066                            return true;
067                    }
068    
069                    return false;
070            }
071    
072            public static boolean isLiferay() {
073                    init();
074    
075                    if (_portalSecurityManagerStrategy ==
076                                    PortalSecurityManagerStrategy.LIFERAY) {
077    
078                            return true;
079                    }
080    
081                    return false;
082            }
083    
084            public static boolean isNone() {
085                    init();
086    
087                    if (_portalSecurityManagerStrategy ==
088                                    PortalSecurityManagerStrategy.NONE) {
089    
090                            return true;
091                    }
092    
093                    return false;
094            }
095    
096            public static boolean isPACLDisabled() {
097                    if (isDefault() || isNone()) {
098                            return true;
099                    }
100    
101                    return false;
102            }
103    
104            public static boolean isSmart() {
105                    init();
106    
107                    if (_portalSecurityManagerStrategy ==
108                                    PortalSecurityManagerStrategy.SMART) {
109    
110                            return true;
111                    }
112    
113                    return false;
114            }
115    
116            private static PortalSecurityManager _portalSecurityManager;
117            private static PortalSecurityManagerStrategy _portalSecurityManagerStrategy;
118    
119            private enum PortalSecurityManagerStrategy {
120    
121                    DEFAULT, LIFERAY, NONE, SMART;
122    
123                    public static PortalSecurityManagerStrategy parse(String value) {
124                            if (value.equals("default")) {
125                                    return DEFAULT;
126                            }
127                            else if (value.equals("liferay")) {
128                                    return LIFERAY;
129                            }
130                            else if (value.equals("smart")) {
131                                    if (ServerDetector.isWebSphere()) {
132                                            return NONE;
133                                    }
134    
135                                    return SMART;
136                            }
137    
138                            return NONE;
139                    }
140    
141            }
142    
143    }