田源
2024-03-07 4b4083fd73dc27ece42f4835483565eef0e4f608
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package com.vci.server.framework.delegate;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import org.apache.commons.lang3.StringUtils;
 
import com.vci.common.log.LogType;
import com.vci.common.log.ServerWithLog4j;
import com.vci.common.utility.ObjectUtility;
import com.vci.corba.common.VCIError;
import com.vci.corba.framework.data.RoleRightInfo;
import com.vci.corba.common.data.UserEntityInfo;
import com.vci.corba.framework.data.UserInfo;
import com.vci.server.base.delegate.BaseDelegate;
import com.vci.server.framework.funcmng.function.Function;
import com.vci.server.framework.funcmng.function.FunctionService;
import com.vci.server.framework.right.roleRight.RoleRight;
import com.vci.server.framework.right.roleRight.RoleRightService;
import com.vci.server.framework.systemConfig.log.LogRecordUtil;
import com.vci.server.framework.systemConfig.stafforgmanage.role.Role;
import com.vci.server.framework.systemConfig.stafforgmanage.role.RoleService;
 
public class RoleRightDelegate extends BaseDelegate {
 
    private RoleRightService service = null;
 
    public RoleRightDelegate(UserEntityInfo userEntityInfo) {
        super(userEntityInfo);
        service = new RoleRightService(userEntity);
    }
 
    public RoleRightDelegate() {
        service = new RoleRightService();
    }
 
    /**
     * 保存权限
     * 
     * @param roleRightInfos
     * @param userName
     * @return
     * @throws VCIError
     */
    public boolean saveRoleRight(RoleRightInfo[] roleRightInfos, String roleId, String userName) throws VCIError {
        boolean res = true;
        if (roleRightInfos == null || roleRightInfos.length == 0) {
            throw new VCIError("没有任何的权限,不能执行授权", new String[0]);
        }
        RoleRight[] roleRights = null;
        try {
            RoleService roleService = new RoleService();
            FunctionService functionService = new FunctionService();
            // UserInfo userInfo = new
            // RightManagementDelegate().fetchUserInfoByName(userName);
            short rightType = 0;
            if (roleRightInfos.length > 0)
                rightType = roleRightInfos[0].rightType;
 
            roleRights = new RoleRight[roleRightInfos.length];
            List<String> functionOids = new ArrayList<String>();
            
            for (int i = 0; i < roleRightInfos.length; i++) {
                roleRights[i] = changeRoleRightInfoToEntity(roleRightInfos[i]);
                roleRights[i].setId(ObjectUtility.getNewObjectID36());
                
                if (i < 20)
                    functionOids.add(roleRights[i].getFuncId());
                //roleRights[i].setRightType(rightType);
            }
 
            Role role = roleService.selectRole(roleId);
            if (roleRights.length > 0) {
                res = service.saveRoleRight(roleRights, roleId, rightType);
                try {
                    // 看看顶层的模块
                    String logInfo = "";
                    if (functionOids != null && functionOids.size() > 0) {
                        List<Function> functions = functionService.listRootFunctionByOids(functionOids);
                        StringBuilder sb = new StringBuilder();
 
                        for (int i = 0; i < functions.size(); i++) {
                            Function func = functions.get(i);
                            sb.append(func.getAliasName()).append(",");
                            if (i > 20) {
                                break;
                            }
                        }
                        sb.append("等共计" + roleRightInfos.length + "个权限");
                        logInfo = "操作的角色信息为:" + role.getLogInfo() + ",权限包括:" + sb.toString();
                    } else {
                        logInfo = "操作的角色信息为:" + role.getLogInfo();
                    }
 
                    if (res)
                        LogRecordUtil.writeLog(userEntity, "授权", "成功", logInfo, LogType.GrantPrivileges, role.getId());
                    else 
                        LogRecordUtil.writeLog(userEntity, "授权", "失败", logInfo, LogType.GrantPrivileges, role.getId());
                        
                } catch (Exception e) {
                    //e.printStackTrace();
                    ServerWithLog4j.logger.error(e);
                }
            }
        } catch (Exception e) {
            //e.printStackTrace();
            ServerWithLog4j.logger.error(e);
            LogRecordUtil.writeLog(userEntity, "授权", "功能模块授权失败!",  e.getMessage(), LogType.GrantPrivileges, roleId);
            throw new VCIError("0", new String[] { e.getMessage() });
        }
        return res;
    }
 
