main/build/api/module/admin.php

116 lines
2.7 KiB
PHP
Raw Normal View History

<?php
namespace api\module;
use \error\core\Error;
use \error\core\Err;
use \database\core\Repo;
use \api\core\AuthSystemDefault;
class admin{
public function __construct(){}
public function __destruct(){}
public function POST_login($argv){
extract($argv);
/* (1) Logout by default
---------------------------------------------------------*/
$_SESSION['TOKEN'] = [];
/* (2) Search for @id_admin from username
---------------------------------------------------------*/
/* (1) Fetch by username */
$fetched_admin = Repo::request('admin', 'getByUsername', $username);
/* (2) If not found -> error */
if( !is_array($fetched_admin) || !isset($fetched_admin['id_admin']) || !is_numeric($fetched_admin['id_admin']) )
return ['connected' => false];
/* (3) Extract @id_admin */
$id_admin = intval( $fetched_admin['id_admin'] );
/* (3) Check password for admin
---------------------------------------------------------*/
/* (1) Check password */
$valid_pass = Repo::request('admin', 'checkPassword', $id_admin, $password);
/* (2) If wrong password -> error */
if( !$valid_pass )
return ['connected' => false];
/* (4) Update session to be connected
---------------------------------------------------------*/
/* (1) Update session */
$_SESSION['TOKEN'] = 'a'.$fetched_admin['token'];
2017-12-07 23:32:18 +00:00
$_SESSION['WS'] = true; // to tell websocket we are connected
new AuthSystemDefault;
/* (2) Return status */
return ['connected' => true];
}
public function POST_signup($argv){
extract($argv);
/* (1) Logout by default
---------------------------------------------------------*/
$_SESSION['TOKEN'] = [];
/* (2) Check if @username is unique
---------------------------------------------------------*/
/* (1) Fetch by username */
$fetched_admin = Repo::request('admin', 'getByUsername', $username);
/* (2) If found -> error */
if( $fetched_admin !== false )
return ['error' => new Error(Err::AlreadyExists)];
/* (3) Check if @mail is unique
---------------------------------------------------------*/
/* (1) Fetch by mail */
$fetched_admin = Repo::request('admin', 'getByMail', $mail);
/* (2) If found -> error */
if( $fetched_admin !== false )
return ['error' => new Error(Err::AlreadyExists)];
/* (4) Create user
---------------------------------------------------------*/
/* (1) Create repo request */
$id_created = Repo::request('admin', '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-12-07 23:32:18 +00:00
public function DELETE_logout(){
$_SESSION = [];
}
}