feature: 开发 Hippo 用户权限管理模块.

pull/161/head
chen.ma 3 years ago
parent 2a8f7537b9
commit 895a57a0c0

33
auth/.gitignore vendored

@ -0,0 +1,33 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/

@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.github.dynamic-threadpool</groupId>
<artifactId>parent</artifactId>
<version>${revision}</version>
</parent>
<artifactId>auth</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
</dependencies>
</project>

@ -0,0 +1,15 @@
package com.github.dynamic.threadpool.auth.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.github.dynamic.threadpool.auth.model.PermissionInfo;
import org.apache.ibatis.annotations.Mapper;
/**
* Permission mapper.
*
* @author chen.ma
* @date 2021/10/30 22:34
*/
@Mapper
public interface PermissionMapper extends BaseMapper<PermissionInfo> {
}

@ -0,0 +1,15 @@
package com.github.dynamic.threadpool.auth.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.github.dynamic.threadpool.auth.model.RoleInfo;
import org.apache.ibatis.annotations.Mapper;
/**
* Role mapper.
*
* @author chen.ma
* @date 2021/10/30 22:55
*/
@Mapper
public interface RoleMapper extends BaseMapper<RoleInfo> {
}

@ -0,0 +1,15 @@
package com.github.dynamic.threadpool.auth.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.github.dynamic.threadpool.auth.model.UserInfo;
import org.apache.ibatis.annotations.Mapper;
/**
* User mapper.
*
* @author chen.ma
* @date 2021/10/30 21:42
*/
@Mapper
public interface UserMapper extends BaseMapper<UserInfo> {
}

@ -0,0 +1,58 @@
package com.github.dynamic.threadpool.auth.model;
import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import java.util.Date;
/**
* Permission info.
*
* @author chen.ma
* @date 2021/10/30 22:33
*/
@Data
@TableName("permission")
public class PermissionInfo {
/**
* id
*/
@TableId
private Long id;
/**
* role
*/
private String role;
/**
* resource
*/
private String resource;
/**
* action
*/
private String action;
/**
* gmtCreate
*/
@TableField(fill = FieldFill.INSERT)
private Date gmtCreate;
/**
* gmtModified
*/
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date gmtModified;
/**
* delFlag
*/
@TableLogic
@TableField(fill = FieldFill.INSERT)
private Integer delFlag;
}

@ -0,0 +1,53 @@
package com.github.dynamic.threadpool.auth.model;
import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import java.util.Date;
/**
* Role info.
*
* @author chen.ma
* @date 2021/10/30 22:54
*/
@Data
@TableName("role")
public class RoleInfo {
/**
* id
*/
@TableId
private Long id;
/**
* role
*/
private String role;
/**
* userName
*/
private String userName;
/**
* gmtCreate
*/
@TableField(fill = FieldFill.INSERT)
private Date gmtCreate;
/**
* gmtModified
*/
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date gmtModified;
/**
* delFlag
*/
@TableLogic
@TableField(fill = FieldFill.INSERT)
private Integer delFlag;
}

@ -0,0 +1,53 @@
package com.github.dynamic.threadpool.auth.model;
import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import java.util.Date;
/**
* User info.
*
* @author chen.ma
* @date 2021/10/30 21:37
*/
@Data
@TableName("user")
public class UserInfo {
/**
* id
*/
@TableId
private Long id;
/**
* userName
*/
private String userName;
/**
* password
*/
private String password;
/**
* gmtCreate
*/
@TableField(fill = FieldFill.INSERT)
private Date gmtCreate;
/**
* gmtModified
*/
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date gmtModified;
/**
* delFlag
*/
@TableLogic
@TableField(fill = FieldFill.INSERT)
private Integer delFlag;
}

@ -0,0 +1,19 @@
package com.github.dynamic.threadpool.auth.model.biz.permission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;
/**
* Permission query page.
*
* @author chen.ma
* @date 2021/10/30 21:47
*/
@Data
public class PermissionQueryPageReqDTO extends Page {
public PermissionQueryPageReqDTO(long current, long size) {
super(current, size);
}
}

