Grant Approval and Status

This commit is contained in:
root 2022-12-03 23:24:18 +01:00
parent 7d83b7234e
commit 04f2123bc6
2 changed files with 78 additions and 26 deletions

View File

@ -6,6 +6,7 @@ require_once __DIR__ . '/vendor/autoload.php';
// First Party
use Tor\Tor;
use Tor\Api;
use Cave\Cave;
use SeriousJSON\JsonDatabase;

View File

@ -21,14 +21,20 @@ class GrantApi extends BaseApi {
while ($data->exists(Grant::class , $uniqid))
$uniqid = uniqid();
// TODO: Services
$service = new Service();
$service->id = uniqid();
$grant = new Grant();
$grant->id = $uniqid;
$grant->service = new Service();
$grant->service = $service;
$grant->create = time();
$data->save($grant);
$data->save($service);
$request->status(201, 'Created')
->send([ 'grant' => $grant->id, 'result' => 'created' ]);
->send([ 'grant' => $grant->id, 'service' => $grant->service->flatIdentifier(), 'result' => 'created' ]);
}
// Destroy a grant and all dependencies
@ -43,8 +49,13 @@ class GrantApi extends BaseApi {
$request->status(404, 'Not Found')->send(["error" => ["message" => "not found"]]);
}
else {
$tickets = $data->fromIndex(Grant::class, $grantID, findClass: Ticket::class);
if (!empty($tickets))
$data->delete(Ticket::class, $tickets[0]);
$data->delete(Grant::class, $grantID);
$request->status(200, 'OK')->send([ 'result' => 'ok' ]);
$request->status(200, 'OK')->send([ 'result' => 'destroyed' ]);
}
}
@ -52,30 +63,36 @@ class GrantApi extends BaseApi {
function approve(\Gac\Routing\Request $request, string $grantID) {
global $data;
// TODO: Also check if it already exists using indexes
$exists = $data->exists(Grant::class, $grantID);
if (!$exists) {
$request->status(404, 'Not Found')->send(["error" => ["message" => "not found"]]);
}
else {
$ticket = new Ticket();
$ticket->id = uniqid();
$ticket->grant = $data->load(Grant::class, $grantID);
$ticket->user = new User();
$ticket->start = time();
$ticket->end = time() + 86400;
$data->save($ticket);
$request->status(201, 'Created')
->send([ 'grant' => $ticket->grant->flatIdentifier(),'ticket' => $ticket->flatIdentifier(), 'result' => 'approved' ]);
$tickets = $data->fromIndex(Grant::class, $grantID, findClass: Ticket::class);
if (empty($tickets)) {
$ticket = new Ticket();
$ticket->id = uniqid();
$ticket->grant = $data->load(Grant::class, $grantID);
$ticket->user = new User();
$ticket->start = time();
$ticket->end = time() + 86400;
$data->save($ticket);
$request->status(201, 'Created')
->send([
'grant' => $ticket->grant->flatIdentifier(),
'ticket' => $ticket->flatIdentifier(),
'result' => 'approved'
]);
} else {
$request->status(406, 'Not Acceptable')->send(["error" => ["message" => "already approved"]]);
}
}
}
// Internal: Reject Grant (Only works if not authorized yet)
function reject(\Gac\Routing\Request $request) {
function reject(\Gac\Routing\Request $request, string $grantID) {
global $data;
// TODO: Also check if Ticket already exists using indexes
@ -86,28 +103,62 @@ class GrantApi extends BaseApi {
$request->status(404, 'Not Found')->send(["error" => ["message" => "not found"]]);
}
else {
$data->delete(Grant::class, $grantID);
$request->status(200, 'OK')
->send([ 'grant' => $ticket->grant->flatIdentifier(),'ticket' => $ticket->flatIdentifier(), 'result' => 'rejected' ]);
$tickets = $data->fromIndex(serClass: Grant::class, serIdentifier: $grantID, findClass: Ticket::class);
if (empty($tickets)) {
$data->delete(Grant::class, $grantID);
$request->status(200, 'OK')->send([ 'result' => 'rejected' ]);
} else {
$request->status(406, 'Not Acceptable')->send(["error" => ["message" => "already decided"]]);
}
}
}
// Fetch status of a grant
function status(\Gac\Routing\Request $request) {
$request->status(200, 'OK')
->send([ 'approved' => false, 'result' => 'ok' ]);
function status(\Gac\Routing\Request $request, string $grantID) {
global $data;
$exists = $data->exists(Grant::class, $grantID);
if (!$exists) {
$request->status(404, 'Not Found')->send(["error" => ["message" => "not found"]]);
}
else {
$grant = $data->load(Grant::class, $grantID);
$tickets = $data->fromIndex(Grant::class, $grantID, findClass: Ticket::class);
$result = [
'create' => $grant->create,
'service' => $grant->service->flatIdentifier(),
'status' => 'approved',
'result' => 'ok'
];
if (!empty($tickets))
$result['ticket'] = $tickets[0];
else
$result['status'] = 'created';
$request->status(200, 'OK')->send($result);
}
}
// Internal: List all grants
function list(\Gac\Routing\Request $request) {
global $data;
// TODO:
$serviceName = '638bcc3500810';
$grants = $data->fromIndex(Service::class, $serviceName, findClass: Grant::class);
$request->status(200, 'OK')
->send([ 'result' => 'ok' ]);
->send([ 'grants' => $grants, 'result' => 'ok' ]);
}
// Default response if no action is defined
function response(\Gac\Routing\Request $request) {
$request->status(200, 'OK')
->send([ 'result' => 'ok' ]);
$request->status(200, 'OK')->send([ 'result' => 'ok' ]);
}
}