田源
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
404
405
406
407
408
409
410
411
412
413
414
package com.vci.client.framework.systemConfig.stafforgmanage;
 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.tree.TreePath;
 
import org.apache.commons.lang3.StringUtils;
 
import com.vci.client.LogonApplication;
import com.vci.client.common.objects.DeptObject;
import com.vci.client.common.objects.UserEntityObject;
import com.vci.client.common.objects.UserObject;
import com.vci.client.framework.delegate.RightManagementClientDelegate;
import com.vci.client.framework.systemConfig.object.TransmitMethod;
import com.vci.client.framework.util.RightControlUtil;
import com.vci.client.ui.exception.VCIException;
import com.vci.client.ui.locale.LocaleDisplay;
import com.vci.client.ui.swing.VCIOptionPane;
import com.vci.client.ui.tree.VCIBaseTreeNode;
 
public class UserActionListener extends JPanel implements ActionListener {
 
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private UserDialog panel = null;
    private UserTablePanel userTablePanel=null;
    private String type = null;
    private String userName = LogonApplication.getUserEntityObject().getUserName();
    private UserObject logonUserInfo;
    private UserObject userObj;
    
    public UserActionListener(UserDialog panel, UserTablePanel userTablePanel ,String type , UserObject userObj) {
        this.panel = panel;
        this.userTablePanel = userTablePanel;
        this.userObj = userObj;
        this.type = type;
    }
    
    public void actionPerformed(ActionEvent e) {
        if (type.equals("add")) {
            add_actionPerformed();
        } else if (type.equals("edit")) {
            edit_actionPerformed();
        } else if (type.equals("delete")) {
            delete_actionPerformed();
        }
    }
    
