zhou-hao 7 år sedan
förälder
incheckning
f3db00d55d
14 ändrade filer med 104 tillägg och 46 borttagningar
  1. 14 0
      hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/AuthenticationPredicate.java
  2. 3 1
      hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/exception/AccessDenyException.java
  3. 1 1
      hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/exception/UnAuthorizedException.java
  4. 17 6
      hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/token/TokenState.java
  5. 17 4
      hsweb-system/hsweb-system-dashboard/hsweb-system-dashboard-api/src/main/java/org/hswebframework/web/dashboard/DashBoardConfigEntity.java
  6. 5 1
      hsweb-system/hsweb-system-dashboard/hsweb-system-dashboard-api/src/main/java/org/hswebframework/web/dashboard/DashBoardService.java
  7. 10 2
      hsweb-system/hsweb-system-dashboard/hsweb-system-dashboard-local/src/main/java/org/hswebframework/web/dashboard/local/DefaultDashBoardService.java
  8. 8 2
      hsweb-system/hsweb-system-dashboard/hsweb-system-dashboard-local/src/main/java/org/hswebframework/web/dashboard/local/DefaultDashBordExecutor.java
  9. 7 1
      hsweb-system/hsweb-system-dashboard/hsweb-system-dashboard-local/src/main/java/org/hswebframework/web/dashboard/local/strategy/ScriptExecutorStrategy.java
  10. 3 2
      hsweb-system/hsweb-system-dashboard/hsweb-system-dashboard-local/src/main/resources/org/hswebframework/web/dao/mybatis/mappers/dashboard/DashBoardConfigMapper.xml
  11. 3 2
      hsweb-system/hsweb-system-dashboard/hsweb-system-dashboard-starter/src/main/resources/hsweb-starter.js
  12. 2 2
      hsweb-system/hsweb-system-dashboard/hsweb-system-dashboard-web/src/main/java/org/hswebframework/web/controller/dashboard/DashBoardConfigController.java
  13. 10 21
      hsweb-system/hsweb-system-dashboard/hsweb-system-dashboard-web/src/main/java/org/hswebframework/web/controller/dashboard/DashBoardUserConfigController.java
  14. 4 1
      hsweb-system/hsweb-system-dashboard/hsweb-system-dashboard-web/src/main/java/org/hswebframework/web/controller/dashboard/model/UserDashBoardResponse.java

+ 14 - 0
hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/AuthenticationPredicate.java

@@ -1,5 +1,7 @@
 package org.hswebframework.web.authorization;
 
+import org.hswebframework.web.authorization.exception.AccessDenyException;
+
 import java.util.Objects;
 import java.util.function.Predicate;
 
@@ -46,4 +48,16 @@ public interface AuthenticationPredicate extends Predicate<Authentication> {
                 .map(this::test)
                 .orElse(false);
     }
+
+    default void assertHas() {
+        if (!test()) {
+            throw new AccessDenyException();
+        }
+    }
+    default void assertHas(Authentication authentication) {
+        if (!test(authentication)) {
+            throw new AccessDenyException();
+        }
+    }
+
 }

+ 3 - 1
hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/exception/AccessDenyException.java

