Subversion Repositories SmartDukaan

Rev

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