郑永安
2023-06-19 7a6abd05683528032687c75e80e0bd2030a3e46c
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
package com.gkhy.safePlatform.account.controller;
 
 
import com.alibaba.fastjson.JSONObject;
import com.gkhy.safePlatform.account.model.annotation.CommonLogEnable;
import com.gkhy.safePlatform.account.model.dto.req.RoleAddReqDTO;
import com.gkhy.safePlatform.account.model.dto.req.RoleModReqDTO;
import com.gkhy.safePlatform.account.model.dto.resp.RoleRespDTO;
import com.gkhy.safePlatform.account.service.RoleService;
import com.gkhy.safePlatform.commons.co.ContextCacheUser;
import com.gkhy.safePlatform.commons.enums.Module;
import com.gkhy.safePlatform.commons.enums.ResultCodes;
import com.gkhy.safePlatform.commons.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
 
import java.security.Principal;
import java.util.List;
 
@RestController
@RequestMapping("/role")
public class RoleController {
 
    @Autowired
    private RoleService roleService;
 
    /**
    * @Description: 获取所有启用角色
    */
 
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public ResultVO<List<RoleRespDTO>> addRole(Authentication authentication){
        List<RoleRespDTO> res = roleService.getEnableRoleInfos();
        return new ResultVO<>(ResultCodes.OK, res);
    }
 
    /**
    * @Description: 新增角色
    */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('role_create')")
    @CommonLogEnable(module = Module.ACCOUNT,content = "新增角色")
    public ResultVO<String> addRole(Authentication authentication, @RequestBody RoleAddReqDTO roleAddDto) {
        ContextCacheUser currentUser = (ContextCacheUser) authentication.getPrincipal();
        roleService.addRole(roleAddDto, currentUser);
        return new ResultVO<>(ResultCodes.OK);
    }
 
 
    /**
     * @Description: 修改角色
     */
    @RequestMapping(value = "/mod",method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('role_modify')")
    @CommonLogEnable(module = Module.ACCOUNT,content = "修改角色")
    public ResultVO<String> modRole(Authentication authentication, @RequestBody RoleModReqDTO roleModDto) {
        ContextCacheUser currentUser = (ContextCacheUser) authentication.getPrincipal();
        roleService.modRole(roleModDto, currentUser);
        return new ResultVO<>(ResultCodes.OK);
    }
 
 
    /**
     * @Description: 删除角色
     */
    @RequestMapping(value = "/del",method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('role_delete')")
    @CommonLogEnable(module = Module.ACCOUNT,content = "删除角色")
    public ResultVO<String> delRole(Authentication authentication, @RequestBody JSONObject json) {
        ContextCacheUser currentUser = (ContextCacheUser) authentication.getPrincipal();
        roleService.delRole(json.getLong("roleId"), currentUser);
        return new ResultVO<>(ResultCodes.OK);
    }
}