@ -0,0 +1,29 @@
package com.github.dynamic.threadpool.auth.model.biz.permission;
import lombok.Data;
/**
* Permission resp dto.
*
* @author chen.ma
* @date 2021/10/30 22:21
*/
@Data
public class PermissionRespDTO {
/**
* role
*/
private String role;
/**
* source
*/
private String resource;
/**
* action
*/
private String action;
}

@ -0,0 +1,19 @@
package com.github.dynamic.threadpool.auth.model.biz.role;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;
/**
* Role query page.
*
* @author chen.ma
* @date 2021/10/30 21:47
*/
@Data
public class RoleQueryPageReqDTO extends Page {
public RoleQueryPageReqDTO(long current, long size) {
super(current, size);
}
}

@ -0,0 +1,24 @@
package com.github.dynamic.threadpool.auth.model.biz.role;
import lombok.Data;
/**
* Role resp dto.
*
* @author chen.ma
* @date 2021/10/30 22:49
*/
@Data
public class RoleRespDTO {
/**
* role
*/
private String role;
/**
* userName
*/
private String userName;
}

@ -0,0 +1,19 @@
package com.github.dynamic.threadpool.auth.model.biz.user;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;
/**
* User query page.
*
* @author chen.ma
* @date 2021/10/30 21:47
*/
@Data
public class UserQueryPageReqDTO extends Page {
public UserQueryPageReqDTO(long current, long size) {
super(current, size);
}
}

