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
package com.bstek.bdf2.core.service.impl;
 
import com.bstek.bdf2.core.business.IUser;
import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.bdf2.core.model.DefaultUser;
import com.bstek.bdf2.core.orm.hibernate.HibernateDao;
import com.bstek.bdf2.core.service.IUserService;
import com.bstek.dorado.core.resource.ResourceManager;
import com.bstek.dorado.core.resource.ResourceManagerUtils;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;
 
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.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
 
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
 
/**
 * @author Jacky.gao
 * @since 2013-1-18
 */
@SuppressWarnings("deprecation")
public class DefaultUserService extends HibernateDao implements IUserService {
 
    private PasswordEncoder passwordEncoder;
    
    private static final ResourceManager resourceManager = ResourceManagerUtils
            .get(DefaultUserService.class);
 
    public UserDetails loadUserByUsername(String username)
            throws UsernameNotFoundException {
 
        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 (list.size() == 0) {
            throw new UsernameNotFoundException("User " + username
                    + " is not exist");
        } else {
            return list.get(0);
        }
    }
 
    public void loadPageUsers(Page<IUser> page, String companyId, Criteria criteria) {
 
        try {
            String hql = " from " + DefaultUser.class.getName() + " where companyId=:companyId";
            Map<String, Object> args = new HashMap<>();
            args.put("companyId", companyId);
 
            String countHql = "select count(*) " + hql;
            this.pagingQuery(page, hql, countHql, args);
 
        } catch (Exception e) {
        }
    }
 
 
    public Collection<IUser> loadUsersByDeptId(String deptId) {
        String hql = " from " + DefaultUser.class.getName() + " where deptId=:deptId";
        Map<String, Object> args = new HashMap<>();
        args.put("deptId", deptId);
 
        return this.query(hql, args);
    }
 
    public void changePassword(String username, String newPassword) {
 
        String hql = " update " + DefaultUser.class.getName() + " set password=:newPassword,salt=:salt where username=:username";
        Session session = this.getSessionFactory().openSession();
        try {
 
            Query query = session.createQuery(hql);
 
            int salt = RandomUtils.nextInt(1000);
            query.setString("newPassword", newPassword);
            query.setString("salt", salt + "");
            query.setString("username", username);
 
            query.executeUpdate();
        } catch (Exception e) {
 
        } finally {
            session.flush();
            session.close();
        }
    }
 
    public String checkPassword(String username, String password) {
        DefaultUser user = (DefaultUser) ContextHolder.getLoginUser();
        String salt = user.getSalt();
        if (!passwordEncoder
                .isPasswordValid(user.getPassword(), password, salt)) {
            return resourceManager.getString("bdf2.core/passwordIncorrect");
        } else {
            return null;
        }
    }
 
    public boolean checkPassword(String username, String password,
                                 DefaultUser user) {
        return passwordEncoder.isPasswordValid(user.getPassword(), password,
                user.getSalt());
    }
 
    public IUser newUserInstance(String username) {
        DefaultUser user = new DefaultUser();
        user.setUsername(username);
        return user;
    }
 
    public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
        this.passwordEncoder = passwordEncoder;
    }
 
    public void registerAdministrator(String username, String cname,
                                      String ename, String password, String email, String mobile,
                                      String companyId) {
 
        int salt = RandomUtils.nextInt(1000);
        password = passwordEncoder.encodePassword(password, salt);
 
        DefaultUser defaultUser = new DefaultUser();
        defaultUser.setUsername(username);
        defaultUser.setCname(cname);
        defaultUser.setPassword(password);
        defaultUser.setSalt(salt + "");
        defaultUser.setEmail(email);
        defaultUser.setMobile(mobile);
        defaultUser.setCompanyId(companyId);
        defaultUser.setAdministrator(false);
 
        Session session = this.getSessionFactory().openSession();
 
        try {
            session.save(defaultUser);
        } catch (Exception e) {
 
        } finally {
            session.flush();
            session.close();
        }
    }
 
    @Override
    public void changeUserInfo(String username, String newPassword,
                               String cname, String mobile) {
 
        String hql = " update " + DefaultUser.class.getName() + " set ";
        Session session = this.getSessionFactory().openSession();
        try {
            int salt = RandomUtils.nextInt(1000);
            if (StringUtils.isNotEmpty(newPassword)) {
 
                hql += " password=:newPassword,salt=:salt ";
                newPassword = passwordEncoder.encodePassword(newPassword, salt);
            }
 
            if (StringUtils.isNotEmpty(cname)) {
                hql += " cname=:cname,";
            }
 
            if (StringUtils.isNotEmpty(mobile)) {
                hql += " mobile=:mobile,";
            }
 
            hql += " where username=:username";
 
            Query query = session.createQuery(hql);
 
            query.setString("password", newPassword);
            query.setString("salt", salt + "");
            query.setString("cname", cname);
            query.setString("mobile", mobile);
            query.setString("username", username);
 
            query.executeUpdate();
 
        } catch (Exception e) {
 
        } finally {
            session.flush();
            session.close();
        }
    }
 
    @Override
    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) return null;
 
        return list.get(0);
    }
}