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                    if (PropsValues.TCK_URL) {
054                            _portalSecurityManagerStrategy = PortalSecurityManagerStrategy.NONE;
055                    }
056                    else {
057                            _portalSecurityManagerStrategy =
058                                    PortalSecurityManagerStrategy.parse(
059                                            PropsValues.PORTAL_SECURITY_MANAGER_STRATEGY);
060                    }
061    
062                    if ((_portalSecurityManagerStrategy ==
063                                    PortalSecurityManagerStrategy.LIFERAY) ||
064                            (_portalSecurityManagerStrategy ==
065                                    PortalSecurityManagerStrategy.SMART)) {
066    
067                            loadPortalSecurityManager();
068    
069                            if (_portalSecurityManager == null) {
070                                    _portalSecurityManagerStrategy =
071                                            PortalSecurityManagerStrategy.DEFAULT;
072    
073                                    if (_log.isInfoEnabled()) {
074                                            _log.info(
075                                                    "No portal security manager implementation was " +
076                                                            "located. Continuing with the default security " +
077                                                                    "strategy.");
078                                    }
079    
080                                    return;
081                            }
082                    }
083    
084                    if (_portalSecurityManagerStrategy ==
085                                    PortalSecurityManagerStrategy.LIFERAY) {
086    
087                            System.setSecurityManager((SecurityManager)_portalSecurityManager);
088                    }
089                    else if (_portalSecurityManagerStrategy ==
090                                            PortalSecurityManagerStrategy.NONE) {
091    
092                            System.setSecurityManager(null);
093                    }
094            }
095    
096            public static boolean isActive() {
097                    if (_portalSecurityManager == null) {
098                            return false;
099                    }
100    
101                    return _portalSecurityManager.isActive();
102            }
103    
104            public static boolean isDefault() {
105                    init();
106    
107                    if (_portalSecurityManagerStrategy ==
108                                    PortalSecurityManagerStrategy.DEFAULT) {
109    
110                            return true;
111                    }
112    
113                    return false;
114            }
115    
116            public static boolean isLiferay() {
117                    init();
118    
119                    if (_portalSecurityManagerStrategy ==
120                                    PortalSecurityManagerStrategy.LIFERAY) {
121    
122                            return true;
123                    }
124    
125                    return false;
126            }
127    
128            public static boolean isNone() {
129                    init();
130    
131                    if (_portalSecurityManagerStrategy ==
132                                    PortalSecurityManagerStrategy.NONE) {
133    
134                            return true;
135                    }
136    
137                    return false;
138            }
139    
140            public static boolean isPACLDisabled() {
141                    if (isDefault() || isNone()) {
142                            return true;
143                    }
144    
145                    return false;
146            }
147    
148            public static boolean isSmart() {
149                    init();
150    
151                    if (_portalSecurityManagerStrategy ==
152                                    PortalSecurityManagerStrategy.SMART) {
153    
154                            return true;
155                    }
156    
157                    return false;
158            }
159    
160            private static void loadPortalSecurityManager() {
161                    try {
162                            List<PortalSecurityManager> portalSecurityManagers =
163                                    ServiceLoader.load(PortalSecurityManager.class);
164    
165                            if (portalSecurityManagers.isEmpty()) {
166                                    return;
167                            }
168    
169                            _portalSecurityManager = portalSecurityManagers.get(0);
170                    }
171                    catch (Exception e) {
172                            _log.error(e, e);
173                    }
174            }
175    
176            private static Log _log = LogFactoryUtil.getLog(SecurityManagerUtil.class);
177    
178            private static SecurityManager _originalSecurityManager;
179            private static PortalSecurityManager _portalSecurityManager;
180            private static PortalSecurityManagerStrategy _portalSecurityManagerStrategy;
181    
182            private enum PortalSecurityManagerStrategy {
183    
184                    DEFAULT, LIFERAY, NONE, SMART;
185    
186                    public static PortalSecurityManagerStrategy parse(String value) {
187                            if (value.equals("default")) {
188                                    return DEFAULT;
189                            }
190                            else if (value.equals("liferay")) {
191                                    return LIFERAY;
192                            }
193                            else if (value.equals("smart")) {
194                                    return SMART;
195                            }
196    
197                            return NONE;
198                    }
199    
200            }
201    
202    }