From c9c313c594bf53e2af828374b2548c43948428f3 Mon Sep 17 00:00:00 2001 From: KaNi-04 <136799210+KaNi-04@users.noreply.github.com> Date: Thu, 1 Oct 2026 16:48:05 +0530 Subject: [PATCH] login method changed --- .../controller/APIOfficialController.java | 9 +- .../service/OfficialAPIService.java | 195 ++++++++++++++---- 2 files changed, 157 insertions(+), 47 deletions(-) diff --git a/src/main/java/in/gov/chennaicorporation/mobileservice/controller/APIOfficialController.java b/src/main/java/in/gov/chennaicorporation/mobileservice/controller/APIOfficialController.java index 31e20400..428d7e01 100644 --- a/src/main/java/in/gov/chennaicorporation/mobileservice/controller/APIOfficialController.java +++ b/src/main/java/in/gov/chennaicorporation/mobileservice/controller/APIOfficialController.java @@ -58,9 +58,14 @@ public class APIOfficialController { 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") - 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") diff --git a/src/main/java/in/gov/chennaicorporation/mobileservice/service/OfficialAPIService.java b/src/main/java/in/gov/chennaicorporation/mobileservice/service/OfficialAPIService.java index d332d7a0..b5f88893 100644 --- a/src/main/java/in/gov/chennaicorporation/mobileservice/service/OfficialAPIService.java +++ b/src/main/java/in/gov/chennaicorporation/mobileservice/service/OfficialAPIService.java @@ -97,53 +97,158 @@ public class OfficialAPIService { return Collections.singletonList(successResponse); } + // @Transactional + // public List> getloginList(String username, String password, String usertype) { + // // PIN added in response for session logout needed by vaibalan + // String sqlQuery = ""; + // List> 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 response = Map.of( + // "status", 404, + // "message", "User not found."); + // return Collections.singletonList(response); + // } + + // // Add a success status and message to the result + // Map successResponse = Map.of( + // "status", 200, + // "message", "login successfuly.", + // "data", result); + // return Collections.singletonList(successResponse); + // } + @Transactional - public List> getloginList(String username, String password, String usertype) { - // PIN added in response for session logout needed by vaibalan - String sqlQuery = ""; - List> result = null; + public List> getloginList(String username, String password, String usertype, String deviceid) { + // PIN added in response for session logout needed by vaibalan + String sqlQuery = ""; + List> 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 response = Map.of( - "status", 404, - "message", "User not found."); - return Collections.singletonList(response); - } - - // Add a success status and message to the result - Map 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 response = Map.of( + "status", 404, + "message", "User not found." + ); + return Collections.singletonList(response); + } + + + // DEVICE SECURITY CHECK + Map 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 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 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 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 successResponse = Map.of( + "status", 200, + "message", "login successfuly.", + "data", result + ); + return Collections.singletonList(successResponse); + } @Transactional public List updatePassword(String username, String password) {