package com.landtool.lanbase.modules.sys.controller;
|
|
import java.util.ArrayList;
|
import java.util.Iterator;
|
import java.util.List;
|
import java.util.Map;
|
|
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONObject;
|
import com.landtool.lanbase.common.annotation.LogAction;
|
import com.landtool.lanbase.common.utils.*;
|
import com.landtool.lanbase.modules.sys.service.RoleResCatalogService;
|
import com.landtool.lanbase.modules.sys.service.SysRoleGroupService;
|
import com.landtool.lanbase.modules.sys.service.SysRoleResourceService;
|
import io.swagger.annotations.Api;
|
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiParam;
|
import org.apache.shiro.authz.annotation.Logical;
|
import org.apache.shiro.authz.annotation.RequiresPermissions;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.web.bind.annotation.*;
|
import com.landtool.lanbase.modules.sys.entity.SysRole;
|
import com.landtool.lanbase.modules.sys.service.SysRoleService;
|
|
/**
|
* @Description: 角色模块
|
* @Author: zimao.guo
|
* @Date: 9:40 2018/1/23
|
*/
|
@RestController
|
@RequestMapping("/sys/role")
|
@Api(value = "", tags = {"角色管理"})
|
public class SysRoleController extends AbstractController{
|
|
@Autowired
|
private SysRoleService roleService;
|
@Autowired
|
private SysRoleGroupService sysRoleGroupService;
|
@Autowired
|
private SysRoleResourceService sysRoleResourceService;
|
@Autowired
|
private RoleResCatalogService roleResCatalogService;
|
|
/**
|
* @Description: 查询列表
|
* @Author: zimao.guo
|
* @Date: 9:41 2018/1/23
|
* @param params
|
* @return Result
|
* @see Result
|
*/
|
@LogAction("用户管理,角色管理,角色查询,查询")
|
@RequestMapping(value="/list", method ={RequestMethod.POST, RequestMethod.GET})
|
// @RequiresPermissions("sys:role:list")
|
@RequiresPermissions(value={"sys:role:list","sys:role:edit"},logical= Logical.OR)
|
@ApiOperation(
|
value = "角色列表",
|
notes = "所有角色列表"
|
)
|
public Result list(@RequestParam Map<String, Object> params){
|
//查询列表数据
|
Query query = new Query(params);
|
|
List<SysRole> roleList = roleService.queryList(query);
|
int total = roleService.queryTotal(query);
|
|
PageUtils pageUtil = new PageUtils(roleList, total, query.getLimit(), query.getPage());
|
|
return Result.ok().put("page", pageUtil);
|
}
|
|
|
/**
|
* @Description: 角色添加/更新界面的初始化
|
* @Author: zimao.guo
|
* @Date: 9:41 2018/1/23
|
* @param roleId
|
* @return role
|
* @return systemNames
|
* @return sysResources
|
* @return ownResources
|
* @see Result
|
*/
|
@GetMapping("/info")
|
// @RequiresPermissions("sys:role:list")
|
@RequiresPermissions(value={"sys:role:list","sys:role:edit"},logical= Logical.OR)
|
@ApiOperation(
|
value = "角色信息",
|
notes = ""
|
)
|
public Result info( @ApiParam(name="roleId",value="角色id",required=true) @RequestParam(name = "roleId") Long roleId ){
|
SysRole role = roleService.queryObject(roleId);
|
List<NameBindId> systemNames = roleService.findSystemNamesByKeyWord(null);
|
List<NameBindId> selectedResources = roleService.getOwnResources(roleId==null ? null : String.valueOf(roleId));
|
|
List<NameBindId> sysResource = null;
|
if(role.getAppId() != null) {
|
sysResource = roleService.getSystemResources(String.valueOf(role.getAppId()));
|
}else{
|
sysResource = new ArrayList<>();
|
}
|
|
return Result.ok().put("role", role)
|
.put("systemNames",systemNames)
|
.put("sysResources",sysResource)
|
.put("ownResources",selectedResources);
|
}
|
|
/**
|
* @Description: 保存数据(包括权限信息)
|
* @Author: zimao.guo
|
* @Date: 9:43 2018/1/23
|
* @param json
|
* @return Result
|
* @see Result
|
*/
|
@LogAction("用户管理,角色管理,角色新增,新增")
|
@PostMapping("/save")
|
@RequiresPermissions("sys:role:edit")
|
@ApiOperation(
|
value = "保存/添加角色",
|
notes = ""
|
)
|
public Result save( @ApiParam(name="复合对象",value="传入json格式",required=true)@RequestBody JSONObject json){
|
|
SysRole role = json.getObject("sysRole",SysRole.class);
|
List<Long> selectedResourceIds = TransformUtils.StringToLongList(JSONArray.toJavaObject(json.getJSONArray("resourceIds"), List.class));
|
List<Long> selectedCatalogIds = TransformUtils.StringToLongList(JSONArray.toJavaObject(json.getJSONArray("catalogIds"), List.class));
|
SysRole uniqueCheck = new SysRole(role.getRoleName());
|
if(roleService.getRoleWithIdByRole(uniqueCheck) != null) return Result.error("角色名已存在");
|
|
roleService.save(role);
|
SysRole sysRoleWithId = roleService.getRoleWithIdByRole(role);
|
sysRoleResourceService.saveBatch(sysRoleWithId.getRoleId(),selectedResourceIds.toArray(new Long[selectedResourceIds.size()]));
|
roleResCatalogService.saveBatch(sysRoleWithId.getRoleId(),selectedCatalogIds);
|
return Result.ok().put("roleid",sysRoleWithId.getRoleId());
|
}
|
|
/**
|
* @Description: 更新数据(包括权限信息)
|
* @Author: zimao.guo
|
* @Date: 9:44 2018/1/23
|
* @param json
|
* @return Result
|
* @see Result
|
*/
|
@LogAction("用户管理,角色管理,角色修改,修改")
|
@PostMapping("/update")
|
@RequiresPermissions("sys:role:edit")
|
@ApiOperation(
|
value = "修改角色",
|
notes = ""
|
)
|
public Result update(@ApiParam(name="复合对象",value="传入json格式",required=true)@RequestBody JSONObject json){
|
|
SysRole role = json.getObject("sysRole",SysRole.class);
|
List<Long> selectingResources = TransformUtils.StringToLongList(JSONArray.toJavaObject(json.getJSONArray("resourceIds"), List.class));
|
List<Long> selectedCatalogIds = TransformUtils.StringToLongList(JSONArray.toJavaObject(json.getJSONArray("catalogIds"), List.class));
|
// TransformUtils.StringToLongList(json.getObject("resourceIds",List.class));
|
List<NameBindId> selectedResources = roleService.getOwnResources(role.getRoleId()==null ? null : String.valueOf(role.getRoleId()));
|
List<Long> commonResourcesList = new ArrayList<>();
|
List<Long> removeResourcesList = new ArrayList<>();
|
Iterator<NameBindId> selectedResourcesIteraor = selectedResources.iterator();
|
|
while(selectedResourcesIteraor.hasNext()){
|
NameBindId nameBindId = selectedResourcesIteraor.next();
|
if(selectingResources.contains(nameBindId.getId())){
|
commonResourcesList.add(nameBindId.getId());
|
}else{
|
removeResourcesList.add(nameBindId.getId());
|
}
|
}
|
|
selectingResources.removeAll(commonResourcesList);
|
|
roleService.processChangForSystem(role);
|
sysRoleResourceService.deleteBatch(removeResourcesList.toArray(new Long[removeResourcesList.size()]),role.getRoleId());
|
sysRoleResourceService.saveBatch(role.getRoleId(),selectingResources.toArray(new Long[selectingResources.size()]));
|
roleResCatalogService.deleteBatch(role.getRoleId());
|
roleResCatalogService.saveBatch(role.getRoleId(),selectedCatalogIds);
|
roleService.update(role);
|
return Result.ok();
|
}
|
|
/**
|
* @Description: 删除数据
|
* @Author: zimao.guo
|
* @Date: 9:45 2018/1/23
|
* @param roleIds
|
* @return Result
|
* @see Result
|
*/
|
@LogAction("用户管理,角色管理,角色删除,删除")
|
@PostMapping("/delete")
|
@RequiresPermissions("sys:role:edit")
|
@ApiOperation(
|
value = "删除角色",
|
notes = ""
|
)
|
public Result delete( @ApiParam(name="roleId",value="角色id数组",required=true) @RequestBody Long[] roleIds){
|
roleService.deleteBatch(roleIds);
|
sysRoleGroupService.deleteBatch(roleIds);
|
roleResCatalogService.deleteBatchByRoleIds(roleIds);
|
sysRoleResourceService.deleteBatchByRoleIds(roleIds);
|
return Result.ok();
|
}
|
|
/**
|
* @Description: 系统初始化下拉框
|
* @Author: zimao.guo
|
* @Date: 18:00 2018/2/1
|
* @return: String[]
|
* @see String
|
* @param roleId
|
*/
|
@GetMapping("/findSystemNames/{roleId}")
|
public List<NameBindId> findSystemNames(@PathVariable(required = false) String roleId) {
|
|
List<NameBindId> systemNames = roleService.findSystemNamesByKeyWord(roleId);
|
|
if (systemNames.isEmpty()) return new ArrayList<>();
|
|
return systemNames;
|
}
|
|
/**
|
* @Description: 获得系统资源
|
* @Author: zimao.guo
|
* @Date: 9:40 2018/2/11
|
* @return: List<NameBindId>
|
* @see NameBindId
|
* @param appId
|
*/
|
@GetMapping("/getSystemResources/{appId}")
|
public List<NameBindId> getSystemResources(@PathVariable String appId){
|
List<NameBindId> resource = roleService.getSystemResources(appId);
|
|
if (resource.isEmpty()) return new ArrayList<>();
|
|
return resource;
|
}
|
|
/**
|
* @Description: 更换所属系统时所做的初始化
|
* @Author: zimao.guo
|
* @Date: 9:38 2018/2/11
|
* @return: Result
|
* @see Result
|
* @param appId
|
*/
|
@GetMapping("/systemChangeWithInit")
|
public Result systemChangeWithInit(@RequestParam(name="roleId") Long roleId,@RequestParam(name = "appId") Long appId){
|
|
List<NameBindId> sysResource = null;
|
if(appId != null) {
|
sysResource = roleService.getSystemResources(String.valueOf(appId));
|
}else{
|
sysResource = new ArrayList<>();
|
}
|
|
List<NameBindId> selectedResources = roleService.getOwnResources(roleId==null ? null : String.valueOf(roleId));
|
|
return Result.ok().put("sysResources",sysResource).put("ownResources",selectedResources);
|
}
|
|
|
|
}
|