Commit 6fbb49a305bee6a0959a752c3514b945d704fca3
1 parent
4908fce0
feature: Login/Logout und Session-Verwaltung
Showing
5 changed files
with
93 additions
and
6 deletions
src/main/java/net/ziemers/swxercise/db/dao/user/UserDao.java
@@ -20,4 +20,16 @@ public class UserDao extends GenericDao { | @@ -20,4 +20,16 @@ public class UserDao extends GenericDao { | ||
20 | return user; | 20 | return user; |
21 | } | 21 | } |
22 | 22 | ||
23 | + public User findByUsername(final String username) { | ||
24 | + User user = null; | ||
25 | + | ||
26 | + try { | ||
27 | + // ermittelt den ersten Datensatz mit dem gesuchten Benutzernamen, auch wenn er sich nicht im Persistence Context befindet | ||
28 | + user = (User) entityManager.createNamedQuery("User.findByUsername").setParameter("username", username).getSingleResult(); | ||
29 | + } catch(Exception e) { | ||
30 | + /* nix */ | ||
31 | + } | ||
32 | + return user; | ||
33 | + } | ||
34 | + | ||
23 | } | 35 | } |
src/main/java/net/ziemers/swxercise/lg/model/user/Session.java
0 → 100644
1 | +package net.ziemers.swxercise.lg.model.user; | ||
2 | + | ||
3 | +import javax.enterprise.context.SessionScoped; | ||
4 | + | ||
5 | +@SessionScoped | ||
6 | +public class Session implements java.io.Serializable { | ||
7 | + | ||
8 | + private User user = null; | ||
9 | + | ||
10 | + public User getUser() { | ||
11 | + return user; | ||
12 | + } | ||
13 | + | ||
14 | + public void setUser(User user) { | ||
15 | + this.user = user; | ||
16 | + } | ||
17 | + | ||
18 | +} |
src/main/java/net/ziemers/swxercise/lg/model/user/User.java
@@ -8,7 +8,7 @@ import net.ziemers.swxercise.db.BaseEntity; | @@ -8,7 +8,7 @@ import net.ziemers.swxercise.db.BaseEntity; | ||
8 | @Entity | 8 | @Entity |
9 | @NamedQueries({ | 9 | @NamedQueries({ |
10 | @NamedQuery(name = "User.findById", query = "SELECT u FROM User u WHERE u.id = :id"), | 10 | @NamedQuery(name = "User.findById", query = "SELECT u FROM User u WHERE u.id = :id"), |
11 | - @NamedQuery(name = "User.findAll", query = "SELECT u FROM User u")}) | 11 | + @NamedQuery(name = "User.findByUsername", query = "SELECT u FROM User u WHERE u.profile.username = :username")}) |
12 | public class User extends BaseEntity { | 12 | public class User extends BaseEntity { |
13 | 13 | ||
14 | private String firstname; | 14 | private String firstname; |
src/main/java/net/ziemers/swxercise/lg/user/service/UserService.java
@@ -7,6 +7,7 @@ import javax.inject.Inject; | @@ -7,6 +7,7 @@ import javax.inject.Inject; | ||
7 | 7 | ||
8 | import net.ziemers.swxercise.db.dao.user.UserDao; | 8 | import net.ziemers.swxercise.db.dao.user.UserDao; |
9 | import net.ziemers.swxercise.lg.model.user.Profile; | 9 | import net.ziemers.swxercise.lg.model.user.Profile; |
10 | +import net.ziemers.swxercise.lg.model.user.Session; | ||
10 | import net.ziemers.swxercise.lg.model.user.User; | 11 | import net.ziemers.swxercise.lg.model.user.User; |
11 | import net.ziemers.swxercise.lg.user.dto.UserDto; | 12 | import net.ziemers.swxercise.lg.user.dto.UserDto; |
12 | 13 | ||
@@ -16,6 +17,24 @@ public class UserService { | @@ -16,6 +17,24 @@ public class UserService { | ||
16 | @Inject | 17 | @Inject |
17 | private UserDao dao; | 18 | private UserDao dao; |
18 | 19 | ||
20 | + @Inject | ||
21 | + private Session session; | ||
22 | + | ||
23 | + public boolean loginUser(UserDto dto) { | ||
24 | + final User user = dao.findByUsername(dto.getUsername()); | ||
25 | + | ||
26 | + if (user != null && user.getProfile().isValidPassword(dto.getPassword())) { | ||
27 | + session.setUser(user); | ||
28 | + return true; | ||
29 | + } | ||
30 | + return false; | ||
31 | + } | ||
32 | + | ||
33 | + public boolean logoutUser() { | ||
34 | + session.setUser(null); | ||
35 | + return session.getUser() == null; | ||
36 | + } | ||
37 | + | ||
19 | public User findUser(final Long id) { | 38 | public User findUser(final Long id) { |
20 | return dao.findById(id); | 39 | return dao.findById(id); |
21 | } | 40 | } |
@@ -38,4 +57,8 @@ public class UserService { | @@ -38,4 +57,8 @@ public class UserService { | ||
38 | dao.remove(User.class, id); | 57 | dao.remove(User.class, id); |
39 | } | 58 | } |
40 | 59 | ||
60 | + public User getSessionUser() { | ||
61 | + return session.getUser(); | ||
62 | + } | ||
63 | + | ||
41 | } | 64 | } |
src/main/java/net/ziemers/swxercise/ui/UserViewController.java
@@ -59,10 +59,10 @@ public class UserViewController { | @@ -59,10 +59,10 @@ public class UserViewController { | ||
59 | */ | 59 | */ |
60 | @POST | 60 | @POST |
61 | @Consumes(MediaType.APPLICATION_JSON) | 61 | @Consumes(MediaType.APPLICATION_JSON) |
62 | - @Produces({MediaType.TEXT_PLAIN}) | 62 | + @Produces(MediaType.TEXT_PLAIN) |
63 | public String createUser(UserDto dto) throws Exception { | 63 | public String createUser(UserDto dto) throws Exception { |
64 | - userService.createUser(dto); | ||
65 | - return "Ok"; | 64 | + final Long id = userService.createUser(dto); |
65 | + return String.format("Ok (#%d)", id); | ||
66 | } | 66 | } |
67 | 67 | ||
68 | /** | 68 | /** |
@@ -78,7 +78,7 @@ public class UserViewController { | @@ -78,7 +78,7 @@ public class UserViewController { | ||
78 | @PUT | 78 | @PUT |
79 | @Path("{id}") | 79 | @Path("{id}") |
80 | @Consumes(MediaType.APPLICATION_JSON) | 80 | @Consumes(MediaType.APPLICATION_JSON) |
81 | - @Produces({MediaType.TEXT_PLAIN}) | 81 | + @Produces(MediaType.TEXT_PLAIN) |
82 | public String updateUser(@PathParam("id") Long id) throws Exception { | 82 | public String updateUser(@PathParam("id") Long id) throws Exception { |
83 | // TODO noch zu implementieren | 83 | // TODO noch zu implementieren |
84 | return "Ok"; | 84 | return "Ok"; |
@@ -92,10 +92,44 @@ public class UserViewController { | @@ -92,10 +92,44 @@ public class UserViewController { | ||
92 | */ | 92 | */ |
93 | @DELETE | 93 | @DELETE |
94 | @Path("{id}") | 94 | @Path("{id}") |
95 | - @Produces({MediaType.TEXT_PLAIN}) | 95 | + @Produces(MediaType.TEXT_PLAIN) |
96 | public String deleteUser(@PathParam("id") Long id) { | 96 | public String deleteUser(@PathParam("id") Long id) { |
97 | userService.deleteUser(id); | 97 | userService.deleteUser(id); |
98 | return "Ok"; | 98 | return "Ok"; |
99 | } | 99 | } |
100 | 100 | ||
101 | + /** | ||
102 | + * Meldet einen Benutzer durch übergebenen username und password mit einer neuen User-{@link net.ziemers.swxercise.lg.model.user.Session} an. | ||
103 | + * | ||
104 | + * @param dto das mittels der als JSON-Objekt übergebenenen Eigenschaften zu füllende {@link UserDto} | ||
105 | + * @return "Ok", wenn die Erstellung des User-Objekts erfolgreich war. | ||
106 | + */ | ||
107 | + @POST | ||
108 | + @Path("/login") | ||
109 | + @Consumes(MediaType.APPLICATION_JSON) | ||
110 | + @Produces(MediaType.TEXT_PLAIN) | ||
111 | + public String loginUser(UserDto dto) { | ||
112 | + if (userService.loginUser(dto)) { | ||
113 | + final User user = userService.getSessionUser(); | ||
114 | + return String.format("Ok (%s %s)", user.getFirstname(), user.getLastname()); | ||
115 | + } | ||
116 | + return "Failed"; | ||
117 | + } | ||
118 | + | ||
119 | + /** | ||
120 | + * Meldet den angemeldeten Benutzer von seiner User-{@link net.ziemers.swxercise.lg.model.user.Session} ab. | ||
121 | + * | ||
122 | + * @return "Ok", wenn die Erstellung des User-Objekts erfolgreich war. | ||
123 | + */ | ||
124 | + @POST | ||
125 | + @Path("/logout") | ||
126 | + @Consumes(MediaType.APPLICATION_JSON) | ||
127 | + @Produces(MediaType.TEXT_PLAIN) | ||
128 | + public String logoutUser() { | ||
129 | + if (userService.logoutUser()) { | ||
130 | + return "Ok"; | ||
131 | + } | ||
132 | + return "Failed"; | ||
133 | + } | ||
134 | + | ||
101 | } | 135 | } |