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