1
13693261870
2022-09-16 762f2fb45db004618ba099aa3c0bd89dba1eb843
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
262
263
264
265
266
267
268
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);
    }
 
 
 
}