Subversion Repositories SmartDukaan

Rev

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