Your IP : 216.73.216.86


Current Path : /home/emeraadmin/public_html/4d695/
Upload File :
Current File : /home/emeraadmin/public_html/4d695/UserService.php.tar

home/emeraadmin/public_html/Service/UserService.php000064400000013214151676732730016472 0ustar00<?php

require_once __DIR__ . '/../Classes/Database.php';
require_once __DIR__ . '/../Classes/User.php';

class UserService
{
    private $db;

    public function __construct()
    {
        $this->db = new Database();
    }

    public function login($email, $password)
    {
        $this->db->query('SELECT id, email, password, role FROM users WHERE email = :email and status = :status');
        $this->db->bind(':email', $email);
        $this->db->bind(':status', 'active');
        $row = $this->db->single();

        if ($row) {
            // Verify the password using password_verify()
            if (password_verify($password, $row->password)) {
                return $row; // Return the entire user row, which includes the role
            }
        }

        return false;
    }



    public function getAllUsers()
    {
        $this->db->query('SELECT id, first_name, last_name, email,phone,status, role ,last_login FROM users');
        return $this->db->resultSet();
    }

    public function authenticate($email, $password)
    {
        $this->db->query('SELECT * FROM users WHERE email = :email');
        $this->db->bind(':email', $email);
        $row = $this->db->single();

        if ($row && password_verify($password, $row->password)) {
            return true;
        }

        return false;
    }

    public function addUser($firstName, $lastName, $email, $phone, $role, $status, $password)
    {
        // Hash the password
        $passwordHash = password_hash($password, PASSWORD_DEFAULT);

        // Prepare and execute the SQL query
        $this->db->query('INSERT INTO users (first_name, last_name, email, phone, role, status, password) VALUES (:first_name, :last_name, :email, :phone, :role, :status, :password)');
        $this->db->bind(':first_name', $firstName);
        $this->db->bind(':last_name', $lastName);
        $this->db->bind(':email', $email);
        $this->db->bind(':phone', $phone);
        $this->db->bind(':role', $role);
        $this->db->bind(':status', $status);
        $this->db->bind(':password', $passwordHash);
        $this->db->execute();

        return $this->db->lastInsertId();
    }


    public function updateUser($id, $firstName, $lastName, $email, $phone, $role)
    {
        $this->db->query('UPDATE users SET first_name = :first_name, last_name = :last_name, email = :email, phone = :phone, role = :role WHERE id = :id');
        $this->db->bind(':id', $id);
        $this->db->bind(':first_name', $firstName);
        $this->db->bind(':last_name', $lastName);
        $this->db->bind(':email', $email);
        $this->db->bind(':phone', $phone);
        $this->db->bind(':role', $role);

        $this->db->execute();
    }

    public function deleteUser($id)
    {
        // Soft delete if already active make it inactive or vice versa
        $this->db->query('SELECT status FROM users WHERE id = :id');
        $this->db->bind(':id', $id);
        $row = $this->db->single();
        $status = $row->status == 'active' ? 'inactive' : 'active';

        $this->db->query('UPDATE users SET status = :status WHERE id = :id');
        $this->db->bind(':id', $id);
        $this->db->bind(':status', $status);
        $this->db->execute();
    }

    public function getUserById($id)
    {
        $this->db->query('SELECT id, first_name, last_name, email, phone, role, status FROM users WHERE id = :id');
        $this->db->bind(':id', $id);
        return $this->db->single();
    }

    public function isEmailExists($email)
    {
        $this->db->query('SELECT id FROM users WHERE email = :email');
        $this->db->bind(':email', $email);
        $row = $this->db->single();
        return $row ? true : false;
    }

    public function isEmailExistsForOthers($id, $email)
    {
        $this->db->query('SELECT id FROM users WHERE email = :email AND id != :id');
        $this->db->bind(':email', $email);
        $this->db->bind(':id', $id);
        $row = $this->db->single();
        return $row ? true : false;
    }

    public function resetPassword($id, $password)
    {
        $passwordHash = password_hash($password, PASSWORD_DEFAULT);
        $this->db->query('UPDATE users SET password = :password WHERE id = :id');
        $this->db->bind(':id', $id);
        $this->db->bind(':password', $passwordHash);
        $this->db->execute();
    }

    public function fetchUserName(int $userId)
    {
        $this->db->query('SELECT first_name, last_name FROM users WHERE id = :id');
        $this->db->bind(':id', $userId);
        $row = $this->db->single();
        return $row->first_name . ' ' . $row->last_name;
    }

    // Function to verify the user's password
    public function verifyUserPassword($email, $password) {
        // Call the getUserByEmail method from within the class
        $user = $this->getUserByEmail($email);

        if ($user) {
            // Verify the password using password_verify
            return password_verify($password, $user->password);
        }
        return false; // Return false if the user is not found
    }

    // Function to retrieve the user by email
    public function getUserByEmail($email) {
        // Get the database connection from the Database class
        $db = $this->db->getConn(); // Ensure the database object is properly initialized

        // Prepare and execute the query
        $stmt = $db->prepare("SELECT * FROM users WHERE email = :email");
        $stmt->bindParam(':email', $email);
        $stmt->execute();

        // Fetch the user as an object
        return $stmt->fetch(PDO::FETCH_OBJ);
    }


}