heheng
9 天以前 bd605ac1e79b73db3b501f323d6f4a10c0f27dd5
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package com.gkhy.exam.admin.controller.system;
 
 
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.poi.excel.ExcelUtil;
import com.gkhy.exam.common.annotation.Log;
import com.gkhy.exam.common.api.CommonResult;
import com.gkhy.exam.common.domain.entity.SysRole;
import com.gkhy.exam.common.domain.entity.SysUser;
import com.gkhy.exam.common.domain.model.LoginUser;
import com.gkhy.exam.common.domain.model.LoginUserDetails;
import com.gkhy.exam.common.enums.BusinessType;
import com.gkhy.exam.common.utils.SecurityUtils;
import com.gkhy.exam.framework.web.service.SysPermissionService;
import com.gkhy.exam.framework.web.service.TokenService;
import com.gkhy.exam.system.domain.SysUserRole;
import com.gkhy.exam.system.service.ISysRoleService;
import com.gkhy.exam.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import static com.gkhy.exam.common.utils.PageUtils.startPage;
import static com.gkhy.exam.common.utils.SecurityUtils.getLoginUser;
import static com.gkhy.exam.common.utils.SecurityUtils.getUsername;
 
/**
 * 角色信息
 * 
 * @author expert
 */
@RestController
@RequestMapping("/system/role")
public class SysRoleController
{
    @Autowired
    private ISysRoleService roleService;
 
    @Autowired
    private TokenService tokenService;
 
    @Autowired
    private SysPermissionService permissionService;
 
    @Autowired
    private SysUserService userService;
 
//    @Autowired
//    private ISysDeptService deptService;
 
