Subversion Repositories SmartDukaan

Rev

Rev 33247 | Rev 34794 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 33247 Rev 34785
Line 1... Line 1...
1
package com.spice.profitmandi.service.user;
1
package com.spice.profitmandi.service.user;
2
 
2
 
3
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
3
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
4
import com.spice.profitmandi.common.model.ProfitMandiConstants;
4
import com.spice.profitmandi.common.model.ProfitMandiConstants;
5
import com.spice.profitmandi.common.util.DesEncrypter;
5
import com.spice.profitmandi.common.util.DesEncrypter;
-
 
6
import com.spice.profitmandi.common.util.Utils;
6
import com.spice.profitmandi.dao.entity.dtr.*;
7
import com.spice.profitmandi.dao.entity.dtr.*;
7
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
8
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
8
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
9
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
9
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
10
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
10
import com.spice.profitmandi.dao.repository.dtr.*;
11
import com.spice.profitmandi.dao.repository.dtr.*;
11
import com.spice.profitmandi.thrift.clients.TransactionClient;
12
import com.spice.profitmandi.thrift.clients.TransactionClient;
12
import in.shop2020.model.v1.order.TransactionService;
13
import in.shop2020.model.v1.order.TransactionService;
-
 
14
import org.apache.commons.lang.RandomStringUtils;
13
import org.hibernate.Criteria;
15
import org.hibernate.Criteria;
14
import org.hibernate.Session;
16
import org.hibernate.Session;
15
import org.hibernate.SessionFactory;
17
import org.hibernate.SessionFactory;
16
import org.hibernate.criterion.Restrictions;
18
import org.hibernate.criterion.Restrictions;
17
import org.springframework.beans.factory.annotation.Autowired;
19
import org.springframework.beans.factory.annotation.Autowired;
18
import org.springframework.beans.factory.annotation.Qualifier;
20
import org.springframework.beans.factory.annotation.Qualifier;
-
 
21
import org.springframework.mail.javamail.JavaMailSender;
19
import org.springframework.stereotype.Component;
22
import org.springframework.stereotype.Component;
20
 
23
 
21
import java.time.LocalDateTime;
24
import java.time.LocalDateTime;
22
import java.util.HashMap;
25
import java.util.HashMap;
23
import java.util.HashSet;
26
import java.util.HashSet;
Line 25... Line 28...
25
import java.util.Map;
28
import java.util.Map;
26
 
29
 
27
@Component
30
@Component
28
public class UserServiceImpl implements UserService {
31
public class UserServiceImpl implements UserService {
29
 
32
 
-
 
33
	private static final String RESET_PASSWORD_BODY = "Dear %s, \nyour password has been reset to %s. \n\nRegards\nSmartdukaan";
-
 
34
	private static final String RESET_PASSWORD_SUBJECT = "Password Reset request";
-
 
35
 
30
	@Autowired
36
	@Autowired
31
	private SessionFactory sessionFactory;
37
	private SessionFactory sessionFactory;
32
 
38
 
33
	@Autowired
39
	@Autowired
34
	@Qualifier(value = "userRepository")
40
	@Qualifier(value = "userRepository")
Line 50... Line 56...
50
	@Autowired
56
	@Autowired
51
	private UserRoleRepository userRoleRepository;
57
	private UserRoleRepository userRoleRepository;
52
	
58
	
53
	@Autowired
59
	@Autowired
54
	private RoleRepository roleRepository;
60
	private RoleRepository roleRepository;
-
 
61
 
-
 
62
	@Autowired
-
 
63
	JavaMailSender mailSender;
55
	
64
	
56
	private static final DesEncrypter encrypter = new DesEncrypter("saholic");
65
	private static final DesEncrypter encrypter = new DesEncrypter("saholic");
57
	
-
 
58
	
66
 
59
	@SuppressWarnings("deprecation")
67
	@SuppressWarnings("deprecation")
60
	@Override
68
	@Override
61
	public boolean updateActivation(int userId, int retailerId, String activationCode) throws Throwable {
69
	public boolean updateActivation(int userId, int retailerId, String activationCode) throws Throwable {
62
		
70
		
63
		TransactionClient transactionServiceClient = new TransactionClient();
71
		TransactionClient transactionServiceClient = new TransactionClient();
Line 76... Line 84...
76
				registered=true;
84
				registered=true;
77
				code.setStatus(false);
85
				code.setStatus(false);
78
				session.update(code);
86
				session.update(code);
79
			}
87
			}
80
		}
88
		}
