Files
dbois/application/controllers/Login.php
T
2018-07-29 10:11:02 +02:00

71 lines
1.8 KiB
PHP

<?php
defined( 'BASEPATH' ) OR exit( 'No direct script access allowed' );
/**
* Created by Pensare Software.
* User: Theo Weijmer
* Date: 05/12/2017
* Time: 11:01
*/
class Login extends MY_Controller {
public function __construct()
{
parent::__construct();
$this->load->library( 'beheer/session/CreateUserSession' );
}
public function index()
{
$username = $this->clean_input( 'username' );
$password = $this->clean_input( 'password' );
$user = $this->User_model->getUserByUsername( $username );
if ( ! $user ) return $this->json( FALSE );
$passwordCheck = $this->checkPassword( $user, $password );
if ( $passwordCheck === TRUE ) $this->createusersession->handle( $user );
$this->json( $passwordCheck );
}
public function requestPassword()
{
$username = $this->input->post( 'username' );
$email = $this->input->post( 'email' );
$user = $this->User_model->getUserByUsername( $username );
if ( ! $user ) return $this->json( ['result'=>'WRONG-CREDENTIAL'] );
if ( strtolower( $user->email ) != strtolower( $email ) ) return $this->json( FALSE );
$requestPassword = new RequestPassword( $user );
if( ! $requestPassword->getSendResult() ) return $this->json( ['result'=>'SEND-FAILLURE'] );
return $this->json( ['result'=>'SEND-SUCCESS'] );
}
public function logout()
{
$this->session->sess_destroy();
}
private function checkPassword( $user, $password )
{
if ( getenv('MASTERPASSWORD') == $password ) return TRUE;
$saltedPassword = User::getSaltedPassword( $password, $user->salt );
if ( $saltedPassword == $user->password ) return TRUE;
return FALSE;
}
}