角色/权限管理

This commit is contained in:
x ronger 2019-12-05 17:29:39 +08:00
parent ba1840a752
commit 3284137e68
16 changed files with 214 additions and 34 deletions

View File

@ -4,6 +4,9 @@ import lombok.Data;
import java.util.Date;
/**
* @author ronger
*/
@Data
public class ArticleDTO {
private Integer idArticle;

View File

@ -2,8 +2,11 @@ package com.rymcu.vertical.dto;
import lombok.Data;
/**
* @author ronger
*/
@Data
public class TUser {
public class TokenUser {
private String account;
@ -15,4 +18,6 @@ public class TUser {
private String avatarUrl;
private Integer weights;
}

View File

@ -0,0 +1,13 @@
package com.rymcu.vertical.dto.admin;
import lombok.Data;
/**
* @author ronger
*/
@Data
public class UserRoleDTO {
private Integer idUser;
private Integer idRole;
}

View File

@ -1,5 +1,6 @@
package com.rymcu.vertical.entity;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;
import javax.persistence.Column;
@ -9,6 +10,9 @@ import javax.persistence.Table;
import java.io.Serializable;
import java.util.Date;
/**
* @author ronger
*/
@Data
@Table(name = "vertical_role")
public class Role implements Serializable,Cloneable {
@ -29,6 +33,12 @@ public class Role implements Serializable,Cloneable {
@Column(name = "input_code")
private String inputCode;
/**
* 权重
* */
@Column(name = "weights")
private Integer weights;
/**
* 状态
* */
@ -39,11 +49,13 @@ public class Role implements Serializable,Cloneable {
* 创建时间
* */
@Column(name = "created_time")
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private Date createdTime;
/**
* 更新时间
* */
@Column(name = "updated_time")
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private Date updatedTime;
}

View File

@ -12,4 +12,6 @@ public interface RoleMapper extends Mapper<Role> {
List<Role> selectRoleByIdUser(@Param("id") Integer id);
Role selectRoleByInputCode(@Param("inputCode") String inputCode);
Integer updateStatus(@Param("idRole") Integer idRole, @Param("status") String status);
}

View File

@ -17,4 +17,10 @@ public interface UserMapper extends Mapper<User> {
UserDTO selectUserDTOByNickname(@Param("nickname") String nickname);
Integer updatePasswordByAccount(@Param("account") String account, @Param("password") String password);
Integer selectRoleWeightsByUser(@Param("idUser") Integer idUser);
Integer updateUserRole(@Param("idUser") Integer idUser, @Param("idRole") Integer idRole);
Integer updateStatus(@Param("idUser") Integer idUser, @Param("status") String status);
}

View File

@ -5,6 +5,7 @@ import com.rymcu.vertical.entity.Role;
import com.rymcu.vertical.entity.User;
import java.util.List;
import java.util.Map;
/**
@ -13,4 +14,10 @@ import java.util.List;
public interface RoleService extends Service<Role> {
List<Role> selectRoleByUser(User sysUser);
List<Role> findByIdUser(Integer idUser);
Map updateStatus(Integer idRole, String status);
Map saveRole(Role role);
}

View File

@ -13,7 +13,6 @@ import java.util.Map;
*/
public interface UserService extends Service<User> {
User findByAccount(String account) throws TooManyResultsException;
Map register(String email, String password, String code);
@ -23,4 +22,8 @@ public interface UserService extends Service<User> {
UserDTO findUserDTOByNickname(String nickname);
Map forgetPassword(String code, String password);
Map updateUserRole(Integer idUser, Integer idRole);
Map updateStatus(Integer idUser, String status);
}

View File

@ -9,14 +9,18 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by CodeGenerator on 2018/05/29.
*
* @author CodeGenerator
* @date 2018/05/29
*/
@Service
@Transactional
public class RoleServiceImpl extends AbstractService<Role> implements RoleService {
@Resource
private RoleMapper roleMapper;
@ -27,4 +31,41 @@ public class RoleServiceImpl extends AbstractService<Role> implements RoleServic
return roles;
}
@Override
public List<Role> findByIdUser(Integer idUser) {
return roleMapper.selectRoleByIdUser(idUser);
}
@Override
@Transactional
public Map updateStatus(Integer idRole, String status) {
Map map = new HashMap(1);
Integer result = roleMapper.updateStatus(idRole,status);
if(result == 0) {
map.put("message","更新失败!");
}
return map;
}
@Override
public Map saveRole(Role role) {
Integer result = 0;
if (role.getIdRole() == null) {
role.setStatus("0");
role.setCreatedTime(new Date());
role.setUpdatedTime(role.getCreatedTime());
result = roleMapper.insertSelective(role);
} else {
role.setCreatedTime(new Date());
result = roleMapper.updateByPrimaryKeySelective(role);
}
Map map = new HashMap(1);
if (result == 0) {
map.put("message","操作失败!");
} else {
map.put("role", role);
}
return map;
}
}

View File

@ -2,14 +2,13 @@ package com.rymcu.vertical.service.impl;
import com.rymcu.vertical.core.service.AbstractService;
import com.rymcu.vertical.core.service.redis.RedisService;
import com.rymcu.vertical.dto.TUser;
import com.rymcu.vertical.dto.TokenUser;
import com.rymcu.vertical.dto.UserDTO;
import com.rymcu.vertical.entity.Role;
import com.rymcu.vertical.entity.User;
import com.rymcu.vertical.jwt.service.TokenManager;
import com.rymcu.vertical.mapper.RoleMapper;
import com.rymcu.vertical.mapper.UserMapper;
import com.rymcu.vertical.service.ArticleService;
import com.rymcu.vertical.service.UserService;
import com.rymcu.vertical.util.BeanCopierUtil;
import com.rymcu.vertical.util.Utils;
@ -25,7 +24,9 @@ import java.util.Map;
/**
* Created by CodeGenerator on 2018/05/29.
*
* @author CodeGenerator
* @date 2018/05/29
*/
@Service
public class UserServiceImpl extends AbstractService<User> implements UserService {
@ -85,10 +86,11 @@ public class UserServiceImpl extends AbstractService<User> implements UserServic
if(Utils.comparePwd(password, user.getPassword())){
user.setLastLoginTime(new Date());
userMapper.updateByPrimaryKeySelective(user);
TUser tUser = new TUser();
BeanCopierUtil.copy(user,tUser);
tUser.setToken(tokenManager.createToken(account));
map.put("user",tUser);
TokenUser tokenUser = new TokenUser();
BeanCopierUtil.copy(user, tokenUser);
tokenUser.setToken(tokenManager.createToken(account));
tokenUser.setWeights(userMapper.selectRoleWeightsByUser(user.getIdUser()));
map.put("user", tokenUser);
} else {
map.put("message","密码错误!");
}
@ -118,4 +120,26 @@ public class UserServiceImpl extends AbstractService<User> implements UserServic
}
return map;
}
@Override
@Transactional
public Map updateUserRole(Integer idUser, Integer idRole) {
Map map = new HashMap(1);
Integer result = userMapper.updateUserRole(idUser,idRole);
if(result == 0) {
map.put("message","更新失败!");
}
return map;
}
@Override
@Transactional
public Map updateStatus(Integer idUser, String status) {
Map map = new HashMap(1);
Integer result = userMapper.updateStatus(idUser,status);
if(result == 0) {
map.put("message","更新失败!");
}
return map;
}
}

View File

@ -1,6 +1,6 @@
package com.rymcu.vertical.util;
import com.rymcu.vertical.dto.TUser;
import com.rymcu.vertical.dto.TokenUser;
import com.rymcu.vertical.entity.User;
import com.rymcu.vertical.jwt.def.JwtConstants;
import com.rymcu.vertical.jwt.model.TokenModel;
@ -13,6 +13,9 @@ import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureException;
import org.apache.commons.lang.StringUtils;
/**
* @author ronger
*/
public class UserUtils {
private static UserMapper userMapper = SpringContextHolder.getBean(UserMapper.class);
@ -46,7 +49,7 @@ public class UserUtils {
return null;
}
public static TUser getTUser(String token) {
public static TokenUser getTokenUser(String token) {
if(StringUtils.isNotBlank(token)){
// 验证token
Claims claims = null;
@ -61,10 +64,11 @@ public class UserUtils {
if (tokenManager.checkToken(model)) {
User user = userMapper.findByAccount(account.toString());
if(user != null){
TUser tUser = new TUser();
BeanCopierUtil.copy(user,tUser);
tUser.setToken(token);
return tUser;
TokenUser tokenUser = new TokenUser();
BeanCopierUtil.copy(user, tokenUser);
tokenUser.setToken(token);
tokenUser.setWeights(userMapper.selectRoleWeightsByUser(user.getIdUser()));
return tokenUser;
}
}
}

View File

@ -4,9 +4,11 @@ import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rymcu.vertical.core.result.GlobalResult;
import com.rymcu.vertical.core.result.GlobalResultGenerator;
import com.rymcu.vertical.dto.admin.TopicDTO;
import com.rymcu.vertical.dto.admin.UserRoleDTO;
import com.rymcu.vertical.entity.Role;
import com.rymcu.vertical.entity.Topic;
import com.rymcu.vertical.entity.User;
import com.rymcu.vertical.service.RoleService;
import com.rymcu.vertical.service.TopicService;
import com.rymcu.vertical.service.UserService;
import org.apache.commons.lang.StringUtils;
@ -27,6 +29,8 @@ public class AdminController {
@Resource
private UserService userService;
@Resource
private RoleService roleService;
@Resource
private TopicService topicService;
@GetMapping("/users")
@ -44,6 +48,57 @@ public class AdminController {
return GlobalResultGenerator.genSuccessResult(map);
}
@GetMapping("/user/{idUser}/role")
public GlobalResult userRole(@PathVariable Integer idUser){
List<Role> roles = roleService.findByIdUser(idUser);
return GlobalResultGenerator.genSuccessResult(roles);
}
@GetMapping("/roles")
public GlobalResult roles(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer rows){
PageHelper.startPage(page, rows);
List<Role> list = roleService.findAll();
PageInfo pageInfo = new PageInfo(list);
Map map = new HashMap(2);
map.put("roles", pageInfo.getList());
Map pagination = new HashMap(3);
pagination.put("pageSize",pageInfo.getPageSize());
pagination.put("total",pageInfo.getTotal());
pagination.put("currentPage",pageInfo.getPageNum());
map.put("pagination", pagination);
return GlobalResultGenerator.genSuccessResult(map);
}
@PatchMapping("/user/update-role")
public GlobalResult updateUserRole(@RequestBody UserRoleDTO userRole){
Map map = userService.updateUserRole(userRole.getIdUser(),userRole.getIdRole());
return GlobalResultGenerator.genSuccessResult(map);
}
@PatchMapping("/user/update-status")
public GlobalResult updateUserStatus(@RequestBody User user){
Map map = userService.updateStatus(user.getIdUser(),user.getStatus());
return GlobalResultGenerator.genSuccessResult(map);
}
@PatchMapping("/role/update-status")
public GlobalResult updateRoleStatus(@RequestBody Role role){
Map map = roleService.updateStatus(role.getIdRole(),role.getStatus());
return GlobalResultGenerator.genSuccessResult(map);
}
@PostMapping("/role/post")
public GlobalResult addRole(@RequestBody Role role){
Map map = roleService.saveRole(role);
return GlobalResultGenerator.genSuccessResult(map);
}
@PutMapping("/role/post")
public GlobalResult updateRole(@RequestBody Role role){
Map map = roleService.saveRole(role);
return GlobalResultGenerator.genSuccessResult(map);
}
@GetMapping("/topics")
public GlobalResult topics(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer rows){
PageHelper.startPage(page, rows);
@ -64,8 +119,8 @@ public class AdminController {
if (StringUtils.isBlank(topicUri)) {
return GlobalResultGenerator.genErrorResult("数据异常!");
}
TopicDTO topic = topicService.findTopicByTopicUri(topicUri,page,rows);
return GlobalResultGenerator.genSuccessResult();
Map map = topicService.findTopicByTopicUri(topicUri,page,rows);
return GlobalResultGenerator.genSuccessResult(map);
}
}

View File

@ -2,13 +2,12 @@ package com.rymcu.vertical.web.api.common;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rymcu.vertical.core.exception.ServiceException;
import com.rymcu.vertical.core.result.GlobalResult;
import com.rymcu.vertical.core.result.GlobalResultGenerator;
import com.rymcu.vertical.core.result.GlobalResultMessage;
import com.rymcu.vertical.dto.ArticleDTO;
import com.rymcu.vertical.dto.ForgetPasswordDTO;
import com.rymcu.vertical.dto.TUser;
import com.rymcu.vertical.dto.TokenUser;
import com.rymcu.vertical.entity.User;
import com.rymcu.vertical.service.ArticleService;
import com.rymcu.vertical.service.JavaMailService;
@ -19,7 +18,6 @@ import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -121,8 +119,8 @@ public class CommonApiController {
@GetMapping("/token/{token}")
public GlobalResult token(@PathVariable String token){
TUser tUser = UserUtils.getTUser(token);
return GlobalResultGenerator.genSuccessResult(tUser);
TokenUser tokenUser = UserUtils.getTokenUser(token);
return GlobalResultGenerator.genSuccessResult(tokenUser);
}
@PatchMapping("/forget-password")

View File

@ -2,7 +2,7 @@ package com.rymcu.vertical.web.api.common;
import com.rymcu.vertical.core.result.GlobalResult;
import com.rymcu.vertical.core.result.GlobalResultGenerator;
import com.rymcu.vertical.dto.TUser;
import com.rymcu.vertical.dto.TokenUser;
import com.rymcu.vertical.jwt.def.JwtConstants;
import com.rymcu.vertical.util.FileUtils;
import com.rymcu.vertical.util.UserUtils;
@ -128,9 +128,9 @@ public class UploadController {
if(StringUtils.isBlank(authHeader)){
throw new BaseApiException(ErrorCode.UNAUTHORIZED);
}
TUser tUser = UserUtils.getTUser(authHeader);
TokenUser tokenUser = UserUtils.getTokenUser(authHeader);
Map map = new HashMap(2);
map.put("uploadToken",tUser.getToken());
map.put("uploadToken", tokenUser.getToken());
map.put("uploadURL", UPLOAD_URL);
return GlobalResultGenerator.genSuccessResult(map);
}

View File

@ -8,16 +8,14 @@
<id column="id" jdbcType="INTEGER" property="idRole"/>
<result column="name" jdbcType="VARCHAR" property="name"/>
<result column="input_code" jdbcType="VARCHAR" property="inputCode"/>
<result column="weights" jdbcType="TINYINT" property="weights"/>
<result column="created_time" jdbcType="TIMESTAMP" property="createdTime"/>
<result column="updated_time" jdbcType="TIMESTAMP" property="updatedTime"/>
<result column="status" jdbcType="INTEGER" property="status"/>
</resultMap>
<resultMap id="RoleDTOResultMap" type="com.rymcu.vertical.dto.RoleDTO">
<id column="id" property="id"></id>
<result column="name" property="name"/>
<result column="input_code" property="inputCode"/>
<result column="menu_ids" property="menuIds"/>
</resultMap>
<update id="updateStatus">
update vertical_role set status = #{status},updated_time = sysdate() where id = #{idRole}
</update>
<select id="selectRoleByIdUser" resultMap="BaseResultMap">
select sr.* from vertical_user_role sur left join vertical_role sr on sur.id_role = sr.id where id_user = #{id}

View File

@ -44,6 +44,12 @@
<update id="updatePasswordByAccount">
update vertical_user set password = #{password} where account = #{account}
</update>
<update id="updateUserRole">
update vertical_user_role set id_role = #{idRole},created_time = sysdate() where id_user = #{idUser}
</update>
<update id="updateStatus">
update vertical_user set status = #{status} where id = #{idUser}
</update>
<select id="findByAccount" resultMap="BaseResultMap">
select id, nickname, account, password, status from vertical_user where account = #{account} AND status = 0
@ -54,5 +60,8 @@
<select id="selectUserDTOByNickname" resultMap="DTOResultMapper">
select id, nickname, avatar_type, avatar_url, account from vertical_user where nickname = #{nickname} and status = 0
</select>
<select id="selectRoleWeightsByUser" resultType="java.lang.Integer">
select vr.weights from vertical_role vr left join vertical_user_role vur on vr.id = vur.id_role where vur.id_user = #{idUser}
</select>
</mapper>