| 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;
|
|
|
37 |
|
| 24383 |
amit.gupta |
38 |
@Controller
|
| 25192 |
tejbeer |
39 |
@Transactional(rollbackFor = Throwable.class)
|
| 24383 |
amit.gupta |
40 |
public class AuthUserController {
|
|
|
41 |
|
|
|
42 |
private static final Logger LOGGER = LogManager.getLogger(AuthUserController.class);
|
|
|
43 |
|
|
|
44 |
@Autowired
|
|
|
45 |
private AuthService authService;
|
|
|
46 |
|
|
|
47 |
@Autowired
|
|
|
48 |
private AuthRepository authRepository;
|
|
|
49 |
|
|
|
50 |
@Autowired
|
|
|
51 |
private UserRepository userRepository;
|
|
|
52 |
|
|
|
53 |
@Autowired
|
|
|
54 |
private RoleRepository roleRepository;
|
|
|
55 |
|
|
|
56 |
@Autowired
|
| 29209 |
manish |
57 |
private MVCResponseSender mvcResponseSender;
|
|
|
58 |
|
|
|
59 |
@Autowired
|
| 24383 |
amit.gupta |
60 |
private UserRoleRepository userRoleRepository;
|
|
|
61 |
|
|
|
62 |
@Autowired
|
|
|
63 |
private CookiesProcessor cookiesProcessor;
|
|
|
64 |
|
|
|
65 |
@Autowired
|
| 27806 |
tejbeer |
66 |
private PositionRepository positionRepository;
|
|
|
67 |
|
|
|
68 |
@Autowired
|
| 32493 |
amit.gupta |
69 |
private PartnerPositionRepository partnerPositionRepository;
|
| 27806 |
tejbeer |
70 |
|
|
|
71 |
@Autowired
|
| 24383 |
amit.gupta |
72 |
@Qualifier("userUserRepository")
|
|
|
73 |
private com.spice.profitmandi.dao.repository.user.UserRepository userUserRepository;
|
|
|
74 |
|
|
|
75 |
@GetMapping(value = "/createAuthUser")
|
|
|
76 |
public String getcreateAuthUser(HttpServletRequest request,
|
|
|
77 |
@RequestParam(name = "offset", defaultValue = "0") int offset,
|
|
|
78 |
@RequestParam(name = "limit", defaultValue = "10") int limit, Model model)
|
|
|
79 |
throws ProfitMandiBusinessException {
|
| 25570 |
tejbeer |
80 |
// List<AuthUser> authUsers = authRepository.selectAll(offset, limit);
|
|
|
81 |
List<AuthUser> authUsers = authRepository.selectAllActiveUser(offset, limit);
|
|
|
82 |
|
| 24383 |
amit.gupta |
83 |
long size = authRepository.selectCountAuthUser();
|
| 29209 |
manish |
84 |
|
|
|
85 |
List<AuthUser> activeAuthUsers = authRepository.selectAllActiveUser();
|
|
|
86 |
|
|
|
87 |
Map<Integer, AuthUser> userManagerMap = new HashMap<>();
|
|
|
88 |
|
|
|
89 |
for (AuthUser authUser : authUsers) {
|
|
|
90 |
if (authUser.getManagerId() != 0) {
|
|
|
91 |
userManagerMap.put(authUser.getId(), authRepository.selectById(authUser.getManagerId()));
|
|
|
92 |
}
|
|
|
93 |
}
|
| 32463 |
jai.hind |
94 |
|
| 29209 |
manish |
95 |
model.addAttribute("userManagerMap", userManagerMap);
|
|
|
96 |
model.addAttribute("activeAuthUsers", activeAuthUsers);
|
| 24383 |
amit.gupta |
97 |
model.addAttribute("authUsers", authUsers);
|
|
|
98 |
model.addAttribute("start", offset + 1);
|
|
|
99 |
model.addAttribute("size", size);
|
| 29209 |
manish |
100 |
model.addAttribute("url", "/getPaginatedAuthUser");
|
| 32559 |
amit.gupta |
101 |
|
| 24383 |
amit.gupta |
102 |
if (authUsers.size() < limit) {
|
|
|
103 |
model.addAttribute("end", offset + authUsers.size());
|
|
|
104 |
} else {
|
|
|
105 |
model.addAttribute("end", offset + limit);
|
|
|
106 |
}
|
| 29209 |
manish |
107 |
|
| 24383 |
amit.gupta |
108 |
return "create-auth-user";
|
|
|
109 |
|
|
|
110 |
}
|
|
|
111 |
|
| 29209 |
manish |
112 |
@PostMapping(value = "/addManagerId")
|
|
|
113 |
public String addManagerId(HttpServletRequest request, @RequestParam(name = "authId") int authId,
|
|
|
114 |
@RequestParam(name = "managerId") int managerId, Model model) throws Exception {
|
|
|
115 |
// List<AuthUser> authUsers = authRepository.selectAll(offset, limit);
|
|
|
116 |
AuthUser authUser = authRepository.selectById(authId);
|
|
|
117 |
|
|
|
118 |
if (authUser.getId() != 0) {
|
|
|
119 |
|
|
|
120 |
authUser.setManagerId(managerId);
|
|
|
121 |
}
|
|
|
122 |
List<AuthUser> activeAuthUsers = authRepository.selectAllActiveUser();
|
|
|
123 |
|
|
|
124 |
AuthUser manager = authRepository.selectById(authUser.getManagerId());
|
|
|
125 |
|
|
|
126 |
model.addAttribute("activeAuthUsers", activeAuthUsers);
|
|
|
127 |
model.addAttribute("authUser", authUser);
|
|
|
128 |
model.addAttribute("manager", manager);
|
|
|
129 |
|
|
|
130 |
return "create-auth-user";
|
|
|
131 |
|
|
|
132 |
}
|
|
|
133 |
|
| 24383 |
amit.gupta |
134 |
@PostMapping(value = "/createAuthUser")
|
|
|
135 |
public String createAuthUser(HttpServletRequest request,
|
|
|
136 |
@RequestParam(name = ProfitMandiConstants.FIRST_NAME, defaultValue = "") String firstName,
|
|
|
137 |
@RequestParam(name = ProfitMandiConstants.LAST_NAME, defaultValue = "") String lastName,
|
|
|
138 |
@RequestParam(name = ProfitMandiConstants.EMAIL_ID, defaultValue = "") String emailId,
|
|
|
139 |
@RequestParam(name = ProfitMandiConstants.MOBILE_NUMBER, defaultValue = "") String mobileNumber,
|
| 25366 |
tejbeer |
140 |
@RequestParam(name = ProfitMandiConstants.GMAIL_ID, defaultValue = "") String gmailId,
|
| 27595 |
tejbeer |
141 |
@RequestParam(name = "employeeCode", defaultValue = "") String employeeCode,
|
| 32463 |
jai.hind |
142 |
@RequestParam(name = "imageUrl", defaultValue = "") String imageUrl,
|
|
|
143 |
@RequestParam(name = "offset", defaultValue = "0") int offset,
|
| 24383 |
amit.gupta |
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);
|
| 32463 |
jai.hind |
189 |
authUser.setImageUrl(imageUrl);
|
| 27595 |
tejbeer |
190 |
authUser.setEmployeeCode(employeeCode);
|
| 25587 |
tejbeer |
191 |
authUser.setActive(true);
|
| 25570 |
tejbeer |
192 |
|
| 24383 |
amit.gupta |
193 |
authUser.setCreatedTimestamp(LocalDateTime.now());
|
|
|
194 |
authService.addAuthUser(authUser);
|
|
|
195 |
|
|
|
196 |
List<AuthUser> authUsers = authRepository.selectAll(offset, limit);
|
|
|
197 |
long size = authRepository.selectCountAuthUser();
|
|
|
198 |
model.addAttribute("authUsers", authUsers);
|
|
|
199 |
model.addAttribute("start", offset + 1);
|
|
|
200 |
model.addAttribute("size", size);
|
|
|
201 |
if (authUsers.size() < limit) {
|
|
|
202 |
model.addAttribute("end", offset + authUsers.size());
|
|
|
203 |
} else {
|
|
|
204 |
model.addAttribute("end", offset + limit);
|
|
|
205 |
}
|
|
|
206 |
return "create-auth-user";
|
|
|
207 |
}
|
|
|
208 |
|
|
|
209 |
@GetMapping(value = "/changePassword")
|
|
|
210 |
public String getChangePassword(HttpServletRequest request, Model model) {
|
|
|
211 |
return "change-auth-user-password";
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
@PostMapping(value = "/changePassword")
|
|
|
215 |
public String changePassword(HttpServletRequest request,
|
|
|
216 |
@RequestParam(name = "oldPassword", defaultValue = "") String oldPassword,
|
|
|
217 |
@RequestParam(name = "newPassword", defaultValue = "") String newPassword, Model model)
|
|
|
218 |
throws ProfitMandiBusinessException {
|
|
|
219 |
|
|
|
220 |
LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
|
|
|
221 |
authService.changePassword(loginDetails.getEmailId(), oldPassword, newPassword);
|
|
|
222 |
return "change-auth-user-password";
|
|
|
223 |
}
|
|
|
224 |
|
|
|
225 |
@GetMapping(value = "/getPaginatedAuthUser")
|
|
|
226 |
public String getPaginatedAuthUser(HttpServletRequest request,
|
|
|
227 |
@RequestParam(name = "offset", defaultValue = "0") int offset,
|
|
|
228 |
@RequestParam(name = "limit", defaultValue = "10") int limit, Model model) {
|
| 27806 |
tejbeer |
229 |
// List<AuthUser> authUsers = authRepository.selectAll(offset, limit);
|
| 29209 |
manish |
230 |
|
|
|
231 |
List<AuthUser> activeAuthUsers = authRepository.selectAllActiveUser();
|
|
|
232 |
|
| 25570 |
tejbeer |
233 |
List<AuthUser> authUsers = authRepository.selectAllActiveUser(offset, limit);
|
| 29209 |
manish |
234 |
|
|
|
235 |
Map<Integer, AuthUser> userManagerMap = new HashMap<>();
|
|
|
236 |
|
|
|
237 |
for (AuthUser authUser : authUsers) {
|
|
|
238 |
if (authUser.getManagerId() != 0) {
|
|
|
239 |
userManagerMap.put(authUser.getId(), authRepository.selectById(authUser.getManagerId()));
|
|
|
240 |
}
|
|
|
241 |
}
|
|
|
242 |
model.addAttribute("userManagerMap", userManagerMap);
|
| 24383 |
amit.gupta |
243 |
model.addAttribute("authUsers", authUsers);
|
| 29209 |
manish |
244 |
model.addAttribute("activeAuthUsers", activeAuthUsers);
|
|
|
245 |
model.addAttribute("url", "/getPaginatedAuthUser");
|
| 24383 |
amit.gupta |
246 |
return "auth-users-paginated";
|
|
|
247 |
}
|
| 25570 |
tejbeer |
248 |
|
|
|
249 |
@GetMapping(value = "/authuser/edit")
|
|
|
250 |
public String updateAuthUser(Model model, @RequestParam String gmailId, @RequestParam String emailId)
|
|
|
251 |
throws Exception {
|
| 24478 |
amit.gupta |
252 |
AuthUser user = authRepository.selectByEmailOrMobile(emailId);
|
|
|
253 |
user.setGmailId(gmailId);
|
| 32559 |
amit.gupta |
254 |
//authRepository.persist(user);
|
| 31238 |
amit.gupta |
255 |
model.addAttribute("response1", "true");
|
| 24478 |
amit.gupta |
256 |
return "response";
|
|
|
257 |
}
|
| 32463 |
jai.hind |
258 |
@GetMapping(value = "/authuser/editImageUrl")
|
|
|
259 |
public String updateAuthUserImage(Model model, @RequestParam String imageUrl ,@RequestParam String emailId)
|
|
|
260 |
throws Exception{
|
|
|
261 |
AuthUser user = authRepository.selectByEmailOrMobile(emailId);
|
|
|
262 |
user.setImageUrl(imageUrl);
|
|
|
263 |
model.addAttribute("response1", "true");
|
|
|
264 |
return "response";
|
|
|
265 |
}
|
| 27595 |
tejbeer |
266 |
|
| 25570 |
tejbeer |
267 |
@PostMapping(value = "/authuser/remove")
|
| 27595 |
tejbeer |
268 |
public String removeAuthUser(Model model, @RequestParam int id) throws Exception {
|
| 25570 |
tejbeer |
269 |
AuthUser user = authRepository.selectById(id);
|
| 27806 |
tejbeer |
270 |
|
| 25570 |
tejbeer |
271 |
user.setActive(false);
|
|
|
272 |
authRepository.persist(user);
|
| 27806 |
tejbeer |
273 |
List<Position> positions = positionRepository.selectAll(user.getId());
|
|
|
274 |
for (Position position : positions) {
|
|
|
275 |
List<PartnerPosition> partnerPositions = partnerPositionRepository.selectByPositionId(position.getId());
|
|
|
276 |
positionRepository.delete(position.getId());
|
|
|
277 |
for (PartnerPosition partnerPosition : partnerPositions) {
|
|
|
278 |
partnerPositionRepository.delete(partnerPosition.getPositionId());
|
|
|
279 |
}
|
|
|
280 |
}
|
| 31238 |
amit.gupta |
281 |
model.addAttribute("response1", "true");
|
| 25570 |
tejbeer |
282 |
return "response";
|
|
|
283 |
}
|
| 25366 |
tejbeer |
284 |
|
| 24383 |
amit.gupta |
285 |
}
|