    @SuppressWarnings("static-access")
    private boolean check(){
//        if(!"sysAdmin".equalsIgnoreCase(ClientContextVariable.getUserName())){
//            return true;
//        }
        String userName = panel.getUserNameText(); 
        String pwd = panel.getPasswordText();
        String configPwd = panel.getConformPwdText();
        String trueName = panel.gettrueNameText();
        String specialties = panel.getSpecialtiesText();
        String email = panel.getEmailText();
        String desc = panel.getDescriptionArea();
//        String companyId = panel.getCompanyId();
        if(new RightControlUtil().isAdminOrDeveloperOrRoot(userName)){
            VCIOptionPane.showMessageDialog(panel,
                    LocaleDisplay.getI18nString("rmip.stafforg.operate.usernameerror1", "RMIPFramework", getLocale()));
            return false;
        }
        if("".equals(userName)){
            VCIOptionPane.showMessageDialog(panel,
                    LocaleDisplay.getI18nString("rmip.stafforg.operate.usernameerror2", "RMIPFramework", getLocale()));
            return false;
        }
        if("".equals(pwd)){
            VCIOptionPane.showMessageDialog(panel,
                    LocaleDisplay.getI18nString("rmip.stafforg.operate.pwderror1", "RMIPFramework", getLocale()));
            return false;
        }
        if ("".equals(configPwd)){
            VCIOptionPane.showMessageDialog(panel,
                    LocaleDisplay.getI18nString("rmip.stafforg.operate.conformpwderror", "RMIPFramework", getLocale()));
            return false;
        }
        if(!configPwd.equals(pwd)){
            VCIOptionPane.showMessageDialog(panel,
                    LocaleDisplay.getI18nString("rmip.stafforg.operate.pwderror2", "RMIPFramework", getLocale()));
            return false;
        }
        if (userName.getBytes().length > 128){
            VCIOptionPane.showMessageDialog(panel,
                    LocaleDisplay.getI18nString("rmip.stafforg.operate.usernameerror3", "RMIPFramework", getLocale()));
            return false;
        }
        if (pwd.getBytes().length > 128){
            VCIOptionPane.showMessageDialog(panel,
                    LocaleDisplay.getI18nString("rmip.stafforg.operate.pwderror3", "RMIPFramework", getLocale()));
            return false;
        }
        if (trueName.getBytes().length > 64){
            VCIOptionPane.showMessageDialog(panel,
                    LocaleDisplay.getI18nString("rmip.stafforg.operate.firstnameerror", "RMIPFramework", getLocale()));
            return false;
        }
        if (specialties.getBytes().length > 255){
            VCIOptionPane.showMessageDialog(panel,
                    LocaleDisplay.getI18nString("rmip.stafforg.operate.specialtieserror", "RMIPFramework", getLocale()));
            return false;
        }
        if (email.getBytes().length > 128){
            VCIOptionPane.showMessageDialog(panel,
                    LocaleDisplay.getI18nString("rmip.stafforg.operate.emailerror", "RMIPFramework", getLocale()));
            return false;
        }
        if (desc.getBytes().length > 255 ){
            VCIOptionPane.showMessageDialog(panel,
                    LocaleDisplay.getI18nString("rmip.stafforg.operate.descerror", "RMIPFramework", getLocale()));
            return false;
        }
        
        String regEx = "^[A-Za-z0-9_]+$";
        if (!TransmitMethod.checkCharacter(userName, regEx)) {
            VCIOptionPane.showMessageDialog(panel, 
                    LocaleDisplay.getI18nString("rmip.stafforg.operate.usernameerror4", "RMIPFramework", getLocale()));
            return false;
        }
        if(trueName.equals("")){
                VCIOptionPane.showMessageDialog(panel,
                        LocaleDisplay.getI18nString("rmip.stafforg.operate.firstnameerror1", "RMIPFramework", getLocale()));
                return false;
        }
//        if("".equals(email)){
//            VCIOptionPane.showMessageDialog(panel,"请输入电子邮箱地址!");
//            return false;
//        }
 
        
        if (!email.equals("")){
            regEx = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
            if (!TransmitMethod.checkCharacter(email, regEx)) {
                VCIOptionPane.showMessageDialog(panel, 
                        LocaleDisplay.getI18nString("rmip.stafforg.operate.emailerror1", "RMIPFramework", getLocale()));
                return false;
            }
        }
 
        boolean rs = checkName(userName);
        if (!rs) {
            return rs;
        }
        //add by caill start 2016.12.8 创建或修改用户时增加对密码策略的校验
        
        UserEntityObject userEntityObj = LogonApplication.getUserEntityObject();
        UserObject userObject=null;
        try {
            RightManagementClientDelegate rcDelegate = new RightManagementClientDelegate(userEntityObj);
            userObject  = rcDelegate.fetchUserInfoByName(userEntityObj.getUserName());
            
            String error = rcDelegate.checkPasswordStrategyByUserId(userObject.getId(), pwd);
            if (!StringUtils.isBlank(error)) {
                VCIOptionPane.showMessageDialog(panel, error);
                return false;
            }
        } catch (VCIException e1) {
            // TODO Auto-generated catch block
            //e1.printStackTrace();
            VCIOptionPane.showMessageDialog(panel, "检查密码策略符合情况失败!2");
            return false;
        }
        
//        System.out.println("======检查密码是否符合密码策略");
//
//        CheckPassWordStrategy checkPWS = new CheckPassWordStrategy();
//        boolean pWSCheck = checkPWS.passwordStrategyCheck(userObject.getId(),pwd,panel); //对密码策略进行校验
//        return pWSCheck;
        //add by caill end
        return true;
    }
    
