LoginClientService.java
2.24 KB
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
package com.srh.api.service;
import com.srh.api.error.exception.InvalidLoginUserException;
import com.srh.api.error.exception.InvalidPasswordUserException;
import com.srh.api.model.Admin;
import com.srh.api.model.Evaluator;
import com.srh.api.utils.PasswordUtil;
import lombok.SneakyThrows;
import org.hibernate.ObjectNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class LoginClientService {
@Autowired
EvaluatorService evaluatorService;
@Autowired
AdminService adminService;
PasswordUtil<Evaluator> passwordUtilEvaluator = new PasswordUtil<>();
PasswordUtil<Admin> passwordUtilAdmin = new PasswordUtil<>();
@SneakyThrows
public Boolean verifyEvaluators(String login, String rawPassword) {
Evaluator evaluator;
try {
evaluator = evaluatorService.findByLogin(login);
} catch (ObjectNotFoundException e) {
throw new InvalidLoginUserException();
}
if (!passwordUtilEvaluator.isEqualsPasswords(rawPassword, evaluator.getPassword())) {
throw new InvalidPasswordUserException();
}
return true;
}
@SneakyThrows
public Boolean generateLoginTokenByAdmins(String login, String rawPassword) {
Admin admin;
try {
admin = adminService.findByLogin(login);
} catch (ObjectNotFoundException e) {
throw new InvalidLoginUserException();
}
if (!passwordUtilEvaluator.isEqualsPasswords(rawPassword, admin.getPassword())) {
throw new InvalidPasswordUserException();
}
return true;
}
@SneakyThrows
public Integer getAdminId(String login) {
try {
Admin admin = adminService.findByLogin(login);
return admin.getId();
} catch (ObjectNotFoundException e) {
throw new InvalidLoginUserException();
}
}
@SneakyThrows
public Integer getEvaluatorId(String login) {
try {
Evaluator evaluator = evaluatorService.findByLogin(login);
return evaluator.getId();
} catch (ObjectNotFoundException e) {
throw new InvalidLoginUserException();
}
}
}