jiazx0107@163.com
2023-05-17 620eab6cca2bc9ef9ea6d3067a0a5ba1deadbd1c
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
package com.ld.igds.sys.service;
 
import com.bstek.bdf2.core.model.DefaultUser;
import com.bstek.bdf2.core.orm.hibernate.HibernateDao;
import com.bstek.dorado.data.provider.Page;
import com.ld.igds.data.BaseParam;
import com.ld.igds.sys.data.SysUserData;
import com.ld.igds.sys.mapper.SysUserMapper;
import com.ld.igds.util.ContextUtil;
import org.apache.commons.lang.math.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.security.authentication.encoding.PasswordEncoder;
import org.springframework.stereotype.Repository;
 
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@SuppressWarnings("deprecation")
@Repository
public class SysUserService extends HibernateDao {
 
    @Resource
    private SysUserMapper sysUserMapper;
 
    @Resource(name = "bdf2.passwordEncoder")
    private PasswordEncoder passwordEncoder;
 
    public void pageUser(Page<DefaultUser> page, Map<String, Object> param)
            throws Exception {
 
        String hql = " from " + DefaultUser.class.getName()
                + " where companyId=:companyId ";
 
        String countHql = " select count(*) " + hql;
 
        hql += " order by enabled desc,username";
 
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("companyId", ContextUtil.getCompanyId());
 
        this.pagingQuery(page, hql, countHql, args);
    }
 
    public String addUser(DefaultUser user) {
        // 判断是否重复
        boolean b = this.userIsExists(user.getUsername());
        if (b) {
            return "当前用户已经存在,不支持相同帐号录入!!";
        }
        Session session = this.getSessionFactory().openSession();
        try {
            if (null == user.getCreateDate()) {
                user.setCreateDate(new Date());
            }
            String salt = String.valueOf(RandomUtils.nextInt(100));
            String password = passwordEncoder.encodePassword(
                    user.getPassword(), salt);
            user.setPassword(password);
            user.setSalt(salt);
 
            session.save(user);
 
        } catch (Exception e) {
            return "系统异常:" + e.getMessage();
        } finally {
            session.flush();
            session.close();
        }
        return null;
    }
 
    public String updateUser(DefaultUser user) {
        Session session = this.getSessionFactory().openSession();
        try {
            if (null == user.getCreateDate()) {
                user.setCreateDate(new Date());
            }
            session.update(user);
        } catch (Exception e) {
            return "系统异常:" + e.getMessage();
        } finally {
            session.flush();
            session.close();
        }
        return null;
    }
 
    public String resetPwd(String companyId, String username, String newPassword) {
 
        if (username.equals(companyId) && null == newPassword)
            return "系统管理员用户密码不允许被重置!";
 
        if (null == newPassword)
            newPassword = "abc123";
 
        String salt = String.valueOf(RandomUtils.nextInt(100));
        String newPwd = passwordEncoder.encodePassword(newPassword, salt);
        String hql = " update " + DefaultUser.class.getName()
                + " set password=:newPwd,salt=:salt "
                + "where companyId=:companyId and username=:username";
 
        Session session = this.getSessionFactory().openSession();
        try {
            Query query = session.createQuery(hql);
            query.setString("newPwd", newPwd);
            query.setString("salt", salt);
            query.setString("companyId", companyId);
            query.setString("username", username);
            query.executeUpdate();
 
        } catch (Exception e) {
            return "系统异常:" + e.getMessage();
        } finally {
            session.flush();
            session.close();
        }
        return "新密码为:" + newPassword;
    }
 
    public boolean userIsExists(String username) {
        String hql = " from " + DefaultUser.class.getName()
                + " where username=:username ";
 
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("username", username);
        List<DefaultUser> list = this.query(hql, args);
 
        if (null == list || list.isEmpty()) {
            return false;
        }
        return true;
    }
 
    /**
     * 获取的用户必须是在职的
     *
     * @param username
     * @return
     */
    public DefaultUser loadUser(String username) {
        String hql = " from " + DefaultUser.class.getName()
                + " where username=:username";
        Map<String, Object> args = new HashMap<>();
        args.put("username", username);
        List<DefaultUser> list = this.query(hql, args);
        if (null == list || list.isEmpty())
            return null;
        return list.get(0);
    }
 
    /**
     * @param username 登陆帐号
     * @param inputPwd 输入的密码
     * @param user     登陆的用户
     * @return
     */
    public boolean checkPassword(String username, String inputPwd, DefaultUser user) {
        return passwordEncoder.isPasswordValid(user.getPassword(), inputPwd,
                user.getSalt());
    }
 
    public String updatePwd(Map<String, Object> param) {
        String username = (String) param.get("username");
        String password = (String) param.get("password");
        String newPassword = (String) param.get("newPassword");
        String confirmPassword = (String) param.get("confirmPassword");
 
        if (!newPassword.equals(confirmPassword)) {
            return "两次输入的新密码不一致,请确认!";
        }
 
        if (password.equals(newPassword)) {
            return "新密码与旧密码一致,请修改!";
        }
 
        DefaultUser user = loadUser(username);
        if (null == user) {
            return "系统检测到当前用户已不存在,无法执行修改!";
        }
 
        boolean check = checkPassword(username, password, user);
        if (!check) {
            return "你输入的旧密码不正确,无法执行修改!";
        }
 
        return resetPwd(user.getCompanyId(), username, newPassword);
    }
 
    public List<DefaultUser> loadUsers(Map<String, Object> param) {
 
        String hql = " from " + DefaultUser.class.getName()
                + " where companyId=:companyId ";
 
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("companyId", ContextUtil.getCompanyId());
 
        String key = (String) param.get("key");
        if (StringUtils.isNotEmpty(key)) {
            hql += "and (username like :key1 or cname like :key2)";
            args.put("key1", "%" + key + "%");
            args.put("key2", "%" + key + "%");
        }
 
        List<DefaultUser> list = this.query(hql, args);
 
        if (null == list || list.isEmpty())
            return null;
        return list;
    }
 
    public List<SysUserData> queryUserList(BaseParam param) {
        return sysUserMapper.queryUserList(param);
    }
}