Subversion Repositories SmartDukaan

Rev

Rev 29269 | Rev 31848 | 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 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;
29282 amit.gupta 42
 
24479 amit.gupta 43
	@Autowired
44
	PromoterRepository promoterRepository;
29282 amit.gupta 45
 
24479 amit.gupta 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
	}
29282 amit.gupta 52
 
29269 manish 53
	@Override
29282 amit.gupta 54
	public List<Integer> getDirectReportees(int authId) {
55
		List<AuthUser> authUsers = authRepository.selectAllByManagerAuthId(authId);
56
		List<Integer> authIds = authUsers.stream().map(x -> x.getId()).collect(Collectors.toList());
57
		LOGGER.info("Auth Id == {}, All Reportees === {}", authId, authIds);
58
		return authIds;
29269 manish 59
	}
29282 amit.gupta 60
 
61
	public List<Integer> getAllReportees(int authId) {
62
 
63
		List<Integer> allReportees = new ArrayList<>();
64
 
65
		List<Integer> directReporteeIds = this.getDirectReportees(authId);
66
		allReportees.addAll(directReporteeIds);
67
		for (int directReporteeId : directReporteeIds) {
68
			allReportees.addAll(this.getAllReportees(directReporteeId));
29269 manish 69
		}
29282 amit.gupta 70
		return allReportees;
29269 manish 71
	}
24330 amit.gupta 72
 
73
	@Override
74
	public boolean authenticate(String emailOrMobile, String password) {
75
		return authRepository.authenticate(emailOrMobile, getHash256(password));
76
	}
77
 
78
	@Override
79
	public void resetPassword(String emailOrMobile) throws ProfitMandiBusinessException {
80
		AuthUser authUser = authRepository.selectByEmailOrMobile(emailOrMobile);
81
		String password = getRandomString();
82
		try {
24389 amit.gupta 83
			Utils.sendMailWithAttachments(mailSender, authUser.getEmailId(), null, RESET_PASSWORD_SUBJECT,
84
					String.format(RESET_PASSWORD_BODY, authUser.getFirstName(), password), null);
85
		} catch (Exception e) {
86
			throw new ProfitMandiBusinessException("Password Reset Email", emailOrMobile,
87
					"Could not send password reset mail. Password Could not be reset");
24330 amit.gupta 88
		}
89
		authUser.setPassword(getHash256(password));
90
		authRepository.persist(authUser);
91
	}
92
 
93
	@Override
94
	public void changePassword(String emailOrMobile, String oldPassword, String newPassword)
95
			throws ProfitMandiBusinessException {
96
		if (authRepository.authenticate(emailOrMobile, getHash256(oldPassword))) {
97
			AuthUser authUser = authRepository.selectByEmailOrMobile(emailOrMobile);
98
			authUser.setPassword(getHash256(newPassword));
99
			authRepository.persist(authUser);
100
		} else {
101
			throw new ProfitMandiBusinessException("Authentication", "Credentials", "Invalid email/mobile or password");
102
		}
103
	}
104
 
105
	@Override
106
	public void addAuthUser(AuthUser authUser) throws ProfitMandiBusinessException {
24389 amit.gupta 107
		try {
108
			authRepository.selectByEmailOrMobile(authUser.getEmailId());
29282 amit.gupta 109
		} catch (ProfitMandiBusinessException pbse) {
24389 amit.gupta 110
			try {
111
				authRepository.selectByEmailOrMobile(authUser.getMobileNumber());
29282 amit.gupta 112
			} catch (ProfitMandiBusinessException e) {
24389 amit.gupta 113
				authRepository.persist(authUser);
114
				this.resetPassword(authUser.getEmailId());
115
				return;
116
			}
117
			throw new ProfitMandiBusinessException("Mobile", authUser.getMobileNumber(), "Mobile number already exist");
29282 amit.gupta 118
		}
24389 amit.gupta 119
		throw new ProfitMandiBusinessException("Email", authUser.getEmailId(), "Email Id already exist");
120
 
24330 amit.gupta 121
	}
122
 
123
	private String getRandomString() {
124
		int length = 10;
125
		boolean useLetters = true;
126
		boolean useNumbers = false;
127
		String generatedString = RandomStringUtils.random(length, useLetters, useNumbers);
128
		return generatedString;
129
	}
130
 
24479 amit.gupta 131
	@Override
132
	public String getNameByEmailId(String email) {
133
		AuthUser authUser = authRepository.selectByGmailId(email);
134
		String userName = null;
29282 amit.gupta 135
 
136
		if (authUser != null) {
24479 amit.gupta 137
			userName = authUser.getFirstName() + " " + authUser.getLastName();
138
		} else {
29282 amit.gupta 139
			if (promoterRepository.isExistByEmailId(email)) {
24479 amit.gupta 140
				Promoter promoter = promoterRepository.selectByEmailId(email);
141
				userName = promoter.getName();
29282 amit.gupta 142
 
143
			} else if (userRepository.isExistBySecondryEmailId(email)) {
24479 amit.gupta 144
				try {
145
					User user = userRepository.selectBySecondryEmailId(email);
146
					userName = user.getFirstName() + " " + user.getLastName();
29282 amit.gupta 147
				} catch (Exception e) {
24479 amit.gupta 148
					e.printStackTrace();
149
				}
150
			}
151
		}
24492 amit.gupta 152
		LOGGER.info("User Name from getNameByEmailId({}) is {}", email, userName);
24479 amit.gupta 153
		return userName;
154
	}
155
 
24330 amit.gupta 156
}