Kaynağa Gözat

增加UserSetting

zhouhao 7 yıl önce
ebeveyn
işleme
5b0620f997

+ 64 - 0
hsweb-system/hsweb-system-authorization/hsweb-system-authorization-controller/src/main/java/org/hswebframework/web/controller/authorization/UserSettingController.java

@@ -0,0 +1,64 @@
+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.User;
+import org.hswebframework.web.authorization.annotation.Authorize;
+import org.hswebframework.web.entity.authorization.UserSettingEntity;
+import org.hswebframework.web.service.authorization.UserSettingService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.ResponseEntity;
+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 ResponseEntity<UserSettingEntity> get(Authentication authentication,
+                                                 @PathVariable String key,
+                                                 @PathVariable String id) {
+        return ResponseEntity.ok(userSettingService.selectByUser(authentication.getUser().getId(), key, id));
+    }
+
+    @GetMapping("/me/{key}")
+    @Authorize(merge = false)
+    @ApiOperation("获取当前用户的配置列表")
+    public ResponseEntity<List<UserSettingEntity>> get(Authentication authentication,
+                                                       @PathVariable String key) {
+        return ResponseEntity.ok(userSettingService.selectByUser(authentication.getUser().getId(), key));
+    }
+
+    @PatchMapping("/me/{key}")
+    @Authorize(merge = false)
+    @ApiOperation("获取当前用户的配置列表")
+    public ResponseEntity<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 ResponseEntity.ok(id);
+    }
+}

+ 11 - 0
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<UserSettingEntity, String> {
+}

+ 68 - 0
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,68 @@
+<?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="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>

+ 37 - 0
hsweb-system/hsweb-system-authorization/hsweb-system-authorization-entity/src/main/java/org/hswebframework/web/entity/authorization/UserSettingEntity.java

@@ -0,0 +1,37 @@
+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 Date createTime;
+
+    private Date updateTime;
+
+}

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

+ 90 - 0
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,90 @@
+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.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) {
+        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) {
+        return super.updateByPk(id, entity);
+    }
+}

+ 6 - 6
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<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();
             }