Subversion Repositories SmartDukaan

Rev

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

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