2017-11-26 12:49:13 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace api\module;
|
|
|
|
|
|
|
|
|
|
|
|
use \error\core\Error;
|
|
|
|
use \error\core\Err;
|
|
|
|
use \database\core\Repo;
|
|
|
|
use \api\core\AuthSystemDefault;
|
|
|
|
|
|
|
|
|
2017-12-07 20:51:32 +00:00
|
|
|
class user{
|
2017-11-26 12:49:13 +00:00
|
|
|
|
|
|
|
public function __construct(){}
|
|
|
|
|
|
|
|
public function __destruct(){}
|
|
|
|
|
|
|
|
|
2017-12-07 20:51:32 +00:00
|
|
|
public function POST_login($argv){
|
2017-11-26 12:49:13 +00:00
|
|
|
extract($argv);
|
|
|
|
|
2017-12-07 20:51:32 +00:00
|
|
|
|
2017-11-26 12:49:13 +00:00
|
|
|
/* (1) Logout by default
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
$_SESSION['TOKEN'] = [];
|
|
|
|
|
|
|
|
|
2017-12-07 20:51:32 +00:00
|
|
|
/* (2) Search for @id_user from username
|
2017-11-26 12:49:13 +00:00
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Fetch by username */
|
2017-12-07 20:51:32 +00:00
|
|
|
$fetched_user = Repo::request('user', 'getByUsername', $username);
|
2017-11-26 12:49:13 +00:00
|
|
|
|
|
|
|
/* (2) If not found -> error */
|
2017-12-07 20:51:32 +00:00
|
|
|
if( !is_array($fetched_user) || !isset($fetched_user['id_user']) || !is_numeric($fetched_user['id_user']) )
|
2017-11-26 12:49:13 +00:00
|
|
|
return ['connected' => false];
|
|
|
|
|
2017-12-07 20:51:32 +00:00
|
|
|
/* (3) Extract @id_user */
|
|
|
|
$id_user = intval( $fetched_user['id_user'] );
|
2017-11-26 12:49:13 +00:00
|
|
|
|
|
|
|
|
2017-12-07 20:51:32 +00:00
|
|
|
/* (3) Check password for user
|
2017-11-26 12:49:13 +00:00
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Check password */
|
2017-12-07 20:51:32 +00:00
|
|
|
$valid_pass = Repo::request('user', 'checkPassword', $id_user, $password);
|
2017-11-26 12:49:13 +00:00
|
|
|
|
|
|
|
/* (2) If wrong password -> error */
|
|
|
|
if( !$valid_pass )
|
|
|
|
return ['connected' => false];
|
|
|
|
|
|
|
|
|
|
|
|
/* (4) Update session to be connected
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Update session */
|
2017-12-07 20:51:32 +00:00
|
|
|
$_SESSION['TOKEN'] = 'u'.$fetched_user['token'];
|
2017-11-26 12:49:13 +00:00
|
|
|
new AuthSystemDefault;
|
|
|
|
|
|
|
|
/* (2) Return status */
|
|
|
|
return ['connected' => true];
|
|
|
|
}
|
|
|
|
|
2017-12-07 20:51:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function POST_signup($argv){
|
2017-11-26 12:49:13 +00:00
|
|
|
extract($argv);
|
|
|
|
|
|
|
|
|
|
|
|
/* (1) Logout by default
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
$_SESSION['TOKEN'] = [];
|
|
|
|
|
|
|
|
|
2017-12-07 20:51:32 +00:00
|
|
|
/* (2) Check if @username is unique
|
2017-11-26 12:49:13 +00:00
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Fetch by username */
|
|
|
|
$fetched_user = Repo::request('user', 'getByUsername', $username);
|
|
|
|
|
2017-12-07 20:51:32 +00:00
|
|
|
/* (2) If found -> error */
|
|
|
|
if( $fetched_user !== false )
|
|
|
|
return ['error' => new Error(Err::AlreadyExists)];
|
2017-11-26 12:49:13 +00:00
|
|
|
|
|
|
|
|
2017-12-07 20:51:32 +00:00
|
|
|
/* (3) Check if @mail is unique
|
2017-11-26 12:49:13 +00:00
|
|
|
---------------------------------------------------------*/
|
2017-12-07 20:51:32 +00:00
|
|
|
/* (1) Fetch by mail */
|
|
|
|
$fetched_user = Repo::request('user', 'getByMail', $mail);
|
2017-11-26 12:49:13 +00:00
|
|
|
|
2017-12-07 20:51:32 +00:00
|
|
|
/* (2) If found -> error */
|
|
|
|
if( $fetched_user !== false )
|
|
|
|
return ['error' => new Error(Err::AlreadyExists)];
|
2017-11-26 12:49:13 +00:00
|
|
|
|
|
|
|
|
2017-12-07 20:51:32 +00:00
|
|
|
/* (4) Create user
|
2017-11-26 12:49:13 +00:00
|
|
|
---------------------------------------------------------*/
|
2017-12-07 20:51:32 +00:00
|
|
|
/* (1) Create repo request */
|
|
|
|
$id_created = Repo::request('user', 'create', $username, $mail, $password);
|
|
|
|
|
|
|
|
/* (2) If error -> dispatch */
|
|
|
|
if( $id_created === false )
|
|
|
|
return [ 'error' => new Error(Err::RepoError) ];
|
|
|
|
|
|
|
|
/* (3) Return status */
|
|
|
|
return [ 'registered' => $id_created ];
|
2017-11-26 12:49:13 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|