Subversion Repositories SmartDukaan

Rev

Rev 32559 | Rev 34077 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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