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