From ecc50c4b5d9a855a8319f661e62271500f43e1b9 Mon Sep 17 00:00:00 2001 From: macro Date: Sun, 16 Feb 2020 14:22:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=9D=83=E9=99=90=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E7=9B=B8=E5=85=B3=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/macro/mall/bo/AdminUserDetails.java | 15 ++- .../macro/mall/config/MallSecurityConfig.java | 29 ++++- .../mall/controller/PmsBrandController.java | 10 -- .../PmsProductCategoryController.java | 9 -- .../mall/controller/PmsProductController.java | 10 -- .../mall/controller/UmsAdminController.java | 28 ++++- .../mall/controller/UmsMenuController.java | 102 +++++++++++++++++ .../UmsResourceCategoryController.java | 69 ++++++++++++ .../controller/UmsResourceController.java | 97 ++++++++++++++++ .../mall/controller/UmsRoleController.java | 63 ++++++++++- .../mall/dao/UmsAdminRoleRelationDao.java | 6 + .../java/com/macro/mall/dao/UmsRoleDao.java | 19 ++++ .../java/com/macro/mall/dto/UmsMenuNode.java | 16 +++ .../macro/mall/service/UmsAdminService.java | 8 +- .../macro/mall/service/UmsMenuService.java | 47 ++++++++ .../service/UmsResourceCategoryService.java | 19 ++++ .../mall/service/UmsResourceService.java | 23 ++++ .../macro/mall/service/UmsRoleService.java | 36 +++++- .../service/impl/UmsAdminServiceImpl.java | 31 ++++-- .../mall/service/impl/UmsMenuServiceImpl.java | 105 ++++++++++++++++++ .../impl/UmsResourceCategoryServiceImpl.java | 45 ++++++++ .../service/impl/UmsResourceServiceImpl.java | 66 +++++++++++ .../mall/service/impl/UmsRoleServiceImpl.java | 71 +++++++++++- mall-admin/src/main/resources/application.yml | 2 + .../resources/dao/UmsAdminRoleRelationDao.xml | 19 ++++ .../src/main/resources/dao/UmsRoleDao.xml | 64 +++++++++++ 26 files changed, 951 insertions(+), 58 deletions(-) create mode 100644 mall-admin/src/main/java/com/macro/mall/controller/UmsMenuController.java create mode 100644 mall-admin/src/main/java/com/macro/mall/controller/UmsResourceCategoryController.java create mode 100644 mall-admin/src/main/java/com/macro/mall/controller/UmsResourceController.java create mode 100644 mall-admin/src/main/java/com/macro/mall/dao/UmsRoleDao.java create mode 100644 mall-admin/src/main/java/com/macro/mall/dto/UmsMenuNode.java create mode 100644 mall-admin/src/main/java/com/macro/mall/service/UmsMenuService.java create mode 100644 mall-admin/src/main/java/com/macro/mall/service/UmsResourceCategoryService.java create mode 100644 mall-admin/src/main/java/com/macro/mall/service/UmsResourceService.java create mode 100644 mall-admin/src/main/java/com/macro/mall/service/impl/UmsMenuServiceImpl.java create mode 100644 mall-admin/src/main/java/com/macro/mall/service/impl/UmsResourceCategoryServiceImpl.java create mode 100644 mall-admin/src/main/java/com/macro/mall/service/impl/UmsResourceServiceImpl.java create mode 100644 mall-admin/src/main/resources/dao/UmsRoleDao.xml diff --git a/mall-admin/src/main/java/com/macro/mall/bo/AdminUserDetails.java b/mall-admin/src/main/java/com/macro/mall/bo/AdminUserDetails.java index 1c54b6f..a46ab68 100644 --- a/mall-admin/src/main/java/com/macro/mall/bo/AdminUserDetails.java +++ b/mall-admin/src/main/java/com/macro/mall/bo/AdminUserDetails.java @@ -1,7 +1,7 @@ package com.macro.mall.bo; import com.macro.mall.model.UmsAdmin; -import com.macro.mall.model.UmsPermission; +import com.macro.mall.model.UmsResource; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.UserDetails; @@ -16,18 +16,17 @@ import java.util.stream.Collectors; */ public class AdminUserDetails implements UserDetails { private UmsAdmin umsAdmin; - private List permissionList; - public AdminUserDetails(UmsAdmin umsAdmin,List permissionList) { + private List resourceList; + public AdminUserDetails(UmsAdmin umsAdmin,List resourceList) { this.umsAdmin = umsAdmin; - this.permissionList = permissionList; + this.resourceList = resourceList; } @Override public Collection getAuthorities() { - //返回当前用户的权限 - return permissionList.stream() - .filter(permission -> permission.getValue()!=null) - .map(permission ->new SimpleGrantedAuthority(permission.getValue())) + //返回当前用户的角色 + return resourceList.stream() + .map(role ->new SimpleGrantedAuthority(role.getId()+":"+role.getName())) .collect(Collectors.toList()); } diff --git a/mall-admin/src/main/java/com/macro/mall/config/MallSecurityConfig.java b/mall-admin/src/main/java/com/macro/mall/config/MallSecurityConfig.java index a82ea2e..23b6f15 100644 --- a/mall-admin/src/main/java/com/macro/mall/config/MallSecurityConfig.java +++ b/mall-admin/src/main/java/com/macro/mall/config/MallSecurityConfig.java @@ -1,29 +1,56 @@ package com.macro.mall.config; +import com.macro.mall.model.UmsResource; +import com.macro.mall.security.component.DynamicSecurityService; import com.macro.mall.security.config.SecurityConfig; import com.macro.mall.service.UmsAdminService; +import com.macro.mall.service.UmsResourceService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.security.access.ConfigAttribute; import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.core.userdetails.UserDetailsService; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + /** * mall-security模块相关配置 * Created by macro on 2019/11/9. */ @Configuration @EnableWebSecurity -@EnableGlobalMethodSecurity(prePostEnabled=true) +@EnableGlobalMethodSecurity(prePostEnabled = true) public class MallSecurityConfig extends SecurityConfig { @Autowired private UmsAdminService adminService; + @Autowired + private UmsResourceService resourceService; @Bean public UserDetailsService userDetailsService() { //获取登录用户信息 return username -> adminService.loadUserByUsername(username); } + + @Bean + public DynamicSecurityService dynamicSecurityService() { + return new DynamicSecurityService() { + @Override + public Map loadDataSource() { + Map map = new ConcurrentHashMap<>(); + List resourceList = resourceService.listAll(); + for (UmsResource resource : resourceList) { + map.put(resource.getUrl(), new org.springframework.security.access.SecurityConfig(resource.getId() + ":" + resource.getName())); + } + return map; + } + }; + } } diff --git a/mall-admin/src/main/java/com/macro/mall/controller/PmsBrandController.java b/mall-admin/src/main/java/com/macro/mall/controller/PmsBrandController.java index 95227ec..e793904 100644 --- a/mall-admin/src/main/java/com/macro/mall/controller/PmsBrandController.java +++ b/mall-admin/src/main/java/com/macro/mall/controller/PmsBrandController.java @@ -8,7 +8,6 @@ import com.macro.mall.service.PmsBrandService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.validation.annotation.Validated; @@ -30,7 +29,6 @@ public class PmsBrandController { @ApiOperation(value = "获取全部品牌列表") @RequestMapping(value = "/listAll", method = RequestMethod.GET) @ResponseBody - @PreAuthorize("hasAuthority('pms:brand:read')") public CommonResult> getList() { return CommonResult.success(brandService.listAllBrand()); } @@ -38,7 +36,6 @@ public class PmsBrandController { @ApiOperation(value = "添加品牌") @RequestMapping(value = "/create", method = RequestMethod.POST) @ResponseBody - @PreAuthorize("hasAuthority('pms:brand:create')") public CommonResult create(@Validated @RequestBody PmsBrandParam pmsBrand, BindingResult result) { CommonResult commonResult; int count = brandService.createBrand(pmsBrand); @@ -53,7 +50,6 @@ public class PmsBrandController { @ApiOperation(value = "更新品牌") @RequestMapping(value = "/update/{id}", method = RequestMethod.POST) @ResponseBody - @PreAuthorize("hasAuthority('pms:brand:update')") public CommonResult update(@PathVariable("id") Long id, @Validated @RequestBody PmsBrandParam pmsBrandParam, BindingResult result) { @@ -70,7 +66,6 @@ public class PmsBrandController { @ApiOperation(value = "删除品牌") @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET) @ResponseBody - @PreAuthorize("hasAuthority('pms:brand:delete')") public CommonResult delete(@PathVariable("id") Long id) { int count = brandService.deleteBrand(id); if (count == 1) { @@ -83,7 +78,6 @@ public class PmsBrandController { @ApiOperation(value = "根据品牌名称分页获取品牌列表") @RequestMapping(value = "/list", method = RequestMethod.GET) @ResponseBody - @PreAuthorize("hasAuthority('pms:brand:read')") public CommonResult> getList(@RequestParam(value = "keyword", required = false) String keyword, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) { @@ -94,7 +88,6 @@ public class PmsBrandController { @ApiOperation(value = "根据编号查询品牌信息") @RequestMapping(value = "/{id}", method = RequestMethod.GET) @ResponseBody - @PreAuthorize("hasAuthority('pms:brand:read')") public CommonResult getItem(@PathVariable("id") Long id) { return CommonResult.success(brandService.getBrand(id)); } @@ -102,7 +95,6 @@ public class PmsBrandController { @ApiOperation(value = "批量删除品牌") @RequestMapping(value = "/delete/batch", method = RequestMethod.POST) @ResponseBody - @PreAuthorize("hasAuthority('pms:brand:delete')") public CommonResult deleteBatch(@RequestParam("ids") List ids) { int count = brandService.deleteBrand(ids); if (count > 0) { @@ -115,7 +107,6 @@ public class PmsBrandController { @ApiOperation(value = "批量更新显示状态") @RequestMapping(value = "/update/showStatus", method = RequestMethod.POST) @ResponseBody - @PreAuthorize("hasAuthority('pms:brand:update')") public CommonResult updateShowStatus(@RequestParam("ids") List ids, @RequestParam("showStatus") Integer showStatus) { int count = brandService.updateShowStatus(ids, showStatus); @@ -129,7 +120,6 @@ public class PmsBrandController { @ApiOperation(value = "批量更新厂家制造商状态") @RequestMapping(value = "/update/factoryStatus", method = RequestMethod.POST) @ResponseBody - @PreAuthorize("hasAuthority('pms:brand:update')") public CommonResult updateFactoryStatus(@RequestParam("ids") List ids, @RequestParam("factoryStatus") Integer factoryStatus) { int count = brandService.updateFactoryStatus(ids, factoryStatus); diff --git a/mall-admin/src/main/java/com/macro/mall/controller/PmsProductCategoryController.java b/mall-admin/src/main/java/com/macro/mall/controller/PmsProductCategoryController.java index 5737e00..71f48cd 100644 --- a/mall-admin/src/main/java/com/macro/mall/controller/PmsProductCategoryController.java +++ b/mall-admin/src/main/java/com/macro/mall/controller/PmsProductCategoryController.java @@ -9,7 +9,6 @@ import com.macro.mall.service.PmsProductCategoryService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.validation.annotation.Validated; @@ -31,7 +30,6 @@ public class PmsProductCategoryController { @ApiOperation("添加产品分类") @RequestMapping(value = "/create", method = RequestMethod.POST) @ResponseBody - @PreAuthorize("hasAuthority('pms:productCategory:create')") public CommonResult create(@Validated @RequestBody PmsProductCategoryParam productCategoryParam, BindingResult result) { int count = productCategoryService.create(productCategoryParam); @@ -45,7 +43,6 @@ public class PmsProductCategoryController { @ApiOperation("修改商品分类") @RequestMapping(value = "/update/{id}", method = RequestMethod.POST) @ResponseBody - @PreAuthorize("hasAuthority('pms:productCategory:update')") public CommonResult update(@PathVariable Long id, @Validated @RequestBody PmsProductCategoryParam productCategoryParam, @@ -61,7 +58,6 @@ public class PmsProductCategoryController { @ApiOperation("分页查询商品分类") @RequestMapping(value = "/list/{parentId}", method = RequestMethod.GET) @ResponseBody - @PreAuthorize("hasAuthority('pms:productCategory:read')") public CommonResult> getList(@PathVariable Long parentId, @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) { @@ -72,7 +68,6 @@ public class PmsProductCategoryController { @ApiOperation("根据id获取商品分类") @RequestMapping(value = "/{id}", method = RequestMethod.GET) @ResponseBody - @PreAuthorize("hasAuthority('pms:productCategory:read')") public CommonResult getItem(@PathVariable Long id) { PmsProductCategory productCategory = productCategoryService.getItem(id); return CommonResult.success(productCategory); @@ -81,7 +76,6 @@ public class PmsProductCategoryController { @ApiOperation("删除商品分类") @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST) @ResponseBody - @PreAuthorize("hasAuthority('pms:productCategory:delete')") public CommonResult delete(@PathVariable Long id) { int count = productCategoryService.delete(id); if (count > 0) { @@ -94,7 +88,6 @@ public class PmsProductCategoryController { @ApiOperation("修改导航栏显示状态") @RequestMapping(value = "/update/navStatus", method = RequestMethod.POST) @ResponseBody - @PreAuthorize("hasAuthority('pms:productCategory:update')") public CommonResult updateNavStatus(@RequestParam("ids") List ids, @RequestParam("navStatus") Integer navStatus) { int count = productCategoryService.updateNavStatus(ids, navStatus); if (count > 0) { @@ -107,7 +100,6 @@ public class PmsProductCategoryController { @ApiOperation("修改显示状态") @RequestMapping(value = "/update/showStatus", method = RequestMethod.POST) @ResponseBody - @PreAuthorize("hasAuthority('pms:productCategory:update')") public CommonResult updateShowStatus(@RequestParam("ids") List ids, @RequestParam("showStatus") Integer showStatus) { int count = productCategoryService.updateShowStatus(ids, showStatus); if (count > 0) { @@ -120,7 +112,6 @@ public class PmsProductCategoryController { @ApiOperation("查询所有一级分类及子分类") @RequestMapping(value = "/list/withChildren", method = RequestMethod.GET) @ResponseBody - @PreAuthorize("hasAuthority('pms:productCategory:read')") public CommonResult> listWithChildren() { List list = productCategoryService.listWithChildren(); return CommonResult.success(list); diff --git a/mall-admin/src/main/java/com/macro/mall/controller/PmsProductController.java b/mall-admin/src/main/java/com/macro/mall/controller/PmsProductController.java index 4899e65..8ac63aa 100644 --- a/mall-admin/src/main/java/com/macro/mall/controller/PmsProductController.java +++ b/mall-admin/src/main/java/com/macro/mall/controller/PmsProductController.java @@ -10,7 +10,6 @@ import com.macro.mall.service.PmsProductService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.*; @@ -31,7 +30,6 @@ public class PmsProductController { @ApiOperation("创建商品") @RequestMapping(value = "/create", method = RequestMethod.POST) @ResponseBody - @PreAuthorize("hasAuthority('pms:product:create')") public CommonResult create(@RequestBody PmsProductParam productParam, BindingResult bindingResult) { int count = productService.create(productParam); if (count > 0) { @@ -44,7 +42,6 @@ public class PmsProductController { @ApiOperation("根据商品id获取商品编辑信息") @RequestMapping(value = "/updateInfo/{id}", method = RequestMethod.GET) @ResponseBody - @PreAuthorize("hasAuthority('pms:product:read')") public CommonResult getUpdateInfo(@PathVariable Long id) { PmsProductResult productResult = productService.getUpdateInfo(id); return CommonResult.success(productResult); @@ -53,7 +50,6 @@ public class PmsProductController { @ApiOperation("更新商品") @RequestMapping(value = "/update/{id}", method = RequestMethod.POST) @ResponseBody - @PreAuthorize("hasAuthority('pms:product:update')") public CommonResult update(@PathVariable Long id, @RequestBody PmsProductParam productParam, BindingResult bindingResult) { int count = productService.update(id, productParam); if (count > 0) { @@ -66,7 +62,6 @@ public class PmsProductController { @ApiOperation("查询商品") @RequestMapping(value = "/list", method = RequestMethod.GET) @ResponseBody - @PreAuthorize("hasAuthority('pms:product:read')") public CommonResult> getList(PmsProductQueryParam productQueryParam, @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) { @@ -85,7 +80,6 @@ public class PmsProductController { @ApiOperation("批量修改审核状态") @RequestMapping(value = "/update/verifyStatus", method = RequestMethod.POST) @ResponseBody - @PreAuthorize("hasAuthority('pms:product:update')") public CommonResult updateVerifyStatus(@RequestParam("ids") List ids, @RequestParam("verifyStatus") Integer verifyStatus, @RequestParam("detail") String detail) { @@ -100,7 +94,6 @@ public class PmsProductController { @ApiOperation("批量上下架") @RequestMapping(value = "/update/publishStatus", method = RequestMethod.POST) @ResponseBody - @PreAuthorize("hasAuthority('pms:product:update')") public CommonResult updatePublishStatus(@RequestParam("ids") List ids, @RequestParam("publishStatus") Integer publishStatus) { int count = productService.updatePublishStatus(ids, publishStatus); @@ -114,7 +107,6 @@ public class PmsProductController { @ApiOperation("批量推荐商品") @RequestMapping(value = "/update/recommendStatus", method = RequestMethod.POST) @ResponseBody - @PreAuthorize("hasAuthority('pms:product:update')") public CommonResult updateRecommendStatus(@RequestParam("ids") List ids, @RequestParam("recommendStatus") Integer recommendStatus) { int count = productService.updateRecommendStatus(ids, recommendStatus); @@ -128,7 +120,6 @@ public class PmsProductController { @ApiOperation("批量设为新品") @RequestMapping(value = "/update/newStatus", method = RequestMethod.POST) @ResponseBody - @PreAuthorize("hasAuthority('pms:product:update')") public CommonResult updateNewStatus(@RequestParam("ids") List ids, @RequestParam("newStatus") Integer newStatus) { int count = productService.updateNewStatus(ids, newStatus); @@ -142,7 +133,6 @@ public class PmsProductController { @ApiOperation("批量修改删除状态") @RequestMapping(value = "/update/deleteStatus", method = RequestMethod.POST) @ResponseBody - @PreAuthorize("hasAuthority('pms:product:delete')") public CommonResult updateDeleteStatus(@RequestParam("ids") List ids, @RequestParam("deleteStatus") Integer deleteStatus) { int count = productService.updateDeleteStatus(ids, deleteStatus); diff --git a/mall-admin/src/main/java/com/macro/mall/controller/UmsAdminController.java b/mall-admin/src/main/java/com/macro/mall/controller/UmsAdminController.java index 3733f04..ee36dc7 100644 --- a/mall-admin/src/main/java/com/macro/mall/controller/UmsAdminController.java +++ b/mall-admin/src/main/java/com/macro/mall/controller/UmsAdminController.java @@ -9,6 +9,7 @@ import com.macro.mall.model.UmsAdmin; import com.macro.mall.model.UmsPermission; import com.macro.mall.model.UmsRole; import com.macro.mall.service.UmsAdminService; +import com.macro.mall.service.UmsRoleService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; @@ -31,12 +32,14 @@ import java.util.Map; @Api(tags = "UmsAdminController", description = "后台用户管理") @RequestMapping("/admin") public class UmsAdminController { - @Autowired - private UmsAdminService adminService; @Value("${jwt.tokenHeader}") private String tokenHeader; @Value("${jwt.tokenHead}") private String tokenHead; + @Autowired + private UmsAdminService adminService; + @Autowired + private UmsRoleService roleService; @ApiOperation(value = "用户注册") @RequestMapping(value = "/register", method = RequestMethod.POST) @@ -82,11 +85,15 @@ public class UmsAdminController { @RequestMapping(value = "/info", method = RequestMethod.GET) @ResponseBody public CommonResult getAdminInfo(Principal principal) { + if(principal==null){ + return CommonResult.unauthorized(null); + } String username = principal.getName(); UmsAdmin umsAdmin = adminService.getAdminByUsername(username); Map data = new HashMap<>(); data.put("username", umsAdmin.getUsername()); data.put("roles", new String[]{"TEST"}); + data.put("menus", roleService.getMenuList(umsAdmin.getId())); data.put("icon", umsAdmin.getIcon()); return CommonResult.success(data); } @@ -101,10 +108,10 @@ public class UmsAdminController { @ApiOperation("根据用户名或姓名分页获取用户列表") @RequestMapping(value = "/list", method = RequestMethod.GET) @ResponseBody - public CommonResult> list(@RequestParam(value = "name", required = false) String name, + public CommonResult> list(@RequestParam(value = "keyword", required = false) String keyword, @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) { - List adminList = adminService.list(name, pageSize, pageNum); + List adminList = adminService.list(keyword, pageSize, pageNum); return CommonResult.success(CommonPage.restPage(adminList)); } @@ -156,6 +163,19 @@ public class UmsAdminController { return CommonResult.failed(); } + @ApiOperation("修改帐号状态") + @RequestMapping(value = "/updateStatus/{id}", method = RequestMethod.POST) + @ResponseBody + public CommonResult updateStatus(@PathVariable Long id,@RequestParam(value = "status") Integer status) { + UmsAdmin umsAdmin = new UmsAdmin(); + umsAdmin.setStatus(status); + int count = adminService.update(id,umsAdmin); + if (count > 0) { + return CommonResult.success(count); + } + return CommonResult.failed(); + } + @ApiOperation("给用户分配角色") @RequestMapping(value = "/role/update", method = RequestMethod.POST) @ResponseBody diff --git a/mall-admin/src/main/java/com/macro/mall/controller/UmsMenuController.java b/mall-admin/src/main/java/com/macro/mall/controller/UmsMenuController.java new file mode 100644 index 0000000..dec3d50 --- /dev/null +++ b/mall-admin/src/main/java/com/macro/mall/controller/UmsMenuController.java @@ -0,0 +1,102 @@ +package com.macro.mall.controller; + +import com.macro.mall.common.api.CommonPage; +import com.macro.mall.common.api.CommonResult; +import com.macro.mall.dto.UmsMenuNode; +import com.macro.mall.model.UmsMenu; +import com.macro.mall.service.UmsMenuService; +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; + +/** + * 后台菜单管理Controller + * Created by macro on 2020/2/4. + */ +@Controller +@Api(tags = "UmsMenuController", description = "后台菜单管理") +@RequestMapping("/menu") +public class UmsMenuController { + + @Autowired + private UmsMenuService menuService; + + @ApiOperation("添加后台菜单") + @RequestMapping(value = "/create", method = RequestMethod.POST) + @ResponseBody + public CommonResult create(@RequestBody UmsMenu umsMenu) { + int count = menuService.create(umsMenu); + if (count > 0) { + return CommonResult.success(count); + } else { + return CommonResult.failed(); + } + } + + @ApiOperation("修改后台菜单") + @RequestMapping(value = "/update/{id}", method = RequestMethod.POST) + @ResponseBody + public CommonResult update(@PathVariable Long id, + @RequestBody UmsMenu umsMenu) { + int count = menuService.update(id, umsMenu); + if (count > 0) { + return CommonResult.success(count); + } else { + return CommonResult.failed(); + } + } + + @ApiOperation("根据ID获取菜单详情") + @RequestMapping(value = "/{id}", method = RequestMethod.GET) + @ResponseBody + public CommonResult getItem(@PathVariable Long id) { + UmsMenu umsMenu = menuService.getItem(id); + return CommonResult.success(umsMenu); + } + + @ApiOperation("根据ID删除后台菜单") + @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST) + @ResponseBody + public CommonResult delete(@PathVariable Long id) { + int count = menuService.delete(id); + if (count > 0) { + return CommonResult.success(count); + } else { + return CommonResult.failed(); + } + } + + @ApiOperation("分页查询后台菜单") + @RequestMapping(value = "/list/{parentId}", method = RequestMethod.GET) + @ResponseBody + public CommonResult> list(@PathVariable Long parentId, + @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize, + @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) { + List menuList = menuService.list(parentId, pageSize, pageNum); + return CommonResult.success(CommonPage.restPage(menuList)); + } + + @ApiOperation("树形结构返回所有菜单列表") + @RequestMapping(value = "/treeList", method = RequestMethod.GET) + @ResponseBody + public CommonResult> treeList() { + List list = menuService.treeList(); + return CommonResult.success(list); + } + + @ApiOperation("修改菜单显示状态") + @RequestMapping(value = "/updateHidden/{id}", method = RequestMethod.POST) + @ResponseBody + public CommonResult updateHidden(@PathVariable Long id, @RequestParam("hidden") Integer hidden) { + int count = menuService.updateHidden(id, hidden); + if (count > 0) { + return CommonResult.success(count); + } else { + return CommonResult.failed(); + } + } +} diff --git a/mall-admin/src/main/java/com/macro/mall/controller/UmsResourceCategoryController.java b/mall-admin/src/main/java/com/macro/mall/controller/UmsResourceCategoryController.java new file mode 100644 index 0000000..b3e427f --- /dev/null +++ b/mall-admin/src/main/java/com/macro/mall/controller/UmsResourceCategoryController.java @@ -0,0 +1,69 @@ +package com.macro.mall.controller; + +import com.macro.mall.common.api.CommonResult; +import com.macro.mall.model.UmsResourceCategory; +import com.macro.mall.service.UmsResourceCategoryService; +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; + +/** + * 后台资源分类管理Controller + * Created by macro on 2020/2/5. + */ +@Controller +@Api(tags = "UmsResourceCategoryController", description = "后台资源分类管理") +@RequestMapping("/resourceCategory") +public class UmsResourceCategoryController { + @Autowired + private UmsResourceCategoryService resourceCategoryService; + + @ApiOperation("查询所有后台资源分类") + @RequestMapping(value = "/listAll", method = RequestMethod.GET) + @ResponseBody + public CommonResult> listAll() { + List resourceList = resourceCategoryService.listAll(); + return CommonResult.success(resourceList); + } + + @ApiOperation("添加后台资源分类") + @RequestMapping(value = "/create", method = RequestMethod.POST) + @ResponseBody + public CommonResult create(@RequestBody UmsResourceCategory umsResourceCategory) { + int count = resourceCategoryService.create(umsResourceCategory); + if (count > 0) { + return CommonResult.success(count); + } else { + return CommonResult.failed(); + } + } + + @ApiOperation("修改后台资源分类") + @RequestMapping(value = "/update/{id}", method = RequestMethod.POST) + @ResponseBody + public CommonResult update(@PathVariable Long id, + @RequestBody UmsResourceCategory umsResourceCategory) { + int count = resourceCategoryService.update(id, umsResourceCategory); + if (count > 0) { + return CommonResult.success(count); + } else { + return CommonResult.failed(); + } + } + + @ApiOperation("根据ID删除后台资源") + @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST) + @ResponseBody + public CommonResult delete(@PathVariable Long id) { + int count = resourceCategoryService.delete(id); + if (count > 0) { + return CommonResult.success(count); + } else { + return CommonResult.failed(); + } + } +} diff --git a/mall-admin/src/main/java/com/macro/mall/controller/UmsResourceController.java b/mall-admin/src/main/java/com/macro/mall/controller/UmsResourceController.java new file mode 100644 index 0000000..09685e9 --- /dev/null +++ b/mall-admin/src/main/java/com/macro/mall/controller/UmsResourceController.java @@ -0,0 +1,97 @@ +package com.macro.mall.controller; + +import com.macro.mall.common.api.CommonPage; +import com.macro.mall.common.api.CommonResult; +import com.macro.mall.model.UmsResource; +import com.macro.mall.security.component.DynamicSecurityMetadataSource; +import com.macro.mall.service.UmsResourceService; +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; + +/** + * 后台资源管理Controller + * Created by macro on 2020/2/4. + */ +@Controller +@Api(tags = "UmsResourceController", description = "后台资源管理") +@RequestMapping("/resource") +public class UmsResourceController { + + @Autowired + private UmsResourceService resourceService; + @Autowired + private DynamicSecurityMetadataSource dynamicSecurityMetadataSource; + + @ApiOperation("添加后台资源") + @RequestMapping(value = "/create", method = RequestMethod.POST) + @ResponseBody + public CommonResult create(@RequestBody UmsResource umsResource) { + int count = resourceService.create(umsResource); + dynamicSecurityMetadataSource.clearDataSource(); + if (count > 0) { + return CommonResult.success(count); + } else { + return CommonResult.failed(); + } + } + + @ApiOperation("修改后台资源") + @RequestMapping(value = "/update/{id}", method = RequestMethod.POST) + @ResponseBody + public CommonResult update(@PathVariable Long id, + @RequestBody UmsResource umsResource) { + int count = resourceService.update(id, umsResource); + dynamicSecurityMetadataSource.clearDataSource(); + if (count > 0) { + return CommonResult.success(count); + } else { + return CommonResult.failed(); + } + } + + @ApiOperation("根据ID获取资源详情") + @RequestMapping(value = "/{id}", method = RequestMethod.GET) + @ResponseBody + public CommonResult getItem(@PathVariable Long id) { + UmsResource umsResource = resourceService.getItem(id); + return CommonResult.success(umsResource); + } + + @ApiOperation("根据ID删除后台资源") + @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST) + @ResponseBody + public CommonResult delete(@PathVariable Long id) { + int count = resourceService.delete(id); + dynamicSecurityMetadataSource.clearDataSource(); + if (count > 0) { + return CommonResult.success(count); + } else { + return CommonResult.failed(); + } + } + + @ApiOperation("分页模糊查询后台资源") + @RequestMapping(value = "/list", method = RequestMethod.GET) + @ResponseBody + public CommonResult> list(@RequestParam(required = false) Long categoryId, + @RequestParam(required = false) String nameKeyword, + @RequestParam(required = false) String urlKeyword, + @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize, + @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) { + List resourceList = resourceService.list(categoryId,nameKeyword, urlKeyword, pageSize, pageNum); + return CommonResult.success(CommonPage.restPage(resourceList)); + } + + @ApiOperation("查询所有后台资源") + @RequestMapping(value = "/listAll", method = RequestMethod.GET) + @ResponseBody + public CommonResult> listAll() { + List resourceList = resourceService.listAll(); + return CommonResult.success(resourceList); + } +} diff --git a/mall-admin/src/main/java/com/macro/mall/controller/UmsRoleController.java b/mall-admin/src/main/java/com/macro/mall/controller/UmsRoleController.java index 4c48c4f..a4b2d82 100644 --- a/mall-admin/src/main/java/com/macro/mall/controller/UmsRoleController.java +++ b/mall-admin/src/main/java/com/macro/mall/controller/UmsRoleController.java @@ -1,8 +1,8 @@ package com.macro.mall.controller; +import com.macro.mall.common.api.CommonPage; import com.macro.mall.common.api.CommonResult; -import com.macro.mall.model.UmsPermission; -import com.macro.mall.model.UmsRole; +import com.macro.mall.model.*; import com.macro.mall.service.UmsRoleService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @@ -77,11 +77,66 @@ public class UmsRoleController { } @ApiOperation("获取所有角色") - @RequestMapping(value = "/list", method = RequestMethod.GET) + @RequestMapping(value = "/listAll", method = RequestMethod.GET) @ResponseBody - public Object list() { + public CommonResult> listAll() { List roleList = roleService.list(); return CommonResult.success(roleList); } + @ApiOperation("根据角色名称分页获取角色列表") + @RequestMapping(value = "/list", method = RequestMethod.GET) + @ResponseBody + public CommonResult> list(@RequestParam(value = "keyword", required = false) String keyword, + @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize, + @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) { + List roleList = roleService.list(keyword, pageSize, pageNum); + return CommonResult.success(CommonPage.restPage(roleList)); + } + + @ApiOperation("修改角色状态") + @RequestMapping(value = "/updateStatus/{id}", method = RequestMethod.POST) + @ResponseBody + public CommonResult updateStatus(@PathVariable Long id, @RequestParam(value = "status") Integer status) { + UmsRole umsRole = new UmsRole(); + umsRole.setStatus(status); + int count = roleService.update(id, umsRole); + if (count > 0) { + return CommonResult.success(count); + } + return CommonResult.failed(); + } + + @ApiOperation("获取角色相关菜单") + @RequestMapping(value = "/listMenu/{roleId}", method = RequestMethod.GET) + @ResponseBody + public CommonResult> listMenu(@PathVariable Long roleId) { + List roleList = roleService.listMenu(roleId); + return CommonResult.success(roleList); + } + + @ApiOperation("获取角色相关资源") + @RequestMapping(value = "/listResource/{roleId}", method = RequestMethod.GET) + @ResponseBody + public CommonResult> listResource(@PathVariable Long roleId) { + List roleList = roleService.listResource(roleId); + return CommonResult.success(roleList); + } + + @ApiOperation("给角色分配菜单") + @RequestMapping(value = "/allocMenu", method = RequestMethod.POST) + @ResponseBody + public CommonResult allocMenu(@RequestParam Long roleId, @RequestParam List menuIds) { + int count = roleService.allocMenu(roleId, menuIds); + return CommonResult.success(count); + } + + @ApiOperation("给角色分配资源") + @RequestMapping(value = "/allocResource", method = RequestMethod.POST) + @ResponseBody + public CommonResult allocResource(@RequestParam Long roleId, @RequestParam List resourceIds) { + int count = roleService.allocResource(roleId, resourceIds); + return CommonResult.success(count); + } + } diff --git a/mall-admin/src/main/java/com/macro/mall/dao/UmsAdminRoleRelationDao.java b/mall-admin/src/main/java/com/macro/mall/dao/UmsAdminRoleRelationDao.java index 710e949..eba581d 100644 --- a/mall-admin/src/main/java/com/macro/mall/dao/UmsAdminRoleRelationDao.java +++ b/mall-admin/src/main/java/com/macro/mall/dao/UmsAdminRoleRelationDao.java @@ -2,6 +2,7 @@ package com.macro.mall.dao; import com.macro.mall.model.UmsAdminRoleRelation; import com.macro.mall.model.UmsPermission; +import com.macro.mall.model.UmsResource; import com.macro.mall.model.UmsRole; import org.apache.ibatis.annotations.Param; @@ -31,4 +32,9 @@ public interface UmsAdminRoleRelationDao { * 获取用户所有权限(包括+-权限) */ List getPermissionList(@Param("adminId") Long adminId); + + /** + * 获取用户所有可访问资源 + */ + List getResourceList(@Param("adminId") Long adminId); } diff --git a/mall-admin/src/main/java/com/macro/mall/dao/UmsRoleDao.java b/mall-admin/src/main/java/com/macro/mall/dao/UmsRoleDao.java new file mode 100644 index 0000000..d38ee0b --- /dev/null +++ b/mall-admin/src/main/java/com/macro/mall/dao/UmsRoleDao.java @@ -0,0 +1,19 @@ +package com.macro.mall.dao; + +import com.macro.mall.model.UmsMenu; +import com.macro.mall.model.UmsResource; +import org.apache.ibatis.annotations.Param; + +import java.util.List; + +/** + * 后台用户角色自定义Dao + * Created by macro on 2020/2/2. + */ +public interface UmsRoleDao { + List getMenuList(@Param("adminId") Long adminId); + + List getMenuListByRoleId(@Param("roleId") Long roleId); + + List getResourceListByRoleId(@Param("roleId") Long roleId); +} diff --git a/mall-admin/src/main/java/com/macro/mall/dto/UmsMenuNode.java b/mall-admin/src/main/java/com/macro/mall/dto/UmsMenuNode.java new file mode 100644 index 0000000..99b25c9 --- /dev/null +++ b/mall-admin/src/main/java/com/macro/mall/dto/UmsMenuNode.java @@ -0,0 +1,16 @@ +package com.macro.mall.dto; + +import com.macro.mall.model.UmsMenu; +import lombok.Getter; +import lombok.Setter; + +import java.util.List; + +/** + * Created by macro on 2020/2/4. + */ +@Getter +@Setter +public class UmsMenuNode extends UmsMenu { + private List children; +} diff --git a/mall-admin/src/main/java/com/macro/mall/service/UmsAdminService.java b/mall-admin/src/main/java/com/macro/mall/service/UmsAdminService.java index ebf0cd4..fc241f6 100644 --- a/mall-admin/src/main/java/com/macro/mall/service/UmsAdminService.java +++ b/mall-admin/src/main/java/com/macro/mall/service/UmsAdminService.java @@ -4,6 +4,7 @@ import com.macro.mall.dto.UmsAdminParam; import com.macro.mall.dto.UpdateAdminPasswordParam; import com.macro.mall.model.UmsAdmin; import com.macro.mall.model.UmsPermission; +import com.macro.mall.model.UmsResource; import com.macro.mall.model.UmsRole; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.transaction.annotation.Transactional; @@ -47,7 +48,7 @@ public interface UmsAdminService { /** * 根据用户名或昵称分页查询用户 */ - List list(String name, Integer pageSize, Integer pageNum); + List list(String keyword, Integer pageSize, Integer pageNum); /** * 修改指定用户信息 @@ -70,6 +71,11 @@ public interface UmsAdminService { */ List getRoleList(Long adminId); + /** + * 获取指定用户的可访问资源 + */ + List getResourceList(Long adminId); + /** * 修改用户的+-权限 */ diff --git a/mall-admin/src/main/java/com/macro/mall/service/UmsMenuService.java b/mall-admin/src/main/java/com/macro/mall/service/UmsMenuService.java new file mode 100644 index 0000000..2863d9d --- /dev/null +++ b/mall-admin/src/main/java/com/macro/mall/service/UmsMenuService.java @@ -0,0 +1,47 @@ +package com.macro.mall.service; + +import com.macro.mall.dto.UmsMenuNode; +import com.macro.mall.model.UmsMenu; + +import java.util.List; + +/** + * 后台菜单管理Service + * Created by macro on 2020/2/2. + */ +public interface UmsMenuService { + /** + * 创建后台菜单 + */ + int create(UmsMenu umsMenu); + + /** + * 修改后台菜单 + */ + int update(Long id, UmsMenu umsMenu); + + /** + * 根据ID获取菜单详情 + */ + UmsMenu getItem(Long id); + + /** + * 根据ID删除菜单 + */ + int delete(Long id); + + /** + * 分页查询后台菜单 + */ + List list(Long parentId, Integer pageSize, Integer pageNum); + + /** + * 树形结构返回所有菜单列表 + */ + List treeList(); + + /** + * 修改菜单显示状态 + */ + int updateHidden(Long id, Integer hidden); +} diff --git a/mall-admin/src/main/java/com/macro/mall/service/UmsResourceCategoryService.java b/mall-admin/src/main/java/com/macro/mall/service/UmsResourceCategoryService.java new file mode 100644 index 0000000..fc29928 --- /dev/null +++ b/mall-admin/src/main/java/com/macro/mall/service/UmsResourceCategoryService.java @@ -0,0 +1,19 @@ +package com.macro.mall.service; + +import com.macro.mall.model.UmsResourceCategory; + +import java.util.List; + +/** + * 后台资源分类管理Service + * Created by macro on 2020/2/5. + */ +public interface UmsResourceCategoryService { + List listAll(); + + int create(UmsResourceCategory umsResourceCategory); + + int update(Long id, UmsResourceCategory umsResourceCategory); + + int delete(Long id); +} diff --git a/mall-admin/src/main/java/com/macro/mall/service/UmsResourceService.java b/mall-admin/src/main/java/com/macro/mall/service/UmsResourceService.java new file mode 100644 index 0000000..e6b3fea --- /dev/null +++ b/mall-admin/src/main/java/com/macro/mall/service/UmsResourceService.java @@ -0,0 +1,23 @@ +package com.macro.mall.service; + +import com.macro.mall.model.UmsResource; + +import java.util.List; + +/** + * 后台资源管理Service + * Created by macro on 2020/2/2. + */ +public interface UmsResourceService { + int create(UmsResource umsResource); + + int update(Long id, UmsResource umsResource); + + UmsResource getItem(Long id); + + int delete(Long id); + + List list(Long categoryId, String nameKeyword, String urlKeyword, Integer pageSize, Integer pageNum); + + List listAll(); +} diff --git a/mall-admin/src/main/java/com/macro/mall/service/UmsRoleService.java b/mall-admin/src/main/java/com/macro/mall/service/UmsRoleService.java index d90e72d..c157a11 100644 --- a/mall-admin/src/main/java/com/macro/mall/service/UmsRoleService.java +++ b/mall-admin/src/main/java/com/macro/mall/service/UmsRoleService.java @@ -1,6 +1,8 @@ package com.macro.mall.service; +import com.macro.mall.model.UmsMenu; import com.macro.mall.model.UmsPermission; +import com.macro.mall.model.UmsResource; import com.macro.mall.model.UmsRole; import org.springframework.transaction.annotation.Transactional; @@ -38,7 +40,39 @@ public interface UmsRoleService { int updatePermission(Long roleId, List permissionIds); /** - * 获取角色列表 + * 获取所有角色列表 */ List list(); + + /** + * 分页获取角色列表 + */ + List list(String keyword, Integer pageSize, Integer pageNum); + + /** + * 根据管理员ID获取对应菜单 + */ + List getMenuList(Long adminId); + + /** + * 获取角色相关菜单 + */ + List listMenu(Long roleId); + + /** + * 获取角色相关资源 + */ + List listResource(Long roleId); + + /** + * 给角色分配菜单 + */ + @Transactional + int allocMenu(Long roleId, List menuIds); + + /** + * 给角色分配资源 + */ + @Transactional + int allocResource(Long roleId, List resourceIds); } diff --git a/mall-admin/src/main/java/com/macro/mall/service/impl/UmsAdminServiceImpl.java b/mall-admin/src/main/java/com/macro/mall/service/impl/UmsAdminServiceImpl.java index eeb0613..484a8f0 100644 --- a/mall-admin/src/main/java/com/macro/mall/service/impl/UmsAdminServiceImpl.java +++ b/mall-admin/src/main/java/com/macro/mall/service/impl/UmsAdminServiceImpl.java @@ -150,13 +150,13 @@ public class UmsAdminServiceImpl implements UmsAdminService { } @Override - public List list(String name, Integer pageSize, Integer pageNum) { + public List list(String keyword, 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 + "%")); + if (!StringUtils.isEmpty(keyword)) { + criteria.andUsernameLike("%" + keyword + "%"); + example.or(example.createCriteria().andNickNameLike("%" + keyword + "%")); } return adminMapper.selectByExample(example); } @@ -164,8 +164,18 @@ public class UmsAdminServiceImpl implements UmsAdminService { @Override public int update(Long id, UmsAdmin admin) { admin.setId(id); - //密码已经加密处理,需要单独修改 - admin.setPassword(null); + UmsAdmin rawAdmin = adminMapper.selectByPrimaryKey(id); + if(rawAdmin.getPassword().equals(admin.getPassword())){ + //与原加密密码相同的不需要修改 + admin.setPassword(null); + }else{ + //与原加密密码不同的需要加密修改 + if(StrUtil.isEmpty(admin.getPassword())){ + admin.setPassword(null); + }else{ + admin.setPassword(passwordEncoder.encode(admin.getPassword())); + } + } return adminMapper.updateByPrimaryKeySelective(admin); } @@ -200,6 +210,11 @@ public class UmsAdminServiceImpl implements UmsAdminService { return adminRoleRelationDao.getRoleList(adminId); } + @Override + public List getResourceList(Long adminId) { + return adminRoleRelationDao.getResourceList(adminId); + } + @Override public int updatePermission(Long adminId, List permissionIds) { //删除原所有权限关系 @@ -269,8 +284,8 @@ public class UmsAdminServiceImpl implements UmsAdminService { //获取用户信息 UmsAdmin admin = getAdminByUsername(username); if (admin != null) { - List permissionList = getPermissionList(admin.getId()); - return new AdminUserDetails(admin,permissionList); + List resourceList = getResourceList(admin.getId()); + return new AdminUserDetails(admin,resourceList); } throw new UsernameNotFoundException("用户名或密码错误"); } diff --git a/mall-admin/src/main/java/com/macro/mall/service/impl/UmsMenuServiceImpl.java b/mall-admin/src/main/java/com/macro/mall/service/impl/UmsMenuServiceImpl.java new file mode 100644 index 0000000..ad27616 --- /dev/null +++ b/mall-admin/src/main/java/com/macro/mall/service/impl/UmsMenuServiceImpl.java @@ -0,0 +1,105 @@ +package com.macro.mall.service.impl; + +import com.github.pagehelper.PageHelper; +import com.macro.mall.dto.UmsMenuNode; +import com.macro.mall.mapper.UmsMenuMapper; +import com.macro.mall.model.*; +import com.macro.mall.service.UmsMenuService; +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 2020/2/2. + */ +@Service +public class UmsMenuServiceImpl implements UmsMenuService { + @Autowired + private UmsMenuMapper menuMapper; + + @Override + public int create(UmsMenu umsMenu) { + umsMenu.setCreateTime(new Date()); + updateLevel(umsMenu); + return menuMapper.insert(umsMenu); + } + + /** + * 修改菜单层级 + */ + private void updateLevel(UmsMenu umsMenu) { + if (umsMenu.getParentId() == 0) { + //没有父菜单时为一级菜单 + umsMenu.setLevel(0); + } else { + //有父菜单时选择根据父菜单level设置 + UmsMenu parentMenu = menuMapper.selectByPrimaryKey(umsMenu.getParentId()); + if (parentMenu != null) { + umsMenu.setLevel(parentMenu.getLevel() + 1); + } else { + umsMenu.setLevel(0); + } + } + } + + @Override + public int update(Long id, UmsMenu umsMenu) { + umsMenu.setId(id); + updateLevel(umsMenu); + return menuMapper.updateByPrimaryKeySelective(umsMenu); + } + + @Override + public UmsMenu getItem(Long id) { + return menuMapper.selectByPrimaryKey(id); + } + + @Override + public int delete(Long id) { + return menuMapper.deleteByPrimaryKey(id); + } + + @Override + public List list(Long parentId, Integer pageSize, Integer pageNum) { + PageHelper.startPage(pageNum, pageSize); + UmsMenuExample example = new UmsMenuExample(); + example.setOrderByClause("sort desc"); + example.createCriteria().andParentIdEqualTo(parentId); + return menuMapper.selectByExample(example); + } + + @Override + public List treeList() { + List menuList = menuMapper.selectByExample(new UmsMenuExample()); + List result = menuList.stream() + .filter(menu -> menu.getParentId().equals(0L)) + .map(menu -> covertMenuNode(menu, menuList)).collect(Collectors.toList()); + return result; + } + + @Override + public int updateHidden(Long id, Integer hidden) { + UmsMenu umsMenu = new UmsMenu(); + umsMenu.setId(id); + umsMenu.setHidden(hidden); + return menuMapper.updateByPrimaryKeySelective(umsMenu); + } + + /** + * 将UmsMenu转化为UmsMenuNode并设置children属性 + */ + private UmsMenuNode covertMenuNode(UmsMenu menu, List menuList) { + UmsMenuNode node = new UmsMenuNode(); + BeanUtils.copyProperties(menu, node); + List children = menuList.stream() + .filter(subMenu -> subMenu.getParentId().equals(menu.getId())) + .map(subMenu -> covertMenuNode(subMenu, menuList)).collect(Collectors.toList()); + node.setChildren(children); + return node; + } +} diff --git a/mall-admin/src/main/java/com/macro/mall/service/impl/UmsResourceCategoryServiceImpl.java b/mall-admin/src/main/java/com/macro/mall/service/impl/UmsResourceCategoryServiceImpl.java new file mode 100644 index 0000000..c6e5938 --- /dev/null +++ b/mall-admin/src/main/java/com/macro/mall/service/impl/UmsResourceCategoryServiceImpl.java @@ -0,0 +1,45 @@ +package com.macro.mall.service.impl; + +import com.macro.mall.mapper.UmsResourceCategoryMapper; +import com.macro.mall.model.UmsResourceCategory; +import com.macro.mall.model.UmsResourceCategoryExample; +import com.macro.mall.service.UmsResourceCategoryService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.Date; +import java.util.List; + +/** + * 后台资源分类管理Service实现类 + * Created by macro on 2020/2/5. + */ +@Service +public class UmsResourceCategoryServiceImpl implements UmsResourceCategoryService { + @Autowired + private UmsResourceCategoryMapper resourceCategoryMapper; + + @Override + public List listAll() { + UmsResourceCategoryExample example = new UmsResourceCategoryExample(); + example.setOrderByClause("sort desc"); + return resourceCategoryMapper.selectByExample(example); + } + + @Override + public int create(UmsResourceCategory umsResourceCategory) { + umsResourceCategory.setCreateTime(new Date()); + return resourceCategoryMapper.insert(umsResourceCategory); + } + + @Override + public int update(Long id, UmsResourceCategory umsResourceCategory) { + umsResourceCategory.setId(id); + return resourceCategoryMapper.updateByPrimaryKeySelective(umsResourceCategory); + } + + @Override + public int delete(Long id) { + return resourceCategoryMapper.deleteByPrimaryKey(id); + } +} diff --git a/mall-admin/src/main/java/com/macro/mall/service/impl/UmsResourceServiceImpl.java b/mall-admin/src/main/java/com/macro/mall/service/impl/UmsResourceServiceImpl.java new file mode 100644 index 0000000..4d866c0 --- /dev/null +++ b/mall-admin/src/main/java/com/macro/mall/service/impl/UmsResourceServiceImpl.java @@ -0,0 +1,66 @@ +package com.macro.mall.service.impl; + +import cn.hutool.core.util.StrUtil; +import com.github.pagehelper.PageHelper; +import com.macro.mall.mapper.UmsResourceMapper; +import com.macro.mall.model.UmsResource; +import com.macro.mall.model.UmsResourceExample; +import com.macro.mall.service.UmsResourceService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.Date; +import java.util.List; + +/** + * 后台资源管理Service实现类 + * Created by macro on 2020/2/2. + */ +@Service +public class UmsResourceServiceImpl implements UmsResourceService { + @Autowired + private UmsResourceMapper resourceMapper; + @Override + public int create(UmsResource umsResource) { + umsResource.setCreateTime(new Date()); + return resourceMapper.insert(umsResource); + } + + @Override + public int update(Long id, UmsResource umsResource) { + umsResource.setId(id); + return resourceMapper.updateByPrimaryKeySelective(umsResource); + } + + @Override + public UmsResource getItem(Long id) { + return resourceMapper.selectByPrimaryKey(id); + } + + @Override + public int delete(Long id) { + return resourceMapper.deleteByPrimaryKey(id); + } + + @Override + public List list(Long categoryId, String nameKeyword, String urlKeyword, Integer pageSize, Integer pageNum) { + PageHelper.startPage(pageNum,pageSize); + UmsResourceExample example = new UmsResourceExample(); + UmsResourceExample.Criteria criteria = example.createCriteria(); + if(categoryId!=null){ + criteria.andCategoryIdEqualTo(categoryId); + } + if(StrUtil.isNotEmpty(nameKeyword)){ + criteria.andNameLike('%'+nameKeyword+'%'); + } + if(StrUtil.isNotEmpty(urlKeyword)){ + criteria.andUrlLike('%'+urlKeyword+'%'); + } + return resourceMapper.selectByExample(example); + } + + @Override + public List listAll() { + return resourceMapper.selectByExample(new UmsResourceExample()); + } +} diff --git a/mall-admin/src/main/java/com/macro/mall/service/impl/UmsRoleServiceImpl.java b/mall-admin/src/main/java/com/macro/mall/service/impl/UmsRoleServiceImpl.java index 2de0019..ddd9230 100644 --- a/mall-admin/src/main/java/com/macro/mall/service/impl/UmsRoleServiceImpl.java +++ b/mall-admin/src/main/java/com/macro/mall/service/impl/UmsRoleServiceImpl.java @@ -1,12 +1,17 @@ package com.macro.mall.service.impl; +import com.github.pagehelper.PageHelper; +import com.macro.mall.dao.UmsRoleDao; import com.macro.mall.dao.UmsRolePermissionRelationDao; import com.macro.mall.mapper.UmsRoleMapper; +import com.macro.mall.mapper.UmsRoleMenuRelationMapper; import com.macro.mall.mapper.UmsRolePermissionRelationMapper; +import com.macro.mall.mapper.UmsRoleResourceRelationMapper; import com.macro.mall.model.*; import com.macro.mall.service.UmsRoleService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; import java.util.ArrayList; import java.util.Date; @@ -23,11 +28,16 @@ public class UmsRoleServiceImpl implements UmsRoleService { @Autowired private UmsRolePermissionRelationMapper rolePermissionRelationMapper; @Autowired + private UmsRoleMenuRelationMapper roleMenuRelationMapper; + @Autowired + private UmsRoleResourceRelationMapper roleResourceRelationMapper; + @Autowired private UmsRolePermissionRelationDao rolePermissionRelationDao; + @Autowired + private UmsRoleDao roleDao; @Override public int create(UmsRole role) { role.setCreateTime(new Date()); - role.setStatus(1); role.setAdminCount(0); role.setSort(0); return roleMapper.insert(role); @@ -36,7 +46,7 @@ public class UmsRoleServiceImpl implements UmsRoleService { @Override public int update(Long id, UmsRole role) { role.setId(id); - return roleMapper.updateByPrimaryKey(role); + return roleMapper.updateByPrimaryKeySelective(role); } @Override @@ -72,4 +82,61 @@ public class UmsRoleServiceImpl implements UmsRoleService { public List list() { return roleMapper.selectByExample(new UmsRoleExample()); } + + @Override + public List list(String keyword, Integer pageSize, Integer pageNum) { + PageHelper.startPage(pageNum, pageSize); + UmsRoleExample example = new UmsRoleExample(); + if (!StringUtils.isEmpty(keyword)) { + example.createCriteria().andNameLike("%" + keyword + "%"); + } + return roleMapper.selectByExample(example); + } + + @Override + public List getMenuList(Long adminId) { + return roleDao.getMenuList(adminId); + } + + @Override + public List listMenu(Long roleId) { + return roleDao.getMenuListByRoleId(roleId); + } + + @Override + public List listResource(Long roleId) { + return roleDao.getResourceListByRoleId(roleId); + } + + @Override + public int allocMenu(Long roleId, List menuIds) { + //先删除原有关系 + UmsRoleMenuRelationExample example=new UmsRoleMenuRelationExample(); + example.createCriteria().andRoleIdEqualTo(roleId); + roleMenuRelationMapper.deleteByExample(example); + //批量插入新关系 + for (Long menuId : menuIds) { + UmsRoleMenuRelation relation = new UmsRoleMenuRelation(); + relation.setRoleId(roleId); + relation.setMenuId(menuId); + roleMenuRelationMapper.insert(relation); + } + return menuIds.size(); + } + + @Override + public int allocResource(Long roleId, List resourceIds) { + //先删除原有关系 + UmsRoleResourceRelationExample example=new UmsRoleResourceRelationExample(); + example.createCriteria().andRoleIdEqualTo(roleId); + roleResourceRelationMapper.deleteByExample(example); + //批量插入新关系 + for (Long resourceId : resourceIds) { + UmsRoleResourceRelation relation = new UmsRoleResourceRelation(); + relation.setRoleId(roleId); + relation.setResourceId(resourceId); + roleResourceRelationMapper.insert(relation); + } + return resourceIds.size(); + } } diff --git a/mall-admin/src/main/resources/application.yml b/mall-admin/src/main/resources/application.yml index d020067..940389a 100644 --- a/mall-admin/src/main/resources/application.yml +++ b/mall-admin/src/main/resources/application.yml @@ -33,6 +33,8 @@ secure: - /druid/** - /admin/login - /admin/register + - /admin/info + - /admin/logout - /minio/upload aliyun: diff --git a/mall-admin/src/main/resources/dao/UmsAdminRoleRelationDao.xml b/mall-admin/src/main/resources/dao/UmsAdminRoleRelationDao.xml index 05c94c8..6b1ec7d 100644 --- a/mall-admin/src/main/resources/dao/UmsAdminRoleRelationDao.xml +++ b/mall-admin/src/main/resources/dao/UmsAdminRoleRelationDao.xml @@ -52,4 +52,23 @@ pr.type = 1 AND pr.admin_id = #{adminId} + \ No newline at end of file diff --git a/mall-admin/src/main/resources/dao/UmsRoleDao.xml b/mall-admin/src/main/resources/dao/UmsRoleDao.xml new file mode 100644 index 0000000..5089889 --- /dev/null +++ b/mall-admin/src/main/resources/dao/UmsRoleDao.xml @@ -0,0 +1,64 @@ + + + + + + + + \ No newline at end of file