用户角色管理
This commit is contained in:
@ -3,7 +3,9 @@ package com.coscoshipping.ebtp.project.role.controller;
|
|||||||
|
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import com.chinaunicom.mall.ebtp.common.base.entity.BaseResponse;
|
import com.chinaunicom.mall.ebtp.common.base.entity.BaseResponse;
|
||||||
|
import com.coscoshipping.ebtp.project.role.entity.vo.AllRolesAndAssignRoleVO;
|
||||||
import com.coscoshipping.ebtp.project.role.entity.vo.SysRoleVO;
|
import com.coscoshipping.ebtp.project.role.entity.vo.SysRoleVO;
|
||||||
|
import com.coscoshipping.ebtp.project.userrole.service.SysUserRoleService;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
@ -27,6 +29,8 @@ public class SysRoleController{
|
|||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private SysRoleService iSysRoleService;
|
private SysRoleService iSysRoleService;
|
||||||
|
@Resource
|
||||||
|
private SysUserRoleService userRoleService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 插入新数据
|
* 插入新数据
|
||||||
@ -119,4 +123,16 @@ public class SysRoleController{
|
|||||||
|
|
||||||
return BaseResponse.success(iSysRoleService.getPage(sysRoleVO));
|
return BaseResponse.success(iSysRoleService.getPage(sysRoleVO));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取角色关联人员
|
||||||
|
*
|
||||||
|
* @return 返回结果
|
||||||
|
*/
|
||||||
|
@ApiOperation("获取角色关联人员")
|
||||||
|
@GetMapping("/role/assign/{id}")
|
||||||
|
public BaseResponse<AllRolesAndAssignRoleVO> getAssigningRoles(@ApiParam("用户id") @PathVariable("id") String userId) {
|
||||||
|
|
||||||
|
return BaseResponse.success(userRoleService.getAllRolesAndAssignRole(userId));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,26 @@
|
|||||||
|
package com.coscoshipping.ebtp.project.role.entity.vo;
|
||||||
|
|
||||||
|
import com.coscoshipping.ebtp.project.role.entity.SysRole;
|
||||||
|
import io.swagger.annotations.ApiModel;
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.experimental.Accessors;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Accessors(chain = true)
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@ApiModel("所有角色信息和已分配的角色id")
|
||||||
|
public class AllRolesAndAssignRoleVO {
|
||||||
|
|
||||||
|
@ApiModelProperty("角色列表")
|
||||||
|
private List<SysRole> allRole;
|
||||||
|
|
||||||
|
@ApiModelProperty("已分配的角色信息")
|
||||||
|
private List<Long> roleIds;
|
||||||
|
|
||||||
|
}
|
@ -11,6 +11,7 @@ import lombok.Data;
|
|||||||
import lombok.experimental.Accessors;
|
import lombok.experimental.Accessors;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 实体类 SysUser-门户用户表
|
* 实体类 SysUser-门户用户表
|
||||||
@ -27,4 +28,7 @@ public class SysUserVO extends SysUser implements Serializable {
|
|||||||
*/
|
*/
|
||||||
@ApiModelProperty(value = "分页对象信息")
|
@ApiModelProperty(value = "分页对象信息")
|
||||||
private BasePageRequest basePageRequest;
|
private BasePageRequest basePageRequest;
|
||||||
|
|
||||||
|
@ApiModelProperty(value = "角色ID集合")
|
||||||
|
private List<Long> roleIds;
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,121 @@
|
|||||||
|
package com.coscoshipping.ebtp.project.userrole.controller;
|
||||||
|
|
||||||
|
|
||||||
|
import com.chinaunicom.mall.ebtp.common.base.entity.BaseResponse;
|
||||||
|
import com.coscoshipping.ebtp.project.user.entity.vo.SysUserVO;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
import io.swagger.annotations.ApiParam;
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import javax.validation.Valid;
|
||||||
|
import java.util.List;
|
||||||
|
import com.coscoshipping.ebtp.project.userrole.entity.SysUserRole;
|
||||||
|
import com.coscoshipping.ebtp.project.userrole.service.SysUserRoleService;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@Api(value = "用户与角色关系表")
|
||||||
|
@RequestMapping("/v1/sysuserrole")
|
||||||
|
public class SysUserRoleController{
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private SysUserRoleService iSysUserRoleService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户分配角色
|
||||||
|
*
|
||||||
|
* @param sysUserVO 分配信息
|
||||||
|
* @return 返回结果
|
||||||
|
*/
|
||||||
|
@ApiOperation("用户分配角色")
|
||||||
|
@PostMapping("/assignsRoles")
|
||||||
|
public BaseResponse<Boolean> assignsRoles(@ApiParam(value = "对象数据", required = true) @RequestBody SysUserVO sysUserVO) {
|
||||||
|
|
||||||
|
return BaseResponse.success(iSysUserRoleService.assignsRoles(sysUserVO));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 插入新数据
|
||||||
|
*
|
||||||
|
* @param sysUserRole
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@ApiOperation("插入新数据")
|
||||||
|
@PostMapping
|
||||||
|
public BaseResponse<Boolean> insert(@ApiParam(value = "对象数据", required = true) @RequestBody @Valid SysUserRole sysUserRole){
|
||||||
|
|
||||||
|
Boolean i = iSysUserRoleService.save(sysUserRole);
|
||||||
|
|
||||||
|
return BaseResponse.success(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 修改数据
|
||||||
|
*
|
||||||
|
* @param sysUserRole
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@ApiOperation("修改数据")
|
||||||
|
@PutMapping
|
||||||
|
public BaseResponse<Boolean> update(@ApiParam(value = "对象数据", required = true) @RequestBody SysUserRole sysUserRole){
|
||||||
|
|
||||||
|
Boolean i = iSysUserRoleService.updateById(sysUserRole);
|
||||||
|
|
||||||
|
return BaseResponse.success(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询数据
|
||||||
|
*
|
||||||
|
* @param id
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@ApiOperation("根据id查询数据")
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public BaseResponse<SysUserRole> get(@ApiParam(value = "主键id", required = true) @PathVariable String id){
|
||||||
|
|
||||||
|
SysUserRole sysUserRole = iSysUserRoleService.getById(id);
|
||||||
|
|
||||||
|
return BaseResponse.success(sysUserRole);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除数据
|
||||||
|
* @param id 主键id
|
||||||
|
* @return BaseResponse<Boolean>
|
||||||
|
* @author dino
|
||||||
|
* @date 2020/10/21 14:56
|
||||||
|
*/
|
||||||
|
@ApiOperation(value = "delete",notes = "删除数据")
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public BaseResponse<Boolean> delete(@ApiParam(value = "主键id", required = true) @PathVariable Long id) {
|
||||||
|
Boolean i = iSysUserRoleService.removeById(id);
|
||||||
|
return BaseResponse.success(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询数据
|
||||||
|
*
|
||||||
|
* @param param
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@ApiOperation("查询列表数据")
|
||||||
|
@GetMapping("/list")
|
||||||
|
public BaseResponse<List<SysUserRole>> list(@ApiParam(value = "查询对象数据", required = false) SysUserRole param) {
|
||||||
|
//查询
|
||||||
|
LambdaQueryWrapper<SysUserRole> query = Wrappers.lambdaQuery(param);
|
||||||
|
List<SysUserRole> list = iSysUserRoleService.list(query);
|
||||||
|
//异常处理
|
||||||
|
//RespsExceptionEnum.FRAME_EXCEPTION_DEMO_NOT_FIND.customValid(list.isEmpty());
|
||||||
|
return BaseResponse.success(list);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
package com.coscoshipping.ebtp.project.userrole.dao;
|
||||||
|
|
||||||
|
|
||||||
|
import com.chinaunicom.mall.ebtp.common.base.dao.IBaseMapper;
|
||||||
|
import java.util.List;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
import com.coscoshipping.ebtp.project.userrole.entity.SysUserRole;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface SysUserRoleMapper extends IBaseMapper<SysUserRole> {
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
|
|
||||||
|
<mapper namespace="com.coscoshipping.ebtp.project.dao.SysUserRoleMapper">
|
||||||
|
<resultMap id="userroleResultMap" type="com.coscoshipping.ebtp.project.userrole.entity.SysUserRole">
|
||||||
|
<result column="user_id" jdbcType="VARCHAR" property="userId" />
|
||||||
|
<result column="role_id" jdbcType="BIGINT" property="roleId" />
|
||||||
|
<result column="del_flag" jdbcType="VARCHAR" property="delFlag" />
|
||||||
|
<result column="create_by" jdbcType="VARCHAR" property="createBy" />
|
||||||
|
</resultMap>
|
||||||
|
|
||||||
|
<!--逻辑删除方法 此方法为代码生成器生成 不允许修改 如有特殊需求 请自行新建SQL语句-->
|
||||||
|
<update id="deleteOff" >
|
||||||
|
update sys_user_role
|
||||||
|
set
|
||||||
|
delete_flag="1"
|
||||||
|
where ID=#{id }
|
||||||
|
</update>
|
||||||
|
</mapper>
|
@ -0,0 +1,33 @@
|
|||||||
|
package com.coscoshipping.ebtp.project.userrole.entity;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import io.swagger.annotations.ApiModel;
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.experimental.Accessors;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 实体类 SysUserRole-用户与角色关系表
|
||||||
|
*
|
||||||
|
* @author yss
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@Accessors(chain = true)
|
||||||
|
@TableName(value = "sys_user_role", autoResultMap = true)
|
||||||
|
@ApiModel(value = "SysUserRole对象", description = "用户与角色关系表")
|
||||||
|
public class SysUserRole implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
@ApiModelProperty(value = "用户ID")
|
||||||
|
private String userId;
|
||||||
|
|
||||||
|
@ApiModelProperty(value = "角色ID")
|
||||||
|
private Long roleId;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package com.coscoshipping.ebtp.project.userrole.service;
|
||||||
|
|
||||||
|
|
||||||
|
import com.chinaunicom.mall.ebtp.common.base.service.IBaseService;
|
||||||
|
import com.coscoshipping.ebtp.project.role.entity.vo.AllRolesAndAssignRoleVO;
|
||||||
|
import com.coscoshipping.ebtp.project.user.entity.vo.SysUserVO;
|
||||||
|
import com.coscoshipping.ebtp.project.userrole.entity.SysUserRole;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对数据表 sys_user_role 操作的 service
|
||||||
|
* @author yss
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public interface SysUserRoleService extends IBaseService<SysUserRole>{
|
||||||
|
|
||||||
|
AllRolesAndAssignRoleVO getAllRolesAndAssignRole(String userId);
|
||||||
|
/**
|
||||||
|
* 用户分配角色
|
||||||
|
* @param sysUserVO 分配信息
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
boolean assignsRoles(SysUserVO sysUserVO);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,59 @@
|
|||||||
|
package com.coscoshipping.ebtp.project.userrole.service.impl;
|
||||||
|
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||||
|
import com.chinaunicom.mall.ebtp.common.exception.common.CommonExceptionEnum;
|
||||||
|
import com.coscoshipping.ebtp.project.role.entity.SysRole;
|
||||||
|
import com.coscoshipping.ebtp.project.role.entity.vo.AllRolesAndAssignRoleVO;
|
||||||
|
import com.coscoshipping.ebtp.project.role.service.SysRoleService;
|
||||||
|
import com.coscoshipping.ebtp.project.user.entity.vo.SysUserVO;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import com.chinaunicom.mall.ebtp.common.base.service.impl.BaseServiceImpl;
|
||||||
|
import com.chinaunicom.mall.ebtp.common.util.PropertyUtils;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import com.coscoshipping.ebtp.project.userrole.dao.SysUserRoleMapper;
|
||||||
|
import com.coscoshipping.ebtp.project.userrole.entity.SysUserRole;
|
||||||
|
import com.coscoshipping.ebtp.project.userrole.service.SysUserRoleService;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对数据表 sys_user_role 操作的 serviceImpl
|
||||||
|
* @author yss
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class SysUserRoleServiceImpl extends BaseServiceImpl<SysUserRoleMapper,SysUserRole> implements SysUserRoleService {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private SysRoleService sysRoleService;
|
||||||
|
@Override
|
||||||
|
public AllRolesAndAssignRoleVO getAllRolesAndAssignRole(String userId) {
|
||||||
|
List<SysRole> list = sysRoleService.list();
|
||||||
|
List<SysUserRole> userRoleList = list(Wrappers.<SysUserRole>lambdaQuery().eq(SysUserRole::getUserId, userId));
|
||||||
|
List<Long> roleIds = userRoleList.stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
|
||||||
|
return new AllRolesAndAssignRoleVO(list, roleIds);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean assignsRoles(SysUserVO sysUserVO) {
|
||||||
|
CommonExceptionEnum.FRAME_EXCEPTION_COMMON_DATA__ERROR.assertNotNull(sysUserVO.getRoleIds());
|
||||||
|
CommonExceptionEnum.FRAME_EXCEPTION_COMMON_DATA__ERROR.assertNotNull(sysUserVO.getUserId());
|
||||||
|
|
||||||
|
this.remove(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getUserId,sysUserVO.getUserId()));
|
||||||
|
|
||||||
|
List<SysUserRole> userRoleList = new ArrayList<>();
|
||||||
|
|
||||||
|
for (Long roleId : sysUserVO.getRoleIds()) {
|
||||||
|
SysUserRole userRole = new SysUserRole();
|
||||||
|
userRole.setRoleId(roleId);
|
||||||
|
userRole.setUserId(sysUserVO.getUserId());
|
||||||
|
userRoleList.add(userRole);
|
||||||
|
}
|
||||||
|
return this.saveBatch(userRoleList);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user