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