fix: isHtmlRequest

This commit is contained in:
devezhao 2020-12-02 17:49:17 +08:00
parent 4e5e97b3ab
commit 797e59253c
10 changed files with 50 additions and 18 deletions

View file

@ -15,6 +15,7 @@ import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rebuild.core.Application;
import com.rebuild.core.DefinedException;
import com.rebuild.core.RebuildException;
import com.rebuild.core.metadata.EntityHelper;
import com.rebuild.core.metadata.MetadataHelper;
@ -22,12 +23,12 @@ import com.rebuild.core.privileges.UserService;
import com.rebuild.core.service.TransactionManual;
import com.rebuild.core.service.files.AttachmentAwareObserver;
import com.rebuild.core.service.general.OperatingContext;
import com.rebuild.core.support.i18n.Language;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.TransactionStatus;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
@ -135,7 +136,7 @@ public class RecycleRestore {
*/
private List<Record> conver2Record(JSONObject content, ID recordId) {
if (!MetadataHelper.containsEntity(recordId.getEntityCode())) {
return Collections.emptyList();
throw new DefinedException(Language.L("EntityUnexistsNotRestore"));
}
JSONArray detailList = content.getJSONArray(RecycleBean.NAME_DETAILLIST);

View file

@ -18,6 +18,7 @@ import com.rebuild.core.UserContextHolder;
import com.rebuild.core.metadata.EntityHelper;
import com.rebuild.core.metadata.MetadataHelper;
import com.rebuild.core.privileges.UserService;
import com.rebuild.core.support.general.FieldValueHelper;
import org.apache.commons.lang.StringUtils;
import java.util.LinkedList;
@ -88,7 +89,7 @@ public class RecycleStore {
JSONObject recordContent = (JSONObject) o[1];
String recordName = recordContent.getString(belongEntity.getNameField().getName());
if (StringUtils.isBlank(recordName)) {
recordName = recordId.toLiteral().toUpperCase();
recordName = FieldValueHelper.NO_LABEL_PREFIX + recordId.toLiteral().toUpperCase();
}
clone.setID("recordId", recordId);

View file

@ -27,6 +27,7 @@ import com.rebuild.core.service.NoRecordFoundException;
import com.rebuild.core.service.approval.ApprovalState;
import com.rebuild.core.support.i18n.Language;
import com.rebuild.utils.JSONUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.Assert;
@ -41,6 +42,7 @@ import java.util.regex.Pattern;
* @author zhaofang123@gmail.com
* @since 09/23/2018
*/
@Slf4j
public class FieldValueHelper {
/**
@ -119,7 +121,11 @@ public class FieldValueHelper {
JSONObject mixValue = JSONUtils.toJSONObject(
new String[] { "id", "text" }, new Object[] { id, text });
if (id != null) {
mixValue.put("entity", MetadataHelper.getEntityName(id));
if (MetadataHelper.containsEntity(id.getEntityCode())) {
mixValue.put("entity", MetadataHelper.getEntityName(id));
} else {
log.warn("Entity no longer exists : {}", id);
}
}
return mixValue;
}

View file

@ -14,13 +14,14 @@ import cn.devezhao.persist4j.engine.ID;
import com.rebuild.api.user.AuthTokenManager;
import com.rebuild.core.Application;
import com.rebuild.core.BootApplication;
import com.rebuild.core.service.DataSpecificationException;
import com.rebuild.core.support.ConfigurationItem;
import com.rebuild.core.support.RebuildConfiguration;
import com.rebuild.core.support.i18n.Language;
import com.rebuild.core.support.i18n.LanguageBundle;
import com.rebuild.web.admin.AdminVerfiyController;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.MimeType;
import org.springframework.util.MimeTypeUtils;
import javax.servlet.http.HttpServletRequest;
import java.nio.file.AccessDeniedException;
@ -179,4 +180,20 @@ public class AppUtils {
String UA = request.getHeader("user-agent");
return UA != null && UA.toUpperCase().startsWith(MOILE_UA_PREFIX);
}
/**
* 请求类型
*
* @param request
* @return
* @see MimeTypeUtils#parseMimeType(String)
*/
public static MimeType parseMimeType(HttpServletRequest request) {
try {
String acceptType = request.getHeader("Accept").split(",")[0];
return MimeTypeUtils.parseMimeType(acceptType);
} catch (Exception ignore) {
}
return null;
}
}

View file

@ -24,6 +24,8 @@ import com.rebuild.utils.AppUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.core.NamedThreadLocal;
import org.springframework.http.HttpStatus;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
@ -59,13 +61,14 @@ public class RebuildWebInterceptor extends HandlerInterceptorAdapter implements
final String requestUri = request.getRequestURI()
+ (request.getQueryString() != null ? ("?" + request.getQueryString()) : "");
final boolean ajaxRequest = ServletUtils.isAjaxRequest(request);
final boolean isHtmlRequest = !ServletUtils.isAjaxRequest(request)
&& MimeTypeUtils.TEXT_HTML.equals(AppUtils.parseMimeType(request));
// Locale
final String locale = detectLocale(request, response);
UserContextHolder.setLocale(locale);
if (!ajaxRequest) {
if (isHtmlRequest) {
request.setAttribute(WebConstants.LOCALE, locale);
request.setAttribute(WebConstants.$BUNDLE, Application.getLanguage().getBundle(locale));
@ -113,17 +116,18 @@ public class RebuildWebInterceptor extends HandlerInterceptorAdapter implements
if (requestUser != null) {
// 管理后台访问
if (requestUri.contains("/admin/") && !AppUtils.isAdminVerified(request)) {
if (ajaxRequest) {
ServletUtils.writeJson(response, RespBody.error(401).toString());
} else {
if (isHtmlRequest) {
sendRedirect(response, "/user/admin-verify", requestUri);
} else {
ServletUtils.writeJson(response, RespBody.error(HttpStatus.FORBIDDEN.value()).toJSONString());
}
return false;
}
UserContextHolder.setUser(requestUser);
if (!ajaxRequest) {
if (isHtmlRequest) {
// Last active
Application.getSessionStore().storeLastActive(request);
@ -137,10 +141,10 @@ public class RebuildWebInterceptor extends HandlerInterceptorAdapter implements
log.warn("Unauthorized access {} via {}",
RebuildWebConfigurer.getRequestUrls(request), ServletUtils.getRemoteAddr(request));
if (ajaxRequest) {
ServletUtils.writeJson(response, RespBody.error(403).toString());
} else {
if (isHtmlRequest) {
sendRedirect(response, "/user/login", requestUri);
} else {
ServletUtils.writeJson(response, RespBody.error(HttpStatus.FORBIDDEN.value()).toJSONString());
}
return false;
}

View file

@ -1234,6 +1234,7 @@
"HookRequestError": "Request failed : %s",
"ModifyFileFolder": "Modify file folder",
"AdminSettings": "Admin Settings",
"EntityUnexistsNotRestore": "The owning entity no longer exists and cannot be restored",
"s.__": "STATE",
"s.ApprovalState.DRAFT": "Draft",

View file

@ -1234,6 +1234,7 @@
"HookRequestError": "请求失败 : %s",
"ModifyFileFolder": "修改文件目录",
"AdminSettings": "管理员后台",
"EntityUnexistsNotRestore": "所属实体已经不存在,无法恢复",
"s.__": "状态",
"s.ApprovalState.DRAFT": "草稿",

View file

@ -1234,6 +1234,7 @@
"HookRequestError": "請求失敗 : %s",
"ModifyFileFolder": "修改文件目錄",
"AdminSettings": "管理員後台",
"EntityUnexistsNotRestore": "所屬實體已經不存在,無法恢復",
"s.__": "狀態",
"s.ApprovalState.DRAFT": "草稿",

View file

@ -119,13 +119,13 @@ CellRenders.renderSimple = function (v, s, k) {
<React.Fragment>
{$L('CasDelete')}
<span className="badge text-id ml-1" title={$L('CasMainId')}>
{v.toUpperCase()}
{v.id}
</span>
</React.Fragment>
) : (
$L('DirectDelete')
)
else if (k.endsWith('.recordId')) v = <span className="badge text-id">{v.toUpperCase()}</span>
else if (k.endsWith('.recordId')) v = <span className="badge text-id">{v.id}</span>
else if (k.endsWith('.belongEntity')) v = _entities[v] || `[${v.toUpperCase()}]`
return (
<td key={k}>

View file

@ -106,14 +106,14 @@ CellRenders.renderSimple = function (v, s, k) {
<React.Fragment>
{$L('CasOperation')}
<span className="badge text-id ml-1" title={$L('CasMainId')}>
{v}
{v.id}
</span>
</React.Fragment>
) : (
$L('DirectOperation')
)
} else if (k.endsWith('.recordId')) {
v = <span className="badge text-id">{v}</span>
v = <span className="badge text-id">{v.id}</span>
} else if (k.endsWith('.belongEntity')) {
v = _ENTITIES[v] || `[${v.toUpperCase()}]`
} else if (k.endsWith('.revisionType')) {