| 24383 |
amit.gupta |
1 |
package com.spice.profitmandi.web.controller;
|
|
|
2 |
|
|
|
3 |
import java.time.LocalDateTime;
|
| 29209 |
manish |
4 |
import java.util.HashMap;
|
| 24383 |
amit.gupta |
5 |
import java.util.List;
|
| 29209 |
manish |
6 |
import java.util.Map;
|
|
|
7 |
import java.util.stream.Collectors;
|
| 24383 |
amit.gupta |
8 |
|
|
|
9 |
import javax.servlet.http.HttpServletRequest;
|
|
|
10 |
|
|
|
11 |
import org.apache.logging.log4j.LogManager;
|
|
|
12 |
import org.apache.logging.log4j.Logger;
|
|
|
13 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
14 |
import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
15 |
import org.springframework.stereotype.Controller;
|
| 25192 |
tejbeer |
16 |
import org.springframework.transaction.annotation.Transactional;
|
| 24383 |
amit.gupta |
17 |
import org.springframework.ui.Model;
|
|
|
18 |
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
19 |
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
20 |
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
21 |
|
|
|
22 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
23 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
|
|
24 |
import com.spice.profitmandi.dao.entity.auth.AuthUser;
|
| 27806 |
tejbeer |
25 |
import com.spice.profitmandi.dao.entity.cs.PartnerPosition;
|
|
|
26 |
import com.spice.profitmandi.dao.entity.cs.Position;
|
| 24383 |
amit.gupta |
27 |
import com.spice.profitmandi.dao.entity.dtr.Role;
|
|
|
28 |
import com.spice.profitmandi.dao.entity.dtr.User;
|
|
|
29 |
import com.spice.profitmandi.dao.entity.dtr.UserRole;
|
|
|
30 |
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
|
| 27806 |
tejbeer |
31 |
import com.spice.profitmandi.dao.repository.cs.PartnersPositionRepository;
|
|
|
32 |
import com.spice.profitmandi.dao.repository.cs.PositionRepository;
|
| 24383 |
amit.gupta |
33 |
import com.spice.profitmandi.dao.repository.dtr.RoleRepository;
|
|
|
34 |
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
|
|
|
35 |
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
|
|
|
36 |
import com.spice.profitmandi.service.AuthService;
|
|
|
37 |
import com.spice.profitmandi.web.model.LoginDetails;
|
|
|
38 |
import com.spice.profitmandi.web.util.CookiesProcessor;
|
| 29209 |
manish |
39 |
import com.spice.profitmandi.web.util.MVCResponseSender;
|
| 24383 |
amit.gupta |
40 |
|
|
|
41 |
@Controller
|
| 25192 |
tejbeer |
42 |
@Transactional(rollbackFor = Throwable.class)
|
| 24383 |
amit.gupta |
43 |
public class AuthUserController {
|
|
|
44 |
|
|
|
45 |
private static final Logger LOGGER = LogManager.getLogger(AuthUserController.class);
|
|
|
46 |
|
|
|
47 |
@Autowired
|
|
|
48 |
private AuthService authService;
|
|
|
49 |
|
|
|
50 |
@Autowired
|
|
|
51 |
private AuthRepository authRepository;
|
|
|
52 |
|
|
|
53 |
@Autowired
|
|
|
54 |
private UserRepository userRepository;
|
|
|
55 |
|
|
|
56 |
@Autowired
|
|
|
57 |
private RoleRepository roleRepository;
|
|
|
58 |
|
|
|
59 |
@Autowired
|
| 29209 |
manish |
60 |
private MVCResponseSender mvcResponseSender;
|
|
|
61 |
|
|
|
62 |
@Autowired
|
| 24383 |
amit.gupta |
63 |
private UserRoleRepository userRoleRepository;
|
|
|
64 |
|
|
|
65 |
@Autowired
|
|
|
66 |
private CookiesProcessor cookiesProcessor;
|
|
|
67 |
|
|
|
68 |
@Autowired
|
| 27806 |
tejbeer |
69 |
private PositionRepository positionRepository;
|
|
|
70 |
|
|
|
71 |
@Autowired
|
|
|
72 |
private PartnersPositionRepository partnerPositionRepository;
|
|
|
73 |
|
|
|
74 |
@Autowired
|
| 24383 |
amit.gupta |
75 |
@Qualifier("userUserRepository")
|
|
|
76 |
private com.spice.profitmandi.dao.repository.user.UserRepository userUserRepository;
|
|
|
77 |
|
|
|
78 |
@GetMapping(value = "/createAuthUser")
|
|
|
79 |
public String getcreateAuthUser(HttpServletRequest request,
|
|
|
80 |
@RequestParam(name = "offset", defaultValue = "0") int offset,
|
|
|
81 |
@RequestParam(name = "limit", defaultValue = "10") int limit, Model model)
|
|
|
82 |
throws ProfitMandiBusinessException {
|
| 25570 |
tejbeer |
83 |
// List<AuthUser> authUsers = authRepository.selectAll(offset, limit);
|
|
|
84 |
List<AuthUser> authUsers = authRepository.selectAllActiveUser(offset, limit);
|
|
|
85 |
|
| 24383 |
amit.gupta |
86 |
long size = authRepository.selectCountAuthUser();
|
| 29209 |
manish |
87 |
|
|
|
88 |
List<AuthUser> activeAuthUsers = authRepository.selectAllActiveUser();
|
|
|
89 |
|
|
|
90 |
Map<Integer, AuthUser> userManagerMap = new HashMap<>();
|
|
|
91 |
|
|
|
92 |
for (AuthUser authUser : authUsers) {
|
|
|
93 |
if (authUser.getManagerId() != 0) {
|
|
|
94 |
userManagerMap.put(authUser.getId(), authRepository.selectById(authUser.getManagerId()));
|
|
|
95 |
}
|
|
|
96 |
}
|
|
|
97 |
model.addAttribute("userManagerMap", userManagerMap);
|
|
|
98 |
model.addAttribute("activeAuthUsers", activeAuthUsers);
|
| 24383 |
amit.gupta |
99 |
model.addAttribute("authUsers", authUsers);
|
|
|
100 |
model.addAttribute("start", offset + 1);
|
|
|
101 |
model.addAttribute("size", size);
|
| 29209 |
manish |
102 |
model.addAttribute("url", "/getPaginatedAuthUser");
|
| 24383 |
amit.gupta |
103 |
if (authUsers.size() < limit) {
|
|
|
104 |
model.addAttribute("end", offset + authUsers.size());
|
|
|
105 |
} else {
|
|
|
106 |
model.addAttribute("end", offset + limit);
|
|
|
107 |
}
|
| 29209 |
manish |
108 |
|
| 24383 |
amit.gupta |
109 |
return "create-auth-user";
|
|
|
110 |
|
|
|
111 |
}
|
|
|
112 |
|
| 29209 |
manish |
113 |
@PostMapping(value = "/addManagerId")
|
|
|
114 |
public String addManagerId(HttpServletRequest request, @RequestParam(name = "authId") int authId,
|
|
|
115 |
@RequestParam(name = "managerId") int managerId, Model model) throws Exception {
|
|
|
116 |
// List<AuthUser> authUsers = authRepository.selectAll(offset, limit);
|
|
|
117 |
AuthUser authUser = authRepository.selectById(authId);
|
|
|
118 |
|
|
|
119 |
if (authUser.getId() != 0) {
|
|
|
120 |
|
|
|
121 |
authUser.setManagerId(managerId);
|
|
|
122 |
}
|
|
|
123 |
List<AuthUser> activeAuthUsers = authRepository.selectAllActiveUser();
|
|
|
124 |
|
|
|
125 |
AuthUser manager = authRepository.selectById(authUser.getManagerId());
|
|
|
126 |
|
|
|
127 |
model.addAttribute("activeAuthUsers", activeAuthUsers);
|
|
|
128 |
model.addAttribute("authUser", authUser);
|
|
|
129 |
model.addAttribute("manager", manager);
|
|
|
130 |
|
|
|
131 |
return "create-auth-user";
|
|
|
132 |
|
|
|
133 |
}
|
|
|
134 |
|
| 24383 |
amit.gupta |
135 |
@PostMapping(value = "/createAuthUser")
|
|
|
136 |
public String createAuthUser(HttpServletRequest request,
|
|
|
137 |
@RequestParam(name = ProfitMandiConstants.FIRST_NAME, defaultValue = "") String firstName,
|
|
|
138 |
@RequestParam(name = ProfitMandiConstants.LAST_NAME, defaultValue = "") String lastName,
|
|
|
139 |
@RequestParam(name = ProfitMandiConstants.EMAIL_ID, defaultValue = "") String emailId,
|
|
|
140 |
@RequestParam(name = ProfitMandiConstants.MOBILE_NUMBER, defaultValue = "") String mobileNumber,
|
| 25366 |
tejbeer |
141 |
@RequestParam(name = ProfitMandiConstants.GMAIL_ID, defaultValue = "") String gmailId,
|
| 27595 |
tejbeer |
142 |
@RequestParam(name = "employeeCode", defaultValue = "") String employeeCode,
|
| 24383 |
amit.gupta |
143 |
@RequestParam(name = "offset", defaultValue = "0") int offset,
|
|
|
144 |
@RequestParam(name = "limit", defaultValue = "10") int limit, Model model)
|
|
|
145 |
throws ProfitMandiBusinessException {
|
|
|
146 |
AuthUser authUser = null;
|
|
|
147 |
User user = null;
|
|
|
148 |
List<Role> roles = roleRepository.selectAll();
|
|
|
149 |
try {
|
|
|
150 |
user = userRepository.selectByEmailId(emailId);
|
|
|
151 |
userRoleRepository.deleteByUserId(user.getId());
|
|
|
152 |
for (Role role : roles) {
|
|
|
153 |
UserRole userRole = new UserRole();
|
|
|
154 |
userRole.setRoleId(role.getId());
|
|
|
155 |
userRole.setUserId(user.getId());
|
|
|
156 |
userRoleRepository.persist(userRole);
|
|
|
157 |
}
|
|
|
158 |
} catch (Exception ex) {
|
|
|
159 |
user = new User();
|
|
|
160 |
user.setFirstName(firstName);
|
|
|
161 |
user.setLastName(lastName);
|
|
|
162 |
user.setUsername(emailId);
|
|
|
163 |
user.setEmailId(emailId);
|
|
|
164 |
user.setPassword("");
|
|
|
165 |
user.setMobileNumber(mobileNumber);
|
|
|
166 |
user.setCity("Noida");
|
|
|
167 |
user.setPinCode(201301);
|
|
|
168 |
user.setActivated(true);
|
|
|
169 |
user.setMobile_verified(true);
|
|
|
170 |
user.setReferral_url("");
|
|
|
171 |
user.setUpdateTimestamp(LocalDateTime.now());
|
|
|
172 |
user.setStatus(0);
|
|
|
173 |
user.setCreateTimestamp(LocalDateTime.now());
|
|
|
174 |
userRepository.persist(user);
|
|
|
175 |
LOGGER.info("User created successfully");
|
|
|
176 |
for (Role role : roles) {
|
|
|
177 |
UserRole userRole = new UserRole();
|
|
|
178 |
userRole.setRoleId(role.getId());
|
|
|
179 |
userRole.setUserId(user.getId());
|
|
|
180 |
userRoleRepository.persist(userRole);
|
|
|
181 |
}
|
|
|
182 |
}
|
|
|
183 |
authUser = new AuthUser();
|
|
|
184 |
authUser.setEmailId(emailId);
|
|
|
185 |
authUser.setFirstName(firstName);
|
|
|
186 |
authUser.setLastName(lastName);
|
|
|
187 |
authUser.setMobileNumber(mobileNumber);
|
| 25366 |
tejbeer |
188 |
authUser.setGmailId(gmailId);
|
| 27595 |
tejbeer |
189 |
authUser.setEmployeeCode(employeeCode);
|
| 25587 |
tejbeer |
190 |
authUser.setActive(true);
|
| 25570 |
tejbeer |
191 |
|
| 24383 |
amit.gupta |
192 |
authUser.setCreatedTimestamp(LocalDateTime.now());
|
|
|
193 |
authService.addAuthUser(authUser);
|
|
|
194 |
|
|
|
195 |
List<AuthUser> authUsers = authRepository.selectAll(offset, limit);
|
|
|
196 |
long size = authRepository.selectCountAuthUser();
|
|
|
197 |
model.addAttribute("authUsers", authUsers);
|
|
|
198 |
model.addAttribute("start", offset + 1);
|
|
|
199 |
model.addAttribute("size", size);
|
|
|
200 |
if (authUsers.size() < limit) {
|
|
|
201 |
model.addAttribute("end", offset + authUsers.size());
|
|
|
202 |
} else {
|
|
|
203 |
model.addAttribute("end", offset + limit);
|
|
|
204 |
}
|
|
|
205 |
return "create-auth-user";
|
|
|
206 |
}
|
|
|
207 |
|
|
|
208 |
@GetMapping(value = "/changePassword")
|
|
|
209 |
public String getChangePassword(HttpServletRequest request, Model model) {
|
|
|
210 |
return "change-auth-user-password";
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
@PostMapping(value = "/changePassword")
|
|
|
214 |
public String changePassword(HttpServletRequest request,
|
|
|
215 |
@RequestParam(name = "oldPassword", defaultValue = "") String oldPassword,
|
|
|
216 |
@RequestParam(name = "newPassword", defaultValue = "") String newPassword, Model model)
|
|
|
217 |
throws ProfitMandiBusinessException {
|
|
|
218 |
|
|
|
219 |
LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
|
|
|
220 |
authService.changePassword(loginDetails.getEmailId(), oldPassword, newPassword);
|
|
|
221 |
return "change-auth-user-password";
|
|
|
222 |
}
|
|
|
223 |
|
|
|
224 |
@GetMapping(value = "/getPaginatedAuthUser")
|
|
|
225 |
public String getPaginatedAuthUser(HttpServletRequest request,
|
|
|
226 |
@RequestParam(name = "offset", defaultValue = "0") int offset,
|
|
|
227 |
@RequestParam(name = "limit", defaultValue = "10") int limit, Model model) {
|
| 27806 |
tejbeer |
228 |
// List<AuthUser> authUsers = authRepository.selectAll(offset, limit);
|
| 29209 |
manish |
229 |
|
|
|
230 |
List<AuthUser> activeAuthUsers = authRepository.selectAllActiveUser();
|
|
|
231 |
|
| 25570 |
tejbeer |
232 |
List<AuthUser> authUsers = authRepository.selectAllActiveUser(offset, limit);
|
| 29209 |
manish |
233 |
|
|
|
234 |
Map<Integer, AuthUser> userManagerMap = new HashMap<>();
|
|
|
235 |
|
|
|
236 |
for (AuthUser authUser : authUsers) {
|
|
|
237 |
if (authUser.getManagerId() != 0) {
|
|
|
238 |
userManagerMap.put(authUser.getId(), authRepository.selectById(authUser.getManagerId()));
|
|
|
239 |
}
|
|
|
240 |
}
|
|
|
241 |
model.addAttribute("userManagerMap", userManagerMap);
|
| 24383 |
amit.gupta |
242 |
model.addAttribute("authUsers", authUsers);
|
| 29209 |
manish |
243 |
model.addAttribute("activeAuthUsers", activeAuthUsers);
|
|
|
244 |
model.addAttribute("url", "/getPaginatedAuthUser");
|
| 24383 |
amit.gupta |
245 |
return "auth-users-paginated";
|
|
|
246 |
}
|
| 25570 |
tejbeer |
247 |
|
|
|
248 |
@GetMapping(value = "/authuser/edit")
|
|
|
249 |
public String updateAuthUser(Model model, @RequestParam String gmailId, @RequestParam String emailId)
|
|
|
250 |
throws Exception {
|
| 24478 |
amit.gupta |
251 |
AuthUser user = authRepository.selectByEmailOrMobile(emailId);
|
|
|
252 |
user.setGmailId(gmailId);
|
| 24489 |
amit.gupta |
253 |
authRepository.persist(user);
|
| 24478 |
amit.gupta |
254 |
model.addAttribute("response", "true");
|
|
|
255 |
return "response";
|
|
|
256 |
}
|
| 27595 |
tejbeer |
257 |
|
| 25570 |
tejbeer |
258 |
@PostMapping(value = "/authuser/remove")
|
| 27595 |
tejbeer |
259 |
public String removeAuthUser(Model model, @RequestParam int id) throws Exception {
|
| 25570 |
tejbeer |
260 |
AuthUser user = authRepository.selectById(id);
|
| 27806 |
tejbeer |
261 |
|
| 25570 |
tejbeer |
262 |
user.setActive(false);
|
|
|
263 |
authRepository.persist(user);
|
| 27806 |
tejbeer |
264 |
List<Position> positions = positionRepository.selectAll(user.getId());
|
|
|
265 |
for (Position position : positions) {
|
|
|
266 |
List<PartnerPosition> partnerPositions = partnerPositionRepository.selectByPositionId(position.getId());
|
|
|
267 |
positionRepository.delete(position.getId());
|
|
|
268 |
for (PartnerPosition partnerPosition : partnerPositions) {
|
|
|
269 |
partnerPositionRepository.delete(partnerPosition.getPositionId());
|
|
|
270 |
}
|
|
|
271 |
}
|
| 25570 |
tejbeer |
272 |
model.addAttribute("response", "true");
|
|
|
273 |
return "response";
|
|
|
274 |
}
|
| 25366 |
tejbeer |
275 |
|
| 24383 |
amit.gupta |
276 |
}
|