Revize 042274e9
Přidáno uživatelem Jiri Trefil před asi 2 roky(ů)
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/controller/UserController.java | ||
---|---|---|
1 |
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.controller; |
|
2 |
|
|
3 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.User; |
|
4 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.UserService; |
|
5 |
import org.springframework.beans.factory.annotation.Autowired; |
|
6 |
import org.springframework.web.bind.annotation.*; |
|
7 |
|
|
8 |
/** |
|
9 |
* Controller for providing basic support for user management |
|
10 |
* @version 1.0 |
|
11 |
* @author Vaclav Hrabik, Jiri Trefil |
|
12 |
*/ |
|
13 |
@RestController |
|
14 |
@RequestMapping("v2/user") |
|
15 |
public class UserController { |
|
16 |
|
|
17 |
/** |
|
18 |
* Service for work with user management |
|
19 |
*/ |
|
20 |
@Autowired |
|
21 |
private UserService userService; |
|
22 |
|
|
23 |
/** |
|
24 |
* Method to register new user in the app |
|
25 |
* @param user User who wants register in to the app |
|
26 |
* @return message |
|
27 |
*/ |
|
28 |
@PostMapping(value = "/register") |
|
29 |
public String registerUser(@RequestBody User user) { |
|
30 |
int statusCode = userService.registerUser(user); |
|
31 |
return "user register"; |
|
32 |
} |
|
33 |
|
|
34 |
/** |
|
35 |
* Method to login user in to the app |
|
36 |
* @param user User who wants to login in to the app |
|
37 |
* @return message |
|
38 |
*/ |
|
39 |
@PostMapping(value = "/login") |
|
40 |
public String loginUser(@RequestBody User user) { |
|
41 |
int statusCode = userService.loginUser(user); |
|
42 |
return "user logged"; |
|
43 |
} |
|
44 |
|
|
45 |
/** |
|
46 |
* Method to logout user from the app |
|
47 |
* @param user User who wants logout from the app |
|
48 |
* @return message after logout |
|
49 |
*/ |
|
50 |
@PostMapping(value = "/logout") |
|
51 |
public String logoutUser(@RequestBody User user) { |
|
52 |
int statusCode = userService.logoutUser(user); |
|
53 |
return "user logout"; |
|
54 |
} |
|
55 |
|
|
56 |
} |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/model/User.java | ||
---|---|---|
1 |
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.model; |
|
2 |
|
|
3 |
import javax.persistence.*; |
|
4 |
|
|
5 |
/** |
|
6 |
* Model of database table represents one user |
|
7 |
* @version 1.0 |
|
8 |
* @author Vaclav Hrabik, Jiri Trefil |
|
9 |
*/ |
|
10 |
@Entity |
|
11 |
public class User { |
|
12 |
|
|
13 |
/** |
|
14 |
* Max length of column in the table |
|
15 |
*/ |
|
16 |
@Transient |
|
17 |
private static final int MAX_COLUMN_LENGTH = 255; |
|
18 |
/** |
|
19 |
* Unique key of table |
|
20 |
*/ |
|
21 |
@Id |
|
22 |
@GeneratedValue(strategy = GenerationType.AUTO) |
|
23 |
private int id; |
|
24 |
|
|
25 |
/** |
|
26 |
* Name of the User |
|
27 |
*/ |
|
28 |
@Column(unique = true,length = MAX_COLUMN_LENGTH) |
|
29 |
private String name; |
|
30 |
|
|
31 |
/** |
|
32 |
* Email of the User |
|
33 |
*/ |
|
34 |
@Column(length = MAX_COLUMN_LENGTH) |
|
35 |
private String email; |
|
36 |
|
|
37 |
|
|
38 |
/** |
|
39 |
* Getter of id |
|
40 |
* @return id of the user |
|
41 |
*/ |
|
42 |
public int getId() { |
|
43 |
return id; |
|
44 |
} |
|
45 |
|
|
46 |
/** |
|
47 |
* Getter of name |
|
48 |
* @return name of the user |
|
49 |
*/ |
|
50 |
public String getName() { |
|
51 |
return name; |
|
52 |
} |
|
53 |
|
|
54 |
/** |
|
55 |
* Getter of email |
|
56 |
* @return email of the user |
|
57 |
*/ |
|
58 |
public String getEmail() { |
|
59 |
return email; |
|
60 |
} |
|
61 |
|
|
62 |
/** |
|
63 |
* Getter for email constraint |
|
64 |
* @return email constraint |
|
65 |
*/ |
|
66 |
public static int getEmailConstraint(){ |
|
67 |
return MAX_COLUMN_LENGTH; |
|
68 |
} |
|
69 |
|
|
70 |
/** |
|
71 |
* Getter for name constraint |
|
72 |
* @return name constraint |
|
73 |
*/ |
|
74 |
public static int getNameConstraint(){ |
|
75 |
return MAX_COLUMN_LENGTH; |
|
76 |
} |
|
77 |
|
|
78 |
} |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/repository/UserRepository.java | ||
---|---|---|
1 |
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.repository; |
|
2 |
|
|
3 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.User; |
|
4 |
import org.springframework.data.jpa.repository.JpaRepository; |
|
5 |
import org.springframework.stereotype.Repository; |
|
6 |
|
|
7 |
import java.util.List; |
|
8 |
|
|
9 |
/** |
|
10 |
* Interface as database object |
|
11 |
* @version 1.0 |
|
12 |
* @author Vaclav Hrabik, Jiri Trefil |
|
13 |
*/ |
|
14 |
@Repository |
|
15 |
public interface UserRepository extends JpaRepository<User, Integer> { |
|
16 |
List<User> findUserByEmail(String email); |
|
17 |
User findByName(String name); |
|
18 |
} |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/UserService.java | ||
---|---|---|
1 |
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.service; |
|
2 |
|
|
3 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.User; |
|
4 |
|
|
5 |
/** |
|
6 |
* Interface implemented by service(s) which are responsible for user management (registration, login, logout) |
|
7 |
* @version 1.0 |
|
8 |
* @author Vaclav Hrabik, Jiri Trefil |
|
9 |
*/ |
|
10 |
public interface UserService { |
|
11 |
/** |
|
12 |
* Method attempts to register a user and returns status code indicating registration result |
|
13 |
* @param user serialized JSON object representing user |
|
14 |
* @return Integer - status code of the operation, ie 1 - successful, 0 - name taken, ... |
|
15 |
*/ |
|
16 |
public int registerUser(User user); |
|
17 |
/** |
|
18 |
* Method attempts to log in a user and returns status code indicating login result |
|
19 |
* @param user serialized JSON object representing user |
|
20 |
* @return Integer - status code of the operation, ie 1 - successful, 0 - failed, .... |
|
21 |
*/ |
|
22 |
public int loginUser(User user); |
|
23 |
/** |
|
24 |
* Method attempts to log out a user |
|
25 |
* @param user serialized JSON object representing user |
|
26 |
* @return Integer - status code of the operation, ie 1 - successful, 0 - failed, .... |
|
27 |
*/ |
|
28 |
public int logoutUser(User user); |
|
29 |
|
|
30 |
|
|
31 |
} |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/UserServiceImpl.java | ||
---|---|---|
1 |
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.service; |
|
2 |
|
|
3 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.User; |
|
4 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.repository.UserRepository; |
|
5 |
import org.springframework.beans.factory.annotation.Autowired; |
|
6 |
import org.springframework.stereotype.Service; |
|
7 |
|
|
8 |
/** |
|
9 |
* Service provides functionality for user login/register/logout |
|
10 |
* @author Vaclav Hrabik, Jiri Trefil |
|
11 |
*/ |
|
12 |
@Service |
|
13 |
public class UserServiceImpl implements UserService { |
|
14 |
|
|
15 |
@Autowired |
|
16 |
private UserRepository userRepository; |
|
17 |
|
|
18 |
/** |
|
19 |
* Method attempts to register a user |
|
20 |
* @param user serialized JSON object representing user |
|
21 |
* @return Integer flag: |
|
22 |
* 1 - if the user is registered successfully |
|
23 |
* 0 - if the user request violates constraints of column(s) |
|
24 |
* -1 - if the username is already taken |
|
25 |
* |
|
26 |
*/ |
|
27 |
@Override |
|
28 |
public int registerUser(User user) { |
|
29 |
final String email = user.getEmail(); |
|
30 |
final String name = user.getName(); |
|
31 |
//if client request violates constraints - kill the request. User will not be registered. |
|
32 |
if (email.length() > User.getEmailConstraint() || name.length() > User.getNameConstraint()) { |
|
33 |
return 0; |
|
34 |
} |
|
35 |
//if the username is taken, kill the request aswell |
|
36 |
if (userRepository.findByName(user.getName()) != null) { |
|
37 |
return -1; |
|
38 |
} |
|
39 |
//save the user |
|
40 |
User u = userRepository.save(user); |
|
41 |
|
|
42 |
//TODO request to OAuth for token - send user info to the oauth app for token |
|
43 |
//return okay status code, the user was created |
|
44 |
return 1; |
|
45 |
} |
|
46 |
|
|
47 |
/** |
|
48 |
* Method |
|
49 |
* @param user serialized JSON object representing user |
|
50 |
* @return Integer Flag: |
|
51 |
* 0 - if username is not registered |
|
52 |
* 1 - if user is logged successfully |
|
53 |
*/ |
|
54 |
@Override |
|
55 |
public int loginUser(User user) { |
|
56 |
User u = userRepository.findByName(user.getName()); |
|
57 |
//TODO request to OAuth for authentication |
|
58 |
return u == null ? 0 : 1; |
|
59 |
} |
|
60 |
|
|
61 |
/** |
|
62 |
* Methods invalidates JWT token in OAuth application |
|
63 |
* @param user serialized JSON object representing user |
|
64 |
* @return |
|
65 |
*/ |
|
66 |
@Override |
|
67 |
public int logoutUser(User user) { |
|
68 |
//TODO with OAuth app |
|
69 |
return 0; |
|
70 |
} |
|
71 |
} |
Také k dispozici: Unified diff
new: #10244 Vytvoření controlleru pro modul správy uživatelů. Připavení service, entity a repository pro komunikaci s databází.