Browse Source

login method changed

master
KaNi-04 2 weeks ago
parent
commit
c9c313c594
  1. 9
      src/main/java/in/gov/chennaicorporation/mobileservice/controller/APIOfficialController.java
  2. 195
      src/main/java/in/gov/chennaicorporation/mobileservice/service/OfficialAPIService.java

9
src/main/java/in/gov/chennaicorporation/mobileservice/controller/APIOfficialController.java

@ -58,9 +58,14 @@ public class APIOfficialController {
return officialAPIService.updatePassword(username,password); return officialAPIService.updatePassword(username,password);
} }
// @GetMapping(value="login")
// public List login(@RequestParam("username") String username, @RequestParam("password") String Password, @RequestParam("usertype") String usertype) {
// return officialAPIService.getloginList(username, Password, usertype);
// }
@GetMapping(value="login") @GetMapping(value="login")
public List login(@RequestParam("username") String username, @RequestParam("password") String Password, @RequestParam("usertype") String usertype) {
return officialAPIService.getloginList(username, Password, usertype);
public List login(@RequestParam("username") String username, @RequestParam("password") String Password, @RequestParam("usertype") String usertype, @RequestParam(value = "deviceid", required = false) String deviceid) {
return officialAPIService.getloginList(username, Password, usertype, deviceid);
} }
@GetMapping(value="getUserByMobile") @GetMapping(value="getUserByMobile")

195
src/main/java/in/gov/chennaicorporation/mobileservice/service/OfficialAPIService.java