81
	
89
 
82
		if(registered) {
90
		if(registered) {
83
			User user = userRepository.selectById(userId);
91
			User user = userRepository.selectById(userId);
84
			user.setActivated(true);
92
			user.setActivated(true);
85
			user.setActivationTime(LocalDateTime.now());
93
			user.setActivationTime(LocalDateTime.now());
86
			user.setReferrer(activationCode);
94
			user.setReferrer(activationCode);
87
			session.update(user);
95
			session.update(user);
88
			
96
 
89
			Retailer retailer = retailerRepo.selectById(retailerId);
97
			Retailer retailer = retailerRepo.selectById(retailerId);
90
			retailer.setActive(true);
98
			retailer.setActive(true);
91
			session.update(retailer);
99
			session.update(retailer);
92
		}
100
		}
93
			
101
 
94
		return registered;
102
		return registered;
95
	}
103
	}
96
 
104
 
97
 
105
 
98
	@Override
106
	@Override
Line 129... Line 137...
129
 
137
 
130
 
138
 
131
	@Override
139
	@Override
132
	public User authenticate(String emailMobile, String password) throws ProfitMandiBusinessException {
140
	public User authenticate(String emailMobile, String password) throws ProfitMandiBusinessException {
133
		User user = userRepository.selectByEmailIdOrMobileNumber(emailMobile);
141
		User user = userRepository.selectByEmailIdOrMobileNumber(emailMobile);
134
		if(!user.getPassword().equals(encrypter.decrypt(password))) {
142
		if(!user.getPassword().equals(encrypter.encrypt(password))) {
135
			throw new ProfitMandiBusinessException("Username Password", emailMobile, "Invalid Username or Password");
143
			throw new ProfitMandiBusinessException("Username Password", emailMobile, "Invalid Username or Password");
136
		}
144
		}
137
		return user;
145
		return user;
138
	}
146
	}
139
 
147
 
-
 
148
	@Override
-
 
149
	public void resetPassword(String emailOrMobile) throws ProfitMandiBusinessException {
-
 
150
		User user = userRepository.selectByEmailIdOrMobileNumber(emailOrMobile);
-
 
151
		String password = getRandomString();
-
 
152
		try {
-
 
153
			Utils.sendMailWithAttachments(mailSender, user.getEmailId(), null, RESET_PASSWORD_SUBJECT, String.format(RESET_PASSWORD_BODY, user.getFirstName(), password), null);
-
 
154
		} catch (Exception e) {
-
 
155
			throw new ProfitMandiBusinessException("Password Reset Email", emailOrMobile, "Could not send password reset mail. Password Could not be reset");
-
 
156
		}
-
 
157
		user.setPassword(encrypter.encrypt(password));
-
 
158
	}
140
 
159
 
141
	@Override
160
	@Override
142
	public boolean resetPassword(String emailMobile, String password) throws ProfitMandiBusinessException {
161
	public boolean resetPassword(String emailMobile, String password) throws ProfitMandiBusinessException {
143
		User user = userRepository.selectByEmailIdOrMobileNumber(emailMobile);
162
		User user = userRepository.selectByEmailIdOrMobileNumber(emailMobile);
144
		user.setPassword(encrypter.encrypt(password));
163
		user.setPassword(encrypter.encrypt(password));
145
		userRepository.persist(user);
164
		userRepository.persist(user);
146
		return true;
165
		return true;
147
	}
166
	}
-
 
167
 
-
 
168
	private String getRandomString() {
-
 
169
		int length = 10;
-
 
170
		boolean useLetters = true;
-
 
171
		boolean useNumbers = false;
-
 
172
		return RandomStringUtils.random(length, useLetters, useNumbers);
-
 
173
	}
148
	
174
	
149
}
175
}