Commit 48b5f522 authored by 张明杰's avatar 张明杰

Merge remote-tracking branch 'origin/developer' into developer

parents 4d8149f0 10957a02
Pipeline #216 canceled with stages
...@@ -6,17 +6,15 @@ import com.restful.common.Constants; ...@@ -6,17 +6,15 @@ import com.restful.common.Constants;
import com.restful.common.entity.ServiceException; import com.restful.common.entity.ServiceException;
import com.restful.domain.Menu; import com.restful.domain.Menu;
import com.restful.domain.Role; import com.restful.domain.Role;
import com.restful.dto.Authority;
import com.restful.service.JurisdictionService; import com.restful.service.JurisdictionService;
import com.restful.subject.Operation;
import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.util.List; import java.util.List;
...@@ -24,7 +22,7 @@ import java.util.List; ...@@ -24,7 +22,7 @@ import java.util.List;
@RequestMapping("jurisdiction") @RequestMapping("jurisdiction")
public class JurisdictionController { public class JurisdictionController {
@Autowired @Resource
JurisdictionService jurisdictionService; JurisdictionService jurisdictionService;
@ApiOperation(value = "查看所有菜单", produces = MediaType.APPLICATION_JSON_VALUE) @ApiOperation(value = "查看所有菜单", produces = MediaType.APPLICATION_JSON_VALUE)
...@@ -33,7 +31,7 @@ public class JurisdictionController { ...@@ -33,7 +31,7 @@ public class JurisdictionController {
@ApiImplicitParam(name = Constants.PAGE_NUM, value = "页码,从1开始, 默认第一页", paramType = "query", dataType = "int"), @ApiImplicitParam(name = Constants.PAGE_NUM, value = "页码,从1开始, 默认第一页", paramType = "query", dataType = "int"),
@ApiImplicitParam(name = Constants.PAGE_SIZE, value = "每页的数量", paramType = "query", dataType = "int") @ApiImplicitParam(name = Constants.PAGE_SIZE, value = "每页的数量", paramType = "query", dataType = "int")
}) })
@RequestMapping(value = "/findMenus", method = RequestMethod.GET) @RequestMapping(value = "/menus", method = RequestMethod.GET)
public Object findMenus(HttpServletRequest request, public Object findMenus(HttpServletRequest request,
@RequestParam(required = false) String remark, @RequestParam(required = false) String remark,
@RequestParam(required = false) String url) { @RequestParam(required = false) String url) {
...@@ -53,7 +51,7 @@ public class JurisdictionController { ...@@ -53,7 +51,7 @@ public class JurisdictionController {
@ApiImplicitParam(name = Constants.PAGE_NUM, value = "页码,从1开始, 默认第一页", paramType = "query", dataType = "int"), @ApiImplicitParam(name = Constants.PAGE_NUM, value = "页码,从1开始, 默认第一页", paramType = "query", dataType = "int"),
@ApiImplicitParam(name = Constants.PAGE_SIZE, value = "每页的数量", paramType = "query", dataType = "int") @ApiImplicitParam(name = Constants.PAGE_SIZE, value = "每页的数量", paramType = "query", dataType = "int")
}) })
@RequestMapping(value = "/findRoles", method = RequestMethod.GET) @RequestMapping(value = "/roles", method = RequestMethod.GET)
public Object findRoles(HttpServletRequest request) throws ServiceException { public Object findRoles(HttpServletRequest request) throws ServiceException {
Integer pageNum = (Integer) request.getAttribute(Constants.PAGE_NUM); Integer pageNum = (Integer) request.getAttribute(Constants.PAGE_NUM);
Integer pageSize = (Integer) request.getAttribute(Constants.PAGE_SIZE); Integer pageSize = (Integer) request.getAttribute(Constants.PAGE_SIZE);
...@@ -64,27 +62,23 @@ public class JurisdictionController { ...@@ -64,27 +62,23 @@ public class JurisdictionController {
@ApiOperation(value = "给角色授权", produces = MediaType.APPLICATION_JSON_VALUE) @ApiOperation(value = "给角色授权", produces = MediaType.APPLICATION_JSON_VALUE)
@ApiImplicitParam(name = Constants.ACCESS_TOKEN, required = true, paramType = "header", dataType = "String") @ApiImplicitParam(name = Constants.ACCESS_TOKEN, required = true, paramType = "header", dataType = "String")
@RequestMapping(value = "/addAuthority", method = RequestMethod.POST) @RequestMapping(value = "/authority/{roleId}/{menuIds}", method = RequestMethod.POST)
public Object addAuthority(@RequestBody Authority authority) throws ServiceException { public Object addAuthority(@PathVariable Long roleId, @PathVariable String menuIds) throws ServiceException {
if (!StringUtils.isBlank(authority.menus)) return jurisdictionService.authority(roleId, menuIds, Operation.add);
authority.menuIds = (Long[]) ConvertUtils.convert(authority.menus.split(","), Long.class);
return jurisdictionService.authority(authority.roleId, authority.menuIds, "add");
} }
@ApiOperation(value = "取消授权", produces = MediaType.APPLICATION_JSON_VALUE) @ApiOperation(value = "取消授权", produces = MediaType.APPLICATION_JSON_VALUE)
@ApiImplicitParam(name = Constants.ACCESS_TOKEN, required = true, paramType = "header", dataType = "String") @ApiImplicitParam(name = Constants.ACCESS_TOKEN, required = true, paramType = "header", dataType = "String")
@RequestMapping(value = "/delAuthority", method = RequestMethod.POST) @RequestMapping(value = "/authority/{roleId}/{menuIds}", method = RequestMethod.DELETE)
public Object delAuthority(@RequestBody Authority authority) throws ServiceException { public Object delAuthority(@PathVariable Long roleId, @PathVariable String menuIds) throws ServiceException {
if (!StringUtils.isBlank(authority.menus)) return jurisdictionService.authority(roleId, menuIds, Operation.del);
authority.menuIds = (Long[]) ConvertUtils.convert(authority.menus.split(","), Long.class);
return jurisdictionService.authority(authority.roleId, authority.menuIds, "del");
} }
@ApiOperation(value = "给用户赋予角色", produces = MediaType.APPLICATION_JSON_VALUE) @ApiOperation(value = "给用户赋予角色", produces = MediaType.APPLICATION_JSON_VALUE)
@ApiImplicitParam(name = Constants.ACCESS_TOKEN, required = true, paramType = "header", dataType = "String") @ApiImplicitParam(name = Constants.ACCESS_TOKEN, required = true, paramType = "header", dataType = "String")
@RequestMapping(value = "/roleToUser", method = RequestMethod.POST) @RequestMapping(value = "/role/{roleIds}/user/{userId}", method = RequestMethod.POST)
public Object roleToUser(@RequestBody Authority authority) throws ServiceException { public Object roleToUser(@PathVariable String roleIds, @PathVariable Long userId) throws ServiceException {
jurisdictionService.roleToUser(authority.roleId, authority.userId); jurisdictionService.roleToUser(roleIds, userId);
return null; return null;
} }
} }
...@@ -10,16 +10,16 @@ public interface JurisdictionDao { ...@@ -10,16 +10,16 @@ public interface JurisdictionDao {
List<Menu> findMenus(Menu menu); List<Menu> findMenus(Menu menu);
Menu getMenu(Long id); Menu getMenu(Long[] id);
List<Role> findRoles(); List<Role> findRoles();
Role getRole(Long id); Role getRole(Long id);
void addAuthority(@Param("roleId") Long roleId, @Param("menuId") Long menuId); void addAuthority(@Param("roleId") Long roleId, @Param("menuId") Long[] menuId);
void delAuthority(@Param("roleId") Long roleId, @Param("menuId") Long menuId); void delAuthority(@Param("roleId") Long roleId, @Param("menuId") Long[] menuId);
void roleToUser(@Param("roleId") Long roleId, @Param("userId") Long userId); void roleToUser(@Param("roleId") Long[] roleId, @Param("userId") Long userId);
} }
...@@ -5,7 +5,9 @@ import com.restful.common.entity.ServiceException; ...@@ -5,7 +5,9 @@ import com.restful.common.entity.ServiceException;
import com.restful.dao.JurisdictionDao; import com.restful.dao.JurisdictionDao;
import com.restful.domain.Menu; import com.restful.domain.Menu;
import com.restful.domain.Role; import com.restful.domain.Role;
import org.springframework.beans.factory.annotation.Autowired; import com.restful.subject.Operation;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Cacheable;
import org.springframework.dao.DuplicateKeyException; import org.springframework.dao.DuplicateKeyException;
...@@ -21,8 +23,6 @@ public class JurisdictionService { ...@@ -21,8 +23,6 @@ public class JurisdictionService {
@Resource @Resource
JurisdictionDao jurisdictionDao; JurisdictionDao jurisdictionDao;
@Autowired
UserService userService;
public List<Menu> findMenus(Menu menu) { public List<Menu> findMenus(Menu menu) {
return jurisdictionDao.findMenus(menu); return jurisdictionDao.findMenus(menu);
...@@ -38,45 +38,37 @@ public class JurisdictionService { ...@@ -38,45 +38,37 @@ public class JurisdictionService {
} }
@CachePut(value = Constants.JURISDICTION, key = "#roleId", unless = "#result==null") @CachePut(value = Constants.JURISDICTION, key = "#roleId", unless = "#result==null")
public Role authority(Long roleId, Long[] menuIds, String operation) throws ServiceException { public Role authority(Long roleId, String menuIds, Operation operation) throws ServiceException {
if (roleId == null) if (roleId == null)
throw new ServiceException("角色id不能为空"); throw new ServiceException("角色id不能为空");
Role role = jurisdictionDao.getRole(roleId); Role role = jurisdictionDao.getRole(roleId);
if (role == null) if (role == null)
throw new ServiceException("角色不存在"); throw new ServiceException("角色不存在");
for (Long menuId : menuIds) { Long[] menuIdArr = (Long[]) ConvertUtils.convert(menuIds.split(","), Long.class);
Menu menu = jurisdictionDao.getMenu(menuId); Menu menu = jurisdictionDao.getMenu(menuIdArr);
if (menu == null) if (Operation.add == operation) {
throw new ServiceException("菜单不存在"); try {
switch (operation) { jurisdictionDao.addAuthority(roleId, menuIdArr);
case "add": { } catch (DuplicateKeyException e) {
try { throw new ServiceException("已经存在该权限");
jurisdictionDao.addAuthority(roleId, menuId);
} catch (DuplicateKeyException e) {
throw new ServiceException("已经存在该权限");
}
role.getMenuList().add(menu);
break;
}
case "del": {
jurisdictionDao.delAuthority(roleId, menuId);
role.getMenuList().removeIf(e -> menu.getId().equals(e.getId()));
break;
}
} }
role.getMenuList().add(menu);
} else if (Operation.del == operation) {
jurisdictionDao.delAuthority(roleId, menuIdArr);
role.getMenuList().removeIf(e -> menu.getId().equals(e.getId()));
} }
return role; return role;
} }
public void roleToUser(Long roleId, Long userId) throws ServiceException { public void roleToUser(String roleIds, Long userId) throws ServiceException {
if (roleId == null) { if (StringUtils.isBlank(roleIds)) {
throw new ServiceException("角色id不能为空"); throw new ServiceException("角色id不能为空");
} }
if (userId == null) { if (userId == null) {
throw new ServiceException("用户id不能为空"); throw new ServiceException("用户id不能为空");
} }
try { try {
jurisdictionDao.roleToUser(roleId, userId); jurisdictionDao.roleToUser((Long[]) ConvertUtils.convert(roleIds.split(","), Long.class), userId);
} catch (DuplicateKeyException e) { } catch (DuplicateKeyException e) {
throw new ServiceException("已经存在"); throw new ServiceException("已经存在");
} }
......
package com.restful.subject;
public enum Operation {
add, del
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment