diff --git a/hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/main/java/org/hswebframework/web/service/GenericEntityService.java b/hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/main/java/org/hswebframework/web/service/GenericEntityService.java index 65517310e..67269a454 100644 --- a/hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/main/java/org/hswebframework/web/service/GenericEntityService.java +++ b/hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/main/java/org/hswebframework/web/service/GenericEntityService.java @@ -53,8 +53,8 @@ public abstract class GenericEntityService, PK> */ protected abstract IDGenerator getIDGenerator(); - @Override - public abstract CrudDao getDao(); +// @Override +// public abstract CrudDao getDao(); @Override public int deleteByPk(PK pk) { diff --git a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-controller/src/main/java/org/hswebframework/web/controller/authorization/UserSettingController.java b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-controller/src/main/java/org/hswebframework/web/controller/authorization/UserSettingController.java new file mode 100644 index 000000000..2b3391a4c --- /dev/null +++ b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-controller/src/main/java/org/hswebframework/web/controller/authorization/UserSettingController.java @@ -0,0 +1,63 @@ +package org.hswebframework.web.controller.authorization; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.hswebframework.web.authorization.Authentication; +import org.hswebframework.web.authorization.annotation.Authorize; +import org.hswebframework.web.controller.message.ResponseMessage; +import org.hswebframework.web.entity.authorization.UserSettingEntity; +import org.hswebframework.web.service.authorization.UserSettingService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +/** + * @author zhouhao + * @since 3.0 + */ +@RestController +@RequestMapping("/user-setting") +@Authorize//(permission = "user-setting", description = "用户配置管理") +@Api(value = "用户配置管理", tags = "用户-用户配置管理") +public class UserSettingController { + + @Autowired + private UserSettingService userSettingService; + + @GetMapping("/me/{key}/{id}") + @Authorize(merge = false) + @ApiOperation("获取当前用户的配置") + public ResponseMessage get(Authentication authentication, + @PathVariable String key, + @PathVariable String id) { + return ResponseMessage.ok(userSettingService.selectByUser(authentication.getUser().getId(), key, id)); + } + + @GetMapping("/me/{key}") + @Authorize(merge = false) + @ApiOperation("获取当前用户的配置列表") + public ResponseMessage> get(Authentication authentication, + @PathVariable String key) { + return ResponseMessage.ok(userSettingService.selectByUser(authentication.getUser().getId(), key)); + } + + @PatchMapping("/me/{key}") + @Authorize(merge = false) + @ApiOperation("获取当前用户的配置列表") + public ResponseMessage save(Authentication authentication, + @PathVariable String key, + @Validated + @RequestBody UserSettingEntity userSettingEntity) { + userSettingEntity.setId(null); + userSettingEntity.setUserId(authentication.getUser().getId()); + userSettingEntity.setKey(key); + UserSettingEntity old = userSettingService.selectByUser(authentication.getUser().getId(), key, userSettingEntity.getSettingId()); + if (old != null) { + userSettingEntity.setId(old.getId()); + } + String id = userSettingService.saveOrUpdate(userSettingEntity); + return ResponseMessage.ok(id); + } +} diff --git a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-dao/hsweb-system-authorization-dao-api/src/main/java/org/hswebframework/web/dao/authorization/UserSettingDao.java b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-dao/hsweb-system-authorization-dao-api/src/main/java/org/hswebframework/web/dao/authorization/UserSettingDao.java new file mode 100644 index 000000000..36e04edbd --- /dev/null +++ b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-dao/hsweb-system-authorization-dao-api/src/main/java/org/hswebframework/web/dao/authorization/UserSettingDao.java @@ -0,0 +1,11 @@ +package org.hswebframework.web.dao.authorization; + +import org.hswebframework.web.dao.CrudDao; +import org.hswebframework.web.entity.authorization.UserSettingEntity; + +/** + * @author zhouhao + * @since 3.0 + */ +public interface UserSettingDao extends CrudDao { +} diff --git a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-dao/hsweb-system-authorization-dao-mybatis/src/main/resources/org/hswebframework/web/dao/mybatis/mappers/authorization/UserSettingMapper.xml b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-dao/hsweb-system-authorization-dao-mybatis/src/main/resources/org/hswebframework/web/dao/mybatis/mappers/authorization/UserSettingMapper.xml new file mode 100644 index 000000000..011b6d6bf --- /dev/null +++ b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-dao/hsweb-system-authorization-dao-mybatis/src/main/resources/org/hswebframework/web/dao/mybatis/mappers/authorization/UserSettingMapper.xml @@ -0,0 +1,70 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + delete from s_user_setting where u_id =#{id} + + + + + + + + + + + + + + + + diff --git a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-entity/src/main/java/org/hswebframework/web/entity/authorization/UserSettingEntity.java b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-entity/src/main/java/org/hswebframework/web/entity/authorization/UserSettingEntity.java new file mode 100644 index 000000000..5b9a1262a --- /dev/null +++ b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-entity/src/main/java/org/hswebframework/web/entity/authorization/UserSettingEntity.java @@ -0,0 +1,41 @@ +package org.hswebframework.web.entity.authorization; + +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import org.hibernate.validator.constraints.NotBlank; +import org.hswebframework.web.commons.entity.SimpleGenericEntity; +import org.hswebframework.web.validator.group.CreateGroup; + +import java.util.Date; + +/** + * @author zhouhao + * @since 3.0 + */ +@Getter +@Setter +@NoArgsConstructor +public class UserSettingEntity extends SimpleGenericEntity { + @NotBlank(groups = CreateGroup.class) + private String userId; + + @NotBlank(groups = CreateGroup.class) + + private String key; + + @NotBlank(groups = CreateGroup.class) + private String settingId; + + @NotBlank(groups = CreateGroup.class) + private String setting; + + private String describe; + + private String name; + + private Date createTime; + + private Date updateTime; + +} diff --git a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-api/src/main/java/org/hswebframework/web/service/authorization/UserSettingService.java b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-api/src/main/java/org/hswebframework/web/service/authorization/UserSettingService.java new file mode 100644 index 000000000..e6b7264ee --- /dev/null +++ b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-api/src/main/java/org/hswebframework/web/service/authorization/UserSettingService.java @@ -0,0 +1,16 @@ +package org.hswebframework.web.service.authorization; + +import org.hswebframework.web.entity.authorization.UserSettingEntity; +import org.hswebframework.web.service.CrudService; + +import java.util.List; + +/** + * @author zhouhao + * @since 3.0 + */ +public interface UserSettingService extends CrudService { + List selectByUser(String userId, String key); + + UserSettingEntity selectByUser(String userId, String key, String settingId); +} diff --git a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/SimpleUserSettingService.java b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/SimpleUserSettingService.java new file mode 100644 index 000000000..939e5608a --- /dev/null +++ b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/SimpleUserSettingService.java @@ -0,0 +1,94 @@ +package org.hswebframework.web.service.authorization.simple; + +import org.hswebframework.web.dao.authorization.UserSettingDao; +import org.hswebframework.web.entity.authorization.UserSettingEntity; +import org.hswebframework.web.id.IDGenerator; +import org.hswebframework.web.service.EnableCacheGenericEntityService; +import org.hswebframework.web.service.authorization.UserSettingService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.annotation.CacheConfig; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.cache.annotation.Caching; +import org.springframework.stereotype.Service; + +import java.util.Date; +import java.util.List; +import java.util.Objects; + +/** + * @author zhouhao + * @since 3.0 + */ +@Service +@CacheConfig(cacheNames = "user-setting") +public class SimpleUserSettingService extends EnableCacheGenericEntityService + implements UserSettingService { + + @Autowired + private UserSettingDao userSettingDao; + + @Override + protected IDGenerator getIDGenerator() { + return IDGenerator.MD5; + } + + @Override + public UserSettingDao getDao() { + return userSettingDao; + } + + @Override + @Cacheable(key = "'user:'+#userId+'.'+#key") + public List selectByUser(String userId, String key) { + Objects.requireNonNull(userId); + Objects.requireNonNull(key); + + return createQuery().where("userId", userId).and("key", key).listNoPaging(); + } + + @Override + @Cacheable(key = "'user:'+#userId+'.'+#key+'.'+#settingId") + public UserSettingEntity selectByUser(String userId, String key, String settingId) { + Objects.requireNonNull(userId); + Objects.requireNonNull(key); + Objects.requireNonNull(settingId); + return createQuery().where("userId", userId).and("key", key).and("settingId", settingId).single(); + } + + @Override + @Caching( + evict = { + @CacheEvict(key = "'user:'+#entity.userId+'.'+#entity.key+'.'+#entity.settingId"), + @CacheEvict(key = "'user:'+#entity.userId+'.'+#entity.key") + } + ) + public String insert(UserSettingEntity entity) { + entity.setCreateTime(new Date()); + entity.setUpdateTime(new Date()); + return super.insert(entity); + } + + @Override + @Caching( + evict = { + @CacheEvict(key = "'user:'+#entity.userId+'.'+#entity.key+'.'+#entity.settingId"), + @CacheEvict(key = "'user:'+#entity.userId+'.'+#entity.key") + } + ) + public String saveOrUpdate(UserSettingEntity entity) { + return super.saveOrUpdate(entity); + } + + @Override + @Caching( + evict = { + @CacheEvict(key = "'user:'+#entity.userId+'.'+#entity.key+'.'+#entity.settingId"), + @CacheEvict(key = "'user:'+#entity.userId+'.'+#entity.key") + } + ) + public int updateByPk(String id, UserSettingEntity entity) { + entity.setUpdateTime(new Date()); + return super.updateByPk(id, entity); + } +} diff --git a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-starter/src/main/java/org/hswebframework/web/authorization/starter/AutoSyncPermission.java b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-starter/src/main/java/org/hswebframework/web/authorization/starter/AutoSyncPermission.java index 41517ff71..d9d9a3af6 100644 --- a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-starter/src/main/java/org/hswebframework/web/authorization/starter/AutoSyncPermission.java +++ b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-starter/src/main/java/org/hswebframework/web/authorization/starter/AutoSyncPermission.java @@ -111,12 +111,13 @@ public class AutoSyncPermission implements ApplicationListener oldAction = oldPermission.getActions(); - if (oldAction == null) { - oldAction = new ArrayList<>(); + Set oldAction = new HashSet<>(); + if (oldPermission.getActions() != null) { + oldAction.addAll(oldPermission.getActions()); } Map actionCache = oldAction - .stream().collect(Collectors.toMap(ActionEntity::getAction, Function.identity())); + .stream() + .collect(Collectors.toMap(ActionEntity::getAction, Function.identity())); boolean permissionChanged = false; for (ActionEntity actionEntity : permission.getActions()) { //添加新的action到旧的action @@ -126,10 +127,9 @@ public class AutoSyncPermission implements ApplicationListener(oldAction)); permissionService.updateByPk(oldPermission.getId(), oldPermission); } - actionCache.clear(); } diff --git a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-starter/src/main/resources/hsweb-starter.js b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-starter/src/main/resources/hsweb-starter.js index a1500c8b2..c58d8c138 100644 --- a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-starter/src/main/resources/hsweb-starter.js +++ b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-starter/src/main/resources/hsweb-starter.js @@ -150,6 +150,18 @@ function install(context) { .addColumn().name("sort_index").alias("sortIndex").comment("排序序号").jdbcType(java.sql.JDBCType.DECIMAL).length(32, 0).commit() .addColumn().name("status").alias("status").comment("状态").jdbcType(java.sql.JDBCType.DECIMAL).length(4, 0).commit() .comment("菜单分组关联").commit(); + + database.createOrAlter("s_user_setting") + .addColumn().name("u_id").varchar(32).notNull().primaryKey().comment("uid").commit() + .addColumn().name("name").varchar(128).comment("配置名称").commit() + .addColumn().name("describe").varchar(512).comment("说明").commit() + .addColumn().name("user_id").varchar(32).notNull().comment("用户ID").commit() + .addColumn().name("key").varchar(128).notNull().comment("配置标识").commit() + .addColumn().name("setting").clob().comment("配置内容").commit() + .addColumn().name("setting_id").varchar(32).notNull().comment("自定义配置id").commit() + .addColumn().name("create_time").datetime().notNull().comment("创建时间").commit() + .addColumn().name("update_time").datetime().comment("创建时间").commit() + .comment("角色表").commit(); } //设置依赖 diff --git a/hsweb-system/hsweb-system-file/hsweb-system-file-controller/src/main/java/org/hswebframework/web/controller/file/FileController.java b/hsweb-system/hsweb-system-file/hsweb-system-file-controller/src/main/java/org/hswebframework/web/controller/file/FileController.java index 500963f1f..c168e9dc0 100644 --- a/hsweb-system/hsweb-system-file/hsweb-system-file-controller/src/main/java/org/hswebframework/web/controller/file/FileController.java +++ b/hsweb-system/hsweb-system-file/hsweb-system-file-controller/src/main/java/org/hswebframework/web/controller/file/FileController.java @@ -53,7 +53,7 @@ import static java.util.Optional.ofNullable; @RestController @RequestMapping("${hsweb.web.mappings.file:file}") @Authorize(permission = "file", description = "文件管理") -@Api(value = "文件管理",tags = "文件管理-文件操作") +@Api(value = "文件管理", tags = "文件管理-文件操作") @SuppressWarnings("all") public class FileController { @@ -75,10 +75,6 @@ public class FileController { this.fileInfoService = fileInfoService; } - public static void main(String[] args) { - System.out.println(Base64.encodeBase64String("hello".getBytes())); - } - /** * 构建并下载zip文件.仅支持POST请求 * @@ -133,7 +129,8 @@ public class FileController { public void downloadTxt(@ApiParam("文件名") @PathVariable("name") String name, @ApiParam("文本内容") @RequestParam("text") String text, HttpServletResponse response) throws IOException { - response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); + response.setCharacterEncoding("utf-8"); + response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE + "; charset=utf-8"); response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(name, "utf-8")); response.getWriter().write(text); }