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.auth;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.model.User;
020    import com.liferay.portal.util.PortalUtil;
021    
022    import java.util.Properties;
023    
024    import javax.servlet.http.HttpServletRequest;
025    
026    /**
027     * @author Tomas Polesovsky
028     */
029    public class PortalSessionAuthVerifier implements AuthVerifier {
030    
031            public static final String AUTH_TYPE =
032                    PortalSessionAuthVerifier.class.getSimpleName();
033    
034            public String getAuthType() {
035                    return AUTH_TYPE;
036            }
037    
038            public AuthVerifierResult verify(
039                            AccessControlContext accessControlContext, Properties properties)
040                    throws AuthException {
041    
042                    try {
043                            AuthVerifierResult authVerifierResult = new AuthVerifierResult();
044    
045                            HttpServletRequest request = accessControlContext.getRequest();
046    
047                            User user = PortalUtil.getUser(request);
048    
049                            if (user == null) {
050                                    return authVerifierResult;
051                            }
052    
053                            authVerifierResult.setState(AuthVerifierResult.State.SUCCESS);
054                            authVerifierResult.setUserId(user.getUserId());
055    
056                            return authVerifierResult;
057                    }
058                    catch (PortalException e) {
059                            throw new AuthException(e);
060                    }
061                    catch (SystemException e) {
062                            throw new AuthException(e);
063                    }
064            }
065    
066    }