作者 朱兆平

用户的权限配置 加入事务

@@ -18,6 +18,7 @@ @@ -18,6 +18,7 @@
18 <java.version>1.8</java.version> 18 <java.version>1.8</java.version>
19 <spring-cloud.version>Greenwich.BUILD-SNAPSHOT</spring-cloud.version> 19 <spring-cloud.version>Greenwich.BUILD-SNAPSHOT</spring-cloud.version>
20 <fastjson_version>1.2.28</fastjson_version> 20 <fastjson_version>1.2.28</fastjson_version>
  21 + <lombok_sersion>1.18.6</lombok_sersion>
21 </properties> 22 </properties>
22 23
23 <dependencies> 24 <dependencies>
@@ -86,6 +87,13 @@ @@ -86,6 +87,13 @@
86 </dependency> 87 </dependency>
87 <!--spring boot--> 88 <!--spring boot-->
88 <!-- tools--> 89 <!-- tools-->
  90 + <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
  91 + <dependency>
  92 + <groupId>org.projectlombok</groupId>
  93 + <artifactId>lombok</artifactId>
  94 + <version>${lombok_sersion}</version>
  95 + <scope>provided</scope>
  96 + </dependency>
89 <dependency> 97 <dependency>
90 <groupId>commons-codec</groupId> 98 <groupId>commons-codec</groupId>
91 <artifactId>commons-codec</artifactId> 99 <artifactId>commons-codec</artifactId>
@@ -20,6 +20,6 @@ @@ -20,6 +20,6 @@
20 * 目前在IMF框架中使用,打开IMF_Task里面的定时任务注释就可以启动IMF客户端功能 20 * 目前在IMF框架中使用,打开IMF_Task里面的定时任务注释就可以启动IMF客户端功能
21 * 集成Spring Cloud 21 * 集成Spring Cloud
22 * 集成websocket 22 * 集成websocket
23 -* 将会集成lombok,简化部分代码录入,比如实体类,使用方法见[lombok集成使用说明](https://jingyan.baidu.com/article/0a52e3f4e53ca1bf63ed725c.html) 23 +* 集成lombok,简化部分代码录入,比如实体类,方便实体及表结构修改,敏捷开发必用,使用方法见[lombok集成使用说明](https://jingyan.baidu.com/article/0a52e3f4e53ca1bf63ed725c.html)
24 ) 24 )
25 25
@@ -11,9 +11,12 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -11,9 +11,12 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
11 import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; 11 import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
12 import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 12 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
13 import org.springframework.scheduling.annotation.EnableScheduling; 13 import org.springframework.scheduling.annotation.EnableScheduling;
  14 +import org.springframework.transaction.annotation.EnableTransactionManagement;
  15 +
14 @SpringBootApplication 16 @SpringBootApplication
15 @EnableScheduling 17 @EnableScheduling
16 //@EnableEurekaClient 18 //@EnableEurekaClient
  19 +@EnableTransactionManagement
