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. 129
      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")

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

@ -97,51 +97,156 @@ 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) {
public List<Map<String, Object>> getloginList(String username, String password, String usertype, String deviceid) {
// PIN added in response for session logout needed by vaibalan // PIN added in response for session logout needed by vaibalan
String sqlQuery = ""; String sqlQuery = "";
List<Map<String, Object>> result = null;
List<Map<String, Object>> result=null;
try { try {
switch (usertype) { switch (usertype) {
case "erp": 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";
//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); result = jdbcOfficalTemplate.queryForList(sqlQuery, username, password);
break; break;
case "nonerp": case "nonerp":
sqlQuery = "SELECT id, USER_NAME, EXTRAFIELD2, PIN FROM `EG_USER` WHERE `EXTRAFIELD2` = ? AND `PIN` = ? AND `ISACTIVE`=1 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); result = jdbcOfficalTemplate.queryForList(sqlQuery, username, password);
break; break;
// additional cases can be added here // additional cases can be added here
default: default:
sqlQuery = "SELECT id, USER_NAME, EXTRAFIELD2, PIN FROM `EG_USER` WHERE `EXTRAFIELD2` = ? AND `PIN` = ? AND `ISACTIVE`=1 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); result = jdbcOfficalTemplate.queryForList(sqlQuery, username, password);
// result = Collections.emptyList();
//result = Collections.emptyList();
break; break;
} }
} catch (Exception e) { } catch (Exception e) {
// Log the error // Log the error
e.printStackTrace(); e.printStackTrace();
// Return an error message as part of the result list // Return an error message as part of the result list
return Collections
.singletonList(Collections.singletonMap("error", "An error occurred while fetching data."));
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 no records found, return a status and message indicating no user found
if (result == null || result.isEmpty()) { if (result == null || result.isEmpty()) {
Map<String, Object> response = Map.of( Map<String, Object> response = Map.of(
"status", 404, "status", 404,
"message", "User not found.");
"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); 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 // Add a success status and message to the result
Map<String, Object> successResponse = Map.of( Map<String, Object> successResponse = Map.of(
"status", 200, "status", 200,
"message", "login successfuly.", "message", "login successfuly.",
"data", result);
"data", result
);
return Collections.singletonList(successResponse); return Collections.singletonList(successResponse);
} }

Loading…
Cancel
Save