001
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
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 }