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            @Override
035            public String getAuthType() {
036                    return AUTH_TYPE;
037            }
038    
039            @Override
040            public AuthVerifierResult verify(
041                            AccessControlContext accessControlContext, Properties properties)
042                    throws AuthException {
043    
044                    try {
045                            AuthVerifierResult authVerifierResult = new AuthVerifierResult();
046    
047                            HttpServletRequest request = accessControlContext.getRequest();
048    
049                            User user = PortalUtil.getUser(request);
050    
051                            if (user == null) {
052                                    return authVerifierResult;
053                            }
054    
055                            authVerifierResult.setState(AuthVerifierResult.State.SUCCESS);
056                            authVerifierResult.setUserId(user.getUserId());
057    
058                            return authVerifierResult;
059                    }
060                    catch (PortalException pe) {
061                            throw new AuthException(pe);
062                    }
063                    catch (SystemException se) {
064                            throw new AuthException(se);
065                    }
066            }
067    
068    }