wangting
2024-09-27 a3e87f78ee262ca9bb7d9b0c997639d5f3295890
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
package com.vci.client.uif.actions.client;
 
import java.util.HashMap;
import java.util.Map;
 
import com.vci.client.bof.ClientBusinessObject;
import com.vci.client.bof.ClientLinkObject;
import com.vci.client.common.providers.ServiceProvider;
import com.vci.client.uif.actions.client.BusinessOperationAction.ValueType;
import com.vci.client.uif.engine.client.IDataModel;
import com.vci.client.uif.engine.common.IDataNode;
import com.vci.corba.framework.data.CheckValue;
import com.vci.corba.common.VCIError;
import com.vci.corba.common.data.VCIInvocationInfo;
import com.vci.mw.InvocationUtility;
 
/**
 * 对数据权限进行校验,分为以下六种情况:
 * 1、修改link
 * 2、修改BO
 * 3、修改link及BO,通过1和2的组合即可完成
 * 4、基于BO创建link,通过判断BO作为from端对象的link(加入link名称的操作)操作权限,即通过步骤2
 * 5、基于LO创建link,通过判断LO的TO端对象作为from端对象的link(加入link名称的操作)操作权限,即通过步骤2
 * 6、基于参照创建对象,通过判断参照对象是否具有创建的权限即可,即通过步骤2
 * 
 * 对于创建BO的权限通过功能模块授权进行处理,从数据层面暂不考虑
 * @author VCI_STGK_Lincq
 *
 */
public class DataRightUtil {
    private Map<String, String> buttonParams = new HashMap<String, String>();
    private IDataModel dataModel = null;
    
    public DataRightUtil(Map map){
        buttonParams = map;
    }
    public Map<String, String> getButtonParams(){
        return this.buttonParams;
    }
    public IDataModel getDataModel(){
        return this.dataModel;
    }
    /**
     * 判断当前用户对链接对象是否具有操作权限
     * @param selectedObject,选中对象
     * @param key,操作键值
     * @return
     * @throws VCIError
     */
    public boolean checkLinkHasEditRight(Object selectedObject, String linkType, String key) throws VCIError {
        boolean hasRight = false;
        String[] loResult = this.getLOcheckObject(selectedObject);
        if (loResult != null && loResult.length != 0) {
            String opname = linkType + "." + key;
            hasRight = getCheckResult(loResult, opname);
        }
        return hasRight;
    }
    
    /**
     * 判断当前用户对链接对象Fromd端BO对象是否具有操作权限
     * @param selectedObject,选中对象
     * @param key,操作键值
     * @return
     * @throws VCIError
     */
    public boolean checkLinkFBHasEditRight(Object selectedObject, String linkType, String key) throws VCIError {
        boolean hasRight = false;
        String[] boResult = this.getTBOcheckObject(selectedObject);
        if (boResult != null && boResult.length != 0) {
            String opname = linkType + "." + key;
            hasRight = getCheckResult(boResult, opname);
        }
        return hasRight;
    }
    
    /**
     * 检查BO对当前对象是否基于修改权限
     * 如果为link时正向得到TO端BO
     * @param selectedObject,选中对象
     * @param key,操作键值
     * @return
     * @throws VCIError
     */
    public boolean checkTBoHasEditRight(Object selectedObject, String key) throws VCIError {
        boolean hasRight = false;
        String[] boResult = this.getTBOcheckObject(selectedObject);
        if (boResult != null && boResult.length != 0) {
            hasRight = getCheckResult(boResult, key);
        }
        return hasRight;
    }
    
    /**
     * 检查BO对当前对象是否基于修改权限
     * 如果为link时正向得到From端BO
     * @param selectedObject,选中对象
     * @param key,操作键值
     * @return
     * @throws VCIError
     */
    public boolean checkFBoHasEditRight(Object selectedObject, String key) throws VCIError {
        boolean hasRight = false;
        String[] boResult = this.getFBOcheckObject(selectedObject);
        if (boResult != null && boResult.length != 0) {
            hasRight = getCheckResult(boResult, key);
        }
        return hasRight;
    }
    
    private String[] getLOcheckObject(Object selectedObject) {
          String[] result = null;
        if (selectedObject instanceof IDataNode){
            IDataNode dataNode = (IDataNode) selectedObject;
            Object masterObj = dataNode.getMaterObject();
            if (masterObj instanceof ClientLinkObject) {
                ClientLinkObject clo = (ClientLinkObject) masterObj;
                result = new String[4];
                result[0] = clo.getFromOid() + ";" + clo.getOid();
                result[1] = clo.getFromBTMName();
                result[2] = "";
                result[3] = "";
            }
        }
        
        return result;
    }
    
