add
This commit is contained in:
@ -4,7 +4,10 @@ import com.qiaoba.api.system.entity.SysDept;
|
||||
import com.qiaoba.api.system.entity.param.SysDeptParam;
|
||||
import com.qiaoba.common.base.result.AjaxResult;
|
||||
import com.qiaoba.module.system.service.SysDeptService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@ -19,50 +22,58 @@ import java.util.List;
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/system/dept")
|
||||
@Tag(name = "部门管理")
|
||||
@RequiredArgsConstructor
|
||||
public class SysDeptController {
|
||||
|
||||
private final SysDeptService sysDeptService;
|
||||
|
||||
//@PreAuthorize("hasAuthority('system:dept:list')")
|
||||
@PreAuthorize("hasAuthority('system:dept:list')")
|
||||
@GetMapping("/list")
|
||||
@Operation(summary = "获取列表")
|
||||
public AjaxResult list(SysDeptParam param) {
|
||||
return AjaxResult.success(sysDeptService.selectList(param));
|
||||
}
|
||||
|
||||
//@PreAuthorize("hasAuthority('system:dept:list')")
|
||||
@PreAuthorize("hasAuthority('system:dept:list')")
|
||||
@GetMapping("/list/exclude/{deptId}")
|
||||
@Operation(summary = "查询部门列表(排除节点)")
|
||||
public AjaxResult excludeChild(@PathVariable(value = "deptId", required = false) String deptId) {
|
||||
return AjaxResult.success(sysDeptService.excludeChild(deptId));
|
||||
}
|
||||
|
||||
//@PreAuthorize("hasAuthority('system:dept:query')")
|
||||
@PreAuthorize("hasAuthority('system:dept:query')")
|
||||
@GetMapping(value = "/{deptId}")
|
||||
public AjaxResult getInfo(@PathVariable Long deptId) {
|
||||
@Operation(summary = "获取详情")
|
||||
public AjaxResult getInfo(@PathVariable String deptId) {
|
||||
return AjaxResult.success(sysDeptService.selectById(deptId));
|
||||
}
|
||||
|
||||
@GetMapping("/deptTree")
|
||||
@Operation(summary = "构建部门树")
|
||||
public AjaxResult deptTree(SysDeptParam param) {
|
||||
List<SysDept> list = sysDeptService.selectList(param);
|
||||
return AjaxResult.success(sysDeptService.buildDeptTree(list));
|
||||
}
|
||||
|
||||
//@PreAuthorize("hasAuthority('system:dept:add')")
|
||||
@PreAuthorize("hasAuthority('system:dept:add')")
|
||||
@PostMapping
|
||||
@Operation(summary = "新增部门")
|
||||
public AjaxResult add(@Validated @RequestBody SysDept dept) {
|
||||
return AjaxResult.toAjax(sysDeptService.insert(dept));
|
||||
}
|
||||
|
||||
//@PreAuthorize("hasAuthority('system:dept:edit')")
|
||||
@PreAuthorize("hasAuthority('system:dept:edit')")
|
||||
@PutMapping
|
||||
@Operation(summary = "修改部门")
|
||||
public AjaxResult edit(@Validated @RequestBody SysDept dept) {
|
||||
return AjaxResult.toAjax(sysDeptService.updateById(dept));
|
||||
}
|
||||
|
||||
//@PreAuthorize("hasAuthority('system:dept:remove')")
|
||||
@PreAuthorize("hasAuthority('system:dept:remove')")
|
||||
@DeleteMapping("/{id}")
|
||||
public AjaxResult remove(@PathVariable Long id) {
|
||||
@Operation(summary = "删除部门")
|
||||
public AjaxResult remove(@PathVariable String id) {
|
||||
return AjaxResult.toAjax(sysDeptService.deleteById(id));
|
||||
}
|
||||
}
|
||||
|
@ -1,18 +1,22 @@
|
||||
package com.qiaoba.module.system.controller;
|
||||
|
||||
import com.qiaoba.api.system.entity.SysMenu;
|
||||
import com.qiaoba.api.system.entity.dto.LoginDto;
|
||||
import com.qiaoba.api.system.entity.vo.SysMenuVo;
|
||||
import com.qiaoba.auth.utils.SecurityUtil;
|
||||
import com.qiaoba.common.base.result.AjaxResult;
|
||||
import com.qiaoba.module.system.service.SysLoginService;
|
||||
import com.qiaoba.module.system.service.SysMenuService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 登录
|
||||
* 登录 Web层
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
@ -23,15 +27,30 @@ import java.util.List;
|
||||
public class SysLoginController {
|
||||
|
||||
private final SysMenuService sysMenuService;
|
||||
private final SysLoginService sysLoginService;
|
||||
|
||||
|
||||
@GetMapping("/captchaImage")
|
||||
public AjaxResult getCaptchaImage() {
|
||||
return sysLoginService.getCaptchaImage();
|
||||
}
|
||||
|
||||
@PostMapping("/login")
|
||||
public String login() {
|
||||
return "{\"msg\":\"操作成功\",\"code\":200,\"token\":\"eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjhhNjY4MGY1LTI0OTAtNDUyYi1hYzQ1LWE5YWI0MzQ0YTRlYyJ9.mTZr7TN1Jk2-_7zbeUbOBuHijVLiqY2QSbhcAIfWuX4oF22xGw_DpxOz3I2_-TLeYlJN4y2Gm1qmw6ricqCBqw\"}";
|
||||
public AjaxResult login(@RequestBody LoginDto dto) {
|
||||
String token = sysLoginService.login(dto);
|
||||
AjaxResult ajax = AjaxResult.success();
|
||||
ajax.put("token", token);
|
||||
return ajax;
|
||||
}
|
||||
|
||||
@GetMapping("/getInfo")
|
||||
public String getInfo() {
|
||||
List<String> roleIds = SecurityUtil.getLoginUser().getRoleIds();
|
||||
Set<String> permissions = SecurityUtil.getLoginUser().getPermissions();
|
||||
AjaxResult ajax = AjaxResult.success();
|
||||
ajax.put("permissions", permissions);
|
||||
ajax.put("roles", roleIds);
|
||||
ajax.put("user",sysLoginService.selectUserFromCache(SecurityUtil.getLoginUserId()));
|
||||
return "{\"msg\":\"操作成功\",\"code\":200,\"permissions\":[\"*:*:*\"],\"roles\":[\"admin\"],\"user\":{\"createBy\":\"admin\",\"createTime\":\"2023-04-23 16:11:38\",\"updateBy\":null,\"updateTime\":null,\"remark\":\"管理员\",\"userId\":1,\"deptId\":103,\"userName\":\"admin\",\"nickName\":\"若依\",\"email\":\"ry@163.com\",\"phonenumber\":\"15888888888\",\"sex\":\"1\",\"avatar\":\"\",\"password\":\"$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2\",\"status\":\"0\",\"delFlag\":\"0\",\"loginIp\":\"27.154.23.192\",\"loginDate\":\"2023-05-05T09:45:24.000+08:00\",\"dept\":{\"createBy\":null,\"createTime\":null,\"updateBy\":null,\"updateTime\":null,\"remark\":null,\"deptId\":103,\"parentId\":101,\"ancestors\":\"0,100,101\",\"deptName\":\"研发部门\",\"orderNum\":1,\"leader\":\"若依\",\"phone\":null,\"email\":null,\"status\":\"0\",\"delFlag\":null,\"parentName\":null,\"children\":[]},\"roles\":[{\"createBy\":null,\"createTime\":null,\"updateBy\":null,\"updateTime\":null,\"remark\":null,\"roleId\":1,\"roleName\":\"超级管理员\",\"roleKey\":\"admin\",\"roleSort\":1,\"dataScope\":\"1\",\"menuCheckStrictly\":false,\"deptCheckStrictly\":false,\"status\":\"0\",\"delFlag\":null,\"flag\":false,\"menuIds\":null,\"deptIds\":null,\"permissions\":null,\"admin\":true}],\"roleIds\":null,\"postIds\":null,\"roleId\":null,\"admin\":true}}";
|
||||
}
|
||||
|
||||
|
@ -7,7 +7,11 @@ import com.qiaoba.common.database.entity.PageQuery;
|
||||
import com.qiaoba.common.database.entity.TableDataInfo;
|
||||
import com.qiaoba.common.poi.utils.ExcelUtil;
|
||||
import com.qiaoba.module.system.service.SysPostService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
@ -23,68 +27,52 @@ import java.util.List;
|
||||
@RestController
|
||||
@RequestMapping("/system/post")
|
||||
@RequiredArgsConstructor
|
||||
@Tag(name = "岗位管理")
|
||||
public class SysPostController {
|
||||
|
||||
private final SysPostService sysPostService;
|
||||
|
||||
/**
|
||||
* 获取岗位列表
|
||||
*/
|
||||
@GetMapping("/list")
|
||||
//@PreAuthorize("hasAuthority('system:post:list')")
|
||||
@PreAuthorize("hasAuthority('system:post:list')")
|
||||
@Operation(summary = "获取列表")
|
||||
public TableDataInfo<SysPost> list(SysPostParam param, PageQuery pageQuery) {
|
||||
return sysPostService.selectPageList(param, pageQuery);
|
||||
}
|
||||
|
||||
@PostMapping("/export")
|
||||
//@PreAuthorize("hasAuthority('system:post:export')")
|
||||
@PreAuthorize("hasAuthority('system:post:export')")
|
||||
@Operation(summary = "导出岗位")
|
||||
public void export(HttpServletResponse response, SysPostParam param) {
|
||||
List<SysPost> list = sysPostService.selectList(param);
|
||||
ExcelUtil.exportExcel(list, SysPost.class, "岗位数据", response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据岗位编号获取详细信息
|
||||
*/
|
||||
@GetMapping(value = "/{postId}")
|
||||
//@PreAuthorize("hasAuthority('system:post:query')")
|
||||
@PreAuthorize("hasAuthority('system:post:query')")
|
||||
@Operation(summary = "获取详情")
|
||||
public AjaxResult getInfo(@PathVariable("postId") String postId) {
|
||||
return AjaxResult.success(sysPostService.selectById(postId));
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增岗位
|
||||
*/
|
||||
@PostMapping
|
||||
//@PreAuthorize("hasAuthority('system:post:add')")
|
||||
public AjaxResult add(@RequestBody SysPost post) {
|
||||
@PreAuthorize("hasAuthority('system:post:add')")
|
||||
@Operation(summary = "新增岗位")
|
||||
public AjaxResult add(@Validated @RequestBody SysPost post) {
|
||||
return AjaxResult.toAjax(sysPostService.insert(post));
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改岗位
|
||||
*/
|
||||
@PutMapping
|
||||
//@PreAuthorize("hasAuthority('system:post:edit')")
|
||||
public AjaxResult edit(@RequestBody SysPost post) {
|
||||
@PreAuthorize("hasAuthority('system:post:edit')")
|
||||
@Operation(summary = "修改岗位")
|
||||
public AjaxResult edit(@Validated @RequestBody SysPost post) {
|
||||
return AjaxResult.toAjax(sysPostService.updateById(post));
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除岗位
|
||||
*/
|
||||
@DeleteMapping("/{ids}")
|
||||
//@PreAuthorize("hasAuthority('system:post:remove')")
|
||||
@PreAuthorize("hasAuthority('system:post:remove')")
|
||||
@Operation(summary = "删除岗位")
|
||||
public AjaxResult remove(@PathVariable List<String> ids) {
|
||||
return AjaxResult.toAjax(sysPostService.deleteByIds(ids));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取岗位选择框列表
|
||||
*/
|
||||
/* @GetMapping("/optionSelect")
|
||||
public AjaxResult optionSelect() {
|
||||
List<SysPost> posts = postService.selectPostAll();
|
||||
return AjaxResult.success(posts);
|
||||
}*/
|
||||
}
|
||||
|
@ -98,7 +98,7 @@ public class SysUserController {
|
||||
SysUser sysUser = sysUserService.selectById(userId, false);
|
||||
ajax.put(AjaxResult.DATA_TAG, sysUser);
|
||||
ajax.put("postIds", sysUserPostService.selectPostIdsByUserId(userId));
|
||||
ajax.put("roleIds", sysUserRoleService.selectRoleIdsByUserId(userId));
|
||||
ajax.put("roleIds", sysUserRoleService.selectRoleIdsByUserId(userId, null));
|
||||
}
|
||||
return ajax;
|
||||
}
|
||||
@ -111,7 +111,7 @@ public class SysUserController {
|
||||
List<SysRole> roles = sysRoleService.selectList(new SysRoleParam());
|
||||
ajax.put("user", user);
|
||||
ajax.put("roles", roles);
|
||||
ajax.put("hasRoleIds", sysUserRoleService.selectRoleIdsByUserId(userId));
|
||||
ajax.put("hasRoleIds", sysUserRoleService.selectRoleIdsByUserId(userId, null));
|
||||
return ajax;
|
||||
}
|
||||
|
||||
|
@ -30,4 +30,20 @@ public interface SysDeptMapper extends BaseMapper<SysDept> {
|
||||
*/
|
||||
List<String> selectDeptIdsByRoleId(String roleId);
|
||||
|
||||
/**
|
||||
* 检查是否存在子部门
|
||||
*
|
||||
* @param deptId deptId
|
||||
* @return != null -> 是
|
||||
*/
|
||||
String existChild(String deptId);
|
||||
|
||||
/**
|
||||
* 检查部门是否被使用
|
||||
*
|
||||
* @param deptId deptId
|
||||
* @return != null -> 是
|
||||
*/
|
||||
String existUsed(String deptId);
|
||||
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import com.qiaoba.api.system.entity.vo.SysMenuVo;
|
||||
import com.qiaoba.common.database.mapper.BaseMapperPlus;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 菜单管理 数据层
|
||||
@ -38,4 +39,12 @@ public interface SysMenuMapper extends BaseMapperPlus<SysMenuMapper, SysMenu, Sy
|
||||
* @return list
|
||||
*/
|
||||
List<SysMenuVo> selectByUserId(String userId);
|
||||
|
||||
/**
|
||||
* 查询用户拥有的权限列表
|
||||
*
|
||||
* @param userId userId
|
||||
* @return 权限列表
|
||||
*/
|
||||
Set<String> selectPermsByUserId(String userId);
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ public interface SysPostMapper extends BaseMapper<SysPost> {
|
||||
* @param sysPost 岗位名称
|
||||
* @return 结果
|
||||
*/
|
||||
int checkPostNameUnique(SysPost sysPost);
|
||||
String checkPostNameUnique(SysPost sysPost);
|
||||
|
||||
/**
|
||||
* 校验岗位编码
|
||||
@ -26,6 +26,6 @@ public interface SysPostMapper extends BaseMapper<SysPost> {
|
||||
* @param sysPost 岗位编码
|
||||
* @return 结果
|
||||
*/
|
||||
int checkPostCodeUnique(SysPost sysPost);
|
||||
String checkPostCodeUnique(SysPost sysPost);
|
||||
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package com.qiaoba.module.system.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.qiaoba.api.system.entity.SysUserPost;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ -20,4 +21,12 @@ public interface SysUserPostMapper extends BaseMapper<SysUserPost> {
|
||||
* @return postIds
|
||||
*/
|
||||
List<String> selectPostIdsByUserId(String userId);
|
||||
|
||||
/**
|
||||
* 查询正在被使用的岗位名称列表
|
||||
*
|
||||
* @param postIds postIds
|
||||
* @return postNames
|
||||
*/
|
||||
List<String> selectUsedPostNameByIds(@Param("list") List<String> postIds);
|
||||
}
|
||||
|
@ -19,9 +19,10 @@ public interface SysUserRoleMapper extends BaseMapper<SysUserRole> {
|
||||
* 通过userId查询所绑定的角色ID列表
|
||||
*
|
||||
* @param userId userId
|
||||
* @param status 状态
|
||||
* @return roleIds
|
||||
*/
|
||||
List<String> selectRoleIdsByUserId(String userId);
|
||||
List<String> selectRoleIdsByUserId(@Param("userId") String userId, @Param("status") String status);
|
||||
|
||||
/**
|
||||
* 批量取消角色所绑定的用户
|
||||
|
@ -0,0 +1,38 @@
|
||||
package com.qiaoba.module.system.service;
|
||||
|
||||
import com.qiaoba.api.system.entity.SysUser;
|
||||
import com.qiaoba.api.system.entity.dto.LoginDto;
|
||||
import com.qiaoba.common.base.result.AjaxResult;
|
||||
|
||||
/**
|
||||
* 登录 服务层
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2023/5/15 15:31
|
||||
*/
|
||||
public interface SysLoginService {
|
||||
|
||||
/**
|
||||
* 生成图片验证码
|
||||
*
|
||||
* @return uuid + base64
|
||||
*/
|
||||
AjaxResult getCaptchaImage();
|
||||
|
||||
/**
|
||||
* 登录
|
||||
*
|
||||
* @param dto dto
|
||||
* @return token
|
||||
*/
|
||||
String login(LoginDto dto);
|
||||
|
||||
/**
|
||||
* 从缓存中读取user
|
||||
*
|
||||
* @param userId userId
|
||||
* @return SysUser
|
||||
*/
|
||||
SysUser selectUserFromCache(String userId);
|
||||
}
|
@ -7,6 +7,7 @@ import com.qiaoba.api.system.entity.vo.SysMenuVo;
|
||||
import com.qiaoba.api.system.service.SysMenuApiService;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 菜单管理 服务层
|
||||
@ -41,6 +42,14 @@ public interface SysMenuService extends SysMenuApiService {
|
||||
*/
|
||||
List<SysMenuVo> selectByUserId(String userId);
|
||||
|
||||
/**
|
||||
* 查询用户拥有的权限列表
|
||||
*
|
||||
* @param userId userId
|
||||
* @return 权限列表
|
||||
*/
|
||||
Set<String> selectPermsByUserId(String userId);
|
||||
|
||||
/**
|
||||
* 菜单转路由
|
||||
*
|
||||
|
@ -34,4 +34,12 @@ public interface SysUserPostService {
|
||||
* @return postIds
|
||||
*/
|
||||
List<String> selectPostIdsByUserId(String userId);
|
||||
|
||||
/**
|
||||
* 查询正在被使用的岗位名称列表
|
||||
*
|
||||
* @param postIds postIds
|
||||
* @return postNames
|
||||
*/
|
||||
List<String> selectUsedPostNameByIds(List<String> postIds);
|
||||
}
|
||||
|
@ -39,9 +39,10 @@ public interface SysUserRoleService {
|
||||
* 通过userId查询所绑定的角色ID列表
|
||||
*
|
||||
* @param userId userId
|
||||
* @param status 状态
|
||||
* @return roleIds
|
||||
*/
|
||||
List<String> selectRoleIdsByUserId(String userId);
|
||||
List<String> selectRoleIdsByUserId(String userId, String status);
|
||||
|
||||
/**
|
||||
* 批量选择用户授权
|
||||
|
@ -62,4 +62,5 @@ public interface SysUserService extends SysUserApiService {
|
||||
* @return list
|
||||
*/
|
||||
TableDataInfo selectUnAllocatedList(SysUserParam param, PageQuery pageQuery);
|
||||
|
||||
}
|
||||
|
@ -9,10 +9,10 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.toolkit.Db;
|
||||
import com.qiaoba.api.system.entity.SysDept;
|
||||
import com.qiaoba.api.system.entity.param.SysDeptParam;
|
||||
import com.qiaoba.auth.utils.SecurityUtil;
|
||||
import com.qiaoba.common.base.constants.BaseConstant;
|
||||
import com.qiaoba.common.base.enums.BaseEnum;
|
||||
import com.qiaoba.common.base.exceptions.ServiceException;
|
||||
import com.qiaoba.common.security.utils.SecurityUtil;
|
||||
import com.qiaoba.module.system.mapper.SysDeptMapper;
|
||||
import com.qiaoba.module.system.service.SysDeptService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@ -36,6 +36,7 @@ public class SysDeptServiceImpl implements SysDeptService {
|
||||
|
||||
private final SysDeptMapper sysDeptMapper;
|
||||
|
||||
|
||||
@Override
|
||||
public int insert(SysDept sysDept) {
|
||||
if (checkDeptNameNotUnique(sysDept)) {
|
||||
@ -83,12 +84,19 @@ public class SysDeptServiceImpl implements SysDeptService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public SysDept selectById(Long deptId) {
|
||||
public SysDept selectById(String deptId) {
|
||||
return sysDeptMapper.selectById(deptId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int deleteById(Long deptId) {
|
||||
public int deleteById(String deptId) {
|
||||
if (StrUtil.isNotBlank(sysDeptMapper.existChild(deptId))) {
|
||||
throw new ServiceException("存在子级部门,不允许删除");
|
||||
}
|
||||
String nickname = sysDeptMapper.existUsed(deptId);
|
||||
if (StrUtil.isNotBlank(nickname)) {
|
||||
throw new ServiceException(StrUtil.format("用户[{}]已绑定部门,不允许删除", nickname));
|
||||
}
|
||||
return sysDeptMapper.deleteById(deptId);
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,146 @@
|
||||
package com.qiaoba.module.system.service.impl;
|
||||
|
||||
import cn.hutool.captcha.CaptchaUtil;
|
||||
import cn.hutool.captcha.LineCaptcha;
|
||||
import cn.hutool.core.lang.UUID;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.qiaoba.api.system.entity.SysUser;
|
||||
import com.qiaoba.api.system.entity.dto.LoginDto;
|
||||
import com.qiaoba.auth.constants.SecurityConstant;
|
||||
import com.qiaoba.auth.utils.SecurityUtil;
|
||||
import com.qiaoba.auth.utils.TokenUtil;
|
||||
import com.qiaoba.common.base.enums.BaseEnum;
|
||||
import com.qiaoba.common.base.exceptions.ServiceException;
|
||||
import com.qiaoba.common.base.result.AjaxResult;
|
||||
import com.qiaoba.common.redis.service.RedisService;
|
||||
import com.qiaoba.module.system.service.SysLoginService;
|
||||
import com.qiaoba.module.system.service.SysUserService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* 登录 服务层实现
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2023/5/15 15:31
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class SysLoginServiceImpl implements SysLoginService {
|
||||
|
||||
private final RedisService redisService;
|
||||
private final HttpServletRequest request;
|
||||
private final UserDetailsService userDetailsService;
|
||||
private final SysUserService sysUserService;
|
||||
|
||||
@Override
|
||||
public AjaxResult getCaptchaImage() {
|
||||
AjaxResult ajax = AjaxResult.success();
|
||||
ajax.put("register", getRegisterConfig());
|
||||
if (!getCaptchaConfig()) {
|
||||
ajax.put("captchaEnabled", false);
|
||||
return ajax;
|
||||
}
|
||||
String uuid = UUID.randomUUID().toString(true);
|
||||
LineCaptcha captcha = CaptchaUtil.createLineCaptcha(150, 50, 4, 20);
|
||||
ajax.put("uuid", uuid);
|
||||
ajax.put("img", captcha.getImageBase64());
|
||||
redisService.set(SecurityConstant.CAPTCHA_KEY + uuid, captcha.getCode(), 120);
|
||||
|
||||
return ajax;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String login(LoginDto dto) {
|
||||
// 校验验证码
|
||||
validateCaptcha(dto.getCode(), dto.getUuid());
|
||||
// username查询用户信息
|
||||
SysUser sysUser = sysUserService.selectByUsername(dto.getUsername());
|
||||
// 检查账号信息
|
||||
validateUser(dto.getUsername(), sysUser);
|
||||
// 检验密码
|
||||
validatePassword(dto.getUsername(), sysUser.getPassword(), dto.getPassword());
|
||||
// 缓存user
|
||||
addUserToCache(sysUser);
|
||||
// 创建UserDetails
|
||||
UserDetails userDetails = userDetailsService.loadUserByUsername(dto.getUsername());
|
||||
// 生成Token
|
||||
return TokenUtil.generateToken(userDetails.getUsername());
|
||||
}
|
||||
|
||||
@Override
|
||||
public SysUser selectUserFromCache(String userId) {
|
||||
return redisService.getObject(userId, SysUser.class);
|
||||
}
|
||||
|
||||
private void addUserToCache(SysUser sysUser) {
|
||||
sysUser.setPassword(null);
|
||||
redisService.set(sysUser.getUserId(), sysUser);
|
||||
}
|
||||
|
||||
private void validatePassword(String username, String password, String inputPassword) {
|
||||
boolean result = SecurityUtil.matchesPassword(inputPassword, password);
|
||||
if (!result) {
|
||||
beforePasswordError(username);
|
||||
throw new ServiceException(SecurityConstant.PASSWORD_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
private void beforePasswordError(String username) {
|
||||
|
||||
}
|
||||
|
||||
private void validateUser(String username, SysUser user) {
|
||||
if (ObjectUtil.isNull(user)) {
|
||||
throw new ServiceException(StrUtil.format("登录用户:{} 不存在", username));
|
||||
} else if (BaseEnum.YES.getCode().equals(user.getIsDelete())) {
|
||||
throw new ServiceException(StrUtil.format("对不起, 您的账号:{} 已被删除", username));
|
||||
} else if (BaseEnum.ABNORMAL.getCode().equals(user.getStatus())) {
|
||||
throw new ServiceException(StrUtil.format("对不起, 您的账号:{} 已被禁用", username));
|
||||
}
|
||||
}
|
||||
|
||||
private boolean getCaptchaConfig() {
|
||||
return false;
|
||||
/* try {
|
||||
return SecurityConstant.CAPTCHA_ON.equals(redisService.get(SecurityConstant.CAPTCHA_ON_OFF_KEY));
|
||||
} catch (Exception e) {
|
||||
throw new ServiceException("Redis中验证码配置不存在!");
|
||||
}*/
|
||||
}
|
||||
|
||||
private boolean getRegisterConfig() {
|
||||
return false;
|
||||
/* try {
|
||||
return SecurityConstant.REGISTER_ON.equals(redisService.get(SecurityConstant.REGISTER_ON_OFF_KEY));
|
||||
} catch (Exception e) {
|
||||
throw new ServiceException("Redis中注册配置不存在!");
|
||||
}*/
|
||||
}
|
||||
|
||||
private void validateCaptcha(String code, String uuid) {
|
||||
if (getCaptchaConfig()) {
|
||||
if (StrUtil.isBlank(code) || StrUtil.isBlank(uuid)) {
|
||||
throw new ServiceException("验证码或uuid获取失败!");
|
||||
}
|
||||
try {
|
||||
if (!redisService.hasKey(SecurityConstant.CAPTCHA_KEY + uuid)) {
|
||||
throw new ServiceException("验证码已经过期失效!");
|
||||
} else {
|
||||
if (!code.equalsIgnoreCase(redisService.get(SecurityConstant.CAPTCHA_KEY + uuid).toString())) {
|
||||
throw new ServiceException("验证码输入错误!");
|
||||
}
|
||||
}
|
||||
|
||||
} finally {
|
||||
redisService.del(SecurityConstant.CAPTCHA_KEY + uuid);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -19,6 +19,7 @@ import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 菜单管理 服务层实现
|
||||
@ -94,6 +95,11 @@ public class SysMenuServiceImpl implements SysMenuService {
|
||||
return sysMenuMapper.selectByUserId(userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> selectPermsByUserId(String userId) {
|
||||
return sysMenuMapper.selectPermsByUserId(userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<RouterVo> menusToRouters(List<SysMenuVo> menus) {
|
||||
return MenuUtil.menusToRouters(menus);
|
||||
|
@ -1,5 +1,6 @@
|
||||
package com.qiaoba.module.system.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.qiaoba.api.system.entity.SysPost;
|
||||
@ -7,9 +8,10 @@ import com.qiaoba.api.system.entity.param.SysPostParam;
|
||||
import com.qiaoba.common.base.exceptions.ServiceException;
|
||||
import com.qiaoba.common.database.entity.PageQuery;
|
||||
import com.qiaoba.common.database.entity.TableDataInfo;
|
||||
import com.qiaoba.common.security.utils.SecurityUtil;
|
||||
import com.qiaoba.auth.utils.SecurityUtil;
|
||||
import com.qiaoba.module.system.mapper.SysPostMapper;
|
||||
import com.qiaoba.module.system.service.SysPostService;
|
||||
import com.qiaoba.module.system.service.SysUserPostService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@ -28,6 +30,7 @@ import java.util.List;
|
||||
public class SysPostServiceImpl implements SysPostService {
|
||||
|
||||
private final SysPostMapper sysPostMapper;
|
||||
private final SysUserPostService sysUserPostService;
|
||||
|
||||
@Override
|
||||
public TableDataInfo<SysPost> selectPageList(SysPostParam param, PageQuery pageQuery) {
|
||||
@ -64,11 +67,13 @@ public class SysPostServiceImpl implements SysPostService {
|
||||
|
||||
@Override
|
||||
public int deleteById(String deptId) {
|
||||
checkAllowDelete(CollUtil.toList(deptId));
|
||||
return sysPostMapper.deleteById(deptId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int deleteByIds(List<String> ids) {
|
||||
checkAllowDelete(ids);
|
||||
return sysPostMapper.deleteBatchIds(ids);
|
||||
}
|
||||
|
||||
@ -88,8 +93,8 @@ public class SysPostServiceImpl implements SysPostService {
|
||||
* @param post 岗位信息
|
||||
*/
|
||||
private void checkPostNameUnique(SysPost post) {
|
||||
if (sysPostMapper.checkPostNameUnique(post) > 0) {
|
||||
throw new ServiceException(StrUtil.format("处理岗位'{}'失败,岗位名称已存在", post.getPostName()));
|
||||
if (StrUtil.isNotBlank(sysPostMapper.checkPostNameUnique(post))) {
|
||||
throw new ServiceException(StrUtil.format("处理岗位{}失败, 岗位名称已存在", post.getPostName()));
|
||||
}
|
||||
}
|
||||
|
||||
@ -99,8 +104,20 @@ public class SysPostServiceImpl implements SysPostService {
|
||||
* @param post 岗位信息
|
||||
*/
|
||||
private void checkPostCodeUnique(SysPost post) {
|
||||
if (sysPostMapper.checkPostCodeUnique(post) > 0) {
|
||||
throw new ServiceException(StrUtil.format("处理岗位'{}'失败,岗位编码已存在", post.getPostName()));
|
||||
if (StrUtil.isNotBlank(sysPostMapper.checkPostCodeUnique(post))) {
|
||||
throw new ServiceException(StrUtil.format("处理岗位{}失败, 岗位编码已存在", post.getPostName()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查岗位是否允许删除
|
||||
*
|
||||
* @param ids ids
|
||||
*/
|
||||
private void checkAllowDelete(List<String> ids) {
|
||||
List<String> names = sysUserPostService.selectUsedPostNameByIds(ids);
|
||||
if (CollUtil.isNotEmpty(names)) {
|
||||
throw new ServiceException(StrUtil.format("删除岗位{}失败, 岗位已绑定用户", names.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ import com.qiaoba.api.system.entity.param.SysRoleParam;
|
||||
import com.qiaoba.common.base.exceptions.ServiceException;
|
||||
import com.qiaoba.common.database.entity.PageQuery;
|
||||
import com.qiaoba.common.database.entity.TableDataInfo;
|
||||
import com.qiaoba.common.security.utils.SecurityUtil;
|
||||
import com.qiaoba.auth.utils.SecurityUtil;
|
||||
import com.qiaoba.module.system.mapper.SysRoleMapper;
|
||||
import com.qiaoba.module.system.service.SysRoleDeptService;
|
||||
import com.qiaoba.module.system.service.SysRoleMenuService;
|
||||
@ -105,6 +105,7 @@ public class SysRoleServiceImpl implements SysRoleService {
|
||||
return row;
|
||||
}
|
||||
|
||||
|
||||
private QueryWrapper<SysRole> param2Wrapper(SysRoleParam param) {
|
||||
QueryWrapper<SysRole> wrapper = new QueryWrapper<>();
|
||||
wrapper.lambda()
|
||||
|
@ -0,0 +1,48 @@
|
||||
package com.qiaoba.module.system.service.impl;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.qiaoba.api.system.entity.SysUser;
|
||||
import com.qiaoba.auth.entity.LoginUser;
|
||||
import com.qiaoba.common.base.enums.BaseEnum;
|
||||
import com.qiaoba.module.system.service.SysMenuService;
|
||||
import com.qiaoba.module.system.service.SysUserRoleService;
|
||||
import com.qiaoba.module.system.service.SysUserService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 用户权限验证处理
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2022-09-22 04:20:28
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class SysUserDetailsServiceImpl implements UserDetailsService {
|
||||
|
||||
private final SysUserService sysUserService;
|
||||
private final SysMenuService sysMenuService;
|
||||
private final SysUserRoleService sysUserRoleService;
|
||||
|
||||
@Override
|
||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||
SysUser user = sysUserService.selectByUsername(username);
|
||||
StrUtil.format("{}",1);
|
||||
return createUserDetails(user);
|
||||
}
|
||||
|
||||
|
||||
private UserDetails createUserDetails(SysUser user) {
|
||||
Set<String> perms = sysMenuService.selectPermsByUserId(user.getUserId());
|
||||
List<String> roleIds = sysUserRoleService.selectRoleIdsByUserId(user.getUserId(), BaseEnum.NORMAL.getCode());
|
||||
return new LoginUser(user.getUserId(), user.getDeptId(), user.getUsername(), user.getNickname(), roleIds, perms);
|
||||
}
|
||||
|
||||
}
|
@ -38,6 +38,11 @@ public class SysUserPostServiceImpl implements SysUserPostService {
|
||||
return sysUserPostMapper.selectPostIdsByUserId(userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> selectUsedPostNameByIds(List<String> postIds) {
|
||||
return sysUserPostMapper.selectUsedPostNameByIds(postIds);
|
||||
}
|
||||
|
||||
private QueryWrapper<SysUserPost> createWrapper(String userId) {
|
||||
QueryWrapper<SysUserPost> wrapper = new QueryWrapper<>();
|
||||
wrapper.lambda()
|
||||
|
@ -40,8 +40,8 @@ public class SysUserRoleServiceImpl implements SysUserRoleService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> selectRoleIdsByUserId(String userId) {
|
||||
return sysUserRoleMapper.selectRoleIdsByUserId(userId);
|
||||
public List<String> selectRoleIdsByUserId(String userId, String status) {
|
||||
return sysUserRoleMapper.selectRoleIdsByUserId(userId, status);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -17,7 +17,9 @@ import com.qiaoba.common.base.enums.BaseEnum;
|
||||
import com.qiaoba.common.base.exceptions.ServiceException;
|
||||
import com.qiaoba.common.database.entity.PageQuery;
|
||||
import com.qiaoba.common.database.entity.TableDataInfo;
|
||||
import com.qiaoba.common.security.utils.SecurityUtil;
|
||||
import com.qiaoba.common.redis.constants.CacheConstant;
|
||||
import com.qiaoba.common.redis.service.RedisService;
|
||||
import com.qiaoba.auth.utils.SecurityUtil;
|
||||
import com.qiaoba.module.system.mapper.SysUserMapper;
|
||||
import com.qiaoba.module.system.service.SysUserPostService;
|
||||
import com.qiaoba.module.system.service.SysUserRoleService;
|
||||
@ -41,6 +43,7 @@ public class SysUserServiceImpl implements SysUserService {
|
||||
private final SysUserMapper sysUserMapper;
|
||||
private final SysUserPostService sysUserPostService;
|
||||
private final SysUserRoleService sysUserRoleService;
|
||||
private final RedisService redisService;
|
||||
|
||||
@Override
|
||||
public int saveOrUpdate(SysUserDto dto, Boolean isUpdate) {
|
||||
@ -71,6 +74,14 @@ public class SysUserServiceImpl implements SysUserService {
|
||||
return sysUser;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SysUser selectByUsername(String username) {
|
||||
QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
|
||||
wrapper.lambda()
|
||||
.eq(SysUser::getUsername, username);
|
||||
return sysUserMapper.selectOne(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int resetPwd(ResetPwdDto dto) {
|
||||
// todo 加密密码
|
||||
@ -157,7 +168,6 @@ public class SysUserServiceImpl implements SysUserService {
|
||||
|
||||
private SysUser dtoToSysUser(SysUserDto dto, Boolean isUpdate) {
|
||||
if (StrUtil.isNotBlank(dto.getPassword())) {
|
||||
// todo 加密密码
|
||||
dto.setPassword("123456");
|
||||
}
|
||||
SysUser sysUser = BeanUtil.copyProperties(dto, SysUser.class);
|
||||
|
@ -19,4 +19,12 @@
|
||||
order by d.parent_id, d.order_num
|
||||
</select>
|
||||
|
||||
<select id="existChild" resultType="string">
|
||||
SELECT dept_id FROM sys_dept where parent_id = #{deptId} LIMIT 1
|
||||
</select>
|
||||
|
||||
<select id="existUsed" resultType="string">
|
||||
SELECT nickname from sys_user where dept_id = #{deptId} and is_delete = '0' limit 1
|
||||
</select>
|
||||
|
||||
</mapper>
|
||||
|
@ -24,11 +24,25 @@
|
||||
<select id="selectByUserId" resultType="com.qiaoba.api.system.entity.vo.SysMenuVo">
|
||||
select distinct t1.menu_id, t1.parent_id, t1.menu_name, t1.path, t1.component, t1.`query`, t1.is_visible, t1.perms, t1.is_frame, t1.is_cache, t1.menu_type, t1.icon, t1.order_num, t1.create_time
|
||||
from sys_menu t1
|
||||
left join sys_role_menu t2 on t1.menu_id = t2.menu_id
|
||||
left join sys_user_role t3 on t2.role_id = t3.role_id
|
||||
left join sys_role t4 on t3.role_id = t4.role_id
|
||||
left join sys_user t5 on t3.user_id = t5.user_id
|
||||
where t5.user_id = #{userId} and t1.menu_type in ('M', 'C') AND t4.status = '1'
|
||||
LEFT JOIN sys_role_menu t2
|
||||
on t2.menu_id = t1.menu_id
|
||||
LEFT JOIN sys_role t3
|
||||
on t3.role_id = t2.role_id
|
||||
LEFT JOIN sys_user_role t4
|
||||
on t4.role_id = t3.role_id
|
||||
where t4.user_id = #{userId} and t1.menu_type in ('M', 'C') AND t3.status = '1'
|
||||
order by t1.parent_id, t1.order_num
|
||||
</select>
|
||||
|
||||
<select id="selectPermsByUserId" resultType="string">
|
||||
SELECT t1.perms
|
||||
from sys_menu t1
|
||||
LEFT JOIN sys_role_menu t2
|
||||
on t2.menu_id = t1.menu_id
|
||||
LEFT JOIN sys_role t3
|
||||
on t3.role_id = t2.role_id
|
||||
LEFT JOIN sys_user_role t4
|
||||
on t4.role_id = t3.role_id
|
||||
where t4.user_id = #{userId} AND t3.status = '1'
|
||||
</select>
|
||||
</mapper>
|
||||
|
@ -4,8 +4,8 @@
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.qiaoba.module.system.mapper.SysPostMapper">
|
||||
|
||||
<select id="checkPostNameUnique" resultType="int">
|
||||
select count(post_id) from sys_post
|
||||
<select id="checkPostNameUnique" resultType="string">
|
||||
select post_id from sys_post
|
||||
where post_name = #{postName}
|
||||
<if test="postId != null and postId != ''">
|
||||
and post_id != #{postId}
|
||||
@ -13,8 +13,8 @@
|
||||
limit 1
|
||||
</select>
|
||||
|
||||
<select id="checkPostCodeUnique" resultType="int">
|
||||
select count(post_id) from sys_post
|
||||
<select id="checkPostCodeUnique" resultType="string">
|
||||
select post_id from sys_post
|
||||
where post_code = #{postCode}
|
||||
<if test="postId != null and postId != ''">
|
||||
and post_id != #{postId}
|
||||
|
@ -7,4 +7,16 @@
|
||||
<select id="selectPostIdsByUserId" resultType="string">
|
||||
select post_id from sys_user_post where user_id = #{userId}
|
||||
</select>
|
||||
|
||||
<select id="selectUsedPostNameByIds" resultType="string">
|
||||
select DISTINCT t1.post_name
|
||||
from sys_post t1
|
||||
LEFT JOIN sys_user_post t2
|
||||
on t1.post_id = t2.post_id
|
||||
where t2.post_id in
|
||||
<foreach collection="list" item="postId" open="(" separator="," close=")">
|
||||
#{postId}
|
||||
</foreach>
|
||||
</select>
|
||||
|
||||
</mapper>
|
||||
|
@ -5,7 +5,12 @@
|
||||
<mapper namespace="com.qiaoba.module.system.mapper.SysUserRoleMapper">
|
||||
|
||||
<select id="selectRoleIdsByUserId" resultType="string">
|
||||
select role_id from sys_user_role where user_id = #{userId}
|
||||
select t1.role_id from sys_user_role t1
|
||||
left join sys_role t2 on t2.role_id = t1.role_id
|
||||
where t1.user_id = #{userId}
|
||||
<if test="status != null and status != ''">
|
||||
and t2.status = #{status}
|
||||
</if>
|
||||
</select>
|
||||
|
||||
<delete id="deleteByRoleIdAndUserIds">
|
||||
|
Reference in New Issue
Block a user