| 24330 |
amit.gupta |
1 |
package com.spice.profitmandi.service;
|
|
|
2 |
|
|
|
3 |
import com.google.common.hash.Hashing;
|
|
|
4 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
5 |
import com.spice.profitmandi.common.util.Utils;
|
|
|
6 |
import com.spice.profitmandi.dao.entity.auth.AuthUser;
|
| 24479 |
amit.gupta |
7 |
import com.spice.profitmandi.dao.entity.dtr.User;
|
|
|
8 |
import com.spice.profitmandi.dao.entity.user.Promoter;
|
| 24330 |
amit.gupta |
9 |
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
|
| 24479 |
amit.gupta |
10 |
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
|
|
|
11 |
import com.spice.profitmandi.dao.repository.user.PromoterRepository;
|
| 34794 |
vikas.jang |
12 |
import com.spice.profitmandi.service.user.UserService;
|
| 32559 |
amit.gupta |
13 |
import org.apache.commons.lang.RandomStringUtils;
|
|
|
14 |
import org.apache.logging.log4j.LogManager;
|
|
|
15 |
import org.apache.logging.log4j.Logger;
|
|
|
16 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
17 |
import org.springframework.mail.javamail.JavaMailSender;
|
|
|
18 |
import org.springframework.stereotype.Component;
|
| 24330 |
amit.gupta |
19 |
|
| 32559 |
amit.gupta |
20 |
import java.nio.charset.StandardCharsets;
|
|
|
21 |
import java.util.ArrayList;
|
| 29269 |
manish |
22 |
import java.util.List;
|
|
|
23 |
import java.util.stream.Collectors;
|
|
|
24 |
|
| 24389 |
amit.gupta |
25 |
@Component
|
| 24330 |
amit.gupta |
26 |
public class AuthServiceImpl implements AuthService {
|
|
|
27 |
|
| 34077 |
tejus.loha |
28 |
// private static final String RESET_PASSWORD_BODY = "Dear %s, your password has
|
|
|
29 |
// been reset. Please click this <a href=\"%s\">link</a> to reset your
|
|
|
30 |
// password.\n\nRegards\nSmartdukaan";
|
|
|
31 |
private static final String RESET_PASSWORD_BODY = "Dear %s, \nyour password has been reset to %s. \n\nRegards\nSmartdukaan";
|
|
|
32 |
private static final String RESET_PASSWORD_SUBJECT = "Password Reset request";
|
| 24330 |
amit.gupta |
33 |
|
| 34077 |
tejus.loha |
34 |
private static final Logger LOGGER = LogManager.getLogger(AuthServiceImpl.class);
|
| 24330 |
amit.gupta |
35 |
|
| 34077 |
tejus.loha |
36 |
@Autowired
|
|
|
37 |
AuthRepository authRepository;
|
| 24330 |
amit.gupta |
38 |
|
| 34077 |
tejus.loha |
39 |
@Autowired
|
|
|
40 |
JavaMailSender mailSender;
|
| 29282 |
amit.gupta |
41 |
|
| 34077 |
tejus.loha |
42 |
@Autowired
|
|
|
43 |
PromoterRepository promoterRepository;
|
| 29282 |
amit.gupta |
44 |
|
| 34077 |
tejus.loha |
45 |
@Autowired
|
|
|
46 |
UserRepository userRepository;
|
| 24330 |
amit.gupta |
47 |
|
| 34794 |
vikas.jang |
48 |
@Autowired
|
|
|
49 |
UserService userService;
|
|
|
50 |
|
| 34077 |
tejus.loha |
51 |
private String getHash256(String originalString) {
|
|
|
52 |
return Hashing.sha256().hashString(originalString, StandardCharsets.UTF_8).toString();
|
|
|
53 |
}
|
| 29282 |
amit.gupta |
54 |
|
| 34077 |
tejus.loha |
55 |
@Override
|
|
|
56 |
public List<Integer> getDirectReportees(int authId) {
|
|
|
57 |
List<AuthUser> authUsers = authRepository.selectAllByManagerAuthId(authId);
|
|
|
58 |
List<Integer> authIds = authUsers.stream().map(x -> x.getId()).collect(Collectors.toList());
|
|
|
59 |
LOGGER.info("Auth Id == {}, All Reportees === {}", authId, authIds);
|
|
|
60 |
return authIds;
|
|
|
61 |
}
|
| 29282 |
amit.gupta |
62 |
|
| 34077 |
tejus.loha |
63 |
@Override
|
|
|
64 |
public List<AuthUser> getAllManagers(int authId) {
|
|
|
65 |
AuthUser authUser = authRepository.selectById(authId);
|
|
|
66 |
List<AuthUser> managersHierarchy = new ArrayList<>();
|
|
|
67 |
while (authUser.getManagerId() != 0) {
|
|
|
68 |
AuthUser authUser1 = authRepository.selectById(authUser.getManagerId());
|
|
|
69 |
managersHierarchy.add(authUser1);
|
|
|
70 |
authUser = authUser1;
|
|
|
71 |
}
|
|
|
72 |
return managersHierarchy;
|
|
|
73 |
}
|
| 29282 |
amit.gupta |
74 |
|
| 34077 |
tejus.loha |
75 |
public List<Integer> getAllReportees(int authId) {
|
| 29282 |
amit.gupta |
76 |
|
| 34077 |
tejus.loha |
77 |
List<Integer> allReportees = new ArrayList<>();
|
| 24330 |
amit.gupta |
78 |
|
| 34077 |
tejus.loha |
79 |
List<Integer> directReporteeIds = this.getDirectReportees(authId);
|
|
|
80 |
allReportees.addAll(directReporteeIds);
|
|
|
81 |
for (int directReporteeId : directReporteeIds) {
|
|
|
82 |
allReportees.addAll(this.getAllReportees(directReporteeId));
|
|
|
83 |
}
|
|
|
84 |
return allReportees;
|
|
|
85 |
}
|
| 24330 |
amit.gupta |
86 |
|
| 34077 |
tejus.loha |
87 |
@Override
|
|
|
88 |
public boolean authenticate(String emailOrMobile, String password) {
|
|
|
89 |
return authRepository.authenticate(emailOrMobile, getHash256(password));
|
|
|
90 |
}
|
| 24330 |
amit.gupta |
91 |
|
| 34077 |
tejus.loha |
92 |
@Override
|
|
|
93 |
public void resetPassword(String emailOrMobile) throws ProfitMandiBusinessException {
|
|
|
94 |
AuthUser authUser = authRepository.selectByEmailOrMobile(emailOrMobile);
|
|
|
95 |
String password = getRandomString();
|
|
|
96 |
try {
|
|
|
97 |
Utils.sendMailWithAttachments(mailSender, authUser.getEmailId(), null, RESET_PASSWORD_SUBJECT,
|
|
|
98 |
String.format(RESET_PASSWORD_BODY, authUser.getFirstName(), password), null);
|
|
|
99 |
} catch (Exception e) {
|
|
|
100 |
throw new ProfitMandiBusinessException("Password Reset Email", emailOrMobile,
|
|
|
101 |
"Could not send password reset mail. Password Could not be reset");
|
|
|
102 |
}
|
| 34794 |
vikas.jang |
103 |
userService.resetPassword(emailOrMobile, password);
|
| 34077 |
tejus.loha |
104 |
authUser.setPassword(getHash256(password));
|
|
|
105 |
}
|
| 24330 |
amit.gupta |
106 |
|
| 34077 |
tejus.loha |
107 |
@Override
|
| 34794 |
vikas.jang |
108 |
public void resetPassword(String emailOrMobile, String password) throws ProfitMandiBusinessException {
|
|
|
109 |
AuthUser authUser = authRepository.selectByEmailOrMobile(emailOrMobile);
|
|
|
110 |
userService.resetPassword(emailOrMobile, password);
|
|
|
111 |
authUser.setPassword(getHash256(password));
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
@Override
|
|
|
115 |
public void changePassword(String emailOrMobile, String oldPassword, String newPassword) throws ProfitMandiBusinessException {
|
| 34077 |
tejus.loha |
116 |
if (authRepository.authenticate(emailOrMobile, getHash256(oldPassword))) {
|
|
|
117 |
AuthUser authUser = authRepository.selectByEmailOrMobile(emailOrMobile);
|
|
|
118 |
authUser.setPassword(getHash256(newPassword));
|
| 34794 |
vikas.jang |
119 |
userService.resetPassword(authUser.getEmailId(), newPassword);
|
| 34077 |
tejus.loha |
120 |
} else {
|
|
|
121 |
throw new ProfitMandiBusinessException("Authentication", "Credentials", "Invalid email/mobile or password");
|
|
|
122 |
}
|
|
|
123 |
}
|
| 24389 |
amit.gupta |
124 |
|
| 34077 |
tejus.loha |
125 |
@Override
|
|
|
126 |
public void addAuthUser(AuthUser authUser) throws ProfitMandiBusinessException {
|
|
|
127 |
try {
|
|
|
128 |
authRepository.selectByEmailOrMobile(authUser.getEmailId());
|
|
|
129 |
} catch (ProfitMandiBusinessException pbse) {
|
|
|
130 |
try {
|
|
|
131 |
authRepository.selectByEmailOrMobile(authUser.getMobileNumber());
|
|
|
132 |
} catch (ProfitMandiBusinessException e) {
|
|
|
133 |
authRepository.persist(authUser);
|
|
|
134 |
this.resetPassword(authUser.getEmailId());
|
|
|
135 |
return;
|
|
|
136 |
}
|
|
|
137 |
throw new ProfitMandiBusinessException("Mobile", authUser.getMobileNumber(), "Mobile number already exist");
|
|
|
138 |
}
|
|
|
139 |
throw new ProfitMandiBusinessException("Email", authUser.getEmailId(), "Email Id already exist");
|
| 24330 |
amit.gupta |
140 |
|
| 34077 |
tejus.loha |
141 |
}
|
| 24330 |
amit.gupta |
142 |
|
| 34077 |
tejus.loha |
143 |
private String getRandomString() {
|
|
|
144 |
int length = 10;
|
|
|
145 |
boolean useLetters = true;
|
|
|
146 |
boolean useNumbers = false;
|
|
|
147 |
String generatedString = RandomStringUtils.random(length, useLetters, useNumbers);
|
|
|
148 |
return generatedString;
|
|
|
149 |
}
|
| 29282 |
amit.gupta |
150 |
|
| 34077 |
tejus.loha |
151 |
@Override
|
|
|
152 |
public String getNameByEmailId(String email) {
|
|
|
153 |
AuthUser authUser = authRepository.selectByGmailId(email);
|
|
|
154 |
String userName = null;
|
| 29282 |
amit.gupta |
155 |
|
| 34077 |
tejus.loha |
156 |
if (authUser != null) {
|
|
|
157 |
userName = authUser.getFirstName() + " " + authUser.getLastName();
|
|
|
158 |
} else {
|
|
|
159 |
if (promoterRepository.selectMappedByEmailId(email) != null) {
|
|
|
160 |
Promoter promoter = promoterRepository.selectMappedByEmailId(email);
|
|
|
161 |
userName = promoter.getName();
|
| 24479 |
amit.gupta |
162 |
|
| 34077 |
tejus.loha |
163 |
} else if (userRepository.isExistBySecondryEmailId(email)) {
|
|
|
164 |
try {
|
|
|
165 |
User user = userRepository.selectBySecondryEmailId(email);
|
|
|
166 |
userName = user.getFirstName() + " " + user.getLastName();
|
|
|
167 |
} catch (Exception e) {
|
|
|
168 |
e.printStackTrace();
|
|
|
169 |
}
|
|
|
170 |
}
|
|
|
171 |
}
|
|
|
172 |
LOGGER.info("User Name from getNameByEmailId({}) is {}", email, userName);
|
|
|
173 |
return userName;
|
|
|
174 |
}
|
|
|
175 |
|
| 24330 |
amit.gupta |
176 |
}
|