| 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;
|
| 35026 |
amit |
22 |
import java.util.HashSet;
|
| 29269 |
manish |
23 |
import java.util.List;
|
| 35026 |
amit |
24 |
import java.util.Set;
|
| 29269 |
manish |
25 |
import java.util.stream.Collectors;
|
|
|
26 |
|
| 24389 |
amit.gupta |
27 |
@Component
|
| 24330 |
amit.gupta |
28 |
public class AuthServiceImpl implements AuthService {
|
|
|
29 |
|
| 34077 |
tejus.loha |
30 |
// private static final String RESET_PASSWORD_BODY = "Dear %s, your password has
|
|
|
31 |
// been reset. Please click this <a href=\"%s\">link</a> to reset your
|
|
|
32 |
// password.\n\nRegards\nSmartdukaan";
|
|
|
33 |
private static final String RESET_PASSWORD_BODY = "Dear %s, \nyour password has been reset to %s. \n\nRegards\nSmartdukaan";
|
|
|
34 |
private static final String RESET_PASSWORD_SUBJECT = "Password Reset request";
|
| 24330 |
amit.gupta |
35 |
|
| 34077 |
tejus.loha |
36 |
private static final Logger LOGGER = LogManager.getLogger(AuthServiceImpl.class);
|
| 24330 |
amit.gupta |
37 |
|
| 34077 |
tejus.loha |
38 |
@Autowired
|
|
|
39 |
AuthRepository authRepository;
|
| 24330 |
amit.gupta |
40 |
|
| 34077 |
tejus.loha |
41 |
@Autowired
|
|
|
42 |
JavaMailSender mailSender;
|
| 29282 |
amit.gupta |
43 |
|
| 34077 |
tejus.loha |
44 |
@Autowired
|
|
|
45 |
PromoterRepository promoterRepository;
|
| 29282 |
amit.gupta |
46 |
|
| 34077 |
tejus.loha |
47 |
@Autowired
|
|
|
48 |
UserRepository userRepository;
|
| 24330 |
amit.gupta |
49 |
|
| 34794 |
vikas.jang |
50 |
@Autowired
|
|
|
51 |
UserService userService;
|
|
|
52 |
|
| 34077 |
tejus.loha |
53 |
private String getHash256(String originalString) {
|
|
|
54 |
return Hashing.sha256().hashString(originalString, StandardCharsets.UTF_8).toString();
|
|
|
55 |
}
|
| 29282 |
amit.gupta |
56 |
|
| 34077 |
tejus.loha |
57 |
@Override
|
|
|
58 |
public List<Integer> getDirectReportees(int authId) {
|
|
|
59 |
List<AuthUser> authUsers = authRepository.selectAllByManagerAuthId(authId);
|
|
|
60 |
List<Integer> authIds = authUsers.stream().map(x -> x.getId()).collect(Collectors.toList());
|
|
|
61 |
LOGGER.info("Auth Id == {}, All Reportees === {}", authId, authIds);
|
|
|
62 |
return authIds;
|
|
|
63 |
}
|
|
|
64 |
@Override
|
|
|
65 |
public List<AuthUser> getAllManagers(int authId) {
|
|
|
66 |
AuthUser authUser = authRepository.selectById(authId);
|
|
|
67 |
List<AuthUser> managersHierarchy = new ArrayList<>();
|
| 35198 |
ranu |
68 |
|
|
|
69 |
// To prevent infinite loops due to circular references
|
|
|
70 |
Set<Integer> visitedIds = new HashSet<>();
|
|
|
71 |
visitedIds.add(authId);
|
|
|
72 |
|
|
|
73 |
while (authUser != null && authUser.getManagerId() != 0) {
|
|
|
74 |
int managerId = authUser.getManagerId();
|
|
|
75 |
|
|
|
76 |
if (visitedIds.contains(managerId)) {
|
|
|
77 |
LOGGER.warn("Circular reference detected: authId {} -> managerId {}", authUser.getId(), managerId);
|
|
|
78 |
break;
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
AuthUser manager = authRepository.selectById(managerId);
|
|
|
82 |
if (manager == null) {
|
|
|
83 |
LOGGER.warn("Manager with ID {} not found for user {}", managerId, authUser.getId());
|
|
|
84 |
break;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
managersHierarchy.add(manager);
|
|
|
88 |
visitedIds.add(managerId);
|
|
|
89 |
|
|
|
90 |
authUser = manager;
|
| 34077 |
tejus.loha |
91 |
}
|
| 35198 |
ranu |
92 |
|
|
|
93 |
LOGGER.info("managersHierarchy {}", managersHierarchy);
|
| 34077 |
tejus.loha |
94 |
return managersHierarchy;
|
|
|
95 |
}
|
| 29282 |
amit.gupta |
96 |
|
| 34077 |
tejus.loha |
97 |
public List<Integer> getAllReportees(int authId) {
|
| 35026 |
amit |
98 |
return getAllReportees(authId, new HashSet<>()); // call helper with visited set
|
|
|
99 |
}
|
| 29282 |
amit.gupta |
100 |
|
| 35026 |
amit |
101 |
private List<Integer> getAllReportees(int authId, Set<Integer> visited) {
|
| 34077 |
tejus.loha |
102 |
List<Integer> allReportees = new ArrayList<>();
|
| 24330 |
amit.gupta |
103 |
|
| 34077 |
tejus.loha |
104 |
List<Integer> directReporteeIds = this.getDirectReportees(authId);
|
| 35026 |
amit |
105 |
|
| 34077 |
tejus.loha |
106 |
for (int directReporteeId : directReporteeIds) {
|
| 35026 |
amit |
107 |
if (!visited.contains(directReporteeId)) {
|
|
|
108 |
visited.add(directReporteeId); // mark visited
|
|
|
109 |
allReportees.add(directReporteeId);
|
|
|
110 |
|
|
|
111 |
// recursively get sub-reportees
|
|
|
112 |
allReportees.addAll(getAllReportees(directReporteeId, visited));
|
|
|
113 |
}
|
| 34077 |
tejus.loha |
114 |
}
|
|
|
115 |
return allReportees;
|
|
|
116 |
}
|
| 24330 |
amit.gupta |
117 |
|
| 34077 |
tejus.loha |
118 |
@Override
|
|
|
119 |
public boolean authenticate(String emailOrMobile, String password) {
|
|
|
120 |
return authRepository.authenticate(emailOrMobile, getHash256(password));
|
|
|
121 |
}
|
| 24330 |
amit.gupta |
122 |
|
| 34077 |
tejus.loha |
123 |
@Override
|
|
|
124 |
public void resetPassword(String emailOrMobile) throws ProfitMandiBusinessException {
|
|
|
125 |
AuthUser authUser = authRepository.selectByEmailOrMobile(emailOrMobile);
|
|
|
126 |
String password = getRandomString();
|
|
|
127 |
try {
|
|
|
128 |
Utils.sendMailWithAttachments(mailSender, authUser.getEmailId(), null, RESET_PASSWORD_SUBJECT,
|
|
|
129 |
String.format(RESET_PASSWORD_BODY, authUser.getFirstName(), password), null);
|
|
|
130 |
} catch (Exception e) {
|
|
|
131 |
throw new ProfitMandiBusinessException("Password Reset Email", emailOrMobile,
|
|
|
132 |
"Could not send password reset mail. Password Could not be reset");
|
|
|
133 |
}
|
| 34794 |
vikas.jang |
134 |
userService.resetPassword(emailOrMobile, password);
|
| 34077 |
tejus.loha |
135 |
authUser.setPassword(getHash256(password));
|
|
|
136 |
}
|
| 24330 |
amit.gupta |
137 |
|
| 34077 |
tejus.loha |
138 |
@Override
|
| 34794 |
vikas.jang |
139 |
public void resetPassword(String emailOrMobile, String password) throws ProfitMandiBusinessException {
|
| 34815 |
vikas |
140 |
try {
|
|
|
141 |
AuthUser authUser = authRepository.selectByEmailOrMobile(emailOrMobile);
|
|
|
142 |
authUser.setPassword(getHash256(password));
|
|
|
143 |
userService.resetPassword(emailOrMobile, password);
|
|
|
144 |
} catch (ProfitMandiBusinessException e) {
|
|
|
145 |
userService.resetPassword(emailOrMobile, password);
|
|
|
146 |
}
|
| 34794 |
vikas.jang |
147 |
}
|
|
|
148 |
|
|
|
149 |
@Override
|
|
|
150 |
public void changePassword(String emailOrMobile, String oldPassword, String newPassword) throws ProfitMandiBusinessException {
|
| 34077 |
tejus.loha |
151 |
if (authRepository.authenticate(emailOrMobile, getHash256(oldPassword))) {
|
|
|
152 |
AuthUser authUser = authRepository.selectByEmailOrMobile(emailOrMobile);
|
|
|
153 |
authUser.setPassword(getHash256(newPassword));
|
| 34794 |
vikas.jang |
154 |
userService.resetPassword(authUser.getEmailId(), newPassword);
|
| 34077 |
tejus.loha |
155 |
} else {
|
|
|
156 |
throw new ProfitMandiBusinessException("Authentication", "Credentials", "Invalid email/mobile or password");
|
|
|
157 |
}
|
|
|
158 |
}
|
| 24389 |
amit.gupta |
159 |
|
| 34077 |
tejus.loha |
160 |
@Override
|
|
|
161 |
public void addAuthUser(AuthUser authUser) throws ProfitMandiBusinessException {
|
|
|
162 |
try {
|
|
|
163 |
authRepository.selectByEmailOrMobile(authUser.getEmailId());
|
|
|
164 |
} catch (ProfitMandiBusinessException pbse) {
|
|
|
165 |
try {
|
|
|
166 |
authRepository.selectByEmailOrMobile(authUser.getMobileNumber());
|
|
|
167 |
} catch (ProfitMandiBusinessException e) {
|
|
|
168 |
authRepository.persist(authUser);
|
|
|
169 |
this.resetPassword(authUser.getEmailId());
|
|
|
170 |
return;
|
|
|
171 |
}
|
|
|
172 |
throw new ProfitMandiBusinessException("Mobile", authUser.getMobileNumber(), "Mobile number already exist");
|
|
|
173 |
}
|
|
|
174 |
throw new ProfitMandiBusinessException("Email", authUser.getEmailId(), "Email Id already exist");
|
| 24330 |
amit.gupta |
175 |
|
| 34077 |
tejus.loha |
176 |
}
|
| 24330 |
amit.gupta |
177 |
|
| 34077 |
tejus.loha |
178 |
private String getRandomString() {
|
|
|
179 |
int length = 10;
|
|
|
180 |
boolean useLetters = true;
|
|
|
181 |
boolean useNumbers = false;
|
|
|
182 |
String generatedString = RandomStringUtils.random(length, useLetters, useNumbers);
|
|
|
183 |
return generatedString;
|
|
|
184 |
}
|
| 29282 |
amit.gupta |
185 |
|
| 34077 |
tejus.loha |
186 |
@Override
|
|
|
187 |
public String getNameByEmailId(String email) {
|
|
|
188 |
AuthUser authUser = authRepository.selectByGmailId(email);
|
|
|
189 |
String userName = null;
|
| 29282 |
amit.gupta |
190 |
|
| 34077 |
tejus.loha |
191 |
if (authUser != null) {
|
|
|
192 |
userName = authUser.getFirstName() + " " + authUser.getLastName();
|
|
|
193 |
} else {
|
|
|
194 |
if (promoterRepository.selectMappedByEmailId(email) != null) {
|
|
|
195 |
Promoter promoter = promoterRepository.selectMappedByEmailId(email);
|
|
|
196 |
userName = promoter.getName();
|
| 24479 |
amit.gupta |
197 |
|
| 34077 |
tejus.loha |
198 |
} else if (userRepository.isExistBySecondryEmailId(email)) {
|
|
|
199 |
try {
|
|
|
200 |
User user = userRepository.selectBySecondryEmailId(email);
|
|
|
201 |
userName = user.getFirstName() + " " + user.getLastName();
|
|
|
202 |
} catch (Exception e) {
|
|
|
203 |
e.printStackTrace();
|
|
|
204 |
}
|
|
|
205 |
}
|
|
|
206 |
}
|
|
|
207 |
LOGGER.info("User Name from getNameByEmailId({}) is {}", email, userName);
|
|
|
208 |
return userName;
|
|
|
209 |
}
|
|
|
210 |
|
| 24330 |
amit.gupta |
211 |
}
|