|
1
|
|
|
<?php |
|
2
|
|
|
namespace OCA\Calendar\Controller; |
|
3
|
|
|
|
|
4
|
|
|
use Exception; |
|
5
|
|
|
|
|
6
|
|
|
use OCP\IRequest; |
|
7
|
|
|
use OCP\AppFramework\Http; |
|
8
|
|
|
use OCP\AppFramework\Http\DataResponse; |
|
9
|
|
|
use OCP\AppFramework\Controller; |
|
10
|
|
|
use OCA\Calendar\Db\OtoConfirmation; |
|
11
|
|
|
use OCA\Calendar\Db\OtoConfirmationMapper; |
|
12
|
|
|
use OCA\Calendar\Service\OtoLayerService; |
|
13
|
|
|
use OCA\Calendar\Service\OtoConfirmationService; |
|
14
|
|
|
use OCP\IUserSession; |
|
15
|
|
|
//using the IUserSession above lets us get info about the user |
|
16
|
|
|
|
|
17
|
|
|
class OtoConfirmationController extends Controller { |
|
18
|
|
|
|
|
19
|
|
|
private $mapper; |
|
20
|
|
|
private $foo; |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @NoAdminRequired |
|
25
|
|
|
* @param string $appName |
|
26
|
|
|
* @param IRequest $request an instance of the request |
|
27
|
|
|
* @param OtoConfirmationMapper $mapper |
|
28
|
|
|
* @param OtoConfirmationService $otoConfirmationService |
|
29
|
|
|
* @param OtoLayerService $otoLayerService |
|
30
|
|
|
* @param IUserSession $userSession |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct($appName, IRequest $request, OtoConfirmationMapper $mapper,OtoConfirmationService $otoConfirmationService, OtoLayerService $otoLayerService, IUserSession $userSession){ |
|
33
|
|
|
parent::__construct($appName, $request); |
|
34
|
|
|
$this->mapper = $mapper; |
|
35
|
|
|
$this->otoLayerService = $otoLayerService; |
|
36
|
|
|
$this->otoConfirmationService = $otoConfirmationService; |
|
37
|
|
|
$this->userSession = $userSession; |
|
38
|
|
|
// $this->userId = $userSession->getUser()->getUID(); |
|
39
|
|
|
} |
|
40
|
|
|
/** |
|
41
|
|
|
* @PublicPage |
|
42
|
|
|
* @NoCSRFRequired |
|
43
|
|
|
* @param string $otoLayerId |
|
44
|
|
|
* @param string $password |
|
45
|
|
|
* @param string $eventId |
|
46
|
|
|
* @param string $name |
|
47
|
|
|
* @param string $password |
|
48
|
|
|
*/ |
|
49
|
|
|
public function create($otoLayerId, $password, $eventId, $name){ |
|
50
|
|
|
//check password first, refuse connection if not matching |
|
51
|
|
|
if(! $this->otoLayerService->passwordCheck($otoLayerId,$password) ){ |
|
52
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND); |
|
53
|
|
|
} |
|
54
|
|
|
//check that we can still confirm this event, if not refuse the connection |
|
55
|
|
|
if(! $this->mapper->canConfirm($otoLayerId) ){ |
|
56
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND); |
|
57
|
|
|
} |
|
58
|
|
|
$confirmation = new OtoConfirmation(); |
|
59
|
|
|
//below log message is only present if we passed these tests |
|
60
|
|
|
$confirmation->setOtoLayerId($otoLayerId); |
|
61
|
|
|
$confirmation->setName($name); |
|
62
|
|
|
$confirmation->setEventId($eventId); |
|
63
|
|
|
//populate confirmation with relevant information |
|
64
|
|
|
try{ |
|
65
|
|
|
$foo = new DataResponse($this->mapper->insert($confirmation)); |
|
66
|
|
|
return $foo; |
|
67
|
|
|
//attempt to insert the confirmation, no log message if successful |
|
68
|
|
|
}catch(Exception $e){ |
|
69
|
|
|
//catch exceptions and add them to our phplog for debugging or review |
|
70
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND); |
|
71
|
|
|
//refuse the connection |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
//returns a data response that contains all of the confirmations for a user |
|
75
|
|
|
//along with their otoLayer's sourceId and destinationId |
|
76
|
|
|
/** |
|
77
|
|
|
* @NoAdminRequired |
|
78
|
|
|
*/ |
|
79
|
|
View Code Duplication |
public function getConfirmationsByUser(){ |
|
|
|
|
|
|
80
|
|
|
//user ID is passed in |
|
81
|
|
|
$userId = $this->userSession->getUser()->getUID(); |
|
82
|
|
|
try{ |
|
83
|
|
|
return new DataResponse($this->mapper->getConfirmationsByUser($userId)); |
|
84
|
|
|
}catch(Exception $e){ |
|
85
|
|
|
return new DataResponse([],Http::STATUS_NOT_FOUND); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
//deletes a layer and all of it's confirmations |
|
89
|
|
|
/** |
|
90
|
|
|
* @NoAdminRequired |
|
91
|
|
|
*/ |
|
92
|
|
|
public function deleteConfirmationsLayers($otoLayerId){ |
|
93
|
|
|
try{ |
|
94
|
|
|
$this->otoConfirmationService->deleteConfirmationsByOtoLayer($otoLayerId); |
|
95
|
|
|
$this->otoLayerService->deleteLayer($otoLayerId); |
|
96
|
|
|
return new DataResponse(True); |
|
97
|
|
|
}catch(Exception $e){ |
|
98
|
|
|
//catch any problems that might arise when deleting a layer |
|
99
|
|
|
return new DataResponse(False); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
//deletes a layer and all of it's confirmations |
|
103
|
|
|
/** |
|
104
|
|
|
* @NoAdminRequired |
|
105
|
|
|
*/ |
|
106
|
|
|
public function deleteBySourceId($sourceId){ |
|
107
|
|
|
try{ |
|
108
|
|
|
$userId = $this->userSession->getUser()->getUID(); |
|
109
|
|
|
$this->otoConfirmationService->deleteBySourceId($sourceId,$userId); |
|
110
|
|
|
$this->otoLayerService->deleteBySourceId($sourceId,$userId); |
|
111
|
|
|
return new DataResponse(True); |
|
112
|
|
|
}catch(Exception $e){ |
|
113
|
|
|
//catch any problems that might arise when deleting a layer |
|
114
|
|
|
return new DataResponse(False); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.