ComunicAPI/RestControllers/userController.php

129 lines
2.9 KiB
PHP
Raw Normal View History

2017-05-17 12:43:12 +00:00
<?php
/**
* Main user controller file
*
* @author Pierre HUBERT
*/
//Enable access to exceptions handler
use \Jacwright\RestServer\RestException;
class userController
{
/**
* Try to connect user and return login tokens
*
* @url POST /user/connectUSER
*/
public function connectUSER(){
//Check variables sent in request
if(!isset($_POST['userMail']) OR !isset($_POST['userPassword']))
throw new RestException(401, "Missing data !");
//Retrieve database connection
$db = CS::get()->db;;
//Extract data
$userMail = $_POST["userMail"];
$userPassword = $_POST['userPassword'];
//Try to perform login
$loginTokens = CS::get()->user->generateUserLoginTokens($userMail, $userPassword, APIServiceID, $db);
if(!$loginTokens)
throw new RestException(401, "Invalid e-mail address / password !");
//Return result with tokens
return array(
"success" => "User logged in !",
"tokens" => array(
"token1" => $loginTokens[0],
"token2" => $loginTokens[1],
),
);
}
/**
* Request token delete (= disconnectUSER)
*
* @url POST /user/disconnectUSER
*/
public function disconnectUSER(){
2017-05-24 16:41:24 +00:00
user_login_required();
2017-05-17 12:43:12 +00:00
//Try to delete token
2017-05-24 16:41:24 +00:00
if(!CS::get()->user->deleteUserLoginToken(userID, APIServiceID))
2017-05-17 12:43:12 +00:00
throw new RestException(500, "Something went wrong while trying to logout user !");
//Everything is ok
return array("success" => "The user has been disconnected !");
}
/**
2017-05-27 10:12:21 +00:00
* Get informations about a user
2017-05-17 12:43:12 +00:00
*
2017-05-27 10:12:21 +00:00
* @url POST /user/getInfos
2017-05-17 12:43:12 +00:00
* @return array The result
*/
2017-05-27 10:12:21 +00:00
public function getUserInfos() : array{
2017-05-19 16:07:52 +00:00
user_login_required();
2017-05-17 12:43:12 +00:00
2017-05-27 10:12:21 +00:00
//Determine userID
if(!isset($_POST['userID']))
Rest_fatal_error(400, "Please specify user ID !");
$userID = $_POST['userID']*1;
//Try to get user infos
$userInfos = CS::get()->user->getUserInfos($userID);
2017-05-17 12:43:12 +00:00
//Check if response is empty
if(count($userInfos) == 0)
throw new RestException(401, "Couldn't get user data !");
//Return result
return array($userInfos);
}
2017-05-27 12:09:05 +00:00
/**
* Get multiple users informations
*
* @url POST /user/getInfosMultiple
* @return array The result
*/
public function getMultipleUserInfos() : array{
user_login_required();
//Determine userID
if(!isset($_POST['usersID']))
Rest_fatal_error(400, "Please specify user ID !");
$usersID = array();
foreach(json_decode($_POST['usersID']) as $userID){
$usersID[] = $userID*1;
}
//Try to get user infos
$userInfos = CS::get()->user->getUserInfos($userID);
//Check if response is empty
if(count($userInfos) == 0)
throw new RestException(401, "Couldn't get user data (maybe user doesn't exists) !");
//Return result
return array($userInfos);
}
2017-05-17 12:43:12 +00:00
/**
* Get current user infos using tokens
*
* @url POST /user/getCurrentUserID
*/
2017-05-27 10:12:21 +00:00
public function getCurrentUserID(){
2017-05-19 16:07:52 +00:00
user_login_required();
2017-05-17 12:43:12 +00:00
2017-05-19 16:07:52 +00:00
//Return userID
return array("userID" => userID);
2017-05-17 12:43:12 +00:00
}
}