    /**
     * 得到BO对象,如果为link时正向得到To端BO,反向得到From端BO
     * @param selectedObject
     * @return
     */
    private String[] getTBOcheckObject(Object selectedObject) {
          String[] result = null;
        if (selectedObject instanceof IDataNode){
            IDataNode dataNode = (IDataNode) selectedObject;
            Object masterObj = dataNode.getMaterObject();
            if (masterObj instanceof ClientBusinessObject) {
                ClientBusinessObject cbo = (ClientBusinessObject) masterObj;
                result = new String[4];
                result[0] = cbo.getBusinessObject().oid;
                result[1] = cbo.getBusinessObject().btName;
                result[2] = cbo.getBusinessObject().revisionid;
                result[3] = cbo.getBusinessObject().nameoid;
            } else if (masterObj instanceof ClientLinkObject) {
                ClientLinkObject clo = (ClientLinkObject) masterObj;
                result = new String[4];
                if (dataNode.isForward()) {
                    result[0] = clo.getLinkObject().toOid;
                    result[1] = clo.getLinkObject().toBTName;
                    result[2] = clo.getLinkObject().toRevOid;
                    result[3] = clo.getLinkObject().toNameOid;
                } else {
                    result[0] = clo.getLinkObject().fromOid;
                    result[1] = clo.getLinkObject().fromBTName;
                    result[2] = clo.getLinkObject().fromRevOid;
                    result[3] = clo.getLinkObject().fromNameOid;
                }
            }
        }
        
        return result;
    }
    
    /**
     * 得到BO对象,如果为link时正向得到From端BO,反向得到To端BO
     * @param selectedObject
     * @return
     */
    private String[] getFBOcheckObject(Object selectedObject) {
          String[] result = null;
        if (selectedObject instanceof IDataNode){
            IDataNode dataNode = (IDataNode) selectedObject;
            Object masterObj = dataNode.getMaterObject();
            if (masterObj instanceof ClientBusinessObject) {
                ClientBusinessObject cbo = (ClientBusinessObject) masterObj;
                result = new String[4];
                result[0] = cbo.getBusinessObject().oid;
                result[1] = cbo.getBusinessObject().btName;
                result[2] = cbo.getBusinessObject().revisionid;
                result[3] = cbo.getBusinessObject().nameoid;
            } else if (masterObj instanceof ClientLinkObject) {
                ClientLinkObject clo = (ClientLinkObject) masterObj;
                result = new String[4];
                if (dataNode.isForward()) {
                    result[0] = clo.getLinkObject().fromOid;
                    result[1] = clo.getLinkObject().fromBTName;
                    result[2] = clo.getLinkObject().fromRevOid;
                    result[3] = clo.getLinkObject().fromNameOid;
                } else {
                    result[0] = clo.getLinkObject().toOid;
                    result[1] = clo.getLinkObject().toBTName;
                    result[2] = clo.getLinkObject().toRevOid;
                    result[3] = clo.getLinkObject().toNameOid;
                }
            }
        }
        
        return result;
    }    
    
    private boolean getCheckResult(String[] result, String opname) throws VCIError {
        VCIInvocationInfo invocationInfo = InvocationUtility.getInvocation();
        String oid = result[0];
        String btmName = result[1];
        String revisionOid = result[2];
        String nameOid = result[3];
        
        CheckValue params = new CheckValue();
        params.users = invocationInfo.userName;
        params.roles = getArrayString(invocationInfo.roleNames);
        params.userGroups = getArrayString(invocationInfo.groupNames);
        StringBuffer sb = new StringBuffer();
        String[] extAttrs = invocationInfo.extAttribs; 
        for(int i = 0; i < extAttrs.length; i++){
            sb.append(extAttrs[i]);
            if(i != extAttrs.length - 1){
                sb.append(",");
            }
        }
        params.paramValues = sb.toString();
        params.opname = opname;
        params.objectmoid = nameOid;
        params.objectroid = revisionOid;
        params.businesstype = btmName;
        params.objectoid = oid;
        
//System.out.println("==========================================");
//System.out.println("DataRightUtil.getCheckResult()");
//System.out.println("user:" + params.users);
//System.out.println("userGroups:" + params.userGroups);
//System.out.println("roles:" + params.roles);
//System.out.println("paramValues:" + params.paramValues);
//System.out.println("opname:" + params.opname);
//System.out.println("objectmoid:" + params.objectmoid);
//System.out.println("objectroid:" + params.objectroid);
//System.out.println("businesstype:" + params.businesstype);
//System.out.println("objectoid:" + params.objectoid);
        String where = ServiceProvider.getFrameService().checkRight(params);
//System.out.println("checkResult where :" + where);
//System.out.println("==========================================");
        
        String[] ops = where.split(":");
        String msg = "0";
        //TODO 需要处理 query 类型的操作,权限定义(返回数据格)不一样的问题
        for (String s : ops) {
            if (s != null && !s.equals("")) {
                String[] op = s.split(",");
                msg = op[1];
                break;
            }
        }
        if(msg.length()>1){
            msg=msg.substring(0, 1);
        }
        boolean res = ("1".equals(msg));
        
        return res;
    }
    
