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;
import com.restful.common.entity.ServiceException;
import com.restful.domain.Menu;
import com.restful.domain.Role;
import com.restful.dto.Authority;
import com.restful.service.JurisdictionService;
import com.restful.subject.Operation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
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.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
......@@ -24,7 +22,7 @@ import java.util.List;
@RequestMapping("jurisdiction")
public class JurisdictionController {
@Autowired
@Resource
JurisdictionService jurisdictionService;
@ApiOperation(value = "查看所有菜单", produces = MediaType.APPLICATION_JSON_VALUE)
......@@ -33,7 +31,7 @@ public class JurisdictionController {
@ApiImplicitParam(name = Constants.PAGE_NUM, value = "页码,从1开始, 默认第一页", 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,
@RequestParam(required = false) String remark,
@RequestParam(required = false) String url) {
......@@ -53,7 +51,7 @@ public class JurisdictionController {
@ApiImplicitParam(name = Constants.PAGE_NUM, value = "页码,从1开始, 默认第一页", 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 {
Integer pageNum = (Integer) request.getAttribute(Constants.PAGE_NUM);
Integer pageSize = (Integer) request.getAttribute(Constants.PAGE_SIZE);
......@@ -64,27 +62,23 @@ public class JurisdictionController {
@ApiOperation(value = "给角色授权", produces = MediaType.APPLICATION_JSON_VALUE)
@ApiImplicitParam(name = Constants.ACCESS_TOKEN, required = true, paramType = "header", dataType = "String")
@RequestMapping(value = "/addAuthority", method = RequestMethod.POST)
public Object addAuthority(@RequestBody Authority authority) throws ServiceException {
if (!StringUtils.isBlank(authority.menus))
authority.menuIds = (Long[]) ConvertUtils.convert(authority.menus.split(","), Long.class);
return jurisdictionService.authority(authority.roleId, authority.menuIds, "add");
@RequestMapping(value = "/authority/{roleId}/{menuIds}", method = RequestMethod.POST)
public Object addAuthority(@PathVariable Long roleId, @PathVariable String menuIds) throws ServiceException {
return jurisdictionService.authority(roleId, menuIds, Operation.add);
}
@ApiOperation(value = "取消授权", produces = MediaType.APPLICATION_JSON_VALUE)
@ApiImplicitParam(name = Constants.ACCESS_TOKEN, required = true, paramType = "header", dataType = "String")
@RequestMapping(value = "/delAuthority", method = RequestMethod.POST)
public Object delAuthority(@RequestBody Authority authority) throws ServiceException {
if (!StringUtils.isBlank(authority.menus))
authority.menuIds = (Long[]) ConvertUtils.convert(authority.menus.split(","), Long.class);
return jurisdictionService.authority(authority.roleId, authority.menuIds, "del");
@RequestMapping(value = "/authority/{roleId}/{menuIds}", method = RequestMethod.DELETE)
public Object delAuthority(@PathVariable Long roleId, @PathVariable String menuIds) throws ServiceException {
return jurisdictionService.authority(roleId, menuIds, Operation.del);
}
@ApiOperation(value = "给用户赋予角色", produces = MediaType.APPLICATION_JSON_VALUE)
@ApiImplicitParam(name = Constants.ACCESS_TOKEN, required = true, paramType = "header", dataType = "String")
@RequestMapping(value = "/roleToUser", method = RequestMethod.POST)
public Object roleToUser(@RequestBody Authority authority) throws ServiceException {
jurisdictionService.roleToUser(authority.roleId, authority.userId);
@RequestMapping(value = "/role/{roleIds}/user/{userId}", method = RequestMethod.POST)
public Object roleToUser(@PathVariable String roleIds, @PathVariable Long userId) throws ServiceException {
jurisdictionService.roleToUser(roleIds, userId);
return null;
}
}
......@@ -10,16 +10,16 @@ public interface JurisdictionDao {
List<Menu> findMenus(Menu menu);
Menu getMenu(Long id);
Menu getMenu(Long[] id);
List<Role> findRoles();
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;
import com.restful.dao.JurisdictionDao;
import com.restful.domain.Menu;
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.Cacheable;
import org.springframework.dao.DuplicateKeyException;
......@@ -21,8 +23,6 @@ public class JurisdictionService {
@Resource
JurisdictionDao jurisdictionDao;
@Autowired
UserService userService;
public List<Menu> findMenus(Menu menu) {
return jurisdictionDao.findMenus(menu);
......@@ -38,45 +38,37 @@ public class JurisdictionService {
}
@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)
throw new ServiceException("角色id不能为空");
Role role = jurisdictionDao.getRole(roleId);
if (role == null)
throw new ServiceException("角色不存在");
for (Long menuId : menuIds) {
Menu menu = jurisdictionDao.getMenu(menuId);
if (menu == null)
throw new ServiceException("菜单不存在");
switch (operation) {
case "add": {
try {
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;
}
Long[] menuIdArr = (Long[]) ConvertUtils.convert(menuIds.split(","), Long.class);
Menu menu = jurisdictionDao.getMenu(menuIdArr);
if (Operation.add == operation) {
try {
jurisdictionDao.addAuthority(roleId, menuIdArr);
} catch (DuplicateKeyException e) {
throw new ServiceException("已经存在该权限");
}
role.getMenuList().add(menu);
} else if (Operation.del == operation) {
jurisdictionDao.delAuthority(roleId, menuIdArr);
role.getMenuList().removeIf(e -> menu.getId().equals(e.getId()));
}
return role;
}
public void roleToUser(Long roleId, Long userId) throws ServiceException {
if (roleId == null) {
public void roleToUser(String roleIds, Long userId) throws ServiceException {
if (StringUtils.isBlank(roleIds)) {
throw new ServiceException("角色id不能为空");
}
if (userId == null) {
throw new ServiceException("用户id不能为空");
}
try {
jurisdictionDao.roleToUser(roleId, userId);
jurisdictionDao.roleToUser((Long[]) ConvertUtils.convert(roleIds.split(","), Long.class), userId);
} catch (DuplicateKeyException e) {
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