添加权限管理功能

This commit is contained in:
zhh
2018-09-30 18:00:01 +08:00
parent 207a72d116
commit 7faffe541a
39 changed files with 5405 additions and 61 deletions

View File

@@ -52,6 +52,6 @@ public class AdminUserDetails implements UserDetails {
@Override
public boolean isEnabled() {
return true;
return umsAdmin.getStatus().equals(1);
}
}

View File

@@ -7,8 +7,6 @@ import com.macro.mall.model.PmsProductCategory;
import com.macro.mall.service.PmsProductCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
@@ -75,7 +73,7 @@ public class PmsProductCategoryController {
}
@ApiOperation("删除商品分类")
@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
@ResponseBody
public Object delete(@PathVariable Long id) {
int count = productCategoryService.delete(id);

View File

@@ -9,18 +9,15 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
@@ -59,9 +56,9 @@ public class UmsAdminController {
if (token == null) {
return new CommonResult().validateFailed("用户名或密码错误");
}
Map<String,String> tokenMap = new HashMap<>();
tokenMap.put("token",token);
tokenMap.put("tokenHead",tokenHead);
Map<String, String> tokenMap = new HashMap<>();
tokenMap.put("token", token);
tokenMap.put("tokenHead", tokenHead);
return new CommonResult().success(tokenMap);
}
@@ -74,28 +71,69 @@ public class UmsAdminController {
if (refreshToken == null) {
return new CommonResult().failed();
}
Map<String,String> tokenMap = new HashMap<>();
tokenMap.put("token",token);
tokenMap.put("tokenHead",tokenHead);
Map<String, String> tokenMap = new HashMap<>();
tokenMap.put("token", token);
tokenMap.put("tokenHead", tokenHead);
return new CommonResult().success(tokenMap);
}
@ApiOperation(value = "获取用户信息")
@RequestMapping(value = "/info",method = RequestMethod.GET)
@ApiOperation(value = "获取当前登录用户信息")
@RequestMapping(value = "/info", method = RequestMethod.GET)
@ResponseBody
public Object getAdminInfo(Principal principal){
public Object getAdminInfo(Principal principal) {
String username = principal.getName();
UmsAdmin umsAdmin = adminService.getAdminByUsername(username);
Map<String,Object> data = new HashMap<>();
data.put("username",umsAdmin.getUsername());
data.put("roles",new String[]{"TEST"});
data.put("icon",umsAdmin.getIcon());
Map<String, Object> data = new HashMap<>();
data.put("username", umsAdmin.getUsername());
data.put("roles", new String[]{"TEST"});
data.put("icon", umsAdmin.getIcon());
return new CommonResult().success(data);
}
@ApiOperation(value = "登出功能")
@RequestMapping(value = "/logout",method = RequestMethod.POST)
@RequestMapping(value = "/logout", method = RequestMethod.POST)
@ResponseBody
public Object logout(){
public Object logout() {
return new CommonResult().success(null);
}
@ApiOperation("根据用户名或姓名分页获取用户列表")
@RequestMapping(value = "/list",method = RequestMethod.GET)
@ResponseBody
public Object list(@RequestParam("name") String name,
@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
List<UmsAdmin> adminList = adminService.list(name,pageSize,pageNum);
return new CommonResult().pageSuccess(adminList);
}
@ApiOperation("获取指定用户信息")
@RequestMapping(value = "/{id}",method = RequestMethod.GET)
@ResponseBody
public Object getItem(@PathVariable Long id){
UmsAdmin admin = adminService.getItem(id);
return new CommonResult().success(admin);
}
@ApiOperation("获取指定用户信息")
@RequestMapping(value = "/update/{id}",method = RequestMethod.POST)
@ResponseBody
public Object update(@PathVariable Long id,@RequestBody UmsAdmin admin){
int count = adminService.update(id,admin);
if(count>0){
return new CommonResult().success(count);
}
return new CommonResult().failed();
}
@ApiOperation("删除指定用户信息")
@RequestMapping(value = "/delete/{id}",method = RequestMethod.POST)
@ResponseBody
public Object delete(@PathVariable Long id){
int count = adminService.delete(id);
if(count>0){
return new CommonResult().success(count);
}
return new CommonResult().failed();
}
}

View File

@@ -0,0 +1,65 @@
package com.macro.mall.controller;
import com.macro.mall.dto.CommonResult;
import com.macro.mall.dto.UmsPermissionNode;
import com.macro.mall.model.UmsPermission;
import com.macro.mall.service.UmsPermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* 后台用户权限管理
* Created by macro on 2018/9/29.
*/
@Controller
@Api(tags = "UmsPermissionController", description = "后台用户权限管理")
@RequestMapping("/admin/permission")
public class UmsPermissionController {
@Autowired
private UmsPermissionService permissionService;
@ApiOperation("添加权限")
@RequestMapping(value = "/create", method = RequestMethod.POST)
@ResponseBody
public Object create(@RequestBody UmsPermission permission) {
int count = permissionService.create(permission);
if(count>0){
return new CommonResult().success(count);
}
return new CommonResult().failed();
}
@ApiOperation("修改权限")
@RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
@ResponseBody
public Object update(@PathVariable Long id, @RequestBody UmsPermission permission) {
int count = permissionService.update(id,permission);
if(count>0){
return new CommonResult().success(count);
}
return new CommonResult().failed();
}
@ApiOperation("根据id批量删除权限")
@RequestMapping(value = "/delete", method = RequestMethod.POST)
@ResponseBody
public Object delete(@RequestParam("ids") List<Long> ids) {
int count = permissionService.delete(ids);
if(count>0){
return new CommonResult().success(count);
}
return new CommonResult().failed();
}
@ApiOperation("以层级结构返回所有权限")
@RequestMapping(value = "/treeList", method = RequestMethod.GET)
@ResponseBody
public Object treeList() {
List<UmsPermissionNode> permissionNodeList = permissionService.treeList();
return new CommonResult().success(permissionNodeList);
}
}

View File

@@ -0,0 +1,79 @@
package com.macro.mall.controller;
import com.macro.mall.dto.CommonResult;
import com.macro.mall.model.UmsPermission;
import com.macro.mall.model.UmsRole;
import com.macro.mall.service.UmsRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* 后台用户角色管理
* Created by macro on 2018/9/30.
*/
@Controller
@Api(tags = "UmsRoleController", description = "后台用户角色管理")
@RequestMapping("/admin/role")
public class UmsRoleController {
@Autowired
private UmsRoleService roleService;
@ApiOperation("添加角色")
@RequestMapping(value = "/create", method = RequestMethod.POST)
@ResponseBody
public Object create(@RequestBody UmsRole role) {
int count = roleService.create(role);
if(count>0){
return new CommonResult().success(count);
}
return new CommonResult().failed();
}
@ApiOperation("修改角色")
@RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
@ResponseBody
public Object update(@PathVariable Long id, @RequestBody UmsRole role) {
int count = roleService.update(id,role);
if(count>0){
return new CommonResult().success(count);
}
return new CommonResult().failed();
}
@ApiOperation("批量删除角色")
@RequestMapping(value = "/delete", method = RequestMethod.POST)
@ResponseBody
public Object delete(@RequestParam("ids") List<Long> ids) {
int count = roleService.delete(ids);
if(count>0){
return new CommonResult().success(count);
}
return new CommonResult().failed();
}
@ApiOperation("获取相应角色权限")
@RequestMapping(value = "/permission/{roleId}", method = RequestMethod.GET)
@ResponseBody
public Object getPermissionList(@PathVariable Long roleId) {
List<UmsPermission> permissionList =roleService.getPermissionList(roleId);
return new CommonResult().success(permissionList);
}
@ApiOperation("修改角色权限")
@RequestMapping(value = "/permission/update", method = RequestMethod.POST)
@ResponseBody
public Object updatePermission(@RequestParam Long roleId,
@RequestParam("permissionIds") List<Long> permissionIds) {
int count = roleService.updatePermission(roleId,permissionIds);
if(count>0){
return new CommonResult().success(count);
}
return new CommonResult().failed();
}
}

View File

@@ -0,0 +1,23 @@
package com.macro.mall.dao;
import com.macro.mall.model.UmsPermission;
import com.macro.mall.model.UmsRolePermissionRelation;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* 后台用户角色管理自定义Dao
* Created by macro on 2018/9/30.
*/
public interface UmsRolePermissionRelationDao {
/**
* 批量插入角色和权限关系
*/
int insertList(@Param("list")List<UmsRolePermissionRelation> list);
/**
* 根据角色获取权限
*/
List<UmsPermission> getPermissionList(@Param("roleId") Long roleId);
}

View File

@@ -1,6 +1,8 @@
package com.macro.mall.dto;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;
@@ -8,6 +10,8 @@ import org.hibernate.validator.constraints.NotEmpty;
* 用户登录参数
* Created by macro on 2018/4/26.
*/
@Getter
@Setter
public class UmsAdminParam {
@ApiModelProperty(value = "用户名", required = true)
@NotEmpty(message = "用户名不能为空")
@@ -20,36 +24,8 @@ public class UmsAdminParam {
@ApiModelProperty(value = "邮箱")
@Email(message = "邮箱格式不合法")
private String email;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@ApiModelProperty(value = "用户昵称")
private String nickName;
@ApiModelProperty(value = "备注")
private String note;
}

View File

@@ -0,0 +1,16 @@
package com.macro.mall.dto;
import com.macro.mall.model.UmsPermission;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
/**
* Created by macro on 2018/9/30.
*/
public class UmsPermissionNode extends UmsPermission {
@Getter
@Setter
private List<UmsPermissionNode> children;
}

View File

@@ -3,6 +3,8 @@ package com.macro.mall.service;
import com.macro.mall.dto.UmsAdminParam;
import com.macro.mall.model.UmsAdmin;
import java.util.List;
/**
* 后台管理员Service
* Created by macro on 2018/4/26.
@@ -31,4 +33,24 @@ public interface UmsAdminService {
* @param oldToken 旧的token
*/
String refreshToken(String oldToken);
/**
* 根据用户id获取用户
*/
UmsAdmin getItem(Long id);
/**
* 根据用户名或昵称分页查询用户
*/
List<UmsAdmin> list(String name, Integer pageSize, Integer pageNum);
/**
* 修改指定用户信息
*/
int update(Long id, UmsAdmin admin);
/**
* 删除指定用户
*/
int delete(Long id);
}

View File

@@ -0,0 +1,32 @@
package com.macro.mall.service;
import com.macro.mall.dto.UmsPermissionNode;
import com.macro.mall.model.UmsPermission;
import java.util.List;
/**
* 后台用户权限管理Service
* Created by macro on 2018/9/29.
*/
public interface UmsPermissionService {
/**
* 添加权限
*/
int create(UmsPermission permission);
/**
* 修改权限
*/
int update(Long id,UmsPermission permission);
/**
* 批量删除权限
*/
int delete(List<Long> ids);
/**
* 以层级结构返回所有权限
*/
List<UmsPermissionNode> treeList();
}

View File

@@ -0,0 +1,39 @@
package com.macro.mall.service;
import com.macro.mall.model.UmsPermission;
import com.macro.mall.model.UmsRole;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
* 后台角色管理Service
* Created by macro on 2018/9/30.
*/
public interface UmsRoleService {
/**
* 添加角色
*/
int create(UmsRole role);
/**
* 修改角色信息
*/
int update(Long id, UmsRole role);
/**
* 批量删除角色
*/
int delete(List<Long> ids);
/**
* 获取指定角色权限
*/
List<UmsPermission> getPermissionList(Long roleId);
/**
* 修改指定角色的权限
*/
@Transactional
int updatePermission(Long roleId, List<Long> permissionIds);
}

View File

@@ -1,5 +1,6 @@
package com.macro.mall.service.impl;
import com.github.pagehelper.PageHelper;
import com.macro.mall.dto.UmsAdminParam;
import com.macro.mall.mapper.UmsAdminMapper;
import com.macro.mall.model.UmsAdmin;
@@ -20,6 +21,7 @@ import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.List;
@@ -96,4 +98,31 @@ public class UmsAdminServiceImpl implements UmsAdminService{
}
return null;
}
@Override
public UmsAdmin getItem(Long id) {
return adminMapper.selectByPrimaryKey(id);
}
@Override
public List<UmsAdmin> list(String name, Integer pageSize, Integer pageNum) {
PageHelper.startPage(pageNum,pageSize);
UmsAdminExample example = new UmsAdminExample();
UmsAdminExample.Criteria criteria = example.createCriteria();
if(!StringUtils.isEmpty(name)){
criteria.andUsernameLike("%"+name+"%");
example.or(example.createCriteria().andNickNameLike("%"+name+"%"));
}
return adminMapper.selectByExample(example);
}
@Override
public int update(Long id, UmsAdmin admin) {
return 0;
}
@Override
public int delete(Long id) {
return 0;
}
}

View File

@@ -0,0 +1,68 @@
package com.macro.mall.service.impl;
import com.macro.mall.dto.UmsPermissionNode;
import com.macro.mall.mapper.UmsPermissionMapper;
import com.macro.mall.model.UmsPermission;
import com.macro.mall.model.UmsPermissionExample;
import com.macro.mall.service.UmsPermissionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
/**
* 后台用户权限管理Service实现类
* Created by macro on 2018/9/29.
*/
@Service
public class UmsPermissionServiceImpl implements UmsPermissionService {
@Autowired
private UmsPermissionMapper permissionMapper;
@Override
public int create(UmsPermission permission) {
permission.setStatus(1);
permission.setCreateTime(new Date());
permission.setSort(0);
return permissionMapper.insert(permission);
}
@Override
public int update(Long id, UmsPermission permission) {
permission.setId(id);
return permissionMapper.updateByPrimaryKey(permission);
}
@Override
public int delete(List<Long> ids) {
UmsPermissionExample example = new UmsPermissionExample();
example.createCriteria().andIdIn(ids);
return permissionMapper.deleteByExample(example);
}
@Override
public List<UmsPermissionNode> treeList() {
List<UmsPermission> permissionList = permissionMapper.selectByExample(new UmsPermissionExample());
List<UmsPermissionNode> result = permissionList.stream()
.filter(permission -> permission.getPid().equals(0L))
.map(permission -> covert(permission,permissionList)).collect(Collectors.toList());
return result;
}
/**
* 将权限转换为带有子级的权限对象
* 当找不到子级权限的时候map操作不会再递归调用covert
*/
private UmsPermissionNode covert(UmsPermission permission,List<UmsPermission> permissionList){
UmsPermissionNode node = new UmsPermissionNode();
BeanUtils.copyProperties(permission,node);
List<UmsPermissionNode> children = permissionList.stream()
.filter(subPermission -> subPermission.getPid().equals(permission.getId()))
.map(subPermission -> covert(subPermission,permissionList)).collect(Collectors.toList());
node.setChildren(children);
return node;
}
}