    /***
     *  看用户名是否重复
     * @param name 用户名
     * @return
     */
    private boolean checkName(String name) {
        UserObject[] userInfo = null;
        try {
            userInfo = new RightManagementClientDelegate(LogonApplication.getUserEntityObject()).fetchUserInfo();
        
        int size = userInfo.length;
        for(int i = 0; i < size; i++){
            if (name.equals(userInfo[i].getUserName())) {
                if("edit".equalsIgnoreCase(type)){ //如果是要进行修改,名称不变的情况下可以成功修改,这种情况下不是名称重复。
                    if (userObj.getId().equals(userInfo[i].getId())) {
                        continue;
                    }
                }
                VCIOptionPane.showMessageDialog(panel,
                        "该用户名在系统中已经存在,请修改!");
                return false;
                
                
            }
            
        }
        } catch (VCIException e) {
            VCIOptionPane.showError(LogonApplication.frame,"RMIPFramework", e);
            return false;
        }
        return true;
    }
    
    
    /***
     * 得到操作对象的用户信息
     *
     */
    private UserObject getUserInfo() {
        UserObject userInfo = new UserObject();
        userInfo.setSecretGrade(panel.getSecurity());
        userInfo.setUserName(panel.getUserNameText());
        userInfo.setPwd(panel.getPasswordText());
        userInfo.setEmail(panel.getEmailText());
        userInfo.setTrueName(panel.gettrueNameText());
        userInfo.setSpecialties(panel.getSpecialtiesText());
        userInfo.setDesc(panel.getDescriptionArea());
        userInfo.setPwdUpdateTime(0);//"1970-1-1 00:00:00");
        if("edit".equalsIgnoreCase(type)){
            userInfo.setId(userObj.getId());
            if (!panel.getPasswordText().equals(userObj.getPwd())){
                userInfo.setPwdUpdateTime(0);//"1970-1-1 00:00:00");
            }else {
                userInfo.setPwdUpdateTime(userObj.getPwdUpdateTime());
            }
            userInfo.setGrantor(userObj.getGrantor());
        }
        userInfo.setUserType(logonUserInfo.getUserType());
        userInfo.setCreateTime(System.currentTimeMillis());
        userInfo.setCreateUser(userName);
        userInfo.setUpdateTime(System.currentTimeMillis());
        userInfo.setUpdateUser(userName);
           return userInfo;
    }
    
    /**
     * 注册成员添加事件
     */
    private void add_actionPerformed() {
        try{
            logonUserInfo = new RightManagementClientDelegate(LogonApplication.getUserEntityObject()).fetchUserInfoByName(userName);
        }catch(VCIException e) {
            VCIOptionPane.showError(LogonApplication.frame,"RMIPFramework", e);
            return;
        }
        
        boolean rs = check();
        if (!rs) {
            //VCIOptionPane.showError(LogonApplication.frame,"RMIPFramework", e);
            VCIOptionPane.showMessageDialog(LogonApplication.frame, "用户信息检查不通过", "用户修改", VCIOptionPane.ERROR_MESSAGE);
            return;
        }
        UserObject userInfo = getUserInfo();
        userInfo.setStatus((short)0);
        String id  = "";
        try {
            id = new RightManagementClientDelegate(LogonApplication.getUserEntityObject()).saveUser(userInfo);
            userInfo.setId(id);
        } catch (VCIException e) {
            VCIOptionPane.showError(LogonApplication.frame,"RMIPFramework",e);
            return;
        }
        
        //保存部门与人员的关系。
        createLink(userInfo);
        VCIOptionPane.showMessageDialog(LogonApplication.frame, 
                LocaleDisplay.getI18nString("rmip.stafforg.operate.usercreateok", "RMIPFramework", getLocale()));
        panel.dispose();
        userTablePanel.tablePanel.refreshTableData();
    }
    
    /**
     * 保存部门与人员的关系。
     * @return
     */
    private void createLink(UserObject userInfo) {
        String userId = userInfo.getId();
        JComboBox deptBox = panel.getDeptBox();
        String deptId = "";
        if ((DeptObject)deptBox.getSelectedItem() != null && ((DeptObject)deptBox.getSelectedItem()).getId() != null ){
            deptId = ((DeptObject)deptBox.getSelectedItem()).getId();
            if("".equals(deptId) || null == deptId || "null".equals(deptId)) {
                return ;
            }
            try{
               new RightManagementClientDelegate(LogonApplication.getUserEntityObject())
                       .saveUserDept(new String[]{userId},deptId);
            }catch(VCIException e) {
                VCIOptionPane.showError(LogonApplication.frame,"RMIPFramework",e);
                return ;
            }
        }
        return ;
    }
 
