Files
mdm/src/main/java/com/midm/controller/HomeController.java
T

274 lines
11 KiB
Java

package com.midm.controller;
import com.google.common.base.Strings;
import com.midm.model.org.Site;
import com.midm.model.org.UserToken;
import com.midm.security.UserPrincipal;
import com.mitac.cell.internet.http.McResponse;
import java.io.IOException;
import java.util.UUID;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.view.RedirectView;
@Controller
public class HomeController extends BaseController {
@RequestMapping(
value = {"/", "/index.html"},
method = {RequestMethod.GET}
)
public String home(HttpServletRequest request, HttpServletResponse response, Authentication authentication, @CookieValue(value = "user.token",defaultValue = "") UUID tokenUuid, @CookieValue(value = "site.uuid",defaultValue = "") UUID siteUuid, @CookieValue(value = "locale",defaultValue = "en-US") String locale, Model model, @RequestHeader("host") String hostName, @RequestParam(required = false) String language, @RequestParam(required = false) String siteId) {
if (Strings.isNullOrEmpty(language)) {
language = locale;
}
if (authentication != null) {
UserToken token = (UserToken)authentication.getCredentials();
UserToken existingToken = token == null ? null : this.userService.findToken(token.getTokenUuid());
if (existingToken == null) {
SecurityContextHolder.getContext().setAuthentication((Authentication)null);
authentication = null;
if (token != null && token.getTokenUuid().equals(tokenUuid)) {
tokenUuid = null;
McResponse.setCookie(response, "user.token", (String)null);
}
}
}
this.init(request, response, authentication, language);
model.addAttribute("theme", this.theme);
if (authentication == null && tokenUuid != null) {
UserToken token = this.userService.findToken(tokenUuid);
if (token != null) {
authentication = this.userService.authorizeLogin(tokenUuid);
} else {
McResponse.setCookie(response, "user.token", (String)null);
}
}
if (authentication == null) {
if (!Strings.isNullOrEmpty(siteId)) {
Site site = siteId.equals("default") ? null : this.siteService.findByIdName(siteId);
if (site != null) {
siteUuid = site.getUuid();
model.addAttribute("siteUuid", siteUuid);
McResponse.setCookie(response, "site.uuid", siteUuid.toString());
} else {
siteUuid = null;
}
}
if (siteUuid != null) {
model.addAttribute("siteUuid", siteUuid);
}
model.addAttribute("hasUser", this.userService.hasUser());
model.addAttribute("pageFor", "login");
model.addAttribute("locale", language);
if (locale != null && language != null && !locale.equals(language)) {
McResponse.setCookie(response, "locale", language);
}
return this.getView(authentication, "/login");
} else {
this.user = UserPrincipal.getUser(authentication);
if (this.user == null) {
model.addAttribute("hasUser", this.userService.hasUser());
model.addAttribute("pageFor", "login");
model.addAttribute("locale", language);
if (locale != null && language != null && !locale.equals(language)) {
McResponse.setCookie(response, "locale", language);
}
return this.getView(authentication, "/login");
} else {
return this.getView(authentication, "/index");
}
}
}
@RequestMapping(
value = {"/remoteLogout"},
method = {RequestMethod.GET}
)
public String home(HttpServletRequest request, HttpServletResponse response, Authentication authentication, Model model, @RequestParam(required = false) String language) {
this.init(request, response, authentication, language);
model.addAttribute("theme", this.theme);
return this.getView(authentication, "/remoteLogout");
}
@RequestMapping(
value = {"/"},
method = {RequestMethod.POST}
)
public void homePost(HttpServletRequest request, HttpServletResponse response) {
try {
response.sendError(400);
} catch (IOException var4) {
}
}
@RequestMapping(
value = {"register"},
method = {RequestMethod.GET}
)
public String registerToModelSite(HttpServletRequest request, HttpServletResponse response, Authentication authentication, @CookieValue(value = "user.token",defaultValue = "") UUID tokenUuid, @RequestParam String modelSite, ModelMap model, @RequestParam(required = false) String language) {
this.init(request, response, authentication, language);
model.addAttribute("theme", this.theme);
UserToken token = null;
if (authentication == null && tokenUuid != null) {
token = this.userService.findToken(tokenUuid);
authentication = this.userService.authorizeLogin(tokenUuid);
}
if (authentication == null && token == null) {
model.addAttribute("hasUser", this.userService.hasUser());
model.addAttribute("pageFor", "register");
return this.getView(authentication, "/login");
} else {
return "forward:/";
}
}
@RequestMapping(
value = {"{modelSite}/register"},
method = {RequestMethod.GET}
)
public RedirectView redirectToRegister(ModelMap model, @PathVariable String modelSite, @RequestParam(required = false) String language) {
model.clear();
return new RedirectView("../register?modelSite=" + modelSite);
}
@RequestMapping(
value = {"/bypass", "/dpc"},
method = {RequestMethod.GET}
)
public String bypassMode(HttpServletRequest request, HttpServletResponse response, Authentication authentication, @CookieValue(value = "user.token",defaultValue = "") UUID tokenUuid, ModelMap model, @RequestParam(required = false) String language, @RequestHeader("host") String hostName) {
this.init(request, response, authentication, language);
model.addAttribute("theme", this.theme);
UserToken token = null;
if (authentication == null && tokenUuid != null) {
token = this.userService.findToken(tokenUuid);
authentication = this.userService.authorizeLogin(tokenUuid);
}
if (authentication == null && token == null) {
model.addAttribute("hasUser", this.userService.hasUser());
model.addAttribute("pageFor", "dpc");
return this.getView(authentication, "/login");
} else {
this.user = UserPrincipal.getUser(authentication);
if (this.user.getSettingAsBoolean("keepSignIn")) {
if (token == null && authentication != null) {
token = (UserToken)authentication.getCredentials();
}
if (token != null) {
Cookie cookie = new Cookie("user.token", token.getTokenUuid().toString());
response.addCookie(cookie);
}
} else {
Cookie cookie = new Cookie("user.token", (String)null);
response.addCookie(cookie);
}
return this.getView(authentication, "/index");
}
}
@RequestMapping(
value = {"{modelSite}/bypass", "{modelSite}/dpc"},
method = {RequestMethod.GET}
)
public String bypassModeWithModel(HttpServletRequest request, HttpServletResponse response, Authentication authentication, @CookieValue(value = "user.token",defaultValue = "") UUID tokenUuid, ModelMap model, @RequestParam(required = false) String language, @RequestHeader("host") String hostName) {
this.init(request, response, authentication, language);
model.addAttribute("theme", this.theme);
UserToken token = null;
if (authentication == null && tokenUuid != null) {
token = this.userService.findToken(tokenUuid);
authentication = this.userService.authorizeLogin(tokenUuid);
}
if (authentication == null && token == null) {
model.addAttribute("hasUser", this.userService.hasUser());
model.addAttribute("pageFor", "dpc");
return this.getView(authentication, "/login");
} else {
this.user = UserPrincipal.getUser(authentication);
if (this.user.getSettingAsBoolean("keepSignIn")) {
if (token == null && authentication != null) {
token = (UserToken)authentication.getCredentials();
}
if (token != null) {
Cookie cookie = new Cookie("user.token", token.getTokenUuid().toString());
response.addCookie(cookie);
}
} else {
Cookie cookie = new Cookie("user.token", (String)null);
response.addCookie(cookie);
}
return this.getView(authentication, "/index");
}
}
@RequestMapping(
value = {"/requestTrial"},
method = {RequestMethod.GET}
)
public String requestTrial(HttpServletRequest request, HttpServletResponse response, Authentication authentication, @CookieValue(value = "user.token",defaultValue = "") UUID tokenUuid, ModelMap model, @RequestParam(required = false) String language, @RequestHeader("host") String hostName) {
this.init(request, response, authentication, language);
model.addAttribute("theme", this.theme);
UserToken token = null;
if (authentication == null && tokenUuid != null) {
token = this.userService.findToken(tokenUuid);
authentication = this.userService.authorizeLogin(tokenUuid);
}
if (authentication == null && token == null) {
model.addAttribute("hasUser", this.userService.hasUser());
model.addAttribute("pageFor", "requestTrial");
return this.getView(authentication, "/login");
} else {
this.user = UserPrincipal.getUser(authentication);
if (this.user.getSettingAsBoolean("keepSignIn")) {
if (token == null && authentication != null) {
token = (UserToken)authentication.getCredentials();
}
if (token != null) {
Cookie cookie = new Cookie("user.token", token.getTokenUuid().toString());
response.addCookie(cookie);
}
} else {
Cookie cookie = new Cookie("user.token", (String)null);
response.addCookie(cookie);
}
return this.getView(authentication, "/index");
}
}
@RequestMapping(
method = {RequestMethod.OPTIONS, RequestMethod.GET},
path = {"/login/{siteId}"}
)
public String forwardToLogin(@PathVariable String siteId) {
return "redirect:/?siteId=" + siteId;
}
}