RoleController.java
3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package com.genersoft.iot.vmp.vmanager.user;
import com.genersoft.iot.vmp.conf.security.SecurityUtils;
import com.genersoft.iot.vmp.service.IRoleService;
import com.genersoft.iot.vmp.service.IUserService;
import com.genersoft.iot.vmp.storager.dao.dto.Role;
import com.genersoft.iot.vmp.storager.dao.dto.User;
import com.genersoft.iot.vmp.vmanager.bean.WVPResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.text.SimpleDateFormat;
import java.util.List;
@Api(tags = "角色管理")
@CrossOrigin
@RestController
@RequestMapping("/api/role")
public class RoleController {
@Autowired
private IRoleService roleService;
private final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@ApiOperation("添加角色")
@ApiImplicitParams({
@ApiImplicitParam(name = "name", required = true, value = "角色名", dataTypeClass = String.class),
@ApiImplicitParam(name = "authority", required = true, value = "权限(自行定义内容,目前未使用)", dataTypeClass = String.class),
})
@PostMapping("/add")
public ResponseEntity<WVPResult<Integer>> add(@RequestParam String name,
@RequestParam(required = false) String authority){
WVPResult<Integer> result = new WVPResult<>();
// 获取当前登录用户id
int currenRoleId = SecurityUtils.getUserInfo().getRole().getId();
if (currenRoleId != 1) {
// 只用角色id为1才可以删除和添加用户
result.setCode(-1);
result.setMsg("用户无权限");
return new ResponseEntity<>(result, HttpStatus.FORBIDDEN);
}
Role role = new Role();
role.setName(name);
role.setAuthority(authority);
role.setCreateTime(format.format(System.currentTimeMillis()));
role.setUpdateTime(format.format(System.currentTimeMillis()));
int addResult = roleService.add(role);
result.setCode(addResult > 0 ? 0 : -1);
result.setMsg(addResult > 0 ? "success" : "fail");
result.setData(addResult);
return new ResponseEntity<>(result, HttpStatus.OK);
}
@ApiOperation("删除角色")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", required = true, value = "用户Id", dataTypeClass = Integer.class),
})
@DeleteMapping("/delete")
public ResponseEntity<WVPResult<String>> delete(@RequestParam Integer id){
// 获取当前登录用户id
int currenRoleId = SecurityUtils.getUserInfo().getRole().getId();
WVPResult<String> result = new WVPResult<>();
if (currenRoleId != 1) {
// 只用角色id为0才可以删除和添加用户
result.setCode(-1);
result.setMsg("用户无权限");
return new ResponseEntity<>(result, HttpStatus.FORBIDDEN);
}
int deleteResult = roleService.delete(id);
result.setCode(deleteResult>0? 0 : -1);
result.setMsg(deleteResult>0? "success" : "fail");
return new ResponseEntity<>(result, HttpStatus.OK);
}
@ApiOperation("查询角色")
@ApiImplicitParams({})
@GetMapping("/all")
public ResponseEntity<WVPResult<List<Role>>> all(){
// 获取当前登录用户id
List<Role> allRoles = roleService.getAll();
WVPResult<List<Role>> result = new WVPResult<>();
result.setCode(0);
result.setMsg("success");
result.setData(allRoles);
return new ResponseEntity<>(result, HttpStatus.OK);
}
}