许鹏程
2023-05-25 213cc37cbf0b2515a4de56cc1e01813211bad183
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
package com.product.admin.service;
 
import com.product.admin.config.CmnConst;
import com.product.admin.config.SystemCode;
import com.product.admin.service.idel.IPasswordSecurityService;
import com.product.core.config.Global;
import com.product.core.entity.DataTableEntity;
import com.product.core.transfer.Transactional;
import com.product.email.service.SendEmailService;
import com.product.text.message.util.HttpSmsSendUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import com.product.core.dao.BaseDao;
import com.product.core.entity.FieldSetEntity;
import com.product.core.exception.BaseException;
import com.product.core.service.support.AbstractBaseService;
 
import java.io.IOException;
import java.util.Date;
 
/**
 * Copyright  LX-BASE
 * @Title: PasswordSecurityService
 * @Project: LX-BASE-SERVER
 * @Date:  2020-05-30 17:29   
 * @Author: ZhouJie
 * @Description: 密码安全设置    
 */
@Component
public class PasswordSecurityService extends AbstractBaseService implements IPasswordSecurityService {
    @Autowired
    public BaseDao baseDao;
    @Autowired
    public SendEmailService sendEmailService;
 
    /**
     * @Date: 2020-05-30 13:53   
     * @Author: ZhouJie
     * @Description:  查询密码强度信息
     */
    public FieldSetEntity findPwdSecurityInfo(String org_level_uuid) throws BaseException {
        
        FieldSetEntity fs = null;
        baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_PASSWORD_SECURITY,CmnConst.ORG_LEVEL_UUID+" = ? ",new Object[]{ org_level_uuid },true);
//        if(fs == null){
//            fs = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_PASSWORD_SECURITY,CmnConst.UUID+" = ? ",new Object[]{ "security_uuid_01" },true);
//            if(fs == null) {
//                throw new BaseException(SystemCode.SYSTEM_PASSWORD_SECURITY_SET.getValue(), SystemCode.SYSTEM_PASSWORD_SECURITY_SET.getText(), this.getClass(),"checkNewPwd");
//            }
//        }
        DataTableEntity t = baseDao.listTable(CmnConst.PRODUCT_SYS_PASSWORD_SECURITY, CmnConst.ORG_LEVEL_UUID + " = ? or "+CmnConst.ORG_LEVEL_UUID+" is null or "+CmnConst.ORG_LEVEL_UUID+"='' ", new Object[]{org_level_uuid}, CmnConst.ORG_LEVEL_UUID+" desc ");
        if(t!=null && t.getRows()>0) {
            fs=t.getFieldSetEntity(0);
        }
        if (fs == null) {
            fs = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_PASSWORD_SECURITY, "security_uuid_01", false);
        }
        if(fs == null) {
            throw new BaseException(SystemCode.SYSTEM_PASSWORD_SECURITY_NOT_FIND.getValue(), SystemCode.SYSTEM_PASSWORD_SECURITY_NOT_FIND.getText());
        }
        return fs;
    }
    /**
     * @Date: 2020-10-09 13:53
     * @Author: ZhouJie
     * @Description:  新增密码强度信息
     */
    @Transactional
    public String addSecurity(FieldSetEntity fse) throws BaseException {
        FieldSetEntity fsorg = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ORG_LEVELS,fse.getString("org_level_uuid"),false);
        DataTableEntity dt = fse.getSubDataTable(CmnConst.PRODUCT_SYS_PASSWORD_SECURITY_ETAILS);
        if(dt != null && dt.getRows()>0){
            for(int i= 0 ; i<dt.getRows() ; i++){
                FieldSetEntity fsu = dt.getFieldSetEntity(i);
                fsu.remove("uuid");
                fsu.remove("account_lock_id");
                fsu.remove("security_uuid");
            }
        }
        //新增密码强度
        fse.setValue(CmnConst.ORG_LEVEL_UUID,fse.getString(CmnConst.ORG_LEVEL_UUID));
        fse.remove("security_id");
        fse.remove("uuid");
        fse.remove("security_uuid");
        fse.remove("updated_by");
        fse.remove("updated_utc_datetime");
        return baseDao.add(fse);
    }
 
    /**
     * @Date: 2020-10-09 15:53
     * @Author: ZhouJie
     * @Description:  修改密码强度信息
     */
    @Transactional
    public boolean updateSecurity(FieldSetEntity fs) throws BaseException {
        return baseDao.update(fs);
    }
 
    /**
     * 密码强度更新频率提醒
     */
    public void updateFrequency() throws BaseException{
        Date now = new Date();//当前时间
        long day = 0;//最近一次修改密码距今天数
        int limitDay = 0;//公司/组织密码更新频率天数
        Date udate = null;//员工创建时间/员工最新修改密码时间
        Date fedate = null;//第一次发送密码频率提示消息时间
        int userid = 0;
        DataTableEntity dt = baseDao.listTable(CmnConst.PRODUCT_SYS_PASSWORD_SECURITY," org_level_uuid <>?",new Object[]{""});
        if(dt!=null && dt.getRows()>0){
            for (int i = 0; i < dt.getRows(); i++) {
                FieldSetEntity fss = dt.getFieldSetEntity(i);
                limitDay = fss.getInteger("security_update_frequency_day");
                DataTableEntity dtstaff = baseDao.listTable("product_sys_staffs"," org_level_uuid=? ",new Object[]{fss.getString("org_level_uuid")} );
                if(dtstaff!=null && dtstaff.getRows()>0 && fss.getInteger("security_update_frequency")==1){
                    for (int j = 0; j < dtstaff.getRows(); j++) {
                        FieldSetEntity fsstaff = dtstaff.getFieldSetEntity(j);
                        userid = fsstaff.getInteger("user_id");
                        FieldSetEntity fuser = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_USERS,"user_id=?",new Object[]{userid},false);
                        udate = fsstaff.getDate("created_utc_datetime");
                        fedate = fuser.getDate("first_email_time");
                        String sql = " select user_id,MAX(created_utc_datetime) created_utc_datetime FROM product_sys_password_security_history where user_id=? GROUP BY user_id ";
                        //判断员工是否修改过密码
                        DataTableEntity dthistory = baseDao.listTable(sql,new Object[]{userid});
                        if(dthistory!=null && dthistory.getRows()>0){
                            for (int k = 0; k < dthistory.getRows(); k++) {
                                udate = dthistory.getFieldSetEntity(k).getDate("created_utc_datetime");
                            }
                        }
                        day = (now.getTime() - udate.getTime()) / (1000 * 60 * 60 * 24);
                        if(day>=limitDay){
                            if(fedate==null || "".equals(fedate)){
                                fuser.setValue("first_email_time",new Date());
                                baseDao.update(fuser);
                            }
                            try {
                                sendMessage(fuser,fss);
                            } catch (Exception e) {
                                continue;
                            }
                        }
                    }
                }
            }
        }
    }
    /**
     * 密码强度更新频率消息发送
     */
    public void sendMessage(FieldSetEntity fs,FieldSetEntity fss) throws IOException {
        Date now = new Date();//当前时间
        int lastDay = Integer.parseInt(Global.getSystemConfig("password.security.day",""));//提示n天后未改密码,锁定账号
        Date fedate = fs.getDate("first_email_time");
        long day = (now.getTime() - fedate.getTime()) / (1000 * 60 * 60 * 24);
        if (fs.getInteger("status") == 1 && fss.getInteger("security_notice_email")==1) {
            if (day<lastDay) {
                sendEmailService.parseMailTemplate("MMGXPLTX", fs);
            }
            if (day==lastDay) {
                sendEmailService.parseMailTemplate("MMPLSDTX", fs);
            }
            if (day>lastDay) {
                fs.setValue("status", 0);
                fs.setValue("first_email_time","");
                baseDao.update(fs);
                sendEmailService.parseMailTemplate("MMPLSDTZ", fs);
            }
        }
        if(fs.getInteger("status") == 1 && fss.getInteger("security_notice_sms")==1 && fss.getString("user_phone_number") != null){
            if (day<lastDay) {
                HttpSmsSendUtil.sendMsg(fss.getString("user_phone_number"),"账号"+fss.getString("user_account")+"的密码安全设定时间已到期,为了您的账户安全,请您尽快登陆修改密码!");
            }
            if (day==lastDay) {
                HttpSmsSendUtil.sendMsg(fss.getString("user_phone_number"),"账号"+fss.getString("user_account")+"的密码安全设定时间已到期,请您尽快登陆修改密码,若24小时内您仍未修改密码账号将被锁定!");
            }
            if (day>lastDay) {
                fs.setValue("status", 0);
                fs.setValue("first_email_time","因未按时修改密码,账号"+fss.getString("user_account")+"已被锁定,解锁请联系管理员处理!");
                baseDao.update(fs);
                HttpSmsSendUtil.sendMsg(fss.getString("user_phone_number"),"");
            }
        }
    }
 
 
    
}