From c29d431f8011fc95520e11cde6fa395cc9e927ac Mon Sep 17 00:00:00 2001 From: sanjeev Date: Thu, 20 Jan 2022 16:23:46 +0530 Subject: [PATCH] user password reset : ps --- .../User_Management_Controller.php | 50 ++++++++++++++----- api/application/libraries/AWS_COGNITO.php | 11 +++- .../models/User_Management_Model.php | 17 ++----- 3 files changed, 51 insertions(+), 27 deletions(-) diff --git a/api/application/controllers/User_Management_Controller.php b/api/application/controllers/User_Management_Controller.php index b411bbb0..168621ee 100644 --- a/api/application/controllers/User_Management_Controller.php +++ b/api/application/controllers/User_Management_Controller.php @@ -20,6 +20,7 @@ class User_Management_Controller extends REST_Controller { $this->load->model('User_Management_Model'); $this->load->library('AWS_S3'); $this->load->library('AWS_SES'); + $this->load->library('AWS_COGNITO'); } public function listAllUsers_get() @@ -543,7 +544,12 @@ else { public function generateOTP_post(){ $records = $this->post('records'); - $result = $this->User_Management_Model->generateOTP($records); + if(empty($records) || !isset($records['email'])){ + $data['dataStatus'] = false; + $data['status'] = REST_Controller::HTTP_NO_CONTENT; + $data['msg'] = 'Check the arguments! '; + }else{ + $result = $this->User_Management_Model->generateAndVerifyOTP($records); if(count($result)>1) { $data['dataStatus'] = false; @@ -557,13 +563,16 @@ else { $userid = $result[0]['userid']; $string2 = str_shuffle('1234567890'); $OTP = substr($string2,0,6); - $msg = "Your OTP for forgot password is ".$OTP."."; - SMS_GUPSHUP::sendSMS($msg,$mobile); + // $msg = "Your OTP for forgot password is ".$OTP."."; + // SMS_GUPSHUP::sendSMS($msg,$mobile); + $msg = "OTP for starting your personal discussions with the verification officer is ".$OTP.".Pls share this only when the officer has reached your place"; + + SMS_GUPSHUP::sendSMS($msg,$mobile,array('userid' => $userid,'status' => 'STARTED','msg' => 'Started OTP msg from user controlller')); $this->User_Management_Model->updateRecords(array('OTP'=>$OTP),USERPROFILE,array('userid'=>$userid)); $data['dataStatus'] = true; $data['status'] = REST_Controller::HTTP_OK; - $data['msg'] = "Success"; + $data['msg'] = "temporarily password been Resetted"; } else @@ -571,7 +580,7 @@ else { $data['dataStatus'] = false; $data['status'] = REST_Controller::HTTP_NO_CONTENT; $data['msg'] = 'No Data Available for given Email. Try Again!'; - } + }} $this->response($data,REST_Controller::HTTP_OK); } @@ -579,22 +588,39 @@ else { // mailid // otp $records = $this->post('records'); - $result = $this->User_Management_Model->verifyOTP($records); + if(empty($records) || !isset($records['OTP']) || !isset($records['email'])){ + $data['dataStatus'] = false; + $data['status'] = REST_Controller::HTTP_NO_CONTENT; + $data['msg'] = 'Check again the Email and OTP ! '; + }else{ + $result = $this->User_Management_Model->generateAndVerifyOTP($records); if(count($result)>0) { $userid = $result[0]['userid']; - // call reset password function on here. - $this->User_Management_Model->updateRecords(array('OTP'=>null),USERPROFILE,array('userid'=>$userid)); - $data['dataStatus'] = true; - $data['status'] = REST_Controller::HTTP_OK; - $data['msg'] = "Success"; + $usertype = $result[0]['fk_entity_type_id']; + if($usertype != '' && $usertype != null){ + // call reset password function on here. + if((int)$usertype == 1) $key = "sine_egde_pool_id"; + else if((int)$usertype == 2) $key = "lender_pool_id"; + else if((int)$usertype == 3) $key = "vendor_pool_id"; + $this->aws_cognito->resetUserPassword($records['email'],$key); + + $this->User_Management_Model->updateRecords(array('OTP'=>null),USERPROFILE,array('userid'=>$userid)); + $data['dataStatus'] = true; + $data['status'] = REST_Controller::HTTP_OK; + $data['msg'] = "Success"; + }else{ + $data['dataStatus'] = false; + $data['status'] = REST_Controller::HTTP_NO_CONTENT; + $data['msg'] = "User's Entity Type Not Mentioned !! "; + } } else { $data['dataStatus'] = false; $data['status'] = REST_Controller::HTTP_NO_CONTENT; $data['msg'] = 'No Data Available for given Email and Its OTP. Try Again!'; - } + }} $this->response($data,REST_Controller::HTTP_OK); } diff --git a/api/application/libraries/AWS_COGNITO.php b/api/application/libraries/AWS_COGNITO.php index 86f5b8d1..afb65335 100644 --- a/api/application/libraries/AWS_COGNITO.php +++ b/api/application/libraries/AWS_COGNITO.php @@ -52,7 +52,16 @@ class AWS_COGNITO ]); print_r($result); } - + + public function resetUserPassword($user_name,$key) + { + $result = $this->client->adminSetUserPassword([ + 'Password' => 'temp1234', // REQUIRED + 'Permanent' => false, + 'UserPoolId' => $this->CI->config->item($key), // REQUIRED + 'Username' => $user_name, // REQUIRED + ]); + } } ?> \ No newline at end of file diff --git a/api/application/models/User_Management_Model.php b/api/application/models/User_Management_Model.php index 10dc70f3..50665fe7 100644 --- a/api/application/models/User_Management_Model.php +++ b/api/application/models/User_Management_Model.php @@ -176,28 +176,17 @@ class User_Management_Model extends SPARQ_Model { } } - public function generateOTP($arg) + public function generateAndVerifyOTP($arg) { - $this->db->select('USERPROFILE.userid, USERPROFILE.first_name as user_first_name, USERPROFILE.last_name as user_last_name, USERPROFILE.email, USERPROFILE.mobile_no,concat(USERPROFILE.first_name," ",USERPROFILE.last_name)as full_name'); + $this->db->select('USERPROFILE.userid, USERPROFILE.first_name as user_first_name, USERPROFILE.last_name as user_last_name, USERPROFILE.email, USERPROFILE.mobile_no,concat(USERPROFILE.first_name," ",USERPROFILE.last_name)as full_name,USERPROFILE.fk_entity_type_id'); $this->db->from(USERPROFILE.' as USERPROFILE'); $this->db->where('USERPROFILE.email',$arg['email']); + if(isset($arg['OTP'])) $this->db->where('USERPROFILE.OTP',$arg['OTP']); $this->db->where('USERPROFILE.isactive',1); // return $this->db->get()->row()->column_list; $query = $this->db->get(); return $query->result_array(); } - public function verifyOTP($arg) - { - // $this->db->select('USERPROFILE.userid, USERPROFILE.first_name as user_first_name, USERPROFILE.last_name as user_last_name, USERPROFILE.email, USERPROFILE.mobile_no,concat(USERPROFILE.first_name," ",USERPROFILE.last_name)as full_name, USERPROFILE.isactive, USERPROFILE.fk_city, USERPROFILE.fk_state,USERPROFILE.fk_entity_type_id,USERPROFILE.designation'); - $this->db->select('USERPROFILE.userid, USERPROFILE.first_name as user_first_name, USERPROFILE.last_name as user_last_name, USERPROFILE.email, USERPROFILE.mobile_no,concat(USERPROFILE.first_name," ",USERPROFILE.last_name)as full_name'); - $this->db->from(USERPROFILE.' as USERPROFILE'); - $this->db->where('USERPROFILE.email',$arg['email']); - $this->db->where('USERPROFILE.OTP',$arg['OTP']); - $this->db->where('USERPROFILE.isactive',1); - // return $this->db->get()->row()->column_list; - $query = $this->db->get(); - return $query->result_array(); - } } \ No newline at end of file