2018-04-11 19:49:35 +02:00
< ? php
namespace Grocy\Controllers ;
2018-04-12 21:13:38 +02:00
use \Grocy\Services\SessionService ;
use \Grocy\Services\DatabaseMigrationService ;
use \Grocy\Services\DemoDataGeneratorService ;
2018-04-11 19:49:35 +02:00
class LoginController extends BaseController
{
2018-04-19 20:44:49 +02:00
public function __construct ( \Slim\Container $container , string $sessionCookieName )
2018-04-11 19:49:35 +02:00
{
parent :: __construct ( $container );
$this -> SessionService = new SessionService ();
2018-04-19 20:44:49 +02:00
$this -> SessionCookieName = $sessionCookieName ;
2018-04-11 19:49:35 +02:00
}
protected $SessionService ;
2018-04-19 20:44:49 +02:00
protected $SessionCookieName ;
2018-04-11 19:49:35 +02:00
2018-04-12 21:13:38 +02:00
public function ProcessLogin ( \Slim\Http\Request $request , \Slim\Http\Response $response , array $args )
2018-04-11 19:49:35 +02:00
{
$postParams = $request -> getParsedBody ();
if ( isset ( $postParams [ 'username' ]) && isset ( $postParams [ 'password' ]))
{
2018-07-24 19:31:43 +02:00
$user = $this -> Database -> users () -> where ( 'username' , $postParams [ 'username' ]) -> fetch ();
$inputPassword = $postParams [ 'password' ];
2018-09-24 13:16:57 +02:00
$stayLoggedInPermanently = $postParams [ 'stay_logged_in' ] == 'on' ;
2018-07-24 19:31:43 +02:00
if ( $user !== null && password_verify ( $inputPassword , $user -> password ))
2018-04-11 19:49:35 +02:00
{
2018-09-24 13:16:57 +02:00
$sessionKey = $this -> SessionService -> CreateSession ( $user -> id , $stayLoggedInPermanently );
2018-10-16 18:21:38 +02:00
setcookie ( $this -> SessionCookieName , $sessionKey , intval ( time () + 31220640000 )); // Cookie expires in 999 years, but session validity is up to SessionService
2018-07-24 19:31:43 +02:00
if ( password_needs_rehash ( $user -> password , PASSWORD_DEFAULT ))
{
$user -> update ( array (
'password' => password_hash ( $inputPassword , PASSWORD_DEFAULT )
));
}
2018-04-11 19:49:35 +02:00
2018-04-18 19:03:39 +02:00
return $response -> withRedirect ( $this -> AppContainer -> UrlManager -> ConstructUrl ( '/' ));
2018-04-11 19:49:35 +02:00
}
else
{
2018-04-18 19:03:39 +02:00
return $response -> withRedirect ( $this -> AppContainer -> UrlManager -> ConstructUrl ( '/login?invalid=true' ));
2018-04-11 19:49:35 +02:00
}
}
else
{
2018-04-18 19:03:39 +02:00
return $response -> withRedirect ( $this -> AppContainer -> UrlManager -> ConstructUrl ( '/login?invalid=true' ));
2018-04-11 19:49:35 +02:00
}
}
2018-04-12 21:13:38 +02:00
public function LoginPage ( \Slim\Http\Request $request , \Slim\Http\Response $response , array $args )
2018-04-11 19:49:35 +02:00
{
2018-04-12 21:13:38 +02:00
return $this -> AppContainer -> view -> render ( $response , 'login' );
2018-04-11 19:49:35 +02:00
}
2018-04-12 21:13:38 +02:00
public function Logout ( \Slim\Http\Request $request , \Slim\Http\Response $response , array $args )
2018-04-11 19:49:35 +02:00
{
2018-04-19 20:44:49 +02:00
$this -> SessionService -> RemoveSession ( $_COOKIE [ $this -> SessionCookieName ]);
2018-04-18 19:03:39 +02:00
return $response -> withRedirect ( $this -> AppContainer -> UrlManager -> ConstructUrl ( '/' ));
2018-04-11 19:49:35 +02:00
}
2018-04-19 20:44:49 +02:00
public function GetSessionCookieName ()
{
return $this -> SessionCookieName ;
}
2018-04-11 19:49:35 +02:00
}