opensupports/server/controllers/ticket/get.php

82 lines
2.7 KiB
PHP
Raw Normal View History

<?php
use Respect\Validation\Validator as DataValidator;
DataValidator::with('CustomValidations', true);
2017-04-17 04:59:11 +02:00
/**
* @api {post} /ticket/get Get ticket
* @apiVersion 4.0.0
2017-04-17 04:59:11 +02:00
*
* @apiName Get ticket
2017-04-17 04:59:11 +02:00
*
* @apiGroup Ticket
*
2017-04-21 08:09:24 +02:00
* @apiDescription This path retrieves information about a ticket.
2017-04-17 04:59:11 +02:00
*
* @apiPermission user
2017-04-17 04:59:11 +02:00
*
2017-04-21 08:09:24 +02:00
* @apiParam {Number} ticketNumber The number of the ticket.
2017-04-17 04:59:11 +02:00
*
2017-04-20 05:55:38 +02:00
* @apiUse INVALID_TICKET
2017-04-21 08:09:24 +02:00
* @apiUse NO_PERMISSION
*
* @apiSuccess {[Ticket](#api-Data_Structures-ObjectTicket)} data Information about the requested ticket.
2017-04-21 08:09:24 +02:00
*
2017-04-17 04:59:11 +02:00
*/
2017-04-20 05:55:38 +02:00
class TicketGetController extends Controller {
const PATH = '/get';
const METHOD = 'POST';
2016-11-21 03:01:38 +01:00
private $ticket;
public function validations() {
$session = Session::getInstance();
if (Controller::isUserSystemEnabled() || Controller::isStaffLogged()) {
return [
'permission' => 'user',
'requestData' => [
'ticketNumber' => [
'validation' => DataValidator::validTicketNumber(),
'error' => ERRORS::INVALID_TICKET
]
]
];
} else {
return [
'permission' => 'any',
'requestData' => [
'ticketNumber' => [
'validation' => DataValidator::equals($session->getTicketNumber()),
'error' => ERRORS::INVALID_TICKET
],
'csrf_token' => [
'validation' => DataValidator::equals($session->getToken()),
'error' => $session->getToken() . ' != ' . Controller::request('csrf_token')
]
]
];
}
}
public function handler() {
2016-11-21 03:01:38 +01:00
$this->ticket = Ticket::getByTicketNumber(Controller::request('ticketNumber'));
if(Controller::isUserSystemEnabled() || Controller::isStaffLogged()) {
if ($this->shouldDenyPermission()) {
throw new Exception(ERRORS::NO_PERMISSION);
} else {
Response::respondSuccess($this->ticket->toArray());
}
} else {
2016-11-21 03:01:38 +01:00
Response::respondSuccess($this->ticket->toArray());
}
}
2016-11-21 03:01:38 +01:00
private function shouldDenyPermission() {
$user = Controller::getLoggedUser();
return (!Controller::isStaffLogged() && (Controller::isUserSystemEnabled() && $this->ticket->author->id !== $user->id)) ||
2017-02-25 07:42:10 +01:00
(Controller::isStaffLogged() && (($this->ticket->owner && $this->ticket->owner->id !== $user->id) || !$user->sharedDepartmentList->includesId($this->ticket->department->id)));
2016-11-21 03:01:38 +01:00
}
}