    protected String getArrayString(String[] values){
        String res = "";
        if (values != null) {
            for (int i = 0; i < values.length; i++) {
                if (i != 0) {
                    res += ",";
                }
                res += values[i];
            }
        }
        return res;
    }
    
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
 
    }
    /**
     * 验证数据是否有操作权限
     * @param rightCheckTypeParam
     * @param selectedObject
     * @param key
     * @return
     * @throws VCIError
     */
    public boolean getCheckRes(String rightCheckTypeParam,Object selectedObject,String key) throws VCIError{
        
        //DataRightUtil dru = new DataRightUtil();
        boolean checkRes = false;
        if(rightCheckTypeParam.equals(RightCheckConstants.RIGHT_CHECK_TYPE_NONE)){
            checkRes = true;
        } else if(rightCheckTypeParam.equals(RightCheckConstants.RIGHT_CHECK_TYPE_B)){
            checkRes =  checkTBoHasEditRight(selectedObject, key);
        } else if(rightCheckTypeParam.equals(RightCheckConstants.RIGHT_CHECK_TYPE_L)){
            String linkType = getButtonParamLinkType();
            checkRes = checkLinkHasEditRight(selectedObject, linkType, key);
            //校验From端权限
            if(checkRes) {
                //校验Form端对象的权限
                String fmapping = getParameterValue(ValueType.ButtonConfig, "fmapping", -1);
                if(fmapping != null && !fmapping.trim().equals("")) {
                    checkRes =  checkFBoHasEditRight(selectedObject, fmapping);
                }
                if(checkRes) {
                    //校验To端权限
                    String tmapping = getParameterValue(ValueType.ButtonConfig, "tmapping", -1);
                    if(tmapping != null && !tmapping.trim().equals("")) {
                        checkRes =  checkTBoHasEditRight(selectedObject, tmapping);
                    }
                }
            }
        } else if(rightCheckTypeParam.equals(RightCheckConstants.RIGHT_CHECK_LOGICAL_B)){
 
//            boolean b =  dru.checkBoHasEditRight(selectedObject, key);
//            String linkType = getButtonParamLinkType();
//            boolean l = dru.checkLinkHasEditRight(selectedObject, linkType, key);
//            checkRes = b && l;
            
            checkRes = false;
        } else if(rightCheckTypeParam.equals(RightCheckConstants.RIGHT_CHECK_TYPE_FB)) {
            String linkType = getButtonParamLinkType();
            checkRes = checkLinkFBHasEditRight(selectedObject, linkType, key);
        } else if(rightCheckTypeParam.equals(RightCheckConstants.RIGHT_CHECK_TYPE_TB)) {
            
        }
        
        return checkRes;
    }
    
    private String getButtonParamLinkType(){
        String res = "";
        res = getParameterValue(ValueType.ButtonConfig, "linktype", -1);
        if(res == null){
            res = getParameterValue(ValueType.ButtonConfig, "linkType", -1);
            if(res == null){
                res = getParameterValue(ValueType.ButtonConfig, "LinkType", -1);
                if(res == null){
                    res = "";
                }
            }
        }
        return res;
    }
    
    /**
     * 获取参数
     * @param valueType 参数值的来源
     * @param key 参数的 key
     * @param dataIndex 参数数据索引(第xx条数据的 x)
     * @return
     */
    public String getParameterValue(ValueType valueType, String key, int dataIndex){
        String res = null;
        Map<String, String> map = null;
        if(valueType == ValueType.ButtonConfig){
            map = getButtonParams();
        } else if(valueType == ValueType.RuntimeData){
            IDataModel dataModel = getDataModel();
            Object rowData = dataModel.getSelectObjects()[dataIndex];
            if(rowData instanceof IDataNode){
                IDataNode dataNode = (IDataNode)rowData;
                map = dataNode.getValueMap();
            }
        }
        res = map.get(key);
        return res;
    }
}