    private int getFunctionType(RoleRight[] roleRights) throws VCIError {
        List<String> funcOidList = new ArrayList<String>();
        for (RoleRight rr : roleRights) {
            funcOidList.add(rr.getFuncId());
        }
        FunctionService functionService = new FunctionService();
        List<Function> functions = functionService.batchGetFunctionObjectByOid(funcOidList);
        if (functions.size() == 0) {
            // 去UI里面看看,
            return 1;
        } else if (roleRights.length != functions.size()) {
            return 2;
        } else {
            return 0;
        }
    }
 
    public boolean removeRoleRight(RoleRightInfo[] roleRightInfos, String roleId, String userName) throws VCIError {
        boolean res = true;
        RoleRight[] roleRights = null;
        try {
            RoleService roleService = new RoleService();
 
            roleRights = new RoleRight[roleRightInfos.length];
            for (int i = 0; i < roleRightInfos.length; i++) {
                roleRights[i] = changeRoleRightInfoToEntity(roleRightInfos[i]);
                roleRights[i].setId(ObjectUtility.getNewObjectID36());
                // roleRights[i].setRightType(rightType);
            }
 
            Role role = roleService.selectRole(roleId);
            // 平台没有清除权限的接口,,weidy@2018-10-12添加
            if (roleRights == null || roleRights.length == 0) {
 
            } else {
                res = service.removeRoleRight(roleRights, roleId);
                
                if (res)
                    LogRecordUtil.writeLog(userEntity, "授权", "成功", role.getLogInfo(), LogType.GrantPrivileges, role.getId());
                else
                    LogRecordUtil.writeLog(userEntity, "授权", "失败", role.getLogInfo(), LogType.GrantPrivileges, role.getId());
                    
            }
        } catch (Exception e) {
            //e.printStackTrace();
            ServerWithLog4j.logger.error(e);
            // LogRecordUtil.writeLog(userEntity, "授权",
            // "功能模块授权失败!",LogType.GrantPrivileges);
            throw new VCIError("0", new String[0]);
        }
        return res;
    }
 
    /**
     * 添加授权
     * 
     * @param roleRightInfos
     * @param roleId
     * @param userName
     * @return
     * @throws VCIError
     */
    public boolean addRoleRight(RoleRightInfo[] roleRightInfos, String roleId, String userName) throws VCIError {
        boolean res = true;
        RoleRight[] roleRights = null;
        try {
            RoleService roleService = new RoleService();
            // FunctionService functionService = new FunctionService();
            // UserInfo userInfo = new
            // RightManagementDelegate().fetchUserInfoByName(userName);
            int rightType = 0;
            if (roleRightInfos.length > 0)
                rightType = roleRightInfos[0].rightType;
 
            /***** 当类型为2时,系统是普通用户,rightType = 2 ***/
//            if(userInfo.userType == 2) {
//                rightType = userInfo.userType;
//            } else {
//                rightType = userInfo.userType + 1;
//            }
            roleRights = new RoleRight[roleRightInfos.length];
            for (int i = 0; i < roleRightInfos.length; i++) {
                roleRights[i] = changeRoleRightInfoToEntity(roleRightInfos[i]);
                roleRights[i].setId(ObjectUtility.getNewObjectID36());
                // roleRights[i].setRightType(rightType);
            }
 
            Role role = roleService.selectRole(roleId);
            res = service.reAddRoleRight(roleRights, roleId, rightType);
            LogRecordUtil.writeLog(userEntity, "增加授权", "成功", role.getLogInfo(), LogType.GrantPrivileges, role.getId());
        } catch (Exception e) {
            //e.printStackTrace();
            ServerWithLog4j.logger.error(e);
            // LogRecordUtil.writeLog(userEntity, "授权",
            // "功能模块授权失败!",LogType.GrantPrivileges);
            throw new VCIError("0", new String[0]);
        }
        return res;
    }
 