    // @PreAuthorize("hasAnyAuthority('system:role:list')")
    @GetMapping("/list")
    public CommonResult list(SysRole role)
    {
 
        return CommonResult.success(roleService.selectRoleList(role));
    }
 
//    @Log(title = "角色管理", businessType = BusinessType.EXPORT)
//    @PreAuthorize("hasAnyAuthority('system:role:export')")
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, SysRole role)
//    {
//        List<SysRole> list = roleService.selectRoleList(role);
//        ExcelUtil<SysRole> util = new ExcelUtil<SysRole>(SysRole.class);
//        util.exportExcel(response, list, "角色数据");
//    }
 
    /**
     * 根据角色编号获取详细信息
     */
    //@PreAuthorize("hasAnyAuthority('system:role:query')")
    @GetMapping(value = "/{roleId}")
    public CommonResult getInfo(@PathVariable Long roleId)
    {
        roleService.checkRoleDataScope(roleId);
        return CommonResult.success(roleService.selectRoleById(roleId));
    }
 
    /**
     * 新增角色
     */
    //@PreAuthorize("hasAnyAuthority('system:role:add')")
    //@Log(title = "角色管理", businessType = BusinessType.INSERT)
    @PostMapping
    public CommonResult add(@Validated @RequestBody SysRole role)
    {
        if (!roleService.checkRoleNameUnique(role))
        {
            return CommonResult.failed("新增角色'" + role.getRoleName() + "'失败,角色名称已存在");
        }
        else if (!roleService.checkRoleKeyUnique(role))
        {
            return CommonResult.failed("新增角色'" + role.getRoleName() + "'失败,角色权限已存在");
        }
        role.setCreateBy(getUsername());
        return CommonResult.success(roleService.insertRole(role));
 
    }
 
    /**
     * 修改保存角色
     */
    //@PreAuthorize("hasAnyAuthority('system:role:edit')")
    //@Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public CommonResult edit(@Validated @RequestBody SysRole role)
    {
        roleService.checkRoleAllowed(role);
        roleService.checkRoleDataScope(role.getRoleId());
        if (!roleService.checkRoleNameUnique(role))
        {
            return CommonResult.failed("修改角色'" + role.getRoleName() + "'失败,角色名称已存在");
        }
        else if (!roleService.checkRoleKeyUnique(role))
        {
            return CommonResult.failed("修改角色'" + role.getRoleName() + "'失败,角色权限已存在");
        }
        role.setUpdateBy(getUsername());
        
        if (roleService.updateRole(role) > 0)
        {
            // 更新缓存用户权限
            LoginUserDetails loginUser = getLoginUser();
 
            if (ObjectUtil.isNotNull(loginUser.getUser()) && !SecurityUtils.adminUser())
            {
                loginUser.setUser(userService.selectUserByUsername(loginUser.getUser().getName()));
                loginUser.setPermissions(permissionService.getMenuPermission(loginUser.getUser()));
                tokenService.setLoginUser(loginUser);
            }
            return CommonResult.success();
        }
        return CommonResult.failed("修改角色'" + role.getRoleName() + "'失败,请联系管理员");
    }
 
    /**
     * 修改保存数据权限
     */
    //@PreAuthorize("hasAnyAuthority('system:role:edit')")
    //@Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PutMapping("/dataScope")
    public CommonResult dataScope(@RequestBody SysRole role)
    {
        roleService.checkRoleAllowed(role);
        roleService.checkRoleDataScope(role.getRoleId());
        return CommonResult.success(roleService.authDataScope(role));
    }
 
    /**
     * 状态修改
     */
    //@PreAuthorize("hasAnyAuthority('system:role:edit')")
    // @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public CommonResult changeStatus(@RequestBody SysRole role)
    {
        roleService.checkRoleAllowed(role);
        roleService.checkRoleDataScope(role.getRoleId());
        role.setUpdateBy(getUsername());
        return CommonResult.success(roleService.updateRoleStatus(role));
    }
 
    /**
     * 删除角色
     */
    //@PreAuthorize("hasAnyAuthority('system:role:remove')")
    @Log(title = "角色管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{roleIds}")
    public CommonResult remove(@PathVariable Long[] roleIds)
    {
        return CommonResult.success(roleService.deleteRoleByIds(roleIds));
    }
 
    /**
     * 获取角色选择框列表
     */
    //@PreAuthorize("hasAnyAuthority('system:role:query')")
    @GetMapping("/optionselect")
    public CommonResult optionselect()
    {
        return CommonResult.success(roleService.selectRoleAll());
    }
 
    /**
     * 查询已分配用户角色列表
     */
    //@PreAuthorize("hasAnyAuthority('system:role:list')")
    @GetMapping("/authUser/allocatedList")
    public CommonResult allocatedList(SysUser user)
    {
        startPage();
        List<SysUser> list = userService.selectAllocatedList(user);
        return CommonResult.success(list);
    }
 
    /**
     * 查询未分配用户角色列表
     */
    //@PreAuthorize("hasAnyAuthority('system:role:list')")
    @GetMapping("/authUser/unallocatedList")
    public CommonResult unallocatedList(SysUser user)
    {
        startPage();
        List<SysUser> list = userService.selectUnallocatedList(user);
        return CommonResult.success(list);
    }
 
    /**
     * 取消授权用户
     */
    //@PreAuthorize("hasAnyAuthority('system:role:edit')")
    @Log(title = "角色管理", businessType = BusinessType.GRANT)
    @PutMapping("/authUser/cancel")
    public CommonResult cancelAuthUser(@RequestBody SysUserRole userRole)
    {
        return CommonResult.success(roleService.deleteAuthUser(userRole));
    }
 
    /**
     * 批量取消授权用户
     */
    //@PreAuthorize("hasAnyAuthority('system:role:edit')")
    @Log(title = "角色管理", businessType = BusinessType.GRANT)
    @PutMapping("/authUser/cancelAll")
    public CommonResult cancelAuthUserAll(Long roleId, Long[] userIds)
    {
        return CommonResult.success(roleService.deleteAuthUsers(roleId, userIds));
    }
 
    /**
     * 批量选择用户授权
     */
    //@PreAuthorize("hasAnyAuthority('system:role:edit')")
    @Log(title = "角色管理", businessType = BusinessType.GRANT)
    @PutMapping("/authUser/selectAll")
    public CommonResult selectAuthUserAll(Long roleId, Long[] userIds)
    {
        roleService.checkRoleDataScope(roleId);
        return CommonResult.success(roleService.insertAuthUsers(roleId, userIds));
    }
 
    /**
     * 获取对应角色部门树列表
     */
    //@PreAuthorize("hasAnyAuthority('system:role:query')")
    @GetMapping(value = "/deptTree/{roleId}")
    public CommonResult deptTree(@PathVariable("roleId") Long roleId)
    {
        Map<String,Object> ajax = new HashMap<>();
//        ajax.put("checkedKeys", deptService.selectDeptListByRoleId(roleId));
//        ajax.put("depts", deptService.selectDeptTreeList(new SysDept()));
        return CommonResult.success(ajax);
    }
}