View File

@@ -0,0 +1,70 @@
package com.macro.mall.service.impl;
import com.macro.mall.dao.UmsRolePermissionRelationDao;
import com.macro.mall.mapper.UmsRoleMapper;
import com.macro.mall.mapper.UmsRolePermissionRelationMapper;
import com.macro.mall.model.*;
import com.macro.mall.service.UmsRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* 后台角色管理Service实现类
* Created by macro on 2018/9/30.
*/
@Service
public class UmsRoleServiceImpl implements UmsRoleService {
@Autowired
private UmsRoleMapper roleMapper;
@Autowired
private UmsRolePermissionRelationMapper rolePermissionRelationMapper;
@Autowired
private UmsRolePermissionRelationDao rolePermissionRelationDao;
@Override
public int create(UmsRole role) {
role.setCreateTime(new Date());
role.setStatus(1);
role.setAdminCount(0);
role.setSort(0);
return roleMapper.insert(role);
}
@Override
public int update(Long id, UmsRole role) {
role.setId(id);
return roleMapper.updateByPrimaryKey(role);
}
@Override
public int delete(List<Long> ids) {
UmsRoleExample example = new UmsRoleExample();
example.createCriteria().andIdIn(ids);
return roleMapper.deleteByExample(example);
}
@Override
public List<UmsPermission> getPermissionList(Long roleId) {
return rolePermissionRelationDao.getPermissionList(roleId);
}
@Override
public int updatePermission(Long roleId, List<Long> permissionIds) {
//先删除原有关系
UmsRolePermissionRelationExample example=new UmsRolePermissionRelationExample();
example.createCriteria().andRoleIdEqualTo(roleId);
rolePermissionRelationMapper.deleteByExample(example);
//批量插入新关系
List<UmsRolePermissionRelation> relationList = new ArrayList<>();
for (Long permissionId : permissionIds) {
UmsRolePermissionRelation relation = new UmsRolePermissionRelation();
relation.setRoleId(roleId);
relation.setPermissionId(permissionId);
relationList.add(relation);
}
return rolePermissionRelationDao.insertList(relationList);
}
}