001    /**
002     * Copyright (c) 2000-present 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.servlet;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.model.User;
021    import com.liferay.portal.security.auth.PrincipalThreadLocal;
022    import com.liferay.portal.service.UserLocalServiceUtil;
023    import com.liferay.portal.util.PortalInstances;
024    
025    import javax.servlet.http.HttpServletRequest;
026    
027    /**
028     * @author Igor Spasic
029     */
030    public class UserResolver {
031    
032            public UserResolver(HttpServletRequest request) throws PortalException {
033                    long companyId = ParamUtil.getLong(request, "companyId");
034                    User user = null;
035    
036                    String remoteUser = request.getRemoteUser();
037    
038                    long userId = GetterUtil.getLong(remoteUser);
039    
040                    if (userId == 0) {
041                            remoteUser = null;
042                    }
043    
044                    if (remoteUser != null) {
045                            PrincipalThreadLocal.setName(remoteUser);
046    
047                            user = UserLocalServiceUtil.getUserById(userId);
048    
049                            if (companyId == 0) {
050                                    companyId = user.getCompanyId();
051                            }
052                    }
053                    else {
054                            if (companyId == 0) {
055                                    companyId = PortalInstances.getCompanyId(request);
056                            }
057    
058                            if (companyId != 0) {
059                                    user = UserLocalServiceUtil.getDefaultUser(companyId);
060                            }
061                    }
062    
063                    _companyId = companyId;
064                    _user = user;
065            }
066    
067            public long getCompanyId() {
068                    return _companyId;
069            }
070    
071            public User getUser() {
072                    return _user;
073            }
074    
075            private final long _companyId;
076            private final User _user;
077    
078    }