    /**
     * 注册成员修改事件
     */
    private void edit_actionPerformed() {
        try{
            logonUserInfo = new RightManagementClientDelegate(LogonApplication.getUserEntityObject()).fetchUserInfoByName(userName);
        }catch(VCIException e) {
            VCIOptionPane.showError(LogonApplication.frame,
                    LocaleDisplay.getI18nString(e, "RMIPFramework", getLocale()));
            return;
        }
        boolean rs = check();
        if (!rs) {
            return;
        }
        UserObject userInfo = getUserInfo();
        //userInfo.setSecretGrade(userObj.getSecretGrade());
        userInfo.setStatus(userObj.getStatus());
        userInfo.setIsDeptLeader(userObj.getIsDeptLeader());
        userInfo.setUserType(userObj.getUserType());
        userInfo.setGrantor(userObj.getGrantor());
 
        try {
            new RightManagementClientDelegate(LogonApplication.getUserEntityObject()).updateUser(userInfo);
            //保存部门与人员的关系。
            createLink(userInfo);
        } catch (VCIException e) {
            VCIOptionPane.showError(LogonApplication.frame,"RMIPFramework", e);
            return;
        }
        panel.dispose();
        userTablePanel.tablePanel.refreshTableData();
        VCIOptionPane.showMessageDialog(LogonApplication.frame, 
                LocaleDisplay.getI18nString("rmip.stafforg.operate.updateok", "RMIPFramework", getLocale()));
    }
    /**
     * 注册成员删除事件
     */
    @SuppressWarnings("unused")
    private void delete_actionPerformed() {
        TreePath[] deleteTreePaths = panel.getTransmitTreeObject().getTree().getSelectionPaths();
        if (deleteTreePaths.length < 1) {
            VCIOptionPane.showMessageDialog(panel, 
                    LocaleDisplay.getI18nString("rmip.stafforg.operate.toselectdel", "RMIPFramework", getLocale()));
            return;
        }
        //2012.01.10 ligang 添加用户时更新用户总数
        int count = 0;
        try{
            logonUserInfo = new RightManagementClientDelegate(LogonApplication.getUserEntityObject()).fetchUserInfoByName(userName);
            count = new RightManagementClientDelegate(LogonApplication.getUserEntityObject()).fetchUserInfoByType(logonUserInfo.getUserType()).length;
        }catch(VCIException e) {
            VCIOptionPane.showError(LogonApplication.frame,
                    LocaleDisplay.getI18nString(e, "RMIPFramework", getLocale()));
            return;
        }
        
        int len = deleteTreePaths.length;
        String[] puids = new String[len];
        VCIBaseTreeNode[] removeNodes = new VCIBaseTreeNode[len];
        for (int i = 0; i < len; i++) {
            VCIBaseTreeNode treeNode = (VCIBaseTreeNode)deleteTreePaths[i].getLastPathComponent();
            boolean canDelete = true; //选择的节点是否可以删除
            UserObject userInfo = null;
            if (!(treeNode.getObj() instanceof UserObject)) {
                canDelete = false;
            } else {
                userInfo = (UserObject) treeNode.getObj();
                if ("root".equals(userInfo.getId())) {
                    canDelete = false;
                }
            }
            if (!canDelete) {
                VCIOptionPane.showMessageDialog(LogonApplication.frame, 
                        LocaleDisplay.getI18nString("rmip.stafforg.operate.toshowdelusererror", "RMIPFramework", getLocale()));
                return;
            }
            
            removeNodes[i] = treeNode;
            puids[i] = userInfo.getId();
        }
        
        int ok=VCIOptionPane.showQuestion(LogonApplication.frame,
                LocaleDisplay.getI18nString("rmip.stafforg.operate.conformdeluser", "RMIPFramework", getLocale()));
       
        if (ok == 0) {
            boolean rs=true;
            try {
                rs = new RightManagementClientDelegate(LogonApplication.getUserEntityObject()).deleteUser(puids);
            } catch (VCIException e) {
                VCIOptionPane.showError(LogonApplication.frame,"RMIPFramework", e);
                return;
            }
            if (!rs) {
                return;
            }
            VCIBaseTreeNode parentNode = (VCIBaseTreeNode) panel.getTransmitTreeObject().getCurrentTreeNode().getParent();
            for (int i = 0; i < len; i++) {
                panel.getTransmitTreeObject().getTreeModel().removeNodeFromParent(removeNodes[i]);
            }
            panel.getTransmitTreeObject().getTree().setSelectionPath(new TreePath(parentNode.getPath()));
            
            panel.getTransmitTreeObject().getTree().updateUI();
            
        }
    }
    
}