128 lines
3.9 KiB
Java
128 lines
3.9 KiB
Java
package com.midm.app;
|
|
|
|
import com.midm.model.org.User;
|
|
import com.midm.security.UserPrincipal;
|
|
import com.midm.service.org.SiteService;
|
|
import com.midm.service.system.HelpMessageService;
|
|
import com.midm.service.system.HostLicenseService;
|
|
import com.midm.service.system.LocaleService;
|
|
import com.midm.service.system.SettingsService;
|
|
import com.mitac.cell.utils.McStringUtils;
|
|
import java.util.Date;
|
|
import java.util.Locale;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.boot.info.BuildProperties;
|
|
import org.springframework.security.core.Authentication;
|
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
|
@ControllerAdvice
|
|
public final class GlobalController {
|
|
private static final String[] excludeBindingPrefixes = new String[]{"/api/", "/img/", "/profile/", "/client/", "/icon/", "/store/", "/logo/", "/device/", "/file/", "/must/", "/userfile/", "/barcode/", "/dpc/"};
|
|
@Autowired
|
|
private BuildProperties buildProperties;
|
|
@Autowired
|
|
protected LocaleService localeService;
|
|
@Autowired
|
|
private SettingsService settingsService;
|
|
@Autowired
|
|
private HostLicenseService licenseService;
|
|
@Autowired
|
|
protected SiteService siteService;
|
|
@Autowired
|
|
private HelpMessageService helpMessageService;
|
|
@Value("#{servletContext.contextPath}")
|
|
private String servletContextPath;
|
|
|
|
@ModelAttribute("buildVersion")
|
|
public String buildVersion() {
|
|
return this.buildProperties.getVersion();
|
|
}
|
|
|
|
@ModelAttribute("buildTime")
|
|
public String buildTime() {
|
|
return Date.from(this.buildProperties.getTime()).toString();
|
|
}
|
|
|
|
@ModelAttribute("user")
|
|
public Object user(HttpServletRequest request, Authentication authentication, @RequestParam(required = false) String language) {
|
|
if (this.bindModel(request)) {
|
|
User user = authentication == null ? this.guest(request, language) : UserPrincipal.getUser(authentication);
|
|
if (user.getSite() == null && user.getSiteUuid() != null) {
|
|
user.setSite(this.siteService.findByUuid(user.getSiteUuid()));
|
|
}
|
|
|
|
return user;
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
@ModelAttribute("settings")
|
|
public SettingsService settings() {
|
|
return this.settingsService;
|
|
}
|
|
|
|
@ModelAttribute("locales")
|
|
public LocaleService locales() {
|
|
return this.localeService;
|
|
}
|
|
|
|
@ModelAttribute("helpService")
|
|
public HelpMessageService helpService() {
|
|
return this.helpMessageService;
|
|
}
|
|
|
|
@ModelAttribute("isHostValid")
|
|
public boolean isHostValid() {
|
|
return this.licenseService.isValid();
|
|
}
|
|
|
|
@ModelAttribute("deviceLimit")
|
|
public int deviceLimit() {
|
|
return this.licenseService.getDeviceLimit();
|
|
}
|
|
|
|
@ModelAttribute("siteService")
|
|
public SiteService siteService() {
|
|
return this.siteService;
|
|
}
|
|
|
|
@ModelAttribute("contextPath")
|
|
public String contextPath() {
|
|
return this.servletContextPath + "/";
|
|
}
|
|
|
|
private User guest(HttpServletRequest request, String localeTag) {
|
|
User user = new User();
|
|
user.setName("Guest");
|
|
if (!McStringUtils.isValid(localeTag)) {
|
|
localeTag = request.getLocale().toLanguageTag();
|
|
}
|
|
|
|
request.getSession().setAttribute("language", localeTag);
|
|
Locale curLocale = Locale.forLanguageTag(localeTag);
|
|
if (curLocale == null) {
|
|
curLocale = request.getLocale();
|
|
}
|
|
|
|
user.setLocale(this.localeService.findAltLocale(curLocale));
|
|
return user;
|
|
}
|
|
|
|
public boolean bindModel(HttpServletRequest request) {
|
|
String uri = request.getRequestURI();
|
|
|
|
for(String prefix : excludeBindingPrefixes) {
|
|
if (uri.startsWith(this.servletContextPath + prefix)) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|