From c1a0c5b1a9215ea7c5d745cb5810a65f314d1e89 Mon Sep 17 00:00:00 2001 From: VE10-Sanjeev Date: Tue, 26 Sep 2023 16:40:20 +0530 Subject: [PATCH] bug fixes : ps --- app/Controllers/Authentication.php | 9 +- app/Controllers/BaseController.php | 4 +- app/Controllers/Users.php | 165 ++++++++++++++++++++--------- app/Models/AuthenticationModel.php | 2 +- app/Views/template/footer.php | 4 +- app/Views/user_form.php | 110 +++++++++---------- app/Views/user_list.php | 2 +- public/uploads/default.ico | Bin public/uploads/default.png | Bin public/uploads/default_logo.png | Bin 10 files changed, 176 insertions(+), 120 deletions(-) mode change 100644 => 100755 public/uploads/default.ico mode change 100644 => 100755 public/uploads/default.png mode change 100644 => 100755 public/uploads/default_logo.png diff --git a/app/Controllers/Authentication.php b/app/Controllers/Authentication.php index 78392676..02a87ca0 100755 --- a/app/Controllers/Authentication.php +++ b/app/Controllers/Authentication.php @@ -36,7 +36,7 @@ class Authentication extends BaseController $username = $this->request->getPost('username'); $password = $this->request->getPost('password'); - $user = $auth_model->where('email', $username)->first(); + $user = $auth_model->where(['email'=>$username,'isactive'=>1])->first(); // $this->logger->info("Authenticate: Function Called."); if (is_null($user)) { @@ -260,12 +260,13 @@ class Authentication extends BaseController $session_uid = get_logged_user_id(); $session_uname = get_logged_name(); $auth_model = new AuthenticationModel(); - + + $details = $auth_model->getheringDetailsForHeader($session_uid); $data['loggedin_person'] = $session_uname; $data['loggedin_person_role'] = $details[0]['role']; - $data['favicon'] = file_exists(base_url()."public/uploads/".$details[0]['favicon']) ? base_url()."public/uploads/".$details[0]['favicon'] : base_url()."public/uploads/default.ico"; - $data['profile_picture'] = file_exists(base_url()."public/uploads/".$details[0]['profile_picture']) ? base_url()."public/uploads/".$details[0]['profile_picture'] : base_url()."public/assets/images/users/avatar-9.jpg"; + $data['favicon'] = file_exists(FCPATH."public/uploads/".$details[0]['favicon']) ? base_url("public/uploads/".$details[0]['favicon']) : base_url("public/uploads/default.ico"); + $data['profile_picture'] = file_exists(FCPATH."public/uploads/".$details[0]['profile_picture']) ? base_url("public/uploads/" . $details[0]['profile_picture']) : base_url("public/assets/images/users/avatar-9.jpg"); $successMessage = session()->getFlashdata('success'); $validationErrors = session()->getFlashdata('error'); // Load and display the form view with the above data diff --git a/app/Controllers/BaseController.php b/app/Controllers/BaseController.php index f9eba771..b88adb39 100644 --- a/app/Controllers/BaseController.php +++ b/app/Controllers/BaseController.php @@ -74,8 +74,8 @@ abstract class BaseController extends Controller $data['loggedin_person'] = $session_uname; $data['loggedin_person_id'] = $session_uid; $data['loggedin_person_role'] = $details[0]['role']; - $data['favicon'] = file_exists(base_url()."public/uploads/".$details[0]['favicon']) ? base_url()."public/uploads/".$details[0]['favicon'] : base_url()."public/uploads/default.ico"; - $data['profile_picture'] = file_exists(base_url()."public/uploads/".$details[0]['profile_picture']) ? base_url()."public/uploads/".$details[0]['profile_picture'] : base_url()."public/assets/images/users/avatar-9.jpg"; + $data['favicon'] = file_exists(FCPATH."public/uploads/".$details[0]['favicon']) ? base_url("public/uploads/".$details[0]['favicon']) : base_url("public/uploads/default.ico"); + $data['profile_picture'] = file_exists(FCPATH."public/uploads/".$details[0]['profile_picture']) ? base_url("public/uploads/" . $details[0]['profile_picture']) : base_url("public/assets/images/users/avatar-9.jpg"); $data['browser_title'] = $data['company_name'] . ' | ' . $data['company_short_name'] . ' ' . $data['page_name']; $data['heading'] = $data['page_name'] == "Dashboard" ? 'Welcome to ' . $data['company_name'] : ""; echo view('template/header.php', $data); diff --git a/app/Controllers/Users.php b/app/Controllers/Users.php index a60396cf..c4df248a 100755 --- a/app/Controllers/Users.php +++ b/app/Controllers/Users.php @@ -12,17 +12,21 @@ class Users extends BaseController public function index() { helper('session'); + $session_role = get_user_role(); $session_bid = get_business_id(); if (!empty($session_role) && $session_role !== "sadmin") { + $this->logger->info("Users: Listing In admin role ."); $where = ['users.business_id' => (int)$session_bid, 'users.isactive' => 1]; } else { + $this->logger->info("Users: Listing In Super-admin role ."); $where = ['users.isactive !=' => NULL]; } $model = new UsersModel(); $model->setTable('users'); $user_details = $model->where($where)->orderBy('user_id', 'DESC')->findAll(); + $this->logger->info("Users: Listing Count .".count($user_details)); $data['page_name'] = 'User Details'; $data['details'] = $user_details; $this->render_page('user_list', $data); @@ -35,9 +39,11 @@ class Users extends BaseController $session_role = get_user_role(); $session_bid = get_business_id(); if ($id === '0') { + $this->logger->info("Users: In Add page"); $data['page_name'] = 'Add User'; $data['details'] = []; } else if ($id !== '0') { + $this->logger->info("Users: In Edit page"); $data['page_name'] = 'Edit User'; $model = new UsersModel(); $model->setTable('users'); @@ -67,56 +73,81 @@ class Users extends BaseController ## For inserting/updating details of user public function insert_users() { - // print_r($this->request->getPost());die(); - helper('session'); - $session_uid = get_logged_user_id(); - $session_bid = get_business_id(); + $this->logger->info("Users: Inserting/Updating Details"); try { - // print_r($_FILES);die(); + ## CI validation rule for profile_picture $validationRule = [ - 'userfile' => [ + 'profile_picture' => [ 'label' => 'Image File', - 'rules' => [ - 'uploaded[profile_picture]', - 'is_image[profile_picture]', - 'mime_in[profile_picture,image/jpg,image/jpeg,image/gif,image/png,image/webp]', - - ], - ], + 'rules' => 'uploaded[profile_picture]|is_image[profile_picture]|mime_in[profile_picture,image/jpg,image/jpeg,image/gif,image/png,image/webp]' + ] ]; - if (!$this->validate($validationRule) && $this->request->getPost('profile_picture') === '') { - $error = $this->validator->getErrors(); - throw new \Exception((string)$error); - } - $img = $this->request->getFile('profile_picture'); + ## Declarions + helper('session'); + $UsersModel = new UsersModel(); + $session_uid = get_logged_user_id(); + $session_bid = get_business_id(); $user_id = $this->request->getPost('user_id'); - $business_id = $this->request->getPost('business_id'); - $filePath = 'public/uploads/' . $this->request->getPost('profile_picture'); - - ## File Already Existing or not - if ($img->isValid() && !$img->hasMoved()) { - $fileName = $img->getName(); - $img->move('public/uploads/', $fileName); - - - // Delete the previous image file if it exists - if ($user_id) { - $previousFileName = $this->request->getPost('previous_ufile'); - if ($previousFileName && is_file('public/uploads/' . $previousFileName)) { - link('public/uploads/'.$previousFileName,''); + $business_id = $this->request->getPost('business_id')?$this->request->getPost('business_id'):$session_bid; + $img_details = $this->request->getFile('profile_picture'); // Here I Have Image details ; + $final_img_name = NULL;//Just flag + $existing_img_name = $this->request->getPost('existing_profile_picture_name'); // HiddenField for if have any pic name means; + $img_path = 'public/uploads/'; + + ##Step 1 : image details available + if($img_details){ + $this->logger->info("Users: image details avaiable"); + ##Step 2 : i have image details .
+ if ($img_details->isValid()) { + ##Step 3 : image Name.$new_img_name."
"; + $new_img_name = $img_details->getName(); // before movement name + $this->logger->info("Users: Image Name B4 Upload".$new_img_name); + ##Step 4 : Validation Rule Apply here.if not throw the error"
"; + if (!$this->validate($validationRule)) { + if($new_img_name !== ''){ + + $error = $this->validator->getErrors(); + // echo "Error : ".(string)$error."
"; + $this->logger->error("Users: Err on image upload".$error['profile_picture']); + throw new \Exception((string)$error['profile_picture']); + } } + ##Step 5 : Check Existing and New Image Name Same Or not same no use to move on target folder + if($new_img_name !== $existing_img_name){ + $this->logger->info("Users: Image Name are Differ".$new_img_name." & ".$existing_img_name); + ## Step 6 : Different Image Name means removed on target folder using Unlink; + if ($existing_img_name && is_file($img_path.$existing_img_name)) { + $this->logger->info("Users: already Image Available on target Folder Path : ".$img_path.$existing_img_name." So, Deleted."); + // echo "Deleted_file : ".(string)$img_path.$existing_img_name."
"; + unlink($img_path.$existing_img_name); + } + ## Step 7 : Moved to target; + $img_details->move($img_path, $new_img_name); + $final_img_name = $img_details->getName(); // after movement name + $this->logger->info("Users: Image Name After Upload".$final_img_name); + }else{ + $final_img_name = $existing_img_name; + $this->logger->info("Users: Image Name are same".$new_img_name." & ".$existing_img_name." Can't Upload"); + } + } + else{ + ## Step 8 : Not a Vaild Image File so replace Existing file name; + $final_img_name = $existing_img_name; + $this->logger->info("Users: Not a Vaild Image File Nothing To Update/Upload"); + // throw new \Exception("Not a Vaild Image File"); } - } else { - $fileName = $this->request->getPost('previous_ufile')?$this->request->getPost('previous_ufile'):""; // Use the previous filename if no new image is provided + } + else{ + ## Step 9 : Testing Purpose File Details Not Available. so i can't move it.; + $this->logger->info("Users: Testing Purpose Image Details Not Available. so i can't move it."); + $final_img_name = $existing_img_name; + //throw new \Exception("Testing Purpose File Details Not Available so i can't move it if you have existing filee means save it or your choice"); } - - $UsersModel = new UsersModel(); $data = [ - 'user_name' => $this->request->getPost('user_name'), - 'profile_picture' => $fileName, + 'profile_picture' => $final_img_name, 'city' => $this->request->getPost('city'), 'state' => $this->request->getPost('state'), 'email' => $this->request->getPost('email'), @@ -131,7 +162,6 @@ class Users extends BaseController 'gender' => $this->request->getPost('gender'), 'business_id' => $business_id ]; - $user_id = $this->request->getPost('user_id'); // Get the business ID for update if (empty($user_id)) { // It's an insert operation @@ -141,19 +171,36 @@ class Users extends BaseController $data['password'] = $hash_password; $data['created_by'] = $session_uid; //print_r($data);die; - ($UsersModel->insert($data)) ? session()->setFlashdata('success', 'User has been added successfully.') - : session()->setFlashdata('error', 'User could not be added. Please try again.'); + // ($UsersModel->insert($data)) ? session()->setFlashdata('success', 'User has been added successfully.') + // : session()->setFlashdata('error', 'User could not be added. Please try again.'); + if ($UsersModel->insert($data)) { + session()->setFlashdata('success', 'User has been added successfully.'); + $this->logger->info("Users: has been added successfully. Inserted ID = ".$UsersModel->insertID()); + } else { + session()->setFlashdata('error', 'User could not be added. Please try again.'); + $this->logger->error("Users: Err could not be added. Please try again."); + } + + } else { // It's an update operation $isactive = $this->request->getPost('isactive'); $data['isactive'] = ($isactive == 'on') ? 1 : 0; + if($session_uid == $user_id && $data['isactive'] == 0){ + throw new \Exception("Cant able to Update Because logged-In persons can't remove. Please Contact your Admin!...."); + } $data['updated_by'] = $session_uid; // $UsersModel->update($user_id, $data); - ($UsersModel->update($user_id, $data)) ? session()->setFlashdata('success', 'User has been updated successfully.') - : session()->setFlashdata('error', 'User update failed. Please try again.'); - + if ($UsersModel->update($user_id, $data)) { + session()->setFlashdata('success', 'User has been updated successfully.'); + $this->logger->info("Users: has been updated successfully. Updated ID = ".$user_id); + } else { + session()->setFlashdata('error', 'User update failed. Please try again.'); + $this->logger->error("Users: Err Failed to update ID =".$user_id); + } } }catch(\Exception $e) { + $this->logger->error("Users: Err Occur =".$e->getMessage()); session()->setFlashdata('error', 'Message: ' .$e->getMessage()); } return redirect()->route('user_list'); @@ -165,19 +212,37 @@ class Users extends BaseController helper('session'); $session_uid = get_logged_user_id(); try { + if($id == $session_uid){ + throw new \Exception("Your Logged-In, Can't able delete"); + } $model = new UsersModel(); - $existingBook = $model->find($id); - if ($existingBook) { - // $data=[]; + $where = ['users.user_id' => $id, 'users.isactive =' => 1]; + $existingUser = $model->where($where)->find($id); + $this->logger->Info("Users: Going to Inactive ID = ".$id); + + if ($existingUser) { $data['isactive'] = 0; $data['updated_by'] = $session_uid; - ($model->update($id, $data)) ? session()->setFlashdata('success', 'Deleted successfully.') - : throw new \Exception("Data Not able to Deleted"); + if(!empty($existingUser['profile_picture']) && file_exists(FCPATH."public/uploads/".$existingUser['profile_picture'])){ + unlink('public/uploads/'.$existingUser['profile_picture']); + $data['profile_picture'] = NULL; + } + + + if ($model->update($id, $data)) { + session()->setFlashdata('success', 'Deleted successfully.'); + $this->logger->info("Users: has been Inactived successfully. Inactived ID = ".$id); + } else { + $this->logger->error("Users: Not able to Inactive ID =".$id); + throw new \Exception("Data Not able to Deleted"); + } } else{ - throw new \Exception("Data Not Available"); + $this->logger->error("Users: Does Not Exist To Inactive, ID = ".$id); + throw new \Exception("User Already Deleted"); } }catch(\Exception $e) { + $this->logger->error("Users: Err Occur = ".$e->getMessage()); session()->setFlashdata('error', 'Message: ' .$e->getMessage()); } return redirect()->route('user_list'); diff --git a/app/Models/AuthenticationModel.php b/app/Models/AuthenticationModel.php index e6a3a95d..2e31c064 100644 --- a/app/Models/AuthenticationModel.php +++ b/app/Models/AuthenticationModel.php @@ -11,7 +11,7 @@ class AuthenticationModel extends Model { $builder = $this->db->table('users'); $builder->select('user_id,user_name,email,first_name,last_name,password,mobile_no,date_of_birth,address,gender,profile_picture,city,state,postal_code,users.country,role,setting_id,site_name,site_title,favicon,logo,terms_service,footer_about,admin_email,mobile,copyright,pagination_limit,site_info,about_info,mail_protocol,mail_title,mail_host,mail_port,mail_encryption,mail_username,mail_password,currency,settings.country'); - $builder->join('settings', 'settings.admin_email = users.email', 'left'); + $builder->join('settings', 'settings.business_id = users.business_id', 'left'); $builder->where('user_id', $user_id); //$template_mapping_details['fk_entity_id']; $query = $builder->get(); diff --git a/app/Views/template/footer.php b/app/Views/template/footer.php index d6aa8617..428f21fc 100644 --- a/app/Views/template/footer.php +++ b/app/Views/template/footer.php @@ -466,6 +466,9 @@ + + + @@ -474,7 +477,6 @@ -