168 lines
5.4 KiB
PHP
168 lines
5.4 KiB
PHP
<?php
|
|
|
|
class TokenApi extends Api
|
|
{
|
|
public $apiName = 'token';
|
|
|
|
/**
|
|
* Method GET
|
|
* Get all records
|
|
* http://DOMAIN/token
|
|
* @return string
|
|
*/
|
|
/*
|
|
curl -H "Content-Type: application/json" -H "Authorization: Server-Token 99dfe35fcb7de1ee" \
|
|
https://svrsavvy.sworks.float.sg/SAVVY/oauth2/api/token/?member_id=3
|
|
*/
|
|
public function indexAction()
|
|
{
|
|
// Get the parameters
|
|
$member_id = (int)($this->requestParams['member_id'] ?? 0);
|
|
if ($member_id<1) {
|
|
return $this->response(
|
|
array(
|
|
'error' => 'Data not found'
|
|
), 404);
|
|
}
|
|
$db = new Db();
|
|
list ($total, $tokens) = OAuth2::getAllTokens($db->getConnect(), $member_id);
|
|
if($total>0){
|
|
return $this->response(
|
|
array(
|
|
'member_id' => $member_id,
|
|
'limit' => (int)$total,
|
|
'offset' => 0,
|
|
'count' => count($tokens),
|
|
'total' => (int)$total,
|
|
'tokens' => $tokens
|
|
), 200);
|
|
}
|
|
return $this->response(
|
|
array(
|
|
'error' => 'Data not found'
|
|
), 404);
|
|
}
|
|
|
|
/**
|
|
* Method GET
|
|
* Get single record (by id)
|
|
* http://DOMAIN/token/1
|
|
* @return string
|
|
*/
|
|
/*
|
|
curl -H "Content-Type: application/json" -H "Authorization: Server-Token 99dfe35fcb7de1ee" \
|
|
https://svrsavvy.sworks.float.sg/SAVVY/oauth2/api/token/108
|
|
*/
|
|
public function viewAction()
|
|
{
|
|
//id must be the first parameter after /token/x
|
|
$id = array_shift($this->requestUri);
|
|
|
|
if($id && (int)$id>0){
|
|
$db = new Db();
|
|
$token = OAuth2::getTokenById($db->getConnect(), (int)$id);
|
|
if(is_array($token) && count($token)>0){
|
|
return $this->response($token, 200);
|
|
}
|
|
}
|
|
return $this->response(
|
|
array(
|
|
'error'=> 'Data not found'
|
|
), 404);
|
|
}
|
|
|
|
/**
|
|
* Method POST
|
|
* Create new record
|
|
* http://DOMAIN/token + request parameters name, email
|
|
* @return string
|
|
*/
|
|
/*
|
|
curl -d '{"oauth2_provider_id":1, "member_id":7, "refresh_token":"refresh", "access_token":"access", "email":"acidumirae@gmail.com", "name":"Anatolii Okhotnikov"}' \
|
|
-H "Content-Type: application/json" -H "Authorization: Server-Token 99dfe35fcb7de1ee" \
|
|
-X POST https://svrsavvy.sworks.float.sg/SAVVY/oauth2/api/token
|
|
|
|
curl -d '{"encrypted_payload": "ba3ea1e6e8102df163e25d1c2fc900241fc4b4b16e831a63589ab7f0646151d23f9b9b02a8ec7b482990a6d977c838a8efd84afea7bd7cef65aeec358220921b72f67365191965aafe211cb33611736f3296064017e98211051a68a4e30c358550ddf4173a3a3cd9891e40d2e29fd7c51795771eb5306787692baf4e16f13472041889b1ce99cba4c0ffd18aac27703881aa7353f583e565a6"}' \
|
|
-H "Content-Type: application/json" -H "Authorization: Server-Token 99dfe35fcb7de1ee" \
|
|
-X POST https://svrsavvy.sworks.float.sg/SAVVY/oauth2/api/token
|
|
*/
|
|
public function createAction()
|
|
{
|
|
$message = "Unknown error";
|
|
$oauth2_provider_id = $this->requestParams["oauth2_provider_id"] ?? 0;
|
|
$member_id = $this->requestParams["member_id"] ?? 0;
|
|
$refresh_token = $this->requestParams["refresh_token"] ?? "";
|
|
$access_token = $this->requestParams["access_token"] ?? "";
|
|
$email = $this->requestParams["email"] ?? 0;
|
|
$name = $this->requestParams["name"] ?? "";
|
|
if ($oauth2_provider_id>0 && $member_id>0 && $refresh_token!="" && $access_token!="" && $name!=""
|
|
&& filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
$db = new Db();
|
|
$token = OAuth2::saveToken(
|
|
$db->getConnect(),
|
|
$oauth2_provider_id,
|
|
$member_id,
|
|
$refresh_token,
|
|
$access_token,
|
|
$email,
|
|
$name);
|
|
if ($token && $token["id"]>0) {
|
|
return $this->response($token, 200);
|
|
}
|
|
}
|
|
return $this->response(
|
|
array(
|
|
"error" => "Invalid request"
|
|
), 500);
|
|
}
|
|
|
|
/**
|
|
* Method PUT
|
|
* Update single record (by id)
|
|
* http://DOMAIN/token/1 + request parameters name, email
|
|
* @return string
|
|
*/
|
|
public function updateAction()
|
|
{
|
|
$member_id = $this->requestParams["member_id"] ?? 0;
|
|
if ($member_id>0) {
|
|
$db = new Db();
|
|
$updated = OAuth2::removeTokens($db->getConnect(), $member_id);
|
|
if ($updated) {
|
|
return $this->response($updated, 200);
|
|
}
|
|
}
|
|
return $this->response(
|
|
array(
|
|
"error" => "Update error"
|
|
), 400);
|
|
}
|
|
|
|
/**
|
|
* Method DELETE
|
|
* Delete single record (by id)
|
|
* http://DOMAIN/token/1
|
|
* @return string
|
|
*/
|
|
public function deleteAction()
|
|
{/*
|
|
$parse_url = parse_url($this->requestUri[0]);
|
|
$userId = $parse_url['path'] ?? null;
|
|
|
|
$db = (new Db())->getConnect();
|
|
|
|
if(!$userId || !Trips::getById($db, $userId)){
|
|
return $this->response("Trip with id=$userId not found", 404);
|
|
}
|
|
if(Trips::deleteById($db, $userId)){
|
|
return $this->response('Data deleted.', 200);
|
|
}*/
|
|
return $this->response(
|
|
array(
|
|
"error" => "Delete error"
|
|
), 500);
|
|
}
|
|
|
|
}
|
|
|