1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.shiro.samples.sprhib.service; |
20 | |
|
21 | |
import org.apache.shiro.SecurityUtils; |
22 | |
import org.apache.shiro.crypto.hash.Sha256Hash; |
23 | |
import org.apache.shiro.samples.sprhib.dao.UserDAO; |
24 | |
import org.apache.shiro.samples.sprhib.model.User; |
25 | |
import org.springframework.beans.factory.annotation.Autowired; |
26 | |
import org.springframework.stereotype.Service; |
27 | |
import org.springframework.transaction.annotation.Transactional; |
28 | |
|
29 | |
import java.util.List; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
@Transactional |
36 | |
@Service("userService") |
37 | 0 | public class DefaultUserService implements UserService { |
38 | |
|
39 | |
private UserDAO userDAO; |
40 | |
|
41 | |
@Autowired |
42 | |
public void setUserDAO(UserDAO userDAO) { |
43 | 0 | this.userDAO = userDAO; |
44 | 0 | } |
45 | |
|
46 | |
public User getCurrentUser() { |
47 | 0 | final Long currentUserId = (Long) SecurityUtils.getSubject().getPrincipal(); |
48 | 0 | if( currentUserId != null ) { |
49 | 0 | return getUser(currentUserId); |
50 | |
} else { |
51 | 0 | return null; |
52 | |
} |
53 | |
} |
54 | |
|
55 | |
public void createUser(String username, String email, String password) { |
56 | 0 | User user = new User(); |
57 | 0 | user.setUsername(username); |
58 | 0 | user.setEmail(email); |
59 | 0 | user.setPassword( new Sha256Hash(password).toHex() ); |
60 | 0 | userDAO.createUser( user ); |
61 | 0 | } |
62 | |
|
63 | |
public List<User> getAllUsers() { |
64 | 0 | return userDAO.getAllUsers(); |
65 | |
} |
66 | |
|
67 | |
public User getUser(Long userId) { |
68 | 0 | return userDAO.getUser(userId); |
69 | |
} |
70 | |
|
71 | |
public void deleteUser(Long userId) { |
72 | 0 | userDAO.deleteUser( userId ); |
73 | 0 | } |
74 | |
|
75 | |
public void updateUser(User user) { |
76 | 0 | userDAO.updateUser( user ); |
77 | 0 | } |
78 | |
|
79 | |
} |