2018-04-11 19:49:35 +02:00
< ? php
namespace Grocy\Controllers ;
class LoginController extends BaseController
{
2020-02-11 17:42:03 +01:00
public function __construct ( \DI\Container $container , string $sessionCookieName )
2018-04-11 19:49:35 +02:00
{
parent :: __construct ( $container );
2018-04-19 20:44:49 +02:00
$this -> SessionCookieName = $sessionCookieName ;
2018-04-11 19:49:35 +02:00
}
2018-04-19 20:44:49 +02:00
protected $SessionCookieName ;
2018-04-11 19:49:35 +02:00
2020-02-11 17:42:03 +01:00
public function ProcessLogin ( \Psr\Http\Message\ServerRequestInterface $request , \Psr\Http\Message\ResponseInterface $response , array $args )
2018-04-11 19:49:35 +02:00
{
$postParams = $request -> getParsedBody ();
if ( isset ( $postParams [ 'username' ]) && isset ( $postParams [ 'password' ]))
{
2020-03-01 23:47:47 +07:00
$user = $this -> getDatabase () -> users () -> where ( 'username' , $postParams [ 'username' ]) -> fetch ();
2018-07-24 19:31:43 +02:00
$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
{
2020-03-01 23:47:47 +07:00
$sessionKey = $this -> getSessionService () -> CreateSession ( $user -> id , $stayLoggedInPermanently );
2019-06-08 16:47:45 +02:00
setcookie ( $this -> SessionCookieName , $sessionKey , PHP_INT_SIZE == 4 ? PHP_INT_MAX : PHP_INT_MAX >> 32 ); // Cookie expires never, 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
2020-02-11 17:42:03 +01:00
return $response -> withRedirect ( $this -> AppContainer -> get ( 'UrlManager' ) -> ConstructUrl ( '/' ));
2018-04-11 19:49:35 +02:00
}
else
{
2020-02-11 17:42:03 +01:00
return $response -> withRedirect ( $this -> AppContainer -> get ( 'UrlManager' ) -> ConstructUrl ( '/login?invalid=true' ));
2018-04-11 19:49:35 +02:00
}
}
else
{
2020-02-11 17:42:03 +01:00
return $response -> withRedirect ( $this -> AppContainer -> get ( 'UrlManager' ) -> ConstructUrl ( '/login?invalid=true' ));
2018-04-11 19:49:35 +02:00
}
}
2020-02-11 17:42:03 +01:00
public function LoginPage ( \Psr\Http\Message\ServerRequestInterface $request , \Psr\Http\Message\ResponseInterface $response , array $args )
2018-04-11 19:49:35 +02:00
{
2020-03-01 23:47:47 +07:00
return $this -> renderPage ( $response , 'login' );
2018-04-11 19:49:35 +02:00
}
2020-02-11 17:42:03 +01:00
public function Logout ( \Psr\Http\Message\ServerRequestInterface $request , \Psr\Http\Message\ResponseInterface $response , array $args )
2018-04-11 19:49:35 +02:00
{
2020-03-01 23:47:47 +07:00
$this -> getSessionService () -> RemoveSession ( $_COOKIE [ $this -> SessionCookieName ]);
2020-02-11 17:42:03 +01:00
return $response -> withRedirect ( $this -> AppContainer -> get ( '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
}