Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
26785 amit.gupta 1
package com.spice.profitmandi.service;
2
 
3
import java.nio.charset.StandardCharsets;
4
 
5
import org.apache.commons.lang3.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;
10
import org.springframework.stereotype.Component;
11
 
12
import com.google.common.hash.Hashing;
13
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
14
import com.spice.profitmandi.dao.entity.fofo.Customer;
15
import com.spice.profitmandi.dao.repository.fofo.CustomerRepository;
16
 
17
@Component
18
public class CustomerServiceImpl implements CustomerService {
19
	// private static final String RESET_PASSWORD_BODY = "Dear %s, your password has
20
	// been reset. Please click this <a href=\"%s\">link</a> to reset your
21
	// password.\n\nRegards\nSmartdukaan";
22
	private static final String RESET_PASSWORD_BODY = "Dear %s, your password has been reset to %s. Regards\nSmartdukaan";
23
	private static final String RESET_PASSWORD_SUBJECT = "Password Reset request";
24
 
25
	private static final Logger LOGGER = LogManager.getLogger(AuthServiceImpl.class);
26
 
27
	@Autowired
28
	CustomerRepository customerRepository;
29
	@Autowired
30
	JavaMailSender mailSender;
31
 
32
	private String getHash256(String originalString) {
33
		return Hashing.sha256().hashString(originalString, StandardCharsets.UTF_8).toString();
34
	}
35
 
36
	@Override
37
	public boolean authenticate(String mobile, String password) {
38
		return customerRepository.authenticate(mobile, getHash256(password));
39
	}
40
 
41
	@Override
26817 amit.gupta 42
	public Customer addCustomer(Customer customer) {
26785 amit.gupta 43
		try {
26817 amit.gupta 44
			customer = customerRepository.selectByMobileNumber(customer.getMobileNumber());
26785 amit.gupta 45
		} catch (ProfitMandiBusinessException pbse) {
26834 amit.gupta 46
			if(customer.getPassword()!=null) {
47
				customer.setPassword(getHash256(customer.getPassword()));
26840 amit.gupta 48
			} else {
49
				customer.setPassword(getHash256(getRandomString()));
26834 amit.gupta 50
			}
26817 amit.gupta 51
			try {
52
				customerRepository.persist(customer);
53
			} catch(Exception e) {
54
				e.printStackTrace();
55
			}
26785 amit.gupta 56
		}
26817 amit.gupta 57
		return customer;
26785 amit.gupta 58
 
59
	}
60
 
61
	private String getRandomString() {
62
		int length = 10;
63
		boolean useLetters = true;
64
		boolean useNumbers = false;
65
		String generatedString = RandomStringUtils.random(length, useLetters, useNumbers);
66
		return generatedString;
67
	}
68
 
69
	@Override
34190 aman.kumar 70
	public boolean changePassword(String mobileNumber, String oldPassword, String newPassword) throws ProfitMandiBusinessException {
71
		if(newPassword==null || newPassword.isEmpty()) {
72
			throw new ProfitMandiBusinessException("error","","Password cannot be empty");
73
		}
74
		if(oldPassword==null || oldPassword.isEmpty()) {
75
			throw new ProfitMandiBusinessException("error", "", "Old Password cannot be empty");
76
		}
77
		if(mobileNumber==null || mobileNumber.isEmpty()) {
78
			throw new ProfitMandiBusinessException("error", "", "Mobile number cannot be empty");
79
		}
80
		if(!customerRepository.authenticate(mobileNumber, getHash256(oldPassword))) {
81
			throw new ProfitMandiBusinessException("error", "", "Old Password is not correct");
82
		}
83
		if(customerRepository.authenticate(mobileNumber, getHash256(newPassword))) {
84
			throw new ProfitMandiBusinessException("error", "", "New Password cannot be same as old password");
85
		}
26785 amit.gupta 86
 
34190 aman.kumar 87
		Customer customer = customerRepository.selectByMobileNumber(mobileNumber);
88
		if(customer==null) {
89
			throw new ProfitMandiBusinessException("error", "", "Mobile number does not exist");
90
		}
91
		 if(customer.getPassword()==null) {
92
			throw new ProfitMandiBusinessException("error", "", "Password cannot be empty");
93
		}
94
		LOGGER.info(customer+" customer data is fetched successfully");
95
		if(customer.getPassword().equals(getHash256(oldPassword))) {
96
			customer.setPassword(getHash256(newPassword));
97
			LOGGER.info("Password changed successfully");
98
			return true;
99
		}
100
		return false;
101
    }
102
 
26785 amit.gupta 103
	@Override
34190 aman.kumar 104
	public void resetPassword(String mobileNumber, String newPassword)
26785 amit.gupta 105
			throws ProfitMandiBusinessException {
26842 amit.gupta 106
		Customer customer = customerRepository.selectByMobileNumber(mobileNumber);
107
		customer.setPassword(getHash256(newPassword));
26785 amit.gupta 108
	}
109
 
110
}