@ -0,0 +1,39 @@
package com.github.dynamic.threadpool.auth.model.biz.user;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import java.util.Date;
/**
* User resp dto.
*
* @author chen.ma
* @date 2021/10/30 21:51
*/
@Data
public class UserRespDTO {
/**
* userName
*/
private String userName;
/**
* password
*/
private String password;
/**
* gmtCreate
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date gmtCreate;
/**
* gmtModified
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date gmtModified;
}

@ -0,0 +1,41 @@
package com.github.dynamic.threadpool.auth.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.dynamic.threadpool.auth.model.biz.permission.PermissionRespDTO;
/**
* Permission service.
*
* @author chen.ma
* @date 2021/10/30 22:13
*/
public interface PermissionService {
/**
* .
*
* @param pageNo
* @param pageSize
* @return
*/
IPage<PermissionRespDTO> listPermission(int pageNo, int pageSize);
/**
* .
*
* @param role
* @param resource
* @param action
*/
void addPermission(String role, String resource, String action);
/**
* .
*
* @param role
* @param resource
* @param action
*/
void deletePermission(String role, String resource, String action);
}

@ -0,0 +1,49 @@
package com.github.dynamic.threadpool.auth.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.dynamic.threadpool.auth.model.biz.role.RoleRespDTO;
import java.util.List;
/**
* Role service.
*
* @author chen.ma
* @date 2021/10/30 22:45
*/
public interface RoleService {
/**
* .
*
* @param pageNo
* @param pageSize
* @return
*/
IPage<RoleRespDTO> listRole(int pageNo, int pageSize);
/**
* .
*
* @param role
* @param userName
*/
void addRole(String role, String userName);
/**
* .
*
* @param role
* @param userName
*/
void deleteRole(String role, String userName);
/**
* .
*
* @param role
* @return
*/
List<String> getRoleLike(String role);
}

@ -0,0 +1,56 @@
package com.github.dynamic.threadpool.auth.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.dynamic.threadpool.auth.model.biz.user.UserRespDTO;
import java.util.List;
/**
* User service.
*
* @author chen.ma
* @date 2021/10/30 21:34
*/
public interface UserService {
/**
* .
*
* @param pageNo
* @param pageSize
* @return
*/
IPage<UserRespDTO> listUser(int pageNo, int pageSize);
/**
* .
*
* @param userName
* @param password
*/
void addUser(String userName, String password);
/**
* .
*
* @param userName
* @param password
*/
void updateUser(String userName, String password);
/**
* .
*
* @param userName
*/
void deleteUser(String userName);
/**
* .
*
* @param userName
* @return
*/
List<String> getUserLikeUsername(String userName);
}

@ -0,0 +1,64 @@
package com.github.dynamic.threadpool.auth.service.impl;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.dynamic.threadpool.auth.mapper.PermissionMapper;
import com.github.dynamic.threadpool.auth.model.PermissionInfo;
import com.github.dynamic.threadpool.auth.model.biz.permission.PermissionQueryPageReqDTO;
import com.github.dynamic.threadpool.auth.model.biz.permission.PermissionRespDTO;
import com.github.dynamic.threadpool.auth.service.PermissionService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
/**
* Permission service impl.
*
* @author chen.ma
* @date 2021/10/30 22:32
*/
@Service
@AllArgsConstructor
public class PermissionServiceImpl implements PermissionService {
private final PermissionMapper permissionMapper;
@Override
public IPage<PermissionRespDTO> listPermission(int pageNo, int pageSize) {
PermissionQueryPageReqDTO queryPage = new PermissionQueryPageReqDTO(pageNo, pageSize);
IPage<PermissionInfo> selectPage = permissionMapper.selectPage(queryPage, null);
return selectPage.convert(each -> BeanUtil.toBean(each, PermissionRespDTO.class));
}
@Override
public void addPermission(String role, String resource, String action) {
LambdaQueryWrapper<PermissionInfo> queryWrapper = Wrappers.lambdaQuery(PermissionInfo.class)
.eq(PermissionInfo::getRole, role)
.eq(PermissionInfo::getResource, resource)
.eq(PermissionInfo::getAction, action);
PermissionInfo existPermissionInfo = permissionMapper.selectOne(queryWrapper);
if (existPermissionInfo != null) {
throw new RuntimeException("权限重复");
}
PermissionInfo insertPermission = new PermissionInfo();
insertPermission.setRole(role);
insertPermission.setResource(resource);
insertPermission.setAction(action);
permissionMapper.insert(insertPermission);
}
@Override
public void deletePermission(String role, String resource, String action) {
LambdaUpdateWrapper<PermissionInfo> updateWrapper = Wrappers.lambdaUpdate(PermissionInfo.class)
.eq(PermissionInfo::getRole, role)
.eq(PermissionInfo::getResource, resource)
.eq(PermissionInfo::getAction, action);
permissionMapper.delete(updateWrapper);
}
}

@ -0,0 +1,74 @@
package com.github.dynamic.threadpool.auth.service.impl;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.dynamic.threadpool.auth.mapper.RoleMapper;
import com.github.dynamic.threadpool.auth.model.RoleInfo;
import com.github.dynamic.threadpool.auth.model.biz.role.RoleQueryPageReqDTO;
import com.github.dynamic.threadpool.auth.model.biz.role.RoleRespDTO;
import com.github.dynamic.threadpool.auth.service.RoleService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
/**
* Role service impl.
*
* @author chen.ma
* @date 2021/10/30 22:53
*/
@Service
@AllArgsConstructor
public class RoleServiceImpl implements RoleService {
private final RoleMapper roleMapper;
@Override
public IPage<RoleRespDTO> listRole(int pageNo, int pageSize) {
RoleQueryPageReqDTO queryPage = new RoleQueryPageReqDTO(pageNo, pageSize);
IPage<RoleInfo> selectPage = roleMapper.selectPage(queryPage, null);
return selectPage.convert(each -> BeanUtil.toBean(each, RoleRespDTO.class));
}
@Override
public void addRole(String role, String userName) {
LambdaQueryWrapper<RoleInfo> queryWrapper = Wrappers.lambdaQuery(RoleInfo.class)
.eq(RoleInfo::getRole, role);
RoleInfo roleInfo = roleMapper.selectOne(queryWrapper);
if (roleInfo != null) {
throw new RuntimeException("角色名重复");
}
RoleInfo insertRole = new RoleInfo();
insertRole.setRole(role);
insertRole.setUserName(userName);
roleMapper.insert(insertRole);
}
@Override
public void deleteRole(String role, String userName) {
LambdaUpdateWrapper<RoleInfo> updateWrapper = Wrappers.lambdaUpdate(RoleInfo.class)
.eq(RoleInfo::getRole, role)
.eq(RoleInfo::getUserName, userName);
roleMapper.delete(updateWrapper);
}
@Override
public List<String> getRoleLike(String role) {
LambdaQueryWrapper<RoleInfo> queryWrapper = Wrappers.lambdaQuery(RoleInfo.class)
.like(RoleInfo::getRole, role)
.select(RoleInfo::getRole);
List<RoleInfo> roleInfos = roleMapper.selectList(queryWrapper);
List<String> roleNames = roleInfos.stream().map(RoleInfo::getRole).collect(Collectors.toList());
return roleNames;
}
}

@ -0,0 +1,86 @@
package com.github.dynamic.threadpool.auth.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.dynamic.threadpool.auth.mapper.UserMapper;
import com.github.dynamic.threadpool.auth.model.UserInfo;
import com.github.dynamic.threadpool.auth.model.biz.user.UserQueryPageReqDTO;
import com.github.dynamic.threadpool.auth.model.biz.user.UserRespDTO;
import com.github.dynamic.threadpool.auth.service.UserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
/**
* User service impl.
*
* @author chen.ma
* @date 2021/10/30 21:40
*/
@Service
@AllArgsConstructor
public class UserServiceImpl implements UserService {
private final UserMapper userMapper;
@Override
public IPage<UserRespDTO> listUser(int pageNo, int pageSize) {
UserQueryPageReqDTO queryPage = new UserQueryPageReqDTO(pageNo, pageSize);
IPage<UserInfo> selectPage = userMapper.selectPage(queryPage, null);
return selectPage.convert(each -> BeanUtil.toBean(each, UserRespDTO.class));
}
@Override
public void addUser(String userName, String password) {
LambdaQueryWrapper<UserInfo> queryWrapper = Wrappers.lambdaQuery(UserInfo.class)
.eq(UserInfo::getUserName, userName);
UserInfo existUserInfo = userMapper.selectOne(queryWrapper);
if (existUserInfo != null) {
throw new RuntimeException("用户名重复");
}
UserInfo insertUser = new UserInfo();
insertUser.setUserName(userName);
// TODO 暂定为 Md5 加密
insertUser.setPassword(SecureUtil.md5(password));
userMapper.insert(insertUser);
}
@Override
public void updateUser(String userName, String password) {
UserInfo userInfo = new UserInfo();
userInfo.setUserName(userName);
userInfo.setPassword(SecureUtil.md5(password));
LambdaUpdateWrapper<UserInfo> updateWrapper = Wrappers.lambdaUpdate(UserInfo.class)
.eq(UserInfo::getUserName, userName);
userMapper.update(userInfo, updateWrapper);
}
@Override
public void deleteUser(String userName) {
LambdaUpdateWrapper<UserInfo> updateWrapper = Wrappers.lambdaUpdate(UserInfo.class)
.eq(UserInfo::getUserName, userName);
userMapper.delete(updateWrapper);
}
@Override
public List<String> getUserLikeUsername(String userName) {
LambdaQueryWrapper<UserInfo> queryWrapper = Wrappers.lambdaQuery(UserInfo.class)
.like(UserInfo::getUserName, userName)
.select(UserInfo::getUserName);
List<UserInfo> userInfos = userMapper.selectList(queryWrapper);
List<String> userNames = userInfos.stream().map(UserInfo::getUserName).collect(Collectors.toList());
return userNames;
}
}
Loading…
Cancel
Save