bbone/application/modules/Auth/controllers/Auth.php

258 lines
8.2 KiB
PHP

<?php defined('BASEPATH') or exit('No direct script access allowed');
/**
* AMS
*
* @package HMVC
* @author Venba
* @copyright 2022 Venba
* @license https://venbainfotech.com/licenses/MIT MIT License
* @link <URI> (description)
* @version GIT: $Id$
* @since Version 0.0.1
* @filesource
*
*/
class Auth extends MY_Controller
{
/**
* [__construct description]
*
* @method __construct
*/
public function __construct()
{
// To inherit directly the attributes of the parent class.
parent::__construct();
### Syntax : $this->load->model('Model Path' , 'Alias name');
### The second parameter (optional) is used to call the method.
$this->load->model('Auth_model','auth_model');
$this->load->model('MY_Model','MY_Model');
}
/**
* [index description]
*
* @method index
*
* @return [type] [description]
*/
public function index()
{
if(check_auth()){
redirect('Dashboard');
}else{
$this->layout->load_view('Auth/login');
}
}
public function authendicate()
{
$user = $this->auth_model->validate_user();
if($user){
//check if user avaiable or not
if (empty($user)) {
$this->session->set_flashdata('msg', "user not avaiable");
redirect('login');
}
//check if user Deactivated by admin or not
if (!empty($user) && $user->is_active == 0) {
$this->session->set_flashdata('msg', "deactivate user");
redirect('login');
}
//check the password of the user is correct or not
if(password_verify($this->input->post('password'), $user->password))
{
$data = array(
'id' => $user->id,
'name' => $user->name,
//'thumb' => $user->thumb,
'email' =>$user->email,
'role' =>$user->role,
'action_permit' =>$user->action_permit,
'business_id' =>$user->business_id,
'logged_in' => TRUE
);
$data = $this->security->xss_clean($data);
$this->session->set_userdata($data);
redirect('Dashboard');
}
// pass the value 1 when the username or password is wrong
$this->session->set_flashdata('msg',"password doesn't match");
redirect('login');
//check the business is active or deactive
if($user->role !== 'sadmin'){
$business = $this->auth_model->validateBusiness($user->business_id);
if ($user->business_id !== '0' && empty($business) && $business !== NULL) {
$this->session->set_flashdata('msg', "Business not avaiable Contact Administrator");
redirect('login');
}
}
}else{
$this->session->set_flashdata('msg', "user not avaiable");
redirect('login');
}
}
public function logout()
{
$this->auth_model->logout();
redirect('login');
}
public function lost_your_password()
{
$this->layout->load_view("Auth/otp_verification");
}
public function sendMail(){
// otp generator
$otp = rand(100000, 999999);
$data['otp'] = $otp;
$mail = $this->input->get('email');
$where = 'email';
$user = $this->auth_model->validateUserForOtp($mail, $where);
if (empty($user)) {
$data['csrf_hash'] = $this->security->get_csrf_hash();
$data['status'] = "failed";
echo json_encode($data);
}
else{
$data['csrf_hash'] = $this->security->get_csrf_hash();
$data['status'] = "success";
$data['id'] = $user->id;
$data['email'] = $mail;
$id = $user->id;
$table="users";
$datas['otp'] = $otp;
$userData = $this->MY_Model->edit_option($datas, $id, $table);
echo json_encode($data);
}
}
public function sendOTP(){
$mail = $this->input->get('email');
$otp = $this->input->get('otp');
$where = 'email';
$user = $this->auth_model->validateUserForOtp($mail, $where);
$mailCredentials = $this->auth_model->validateBusiness($user->business_id);
$id = $user->id;
$data['csrf_hash'] = $this->security->get_csrf_hash();
$data['id'] = $id;
$data['email'] = $mail;
$body = <<<EOD
<body style="font-family: Arial, sans-serif;">
<div style="max-width: 600px; margin: 0 auto; padding: 20px; border: 1px solid #ccc; border-radius: 5px;">
<h2 style="color: #333; text-align: center;">One-Time Password (OTP) Verification</h2>
<p>Hello,</p>
<p>Your One-Time Password (OTP) for verification is:</p>
<div style="text-align: center; background-color: #f5f5f5; padding: 10px; border-radius: 5px; font-size: 24px; margin-top: 15px;">
<strong>$otp</strong>
</div>
<p>This OTP is valid for a short period. Please do not share it with anyone.</p>
<p>If you did not request this OTP, please ignore this email.</p>
<p>Thank you!</p>
<hr style="border: 1px solid #ccc; margin-top: 20px; margin-bottom: 20px;">
<p style="text-align: center; color: #777;">&copy; 2023 $mailCredentials->business_name </p>
</div>
</body>
EOD;
$subject = 'Forgot Password OTP Send';
$result = send_emails_forgot_password($mail, $subject, $body,$mailCredentials);
if($result){
$data['csrf_hash'] = $this->security->get_csrf_hash();
$data['status'] = "success";
$data['id'] = $id;
$data['email'] = $mail;
$data['result'] = $result;
echo json_encode($data);
}else{
$data['csrf_hash'] = $this->security->get_csrf_hash();
$data['status'] = "failed";
$data['id'] = $id;
$data['email'] = $mail;
$data['result'] = $result;
echo json_encode($data);
}
}
public function verify_otp(){
$id = $this->input->post('id');
$email = $this->input->post('email');
$where = 'id';
$user_entered_otp = $this->input->post('otp');
$user = $this->auth_model->validateUserForOtp($id, $where);
if ($user->otp == $user_entered_otp)
{
$data['generate_otp'] = $user->otp;
$data['user_entered_otp'] = $user_entered_otp;
$data['csrf_hash'] = $this->security->get_csrf_hash();
$data['status'] = "success";
$data['id'] = $id;
$data['email'] = $email;
$id = $id;
$table = "users";
$datas['otp'] = '';
$userData = $this->MY_Model->edit_option($datas, $id, $table);
$this->layout->load_view('Auth/change_password_form', $data);
}
else
{
$data['csrf_hash'] = $this->security->get_csrf_hash();
$data['status'] = "failed";
$data['id'] = $id;
$data['email'] = $email;
$this->layout->load_view("Auth/otp_verification", $data);
}
}
public function change_password()
{
$id = $this->input->post('id');
$data['password'] = hash_password($this->input->post('password'));
$table = "users";
$userData = $this->MY_Model->edit_option($data, $id, $table);
redirect('login');
}
public function is_superadmin_check()
{
if (user()->role == 'sadmin') {
return true;
} else {
return false;
}
}
}