2017-09-15 14:44:40 +03:00
|
|
|
<?php defined('BASEPATH') OR exit('No direct script access allowed');
|
2015-12-30 13:02:14 +02:00
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Easy!Appointments - Open Source Web Scheduler
|
|
|
|
*
|
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
2017-01-31 09:35:34 +03:00
|
|
|
* @copyright Copyright (c) 2013 - 2017, Alex Tselegidis
|
2015-12-30 13:02:14 +02:00
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link http://easyappointments.org
|
|
|
|
* @since v1.0.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
2016-11-09 21:56:24 +03:00
|
|
|
use \EA\Engine\Types\NonEmptyText;
|
2016-07-16 17:56:02 +03:00
|
|
|
use \EA\Engine\Types\Email;
|
|
|
|
|
2015-12-30 13:02:14 +02:00
|
|
|
/**
|
|
|
|
* User Controller
|
|
|
|
*
|
|
|
|
* @package Controllers
|
|
|
|
*/
|
|
|
|
class User extends CI_Controller {
|
|
|
|
/**
|
|
|
|
* Class Constructor
|
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function __construct()
|
|
|
|
{
|
2015-12-30 13:02:14 +02:00
|
|
|
parent::__construct();
|
|
|
|
$this->load->library('session');
|
|
|
|
|
|
|
|
// Set user's selected language.
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($this->session->userdata('language'))
|
|
|
|
{
|
|
|
|
$this->config->set_item('language', $this->session->userdata('language'));
|
|
|
|
$this->lang->load('translations', $this->session->userdata('language'));
|
2018-01-23 12:08:37 +03:00
|
|
|
}
|
|
|
|
else
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
|
|
|
$this->lang->load('translations', $this->config->item('language')); // default
|
2015-12-30 13:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default Method
|
|
|
|
*
|
|
|
|
* The default method will redirect the browser to the user/login URL.
|
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function index()
|
|
|
|
{
|
2016-03-07 21:59:04 +02:00
|
|
|
header('Location: ' . site_url('user/login'));
|
2015-12-30 13:02:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the login page.
|
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function login()
|
|
|
|
{
|
2015-12-30 13:02:14 +02:00
|
|
|
$this->load->model('settings_model');
|
|
|
|
|
|
|
|
$view['base_url'] = $this->config->item('base_url');
|
|
|
|
$view['dest_url'] = $this->session->userdata('dest_url');
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ( ! $view['dest_url'])
|
|
|
|
{
|
2016-03-07 21:59:04 +02:00
|
|
|
$view['dest_url'] = site_url('backend');
|
2015-12-30 13:02:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$view['company_name'] = $this->settings_model->get_setting('company_name');
|
|
|
|
$this->load->view('user/login', $view);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the logout page.
|
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function logout()
|
|
|
|
{
|
2015-12-30 13:02:14 +02:00
|
|
|
$this->load->model('settings_model');
|
|
|
|
|
|
|
|
$this->session->unset_userdata('user_id');
|
|
|
|
$this->session->unset_userdata('user_email');
|
|
|
|
$this->session->unset_userdata('role_slug');
|
|
|
|
$this->session->unset_userdata('username');
|
|
|
|
$this->session->unset_userdata('dest_url');
|
|
|
|
|
|
|
|
$view['base_url'] = $this->config->item('base_url');
|
|
|
|
$view['company_name'] = $this->settings_model->get_setting('company_name');
|
|
|
|
$this->load->view('user/logout', $view);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-04-27 09:21:40 +03:00
|
|
|
* Display the "forgot password" page.
|
2015-12-30 13:02:14 +02:00
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function forgot_password()
|
|
|
|
{
|
2015-12-30 13:02:14 +02:00
|
|
|
$this->load->model('settings_model');
|
|
|
|
$view['base_url'] = $this->config->item('base_url');
|
|
|
|
$view['company_name'] = $this->settings_model->get_setting('company_name');
|
|
|
|
$this->load->view('user/forgot_password', $view);
|
|
|
|
}
|
|
|
|
|
2016-04-27 09:21:40 +03:00
|
|
|
/**
|
|
|
|
* Display the "not authorized" page.
|
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function no_privileges()
|
|
|
|
{
|
2015-12-30 13:02:14 +02:00
|
|
|
$this->load->model('settings_model');
|
|
|
|
$view['base_url'] = $this->config->item('base_url');
|
|
|
|
$view['company_name'] = $this->settings_model->get_setting('company_name');
|
|
|
|
$this->load->view('user/no_privileges', $view);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* [AJAX] Check whether the user has entered the correct login credentials.
|
|
|
|
*
|
|
|
|
* The session data of a logged in user are the following:
|
|
|
|
* - 'user_id'
|
|
|
|
* - 'user_email'
|
|
|
|
* - 'role_slug'
|
|
|
|
* - 'dest_url'
|
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function ajax_check_login()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2017-09-19 16:44:32 +03:00
|
|
|
if ( ! $this->input->post('username') || ! $this->input->post('password'))
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2015-12-30 13:02:14 +02:00
|
|
|
throw new Exception('Invalid credentials given!');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->load->model('user_model');
|
2017-09-19 16:44:32 +03:00
|
|
|
$user_data = $this->user_model->check_login($this->input->post('username'), $this->input->post('password'));
|
2015-12-30 13:02:14 +02:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($user_data)
|
|
|
|
{
|
2015-12-30 13:02:14 +02:00
|
|
|
$this->session->set_userdata($user_data); // Save data on user's session.
|
2017-09-20 17:09:01 +03:00
|
|
|
$this->output
|
|
|
|
->set_content_type('application/json')
|
|
|
|
->set_output(json_encode(AJAX_SUCCESS));
|
2018-01-23 12:08:37 +03:00
|
|
|
}
|
|
|
|
else
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2017-09-20 17:09:01 +03:00
|
|
|
$this->output
|
|
|
|
->set_content_type('application/json')
|
|
|
|
->set_output(json_encode(AJAX_FAILURE));
|
2015-12-30 13:02:14 +02:00
|
|
|
}
|
|
|
|
|
2017-09-23 02:30:22 +03:00
|
|
|
}
|
|
|
|
catch (Exception $exc)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2017-09-20 17:09:01 +03:00
|
|
|
$this->output
|
|
|
|
->set_content_type('application/json')
|
|
|
|
->set_output(json_encode(['exceptions' => [exceptionToJavaScript($exc)]]));
|
2015-12-30 13:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Regenerate a new password for the current user, only if the username and
|
2016-10-10 19:29:48 +03:00
|
|
|
* email address given correspond to an existing user in db.
|
2015-12-30 13:02:14 +02:00
|
|
|
*
|
2017-09-06 16:22:11 +03:00
|
|
|
* Required POST Parameters:
|
|
|
|
*
|
|
|
|
* - string $_POST['username'] Username to be validated.
|
|
|
|
* - string $_POST['email'] Email to be validated.
|
2015-12-30 13:02:14 +02:00
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function ajax_forgot_password()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2017-09-19 16:44:32 +03:00
|
|
|
if ( ! $this->input->post('username') || ! $this->input->post('email'))
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2015-12-30 13:02:14 +02:00
|
|
|
throw new Exception('You must enter a valid username and email address in '
|
2017-09-15 14:36:37 +03:00
|
|
|
. 'order to get a new password!');
|
2015-12-30 13:02:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->load->model('user_model');
|
|
|
|
$this->load->model('settings_model');
|
|
|
|
|
2017-09-20 17:09:01 +03:00
|
|
|
$new_password = $this->user_model->regenerate_password($this->input->post('username'),
|
|
|
|
$this->input->post('email'));
|
2015-12-30 13:02:14 +02:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($new_password != FALSE)
|
|
|
|
{
|
|
|
|
$this->config->load('email');
|
2016-07-16 17:56:02 +03:00
|
|
|
$email = new \EA\Engine\Notifications\Email($this, $this->config->config);
|
2017-09-15 14:36:37 +03:00
|
|
|
$company_settings = [
|
2015-12-30 13:02:14 +02:00
|
|
|
'company_name' => $this->settings_model->get_setting('company_name'),
|
|
|
|
'company_link' => $this->settings_model->get_setting('company_link'),
|
|
|
|
'company_email' => $this->settings_model->get_setting('company_email')
|
2017-09-15 14:36:37 +03:00
|
|
|
];
|
2016-07-16 17:56:02 +03:00
|
|
|
|
2017-09-20 17:09:01 +03:00
|
|
|
$email->sendPassword(new NonEmptyText($new_password), new Email($this->input->post('email')),
|
|
|
|
$company_settings);
|
2015-12-30 13:02:14 +02:00
|
|
|
}
|
|
|
|
|
2017-09-20 17:09:01 +03:00
|
|
|
$this->output
|
|
|
|
->set_content_type('application/json')
|
|
|
|
->set_output(json_encode($new_password != FALSE ? AJAX_SUCCESS : AJAX_FAILURE));
|
2017-09-23 02:30:22 +03:00
|
|
|
}
|
|
|
|
catch (Exception $exc)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2017-09-20 17:09:01 +03:00
|
|
|
$this->output
|
|
|
|
->set_content_type('application/json')
|
|
|
|
->set_output(json_encode(['exceptions' => [exceptionToJavaScript($exc)]]));
|
2015-12-30 13:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|