@ -97,53 +97,158 @@ public class OfficialAPIService {
return Collections.singletonList(successResponse); return Collections.singletonList(successResponse);
} }
// @Transactional
// public List<Map<String, Object>> getloginList(String username, String password, String usertype) {
// // PIN added in response for session logout needed by vaibalan
// String sqlQuery = "";
// List<Map<String, Object>> result = null;
// try {
// switch (usertype) {
// case "erp":
// // sqlQuery = "SELECT * FROM `EG_USER` WHERE `ISPORTALUSER` <> 1 AND `ISACTIVE`
// // = 1 AND `EXTRAFIELD2` = ? AND `PIN` = ? LIMIT 1";
// sqlQuery = "SELECT id, USER_NAME, EXTRAFIELD2, PIN FROM `EG_USER` WHERE `EXTRAFIELD2` = ? AND `PIN` = ? AND `ISACTIVE`=1 LIMIT 1";
// result = jdbcOfficalTemplate.queryForList(sqlQuery, username, password);
// break;
// case "nonerp":
// sqlQuery = "SELECT id, USER_NAME, EXTRAFIELD2, PIN FROM `EG_USER` WHERE `EXTRAFIELD2` = ? AND `PIN` = ? AND `ISACTIVE`=1 LIMIT 1";
// result = jdbcOfficalTemplate.queryForList(sqlQuery, username, password);
// break;
// // additional cases can be added here
// default:
// sqlQuery = "SELECT id, USER_NAME, EXTRAFIELD2, PIN FROM `EG_USER` WHERE `EXTRAFIELD2` = ? AND `PIN` = ? AND `ISACTIVE`=1 LIMIT 1";
// result = jdbcOfficalTemplate.queryForList(sqlQuery, username, password);
// // result = Collections.emptyList();
// break;
// }
// } catch (Exception e) {
// // Log the error
// e.printStackTrace();
// // Return an error message as part of the result list
// return Collections
// .singletonList(Collections.singletonMap("error", "An error occurred while fetching data."));
// }
// // If no records found, return a status and message indicating no user found
// if (result == null || result.isEmpty()) {
// Map<String, Object> response = Map.of(
// "status", 404,
// "message", "User not found.");
// return Collections.singletonList(response);
// }
// // Add a success status and message to the result
// Map<String, Object> successResponse = Map.of(
// "status", 200,
// "message", "login successfuly.",
// "data", result);
// return Collections.singletonList(successResponse);
// }
@Transactional @Transactional
public List<Map<String, Object>> getloginList(String username, String password, String usertype) {
// PIN added in response for session logout needed by vaibalan
String sqlQuery = "";
List<Map<String, Object>> result = null;
public List<Map<String, Object>> getloginList(String username, String password, String usertype, String deviceid) {
// PIN added in response for session logout needed by vaibalan
String sqlQuery = "";
List<Map<String, Object>> result=null;
try { try {
switch (usertype) {
case "erp":
// sqlQuery = "SELECT * FROM `EG_USER` WHERE `ISPORTALUSER` <> 1 AND `ISACTIVE`
// = 1 AND `EXTRAFIELD2` = ? AND `PIN` = ? LIMIT 1";
sqlQuery = "SELECT id, USER_NAME, EXTRAFIELD2, PIN FROM `EG_USER` WHERE `EXTRAFIELD2` = ? AND `PIN` = ? AND `ISACTIVE`=1 LIMIT 1";
result = jdbcOfficalTemplate.queryForList(sqlQuery, username, password);
break;
case "nonerp":
sqlQuery = "SELECT id, USER_NAME, EXTRAFIELD2, PIN FROM `EG_USER` WHERE `EXTRAFIELD2` = ? AND `PIN` = ? AND `ISACTIVE`=1 LIMIT 1";
result = jdbcOfficalTemplate.queryForList(sqlQuery, username, password);
break;
// additional cases can be added here
default:
sqlQuery = "SELECT id, USER_NAME, EXTRAFIELD2, PIN FROM `EG_USER` WHERE `EXTRAFIELD2` = ? AND `PIN` = ? AND `ISACTIVE`=1 LIMIT 1";
result = jdbcOfficalTemplate.queryForList(sqlQuery, username, password);
// result = Collections.emptyList();
break;
}
} catch (Exception e) {
// Log the error
e.printStackTrace();
// Return an error message as part of the result list
return Collections
.singletonList(Collections.singletonMap("error", "An error occurred while fetching data."));
}
// If no records found, return a status and message indicating no user found
if (result == null || result.isEmpty()) {
Map<String, Object> response = Map.of(
"status", 404,
"message", "User not found.");
return Collections.singletonList(response);
}
// Add a success status and message to the result
Map<String, Object> successResponse = Map.of(
"status", 200,
"message", "login successfuly.",
"data", result);
return Collections.singletonList(successResponse);
}
switch (usertype) {
case "erp":
//sqlQuery = "SELECT * FROM `EG_USER` WHERE `ISPORTALUSER` <> 1 AND `ISACTIVE` = 1 AND `EXTRAFIELD2` = ? AND `PIN` = ? LIMIT 1";
sqlQuery = "SELECT id, USER_NAME, EXTRAFIELD2, PIN, deviceid, issecure FROM `EG_USER` WHERE `EXTRAFIELD2` = ? AND `PIN` = ? AND `ISACTIVE`=1 LIMIT 1";
result = jdbcOfficalTemplate.queryForList(sqlQuery, username, password);
break;
case "nonerp":
sqlQuery = "SELECT id, USER_NAME, EXTRAFIELD2, PIN, deviceid, issecure FROM `EG_USER` WHERE `EXTRAFIELD2` = ? AND `PIN` = ? AND `ISACTIVE`=1 LIMIT 1";
result = jdbcOfficalTemplate.queryForList(sqlQuery, username, password);
break;
// additional cases can be added here
default:
sqlQuery = "SELECT id, USER_NAME, EXTRAFIELD2, PIN, deviceid, issecure FROM `EG_USER` WHERE `EXTRAFIELD2` = ? AND `PIN` = ? AND `ISACTIVE`=1 LIMIT 1";
result = jdbcOfficalTemplate.queryForList(sqlQuery, username, password);
//result = Collections.emptyList();
break;
}
} catch (Exception e) {
// Log the error
e.printStackTrace();
// Return an error message as part of the result list
return Collections.singletonList(Collections.singletonMap("error", "An error occurred while fetching data."));
}
// If no records found, return a status and message indicating no user found
if (result == null || result.isEmpty()) {
Map<String, Object> response = Map.of(
"status", 404,
"message", "User not found."
);
return Collections.singletonList(response);
}
// DEVICE SECURITY CHECK
Map<String, Object> user = result.get(0);
Object isSecureObject = user.get("issecure");
Object storedDeviceObject = user.get("deviceid");
boolean isSecure =
isSecureObject != null &&
(
Boolean.TRUE.equals(isSecureObject) ||
"1".equals(String.valueOf(isSecureObject)) ||
"true".equalsIgnoreCase(String.valueOf(isSecureObject))
);
String storedDeviceId =
storedDeviceObject == null
? null
: String.valueOf(storedDeviceObject);
if (isSecure) {
// Device ID is mandatory for secure users
if (deviceid == null || deviceid.trim().isEmpty()) {
Map<String, Object> response = Map.of(
"status", 403,
"message", "Device ID is required for this user."
);
return Collections.singletonList(response);
}
// Stored device ID is missing
if (storedDeviceId == null || storedDeviceId.trim().isEmpty()) {
Map<String, Object> response = Map.of(
"status", 403,
"message", "No device is registered for this user."
);
return Collections.singletonList(response);
}
// Device ID doesn't match
if (!storedDeviceId.equals(deviceid.trim())) {
Map<String, Object> response = Map.of(
"status", 403,
"message", "Device ID does not match the registered device."
);
return Collections.singletonList(response);
}
}
// LOGIN SUCCESS
// Add a success status and message to the result
Map<String, Object> successResponse = Map.of(
"status", 200,
"message", "login successfuly.",
"data", result
);
return Collections.singletonList(successResponse);
}
@Transactional @Transactional
public List<UpdateResponse> updatePassword(String username, String password) { public List<UpdateResponse> updatePassword(String username, String password) {

Loading…
Cancel
Save