17 @MapperScan("com.tianbo.warehouse.dao") 20 @MapperScan("com.tianbo.warehouse.dao")
18 public class WarehouseApplication { 21 public class WarehouseApplication {
19 22
@@ -5,6 +5,7 @@ import com.tianbo.warehouse.annotation.LogAnnotation; @@ -5,6 +5,7 @@ import com.tianbo.warehouse.annotation.LogAnnotation;
5 import com.tianbo.warehouse.annotation.UserPasswordMd5; 5 import com.tianbo.warehouse.annotation.UserPasswordMd5;
6 import com.tianbo.warehouse.controller.response.ResultJson; 6 import com.tianbo.warehouse.controller.response.ResultJson;
7 import com.tianbo.warehouse.model.USERS; 7 import com.tianbo.warehouse.model.USERS;
  8 +import com.tianbo.warehouse.model.UserRole;
8 import com.tianbo.warehouse.service.UserService; 9 import com.tianbo.warehouse.service.UserService;
9 import org.springframework.beans.factory.annotation.Autowired; 10 import org.springframework.beans.factory.annotation.Autowired;
10 import org.springframework.security.core.context.SecurityContextHolder; 11 import org.springframework.security.core.context.SecurityContextHolder;
@@ -43,12 +44,8 @@ public class UserController { @@ -43,12 +44,8 @@ public class UserController {
43 public ResultJson updateUserById(@RequestBody @Valid USERS user){ 44 public ResultJson updateUserById(@RequestBody @Valid USERS user){
44 int i = userService.updateByPrimaryKeySelective(user); 45 int i = userService.updateByPrimaryKeySelective(user);
45 ResultJson resultJson = new ResultJson(); 46 ResultJson resultJson = new ResultJson();
46 - if (1==i){  
47 - resultJson = new ResultJson("200","success");  
48 - }else {  
49 - resultJson = new ResultJson("500","update faild");  
50 - }  
51 - return resultJson; 47 + return i==1 ? new ResultJson("200","success") :new ResultJson("500","update faild");
  48 +
52 } 49 }
53 50
54 @UserPasswordMd5 51 @UserPasswordMd5
@@ -62,12 +59,8 @@ public class UserController { @@ -62,12 +59,8 @@ public class UserController {
62 59
63 int i = userService.insertSelective(user); 60 int i = userService.insertSelective(user);
64 ResultJson resultJson = new ResultJson(); 61 ResultJson resultJson = new ResultJson();
65 - if (1==i){  
66 - resultJson = new ResultJson("200","新建账户成功");  
67 - }else {  
68 - resultJson = new ResultJson("500","insert faild");  
69 - }  
70 - return resultJson; 62 + return i==1 ? new ResultJson("200","新建账户成功") :new ResultJson("500","insert faild");
  63 +
71 } 64 }
72 65
73 @LogAnnotation(moduleName = "用户管理",operate = "用户删除") 66 @LogAnnotation(moduleName = "用户管理",operate = "用户删除")
@@ -76,12 +69,17 @@ public class UserController { @@ -76,12 +69,17 @@ public class UserController {
76 //String username = getusername(); 69 //String username = getusername();
77 int i = userService.deleteByPrimaryKey(user.getUserId()); 70 int i = userService.deleteByPrimaryKey(user.getUserId());
78 ResultJson resultJson = new ResultJson(); 71 ResultJson resultJson = new ResultJson();
79 - if (1==i){  
80 - resultJson = new ResultJson("200","删除账户成功");  
81 - }else {  
82 - resultJson = new ResultJson("500","delete faild"); 72 + return i==1 ? new ResultJson("200","删除账户成功") :new ResultJson("500","delete faild");
  73 +
83 } 74 }
84 - return resultJson; 75 +
  76 + @PutMapping("/user/roleset")
  77 + public ResultJson roleSet(@RequestBody UserRole userRole){
  78 +
  79 + int i = userService.setUserRole(userRole);
  80 +
  81 + return i==1 ? new ResultJson("200","设置权限成功") :new ResultJson("500","设置权限失败");
  82 +
85 } 83 }
86 84
87 } 85 }
  1 +package com.tianbo.warehouse.dao;
  2 +
  3 +import com.tianbo.warehouse.model.UserRole;
  4 +
  5 +public interface UserRoleMapper {
  6 + int deleteByPrimaryKey(Integer id);
  7 +
  8 + int deleteByUserId(Integer userId);
  9 +
  10 + int insert(UserRole record);
  11 +
  12 + int insertSelective(UserRole record);
  13 +
  14 + UserRole selectByPrimaryKey(Integer id);
  15 +
  16 + int updateByPrimaryKeySelective(UserRole record);
  17 +
  18 + int updateByPrimaryKey(UserRole record);
  19 +}
  1 +package com.tianbo.warehouse.model;
  2 +
  3 +import lombok.Data;
  4 +
  5 +import java.util.List;
  6 +
  7 +@Data
  8 +public class UserRole {
  9 +
  10 + private Integer id;
  11 +
  12 + private Integer userId;
  13 +
  14 + private Integer roleId;
  15 +
  16 + private List<Integer> roleIds;
  17 +
  18 + public UserRole(){
  19 + }
  20 + public UserRole(Integer userId,Integer roleId){
  21 + this.roleId=roleId;
  22 + this.userId = userId;
  23 + }
  24 +}
@@ -2,6 +2,7 @@ package com.tianbo.warehouse.service; @@ -2,6 +2,7 @@ package com.tianbo.warehouse.service;
2 2
3 import com.github.pagehelper.PageInfo; 3 import com.github.pagehelper.PageInfo;
4 import com.tianbo.warehouse.model.USERS; 4 import com.tianbo.warehouse.model.USERS;
  5 +import com.tianbo.warehouse.model.UserRole;
5 6
6 import java.util.List; 7 import java.util.List;
7 8
@@ -15,4 +16,6 @@ public interface UserService { @@ -15,4 +16,6 @@ public interface UserService {
15 int insertSelective(USERS record); 16 int insertSelective(USERS record);
16 17
17 int deleteByPrimaryKey(Integer userId); 18 int deleteByPrimaryKey(Integer userId);
  19 +
  20 + int setUserRole(UserRole userRole);
18 } 21 }
@@ -6,12 +6,15 @@ import com.github.pagehelper.PageInfo; @@ -6,12 +6,15 @@ import com.github.pagehelper.PageInfo;
6 import com.tianbo.warehouse.dao.PERMISSIONMapper; 6 import com.tianbo.warehouse.dao.PERMISSIONMapper;
7 import com.tianbo.warehouse.dao.ROLEMapper; 7 import com.tianbo.warehouse.dao.ROLEMapper;
8 import com.tianbo.warehouse.dao.USERSMapper; 8 import com.tianbo.warehouse.dao.USERSMapper;
  9 +import com.tianbo.warehouse.dao.UserRoleMapper;
9 import com.tianbo.warehouse.model.PERMISSION; 10 import com.tianbo.warehouse.model.PERMISSION;
10 import com.tianbo.warehouse.model.ROLE; 11 import com.tianbo.warehouse.model.ROLE;
11 import com.tianbo.warehouse.model.USERS; 12 import com.tianbo.warehouse.model.USERS;
  13 +import com.tianbo.warehouse.model.UserRole;
12 import com.tianbo.warehouse.service.UserService; 14 import com.tianbo.warehouse.service.UserService;
13 import org.springframework.beans.factory.annotation.Autowired; 15 import org.springframework.beans.factory.annotation.Autowired;
14 import org.springframework.stereotype.Service; 16 import org.springframework.stereotype.Service;
  17 +import org.springframework.transaction.annotation.Transactional;
15 18
16 import java.util.List; 19 import java.util.List;
17 20
@@ -27,6 +30,9 @@ public class UserServiceImpl implements UserService{ @@ -27,6 +30,9 @@ public class UserServiceImpl implements UserService{
27 @Autowired 30 @Autowired
28 private PERMISSIONMapper permissionMapper; 31 private PERMISSIONMapper permissionMapper;
29 32
  33 + @Autowired
  34 + private UserRoleMapper userRoleMapper;
  35 +
30 @Override 36 @Override
31 public USERS loadByUsername(String username){ 37 public USERS loadByUsername(String username){
32 List<USERS> userList = usersMapper.selectByUsername(username); 38 List<USERS> userList = usersMapper.selectByUsername(username);
@@ -90,4 +96,32 @@ public class UserServiceImpl implements UserService{ @@ -90,4 +96,32 @@ public class UserServiceImpl implements UserService{
90 return 0; 96 return 0;
91 } 97 }
92 } 98 }
  99 +
  100 + /**重置用户的权限
  101 + *这里需要开启事务,删除 或者插入不生效就回滚
  102 + * @param userRole
  103 + * @return
  104 + */
  105 + @Override
  106 + @Transactional(rollbackFor = Exception.class)
  107 + public int setUserRole(UserRole userRole){
  108 + try{
  109 + Integer userId = userRole.getUserId();
  110 + List<Integer> ids = userRole.getRoleIds();
  111 +
  112 + userRoleMapper.deleteByUserId(userId);
  113 + if (null!=ids && !ids.isEmpty()){
  114 + for (Integer id:ids) {
  115 + UserRole ur = new UserRole(userId,id);
  116 + userRoleMapper.insertSelective(ur);
  117 + }
  118 + }
  119 + return 1;
  120 + }catch (Exception e){
  121 + e.printStackTrace();
  122 + return 0;
  123 + }
  124 +
  125 + }
  126 +
93 } 127 }
@@ -45,6 +45,6 @@ @@ -45,6 +45,6 @@
45 <property name="enableSubPackages" value="true"/> 45 <property name="enableSubPackages" value="true"/>
46 </javaClientGenerator> 46 </javaClientGenerator>
47 <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名--> 47 <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
48 - <table tableName="log" domainObjectName="LOG" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> 48 + <table tableName="user_role" domainObjectName="UserRole" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
49 </context> 49 </context>
50 </generatorConfiguration> 50 </generatorConfiguration>
  1 +<?xml version="1.0" encoding="UTF-8" ?>
  2 +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3 +<mapper namespace="com.tianbo.warehouse.dao.UserRoleMapper" >
  4 + <resultMap id="BaseResultMap" type="com.tianbo.warehouse.model.UserRole" >
  5 + <id column="id" property="id" jdbcType="INTEGER" />
  6 + <result column="user_id" property="userId" jdbcType="INTEGER" />
  7 + <result column="role_id" property="roleId" jdbcType="INTEGER" />
  8 + </resultMap>
  9 + <sql id="Base_Column_List" >
  10 + id, user_id, role_id
  11 + </sql>
  12 + <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
  13 + select
  14 + <include refid="Base_Column_List" />
  15 + from user_role
  16 + where id = #{id,jdbcType=INTEGER}
  17 + </select>
  18 + <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
  19 + delete from user_role
  20 + where id = #{id,jdbcType=INTEGER}
  21 + </delete>
  22 + <delete id="deleteByUserId" parameterType="java.lang.Integer" >
  23 + delete from user_role
  24 + where user_id = #{userId,jdbcType=INTEGER}
  25 + </delete>
  26 + <insert id="insert" parameterType="com.tianbo.warehouse.model.UserRole" >
  27 + insert into user_role (id, user_id, role_id
  28 + )
  29 + values (#{id,jdbcType=INTEGER}, #{userId,jdbcType=INTEGER}, #{roleId,jdbcType=INTEGER}
  30 + )
  31 + </insert>
  32 + <insert id="insertSelective" parameterType="com.tianbo.warehouse.model.UserRole" >
  33 + insert into user_role
  34 + <trim prefix="(" suffix=")" suffixOverrides="," >
  35 + <if test="id != null" >
  36 + id,
  37 + </if>
  38 + <if test="userId != null" >
  39 + user_id,
  40 + </if>
  41 + <if test="roleId != null" >
  42 + role_id,
  43 + </if>
  44 + </trim>
  45 + <trim prefix="values (" suffix=")" suffixOverrides="," >
  46 + <if test="id != null" >
  47 + #{id,jdbcType=INTEGER},
  48 + </if>
  49 + <if test="userId != null" >
  50 + #{userId,jdbcType=INTEGER},
  51 + </if>
  52 + <if test="roleId != null" >
  53 + #{roleId,jdbcType=INTEGER},
  54 + </if>
  55 + </trim>
  56 + </insert>
  57 + <update id="updateByPrimaryKeySelective" parameterType="com.tianbo.warehouse.model.UserRole" >
  58 + update user_role
  59 + <set >
  60 + <if test="userId != null" >
  61 + user_id = #{userId,jdbcType=INTEGER},
  62 + </if>
  63 + <if test="roleId != null" >
  64 + role_id = #{roleId,jdbcType=INTEGER},
  65 + </if>
  66 + </set>
  67 + where id = #{id,jdbcType=INTEGER}
  68 + </update>
  69 + <update id="updateByPrimaryKey" parameterType="com.tianbo.warehouse.model.UserRole" >
  70 + update user_role
  71 + set user_id = #{userId,jdbcType=INTEGER},
  72 + role_id = #{roleId,jdbcType=INTEGER}
  73 + where id = #{id,jdbcType=INTEGER}
  74 + </update>
  75 +</mapper>