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.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.ServerDetector;
020    import com.liferay.portal.kernel.util.ServiceLoader;
021    import com.liferay.portal.util.PropsValues;
022    
023    import java.util.List;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     * @author Raymond Aug??
028     * @author Zsolt Berentey
029     */
030    public class SecurityManagerUtil {
031    
032            public static void applySmartStrategy() {
033                    if ((_portalSecurityManagerStrategy ==
034                                    PortalSecurityManagerStrategy.SMART) &&
035                            (_originalSecurityManager == null) &&
036                            ServerDetector.isWebSphere()) {
037    
038                            System.setSecurityManager(null);
039                    }
040            }
041    
042            public static PortalSecurityManager getPortalSecurityManager() {
043                    return _portalSecurityManager;
044            }
045    
046            public static void init() {
047                    if (_portalSecurityManagerStrategy != null) {
048                            return;
049                    }
050    
051                    _originalSecurityManager = System.getSecurityManager();
052    
053                    _portalSecurityManagerStrategy = PortalSecurityManagerStrategy.parse(
054                            PropsValues.PORTAL_SECURITY_MANAGER_STRATEGY);
055    
056                    if ((_portalSecurityManagerStrategy ==
057                                    PortalSecurityManagerStrategy.LIFERAY) ||
058                            (_portalSecurityManagerStrategy ==
059                                    PortalSecurityManagerStrategy.SMART)) {
060    
061                            loadPortalSecurityManager();
062    
063                            if (_portalSecurityManager == null) {
064                                    _portalSecurityManagerStrategy =
065                                            PortalSecurityManagerStrategy.DEFAULT;
066    
067                                    if (_log.isInfoEnabled()) {
068                                            _log.info(
069                                                    "No portal security manager implementation was " +
070                                                            "located. Continuing with the default security " +
071                                                                    "strategy.");
072                                    }
073    
074                                    return;
075                            }
076                    }
077    
078                    if (_portalSecurityManagerStrategy ==
079                                    PortalSecurityManagerStrategy.LIFERAY) {
080    
081                            System.setSecurityManager((SecurityManager)_portalSecurityManager);
082                    }
083                    else if (_portalSecurityManagerStrategy ==
084                                            PortalSecurityManagerStrategy.NONE) {
085    
086                            System.setSecurityManager(null);
087                    }
088            }
089    
090            public static boolean isActive() {
091                    if (_portalSecurityManager == null) {
092                            return false;
093                    }
094    
095                    return _portalSecurityManager.isActive();
096            }
097    
098            public static boolean isDefault() {
099                    init();
100    
101                    if (_portalSecurityManagerStrategy ==
102                                    PortalSecurityManagerStrategy.DEFAULT) {
103    
104                            return true;
105                    }
106    
107                    return false;
108            }
109    
110            public static boolean isLiferay() {
111                    init();
112    
113                    if (_portalSecurityManagerStrategy ==
114                                    PortalSecurityManagerStrategy.LIFERAY) {
115    
116                            return true;
117                    }
118    
119                    return false;
120            }
121    
122            public static boolean isNone() {
123                    init();
124    
125                    if (_portalSecurityManagerStrategy ==
126                                    PortalSecurityManagerStrategy.NONE) {
127    
128                            return true;
129                    }
130    
131                    return false;
132            }
133    
134            public static boolean isPACLDisabled() {
135                    if (isDefault() || isNone()) {
136                            return true;
137                    }
138    
139                    return false;
140            }
141    
142            public static boolean isSmart() {
143                    init();
144    
145                    if (_portalSecurityManagerStrategy ==
146                                    PortalSecurityManagerStrategy.SMART) {
147    
148                            return true;
149                    }
150    
151                    return false;
152            }
153    
154            private static void loadPortalSecurityManager() {
155                    try {
156                            List<PortalSecurityManager> portalSecurityManagers =
157                                    ServiceLoader.load(PortalSecurityManager.class);
158    
159                            if (portalSecurityManagers.isEmpty()) {
160                                    return;
161                            }
162    
163                            _portalSecurityManager = portalSecurityManagers.get(0);
164                    }
165                    catch (Exception e) {
166                            _log.error(e, e);
167                    }
168            }
169    
170            private static Log _log = LogFactoryUtil.getLog(SecurityManagerUtil.class);
171    
172            private static SecurityManager _originalSecurityManager;
173            private static PortalSecurityManager _portalSecurityManager;
174            private static PortalSecurityManagerStrategy _portalSecurityManagerStrategy;
175    
176            private enum PortalSecurityManagerStrategy {
177    
178                    DEFAULT, LIFERAY, NONE, SMART;
179    
180                    public static PortalSecurityManagerStrategy parse(String value) {
181                            if (PropsValues.TCK_URL) {
182                                    return NONE;
183                            }
184                            else if (value.equals("default")) {
185                                    return DEFAULT;
186                            }
187                            else if (value.equals("liferay")) {
188                                    return LIFERAY;
189                            }
190                            else if (value.equals("smart")) {
191                                    return SMART;
192                            }
193    
194                            return NONE;
195                    }
196    
197            }
198    
199    }