Subversion Repositories SmartDukaan

Rev

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