@@ -8,8 +8,10 @@ package org.hswebframework.web.authorization.exception;
  */
 public class AccessDenyException extends RuntimeException {
 
+    private static final long serialVersionUID = -5135300127303801430L;
+
     public AccessDenyException() {
-        this("{access_deny}");
+        this("权限不足,拒绝访问!");
     }
 
     public AccessDenyException(String message) {

+ 1 - 1
hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/exception/UnAuthorizedException.java

@@ -36,7 +36,7 @@ public class UnAuthorizedException extends RuntimeException {
     }
 
     public UnAuthorizedException(TokenState state) {
-        this("{un_authorization}", state);
+        this(state.getText(), state);
     }
 
     public UnAuthorizedException(String message, TokenState state) {

+ 17 - 6
hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/token/TokenState.java

@@ -1,31 +1,42 @@
 package org.hswebframework.web.authorization.token;
 
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import org.hswebframework.web.dict.EnumDict;
+
 /**
  * 令牌状态
  */
-public enum TokenState {
+@Getter
+@AllArgsConstructor
+public enum TokenState implements EnumDict<String> {
     /**
      * 正常,有效
      */
-    effective,
+    effective("effective", "正常"),
 
     /**
      * 已被禁止访问
      */
-    deny,
+    deny("deny", "已被禁止访问"),
 
     /**
      * 已过期
      */
-    expired,
+    expired("expired", "用户未登录"),
 
     /**
      * 已被踢下线
      */
-    offline,
+    offline("offline", "用户已在其他地方登录"),
 
     /**
      * 锁定
      */
-    lock
+    lock("lock", "登录状态已被锁定");
+
+
+    private String value;
+
+    private String text;
 }

+ 17 - 4
hsweb-system/hsweb-system-dashboard/hsweb-system-dashboard-api/src/main/java/org/hswebframework/web/dashboard/DashBoardConfigEntity.java

@@ -3,14 +3,15 @@ package org.hswebframework.web.dashboard;
 import lombok.Data;
 import lombok.EqualsAndHashCode;
 import org.hibernate.validator.constraints.NotBlank;
-import org.hswebframework.web.commons.entity.DataStatusEnum;
 import org.hswebframework.web.commons.entity.RecordCreationEntity;
 import org.hswebframework.web.commons.entity.SimpleGenericEntity;
 import org.hswebframework.web.validator.group.CreateGroup;
 
+import javax.validation.constraints.NotNull;
+
 @EqualsAndHashCode(callSuper = true)
 @Data
-public class DashBoardConfigEntity extends SimpleGenericEntity<String> implements RecordCreationEntity {
+public class DashBoardConfigEntity extends SimpleGenericEntity<String> implements RecordCreationEntity,Comparable<DashBoardConfigEntity> {
 
     private static final long serialVersionUID = 3911748291957287662L;
 
@@ -30,12 +31,24 @@ public class DashBoardConfigEntity extends SimpleGenericEntity<String> implement
 
     private Boolean defaultConfig;
 
-    private DataStatusEnum status;
+    private Byte status;
 
     @NotBlank(groups = CreateGroup.class)
     private String creatorId;
 
-    @NotBlank(groups = CreateGroup.class)
+    @NotNull(groups = CreateGroup.class)
     private Long createTime;
 
+    private Long sortIndex;
+
+    @Override
+    public int compareTo(DashBoardConfigEntity o) {
+        if(sortIndex==null){
+            return 0;
+        }
+        if(o.sortIndex==null){
+            return 1;
+        }
+        return Long.compare(sortIndex,o.sortIndex);
+    }
 }

+ 5 - 1
hsweb-system/hsweb-system-dashboard/hsweb-system-dashboard-api/src/main/java/org/hswebframework/web/dashboard/DashBoardService.java

@@ -2,5 +2,9 @@ package org.hswebframework.web.dashboard;
 
 import org.hswebframework.web.service.CrudService;
 
-public interface DashBoardService extends CrudService<DashBoardConfigEntity,String> {
+import java.util.List;
+
+public interface DashBoardService extends CrudService<DashBoardConfigEntity, String> {
+
+    List<DashBoardConfigEntity> selectAllDefaults();
 }

+ 10 - 2
hsweb-system/hsweb-system-dashboard/hsweb-system-dashboard-local/src/main/java/org/hswebframework/web/dashboard/local/DefaultDashBoardService.java

@@ -5,14 +5,17 @@ import org.hswebframework.web.dashboard.DashBoardConfigEntity;
 import org.hswebframework.web.dashboard.DashBoardService;
 import org.hswebframework.web.dashboard.local.dao.DashBoardConfigDao;
 import org.hswebframework.web.id.IDGenerator;
-import org.hswebframework.web.service.EnableCacheGenericEntityService;
+import org.hswebframework.web.service.EnableCacheAllEvictGenericEntityService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.cache.annotation.CacheConfig;
+import org.springframework.cache.annotation.Cacheable;
 import org.springframework.stereotype.Service;
 
+import java.util.List;
+
 @Service
 @CacheConfig(cacheNames = "hsweb:dashboard-conf")
-public class DefaultDashBoardService extends EnableCacheGenericEntityService<DashBoardConfigEntity, String> implements DashBoardService {
+public class DefaultDashBoardService extends EnableCacheAllEvictGenericEntityService<DashBoardConfigEntity, String> implements DashBoardService {
 
     @Autowired
     private DashBoardConfigDao dashBoardConfigDao;
@@ -26,4 +29,9 @@ public class DefaultDashBoardService extends EnableCacheGenericEntityService<Das
     public CrudDao<DashBoardConfigEntity, String> getDao() {
         return dashBoardConfigDao;
     }
+
+    @Cacheable(key = "'all-defaults'")
+    public List<DashBoardConfigEntity> selectAllDefaults() {
+        return createQuery().where("defaultConfig", true).or().isNull("defaultConfig").listNoPaging();
+    }
 }

+ 8 - 2
hsweb-system/hsweb-system-dashboard/hsweb-system-dashboard-local/src/main/java/org/hswebframework/web/dashboard/local/DefaultDashBordExecutor.java

@@ -1,11 +1,12 @@
 package org.hswebframework.web.dashboard.local;
 
 import org.hswebframework.web.authorization.Authentication;
+import org.hswebframework.web.authorization.AuthenticationPredicate;
 import org.hswebframework.web.dashboard.DashBoardConfigEntity;
 import org.hswebframework.web.dashboard.executor.DashBoardExecutor;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
-import org.springframework.util.Assert;
+import org.springframework.util.StringUtils;
 
 import java.util.List;
 
@@ -18,7 +19,12 @@ public class DefaultDashBordExecutor implements DashBoardExecutor {
     @Override
     public Object execute(DashBoardConfigEntity entity, Authentication authentication) {
 
-        Assert.notNull(entity, "配置不能为空");
+        if (entity == null) {
+            return null;
+        }
+        if (StringUtils.hasText(entity.getPermission())) {
+            AuthenticationPredicate.has(entity.getPermission()).assertHas(authentication);
+        }
 
         return strategies.stream()
                 .filter(strategy -> strategy.support(entity))

+ 7 - 1
hsweb-system/hsweb-system-dashboard/hsweb-system-dashboard-local/src/main/java/org/hswebframework/web/dashboard/local/strategy/ScriptExecutorStrategy.java

@@ -10,8 +10,11 @@ import org.hswebframework.web.dashboard.local.DashBoardExecutorStrategy;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 import org.springframework.util.DigestUtils;
+import org.springframework.util.StringUtils;
 
+import java.util.Arrays;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 @Component
@@ -20,9 +23,12 @@ public class ScriptExecutorStrategy implements DashBoardExecutorStrategy {
     @Autowired
     private SqlExecutor sqlExecutor;
 
+    static List<String> supportLang = Arrays.asList("js", "javascript", "groovy", "sql");
+
     @Override
     public boolean support(DashBoardConfigEntity entity) {
-        return "script".equals(entity.getType());
+        return StringUtils.hasText(entity.getScriptLanguage()) && StringUtils.hasText(entity.getScript())
+                && supportLang.contains(entity.getScriptLanguage());
     }
 
     @Override

+ 3 - 2
hsweb-system/hsweb-system-dashboard/hsweb-system-dashboard-local/src/main/resources/org/hswebframework/web/dao/mybatis/mappers/dashboard/DashBoardConfigMapper.xml

@@ -12,9 +12,10 @@
         <result property="scriptLanguage" column="script_lang" javaType="String" jdbcType="VARCHAR"/>
         <result property="permission" column="permission" javaType="String" jdbcType="VARCHAR"/>
         <result property="creatorId" column="creator_id" javaType="String" jdbcType="VARCHAR"/>
-        <result property="createTime" column="create_time" javaType="Date" jdbcType="TIMESTAMP"/>
+        <result property="createTime" column="create_time" javaType="Long" jdbcType="BIGINT"/>
+        <result property="sortIndex" column="sort_index" javaType="Long" jdbcType="BIGINT"/>
         <result property="defaultConfig" column="is_default" javaType="Boolean" jdbcType="INTEGER"/>
-        <result property="status" column="status" javaType="org.hswebframework.web.commons.entity.DataStatusEnum" jdbcType="INTEGER"/>
+        <result property="status" column="status" javaType="Byte" jdbcType="INTEGER"/>
     </resultMap>
 
     <!--用于动态生成sql所需的配置-->

+ 3 - 2
hsweb-system/hsweb-system-dashboard/hsweb-system-dashboard-starter/src/main/resources/hsweb-starter.js

@@ -30,10 +30,11 @@ function install(context) {
         .addColumn().name("script_lang").alias("scriptLanguage").comment("脚本语言").varchar(32).commit()
         .addColumn().name("permission").alias("permission").comment("权限设置").varchar(512).commit()
         .addColumn().name("creator_id").alias("creatorId").comment("创建人").varchar(32).commit()
-        .addColumn().name("create_time").alias("createTime").comment("创建时间").datetime().commit()
+        .addColumn().name("create_time").alias("createTime").comment("创建时间").number(32).commit()
+        .addColumn().name("sort_index").alias("sortIndex").comment("排序").number(32).commit()
         .addColumn().name("status").alias("status").comment("状态").number(4).commit()
         .addColumn().name("is_default").alias("defaultConfig").comment("是否默认").number(2).commit()
-        .comment("模板").commit();
+        .comment("仪表盘配置").commit();
 }
 
 //设置依赖

+ 2 - 2
hsweb-system/hsweb-system-dashboard/hsweb-system-dashboard-web/src/main/java/org/hswebframework/web/controller/dashboard/DashBoardConfigController.java

@@ -39,13 +39,13 @@ public class DashBoardConfigController implements SimpleGenericEntityController<
     }
 
     @Override
-    public ResponseMessage<String> saveOrUpdate(DashBoardConfigEntity data) {
+    public ResponseMessage<String> saveOrUpdate(@RequestBody DashBoardConfigEntity data) {
         Authentication.current().ifPresent(a -> data.setCreatorId(a.getUser().getId()));
         return SimpleGenericEntityController.super.saveOrUpdate(data);
     }
 
     @GetMapping("{id}/execute")
-    @Authorize
+    @Authorize(merge = false)
     @ApiOperation("执行仪表盘配置")
     public ResponseMessage<Object> execute(@PathVariable String id) {
         return ResponseMessage.ok(dashBoardExecutor.execute(dashBoardService.selectByPk(id), Authentication.current().orElse(null)));

+ 10 - 21
hsweb-system/hsweb-system-dashboard/hsweb-system-dashboard-web/src/main/java/org/hswebframework/web/controller/dashboard/DashBoardUserConfigController.java

@@ -42,17 +42,6 @@ public class DashBoardUserConfigController {
     @Autowired
     private DashBoardService dashBoardService;
 
-    @Autowired
-    private DashBoardExecutor dashBoardExecutor;
-
-    @Autowired(required = false)
-    private Executor executor;
-
-    @PostConstruct
-    public void init() {
-        executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2);
-    }
-
     @GetMapping("/all")
     @ApiOperation("获取用户可选择的仪表盘配置")
     public ResponseMessage<List<UserDashBoardResponse>> getUserAllDashBoardConfig(Authentication authentication) {
@@ -71,21 +60,21 @@ public class DashBoardUserConfigController {
     @ApiOperation("获取用户自定义的仪表盘配置")
     public ResponseMessage<List<UserDashBoardResponse>> getUserConfigDashBoardConfig(Authentication authentication) {
         UserSettingEntity settingEntity = userSettingService.selectByUser(authentication.getUser().getId(), "dashboard-config", "current");
+        List<DashBoardConfigEntity> configs;
+
         if (settingEntity == null) {
-            return ResponseMessage.ok(Collections.emptyList());
+            configs = dashBoardService.selectAllDefaults();
+            Collections.sort(configs);
+        } else {
+            List<String> ids = JSON.parseArray(settingEntity.getSetting(), String.class);
+            configs = dashBoardService.selectByPk(ids);
+            configs.sort(Comparator.comparing(conf -> ids.indexOf(conf.getId())));
         }
-
-        List<String> ids = JSON.parseArray(settingEntity.getSetting(), String.class);
-
-        List<UserDashBoardResponse> configList = ids
-                .parallelStream()
-                .map(dashBoardService::selectByPk) //为什么要单个查询不用批量查询?因为单个查询有缓存
-                .filter(Objects::nonNull)
+        List<UserDashBoardResponse> configList = configs.stream()
                 //过滤权限
-                .filter(config -> StringUtils.isEmpty(config) ||
+                .filter(config -> StringUtils.hasText(config.getPermission()) ||
                         AuthenticationPredicate.has(config.getPermission()).test(authentication))
                 .map(config -> config.copyTo(new UserDashBoardResponse()))
-                .sorted(Comparator.comparing(conf -> ids.indexOf(conf.getId())))
                 .collect(Collectors.toList());
 
         return ResponseMessage.ok(configList);

+ 4 - 1
hsweb-system/hsweb-system-dashboard/hsweb-system-dashboard-web/src/main/java/org/hswebframework/web/controller/dashboard/model/UserDashBoardResponse.java

@@ -14,7 +14,10 @@ public class UserDashBoardResponse {
 
     private String type;
 
+    private String name;
+
     private String template;
 
-    private Object data;
+//    private Object data;
+
 }