    public boolean addRoleRightWithNoCheck(RoleRightInfo[] roleRightInfos, String roleId, String userName)
            throws VCIError {
        boolean res = true;
        RoleRight[] roleRights = null;
        try {
            RoleService roleService = new RoleService();
//            UserInfo userInfo = new RightManagementDelegate().fetchUserInfoByName(userName);
            int rightType = 0;
            if (roleRightInfos.length > 0)
                rightType = roleRightInfos[0].rightType;
 
            /***** 当类型为2时,系统是普通用户,rightType = 2 ***/
//            if(userInfo.userType == 2) {
//                rightType = userInfo.userType;
//            } else {
//                rightType = userInfo.userType + 1;
//            }
            roleRights = new RoleRight[roleRightInfos.length];
            for (int i = 0; i < roleRightInfos.length; i++) {
                roleRights[i] = changeRoleRightInfoToEntity(roleRightInfos[i]);
                roleRights[i].setId(ObjectUtility.getNewObjectID36());
                // roleRights[i].setRightType(rightType);
            }
 
            Role role = roleService.selectRole(roleId);
            res = service.reAddRoleRightWithNoCheck(roleRights, rightType);
            if (res)
                LogRecordUtil.writeLog(userEntity, "增加授权", "成功", role.getLogInfo(), LogType.GrantPrivileges, role.getId());
            else
                LogRecordUtil.writeLog(userEntity, "增加授权", "失败", role.getLogInfo(), LogType.GrantPrivileges, role.getId());
        } catch (Exception e) {
            //e.printStackTrace();
            ServerWithLog4j.logger.error(e);
            // LogRecordUtil.writeLog(userEntity, "授权",
            // "功能模块授权失败!",LogType.GrantPrivileges);
            throw new VCIError("0", new String[0]);
        }
        return res;
    }
 
    /**
     * 获取权限
     * 
     * @param userName
     * @return
     * @throws VCIError
     */
    public RoleRightInfo[] getRoleRightList(String roleId, String userName) throws VCIError {
        RoleRightInfo[] roleRightInfos = null;
        try {
//            UserInfo userInfo = new RightManagementDelegate().fetchUserInfoByName(userName);
            /***** 当类型为2时,系统是普通用户,rightType = 2 ***/
            int rightType = 0;
 
//            if(userInfo.userType == 2) {
//                rightType = userInfo.userType;
//            } else {
//                rightType = userInfo.userType + 1;
//            }
//            int rightType = userInfo.userType + 1;
            List<RoleRight> ls = service.getRoleRightList(roleId, rightType);
            roleRightInfos = new RoleRightInfo[ls.size()];
            for (int i = 0; i < ls.size(); i++) {
                roleRightInfos[i] = changeRoleRightEntityToInfo(ls.get(i));
            }
        } catch (Exception e) {
            //e.printStackTrace();
            ServerWithLog4j.logger.error(e);
            throw new VCIError("0", new String[0]);
        }
        return roleRightInfos;
    }
 
    /**
     * 
     * <p>
     * Description:根据授权角色的类型获取权限
     * </p>
     * 
     * @author sunbo
     * @time 2013-2-21
     * @param rightType
     * @return
     * @throws VCIError
     */
    public RoleRightInfo[] getRoleRightListByType(final String[] rightType) throws VCIError {
        RoleRightInfo[] roleRightInfos = null;
        try {
            List<RoleRight> ls = service.getRoleRightListByType(rightType);
            roleRightInfos = new RoleRightInfo[ls.size()];
            for (int i = 0; i < ls.size(); i++) {
                roleRightInfos[i] = changeRoleRightEntityToInfo(ls.get(i));
            }
        } catch (Exception e) {
            //e.printStackTrace();
            ServerWithLog4j.logger.error(e);
            throw new VCIError("0", new String[0]);
        }
        return roleRightInfos;
    }
 
