Merge remote-tracking branch 'origin/master'

This commit is contained in:
zhou-hao
2017-12-20 20:56:56 +08:00
10 changed files with 318 additions and 14 deletions

View File

@@ -53,8 +53,8 @@ public abstract class GenericEntityService<E extends GenericEntity<PK>, PK>
*/
protected abstract IDGenerator<PK> getIDGenerator();
@Override
public abstract CrudDao<E, PK> getDao();
// @Override
// public abstract CrudDao<E, PK> getDao();
@Override
public int deleteByPk(PK pk) {

View File

@@ -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<UserSettingEntity> 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<List<UserSettingEntity>> get(Authentication authentication,
@PathVariable String key) {
return ResponseMessage.ok(userSettingService.selectByUser(authentication.getUser().getId(), key));
}
@PatchMapping("/me/{key}")
@Authorize(merge = false)
@ApiOperation("获取当前用户的配置列表")
public ResponseMessage<String> 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);
}
}

View File

@@ -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<UserSettingEntity, String> {
}

View File

@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--
~ Copyright 2016 http://www.hswebframework.org
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
~
-->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://www.mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.hswebframework.web.dao.authorization.UserSettingDao">
<resultMap id="UserSettingResultMap" type="org.hswebframework.web.entity.authorization.UserSettingEntity">
<id property="id" column="u_id" javaType="string" jdbcType="VARCHAR"/>
<result property="key" column="key" javaType="String" jdbcType="VARCHAR"/>
<result property="settingId" column="setting_id" javaType="String" jdbcType="VARCHAR"/>
<result property="userId" column="user_id" javaType="String" jdbcType="VARCHAR"/>
<result property="setting" column="setting" javaType="String" jdbcType="CLOB"/>
<result property="describe" column="describe" javaType="String" jdbcType="VARCHAR"/>
<result property="name" column="name" javaType="String" jdbcType="VARCHAR"/>
<result property="createTime" column="create_time" javaType="Date" jdbcType="TIMESTAMP"/>
<result property="updateTime" column="update_time" javaType="Date" jdbcType="TIMESTAMP"/>
</resultMap>
<!--用于动态生成sql所需的配置-->
<sql id="config">
<bind name="resultMapId" value="'UserSettingResultMap'"/>
<bind name="tableName" value="'s_user_setting'"/>
</sql>
<insert id="insert" parameterType="org.hswebframework.web.entity.authorization.UserSettingEntity">
<include refid="config"/>
<include refid="BasicMapper.buildInsertSql"/>
</insert>
<delete id="deleteByPk" parameterType="String">
delete from s_user_setting where u_id =#{id}
</delete>
<delete id="delete" parameterType="org.hswebframework.web.commons.entity.Entity">
<include refid="config"/>
<include refid="BasicMapper.buildDeleteSql"/>
</delete>
<update id="update" parameterType="org.hswebframework.web.commons.entity.Entity">
<include refid="config"/>
<include refid="BasicMapper.buildUpdateSql"/>
</update>
<select id="query" parameterType="org.hswebframework.web.commons.entity.Entity" resultMap="UserSettingResultMap">
<include refid="config"/>
<include refid="BasicMapper.buildSelectSql"/>
</select>
<select id="count" parameterType="org.hswebframework.web.commons.entity.Entity" resultType="int">
<include refid="config"/>
<include refid="BasicMapper.buildTotalSql"/>
</select>
</mapper>

View File

@@ -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<String> {
@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;
}

View File

@@ -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<UserSettingEntity, String> {
List<UserSettingEntity> selectByUser(String userId, String key);
UserSettingEntity selectByUser(String userId, String key, String settingId);
}

View File

@@ -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<UserSettingEntity, String>
implements UserSettingService {
@Autowired
private UserSettingDao userSettingDao;
@Override
protected IDGenerator<String> getIDGenerator() {
return IDGenerator.MD5;
}
@Override
public UserSettingDao getDao() {
return userSettingDao;
}
@Override
@Cacheable(key = "'user:'+#userId+'.'+#key")
public List<UserSettingEntity> 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);
}
}

View File

@@ -111,12 +111,13 @@ public class AutoSyncPermission implements ApplicationListener<AuthorizeDefiniti
if (oldPermission == null) {
permissionService.insert(permission);
} else {
List<ActionEntity> oldAction = oldPermission.getActions();
if (oldAction == null) {
oldAction = new ArrayList<>();
Set<ActionEntity> oldAction = new HashSet<>();
if (oldPermission.getActions() != null) {
oldAction.addAll(oldPermission.getActions());
}
Map<String, ActionEntity> 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<AuthorizeDefiniti
}
}
if (permissionChanged) {
oldPermission.setActions(oldAction);
oldPermission.setActions(new ArrayList<>(oldAction));
permissionService.updateByPk(oldPermission.getId(), oldPermission);
}
actionCache.clear();
}

View File

@@ -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();
}
//设置依赖

View File

@@ -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);
}