if(user == null){
return path;
}
PermissionAPI pAPI = APILocator.getPermissionAPI();
HostAPI hostAPI = APILocator.getHostAPI();
Host host = null;
// if someone is changing hosts as a parameter, check permissions
if(UtilMethods.isSet(req.getParameter("host_id"))){
try{
host = hostAPI.find(req.getParameter("host_id"), user, false);
if(host != null && pAPI.doesUserHavePermission(host, PermissionAPI.PERMISSION_READ, user, false)){
req.getSession().setAttribute(com.dotmarketing.util.WebKeys.CMS_SELECTED_HOST_ID, req.getParameter("host_id"));
UserUtil.setLastHost(user, host);
}
else{
UserUtil.setLastHost(user, null);
req.getSession().removeAttribute(com.dotmarketing.util.WebKeys.CMS_SELECTED_HOST_ID);
Logger.info(this.getClass(), "user " + user.getUserId() + " does not have permission to host " +req.getParameter("host_id"));
}
}
catch(Exception e){
req.getSession().removeAttribute(com.dotmarketing.util.WebKeys.CMS_SELECTED_HOST_ID);
Logger.error(this.getClass(), "user " + user.getUserId() + " does not have permission to host " +req.getParameter("host_id"));
}
}
// else check if the user as permissions to the host in their session (can change, login as, etc..)
else if(UtilMethods.isSet(req.getSession().getAttribute(com.dotmarketing.util.WebKeys.CMS_SELECTED_HOST_ID)) ){
String x = (String) req.getSession().getAttribute(com.dotmarketing.util.WebKeys.CMS_SELECTED_HOST_ID);
try{
host = hostAPI.find(x, user, false);
if(host != null && pAPI.doesUserHavePermission(host, PermissionAPI.PERMISSION_READ, user, false)){
req.getSession().setAttribute(com.dotmarketing.util.WebKeys.CMS_SELECTED_HOST_ID, host.getIdentifier());
}
else{
Logger.error(this.getClass(), "user " + user.getUserId() + " does not have permission to host " +req.getSession().getAttribute(com.dotmarketing.util.WebKeys.CMS_SELECTED_HOST_ID));
req.getSession().removeAttribute(com.dotmarketing.util.WebKeys.CMS_SELECTED_HOST_ID);
UserUtil.setLastHost(user,null);
}
}
catch(Exception e){
Logger.error(this.getClass(), "user " + user.getUserId() + " does not have permission to host " +req.getSession().getAttribute(com.dotmarketing.util.WebKeys.CMS_SELECTED_HOST_ID));
req.getSession().removeAttribute(com.dotmarketing.util.WebKeys.CMS_SELECTED_HOST_ID);
}
}
// try to get the last host from the user record
else{
try {
host = UserUtil.getLastHost(user);
req.getSession().setAttribute(com.dotmarketing.util.WebKeys.CMS_SELECTED_HOST_ID, host.getIdentifier());
} catch (DotDataException e) {
Logger.debug(this.getClass(), e.toString());
} catch (DotSecurityException e) {
Logger.warn(this.getClass(), "User " + user.getUserId() + " does not have permissions to host " + e.toString());
}
}
// finally, if user does not have a host
// set to default host if it is not in the session
if(!UtilMethods.isSet(req.getSession().getAttribute(com.dotmarketing.util.WebKeys.CMS_SELECTED_HOST_ID))){
try{
List<Host> list = hostAPI.getHostsWithPermission(PermissionAPI.PERMISSION_READ, user, false);
host = null;
for(Host h : list) {
if(!h.isSystemHost()){
if(h.isDefault()){
host = h;