    /**
     * 获取用户对当前模块的权限
     * 
     * @param funcId
     * @param userName
     * @return
     * @throws VCIError
     */
    public RoleRightInfo[] getRoleRightByModule(String funcId, String userName) throws VCIError {
        RoleRightInfo[] roleRightInfos = null;
        try {
            List<RoleRight> ls = service.getRoleRightByModule(funcId, userName);
            roleRightInfos = new RoleRightInfo[ls.size()];
            for (int i = 0; i < ls.size(); i++) {
                roleRightInfos[i] = changeRoleRightEntityToInfo(ls.get(i));
            }
        } catch (Exception e) {
            //e.printStackTrace();
            ServerWithLog4j.logger.error(e);
            throw new VCIError("120601", new String[0]);
        }
        return roleRightInfos;
    }
 
    public RoleRightInfo[] getRoleRightByUserName(String userName) throws VCIError {
        RoleRightInfo[] roleRightInfos = null;
        try {
            List<RoleRight> ls = service.getRoleRightByUserName(userName);
            roleRightInfos = new RoleRightInfo[ls.size()];
            for (int i = 0; i < ls.size(); i++) {
                roleRightInfos[i] = changeRoleRightEntityToInfo(ls.get(i));
            }
        } catch (Exception e) {
            //e.printStackTrace();
            ServerWithLog4j.logger.error(e);
            throw new VCIError("0", new String[0]);
        }
        return roleRightInfos;
    }
 
    public RoleRightInfo[] getFunctionRoleRightByUserName(String userName) throws VCIError {
        RoleRightInfo[] roleRightInfos = null;
        try {
            List<RoleRight> ls = service.getFunctionRoleRightByUserName(userName);
            roleRightInfos = new RoleRightInfo[ls.size()];
            for (int i = 0; i < ls.size(); i++) {
                roleRightInfos[i] = changeRoleRightEntityToInfo(ls.get(i));
            }
        } catch (Exception e) {
            //e.printStackTrace();
            ServerWithLog4j.logger.error(e);
            throw new VCIError("0", new String[0]);
        }
        return roleRightInfos;
    }
 
    private RoleRightInfo changeRoleRightEntityToInfo(RoleRight roleRight) {
        RoleRightInfo info = new RoleRightInfo();
        info.id = roleRight.getId() == null ? "" : roleRight.getId();
        info.roleId = roleRight.getRoleId() == null ? "" : roleRight.getRoleId();
        info.rightValue = roleRight.getRightValue();
        info.rightType = roleRight.getRightType();
        info.funcId = roleRight.getFuncId() == null ? "" : roleRight.getFuncId();
        return info;
    }
 
    private RoleRight changeRoleRightInfoToEntity(RoleRightInfo roleRightInfo) {
        RoleRight entity = new RoleRight();
        entity.setId(roleRightInfo.id);
        entity.setFuncId(roleRightInfo.funcId);
        entity.setRoleId(roleRightInfo.roleId);
        entity.setRightType(roleRightInfo.rightType);
        entity.setRightValue(roleRightInfo.rightValue);
 
        entity.setCreateUser(roleRightInfo.createUser);
        entity.setCreateTime(new Date(roleRightInfo.createTime));
        entity.setModifyTime(new Date(roleRightInfo.modifyTime));
        entity.setModifyUser(roleRightInfo.modifyUser);
        entity.setLicensor(roleRightInfo.licensor);
 
        return entity;
    }
 
    public boolean clearRoleRight(String roleId, String userName, int functionType) {
        return service.clearRoleRight(roleId, functionType);
    }
 
}