|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2017 Vinzenz Rosenkranz <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @author René Gieling <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* @license GNU AGPL version 3 or any later version |
|
8
|
|
|
* |
|
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
12
|
|
|
* License, or (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU Affero General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
21
|
|
|
* |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace OCA\Polls\Controller; |
|
25
|
|
|
|
|
26
|
|
|
use Exception; |
|
27
|
|
|
use OCP\AppFramework\Db\DoesNotExistException; |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
use OCP\IRequest; |
|
31
|
|
|
use OCP\ILogger; |
|
32
|
|
|
use OCP\AppFramework\Controller; |
|
33
|
|
|
use OCP\AppFramework\Http; |
|
34
|
|
|
use OCP\AppFramework\Http\DataResponse; |
|
35
|
|
|
|
|
36
|
|
|
use OCP\IGroupManager; |
|
37
|
|
|
|
|
38
|
|
|
use OCA\Polls\Db\Poll; |
|
39
|
|
|
use OCA\Polls\Db\PollMapper; |
|
40
|
|
|
use OCA\Polls\Db\Comment; |
|
41
|
|
|
use OCA\Polls\Db\CommentMapper; |
|
42
|
|
|
use OCA\Polls\Service\AnonymizeService; |
|
43
|
|
|
use OCA\Polls\Model\Acl; |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
class CommentController extends Controller { |
|
48
|
|
|
|
|
49
|
|
|
private $userId; |
|
50
|
|
|
private $commentMapper; |
|
51
|
|
|
private $comment; |
|
52
|
|
|
private $anonymizer; |
|
53
|
|
|
private $acl; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* CommentController constructor. |
|
57
|
|
|
* @param string $appName |
|
58
|
|
|
* @param $UserId |
|
59
|
|
|
* @param CommentMapper $commentMapper |
|
60
|
|
|
* @param Comment $comment |
|
61
|
|
|
* @param AnonymizeService $anonymizer |
|
62
|
|
|
* @param Acl $acl |
|
63
|
|
|
*/ |
|
64
|
|
|
|
|
65
|
|
|
public function __construct( |
|
66
|
|
|
string $appName, |
|
67
|
|
|
$userId, |
|
68
|
|
|
IRequest $request, |
|
69
|
|
|
CommentMapper $commentMapper, |
|
70
|
|
|
Comment $comment, |
|
71
|
|
|
AnonymizeService $anonymizer, |
|
72
|
|
|
Acl $acl |
|
73
|
|
|
) { |
|
74
|
|
|
parent::__construct($appName, $request); |
|
75
|
|
|
$this->userId = $userId; |
|
76
|
|
|
$this->commentMapper = $commentMapper; |
|
77
|
|
|
$this->comment = $comment; |
|
78
|
|
|
$this->anonymizer = $anonymizer; |
|
79
|
|
|
$this->acl = $acl; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* get |
|
85
|
|
|
* Read all comments of a poll based on the poll id and return list as array |
|
86
|
|
|
* @NoAdminRequired |
|
87
|
|
|
* @NoCSRFRequired |
|
88
|
|
|
* @PublicPage |
|
89
|
|
|
* @param integer $pollId |
|
90
|
|
|
* @param string $token |
|
91
|
|
|
* @return DataResponse |
|
92
|
|
|
*/ |
|
93
|
|
|
public function list($pollId, $token = '') { |
|
94
|
|
|
|
|
95
|
|
|
if (\OC::$server->getUserSession()->isLoggedIn()) { |
|
96
|
|
|
$this->acl->setPollId($pollId); |
|
97
|
|
|
} elseif (!$this->acl->setToken($token)->getTokenIsValid()) { |
|
98
|
|
|
return new DataResponse(null, Http::STATUS_UNAUTHORIZED); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
try { |
|
102
|
|
|
if (!$this->acl->getAllowSeeUsernames()) { |
|
103
|
|
|
$this->anonymizer->set($this->acl->getPollId(), $this->acl->getUserId()); |
|
104
|
|
|
return new DataResponse((array) |
|
105
|
|
|
$this->anonymizer->getComments(), |
|
106
|
|
|
Http::STATUS_OK |
|
107
|
|
|
); |
|
108
|
|
|
} else { |
|
109
|
|
|
return new DataResponse((array) |
|
110
|
|
|
$this->commentMapper->findByPoll($this->acl->getPollId()), |
|
111
|
|
|
Http::STATUS_OK |
|
112
|
|
|
); |
|
113
|
|
|
} |
|
114
|
|
|
} catch (DoesNotExistException $e) { |
|
115
|
|
|
return new DataResponse($e, Http::STATUS_NOT_FOUND); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Write a new comment to the db and returns the new comment as array |
|
121
|
|
|
* @NoAdminRequired |
|
122
|
|
|
* @PublicPage |
|
123
|
|
|
* @NoCSRFRequired |
|
124
|
|
|
* @param int $pollId |
|
125
|
|
|
* @param string $userId |
|
126
|
|
|
* @param string $message |
|
127
|
|
|
* @param string $token |
|
128
|
|
|
* @return DataResponse |
|
129
|
|
|
*/ |
|
130
|
|
|
public function write($pollId, $userId, $message, $token = '') { |
|
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
if (\OC::$server->getUserSession()->isLoggedIn()) { |
|
133
|
|
|
$this->acl->setPollId($pollId); |
|
134
|
|
|
} elseif (!$this->acl->setToken($token)->getTokenIsValid()) { |
|
135
|
|
|
return new DataResponse(null, Http::STATUS_UNAUTHORIZED); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
if (!$this->acl->getAllowComment()) { |
|
139
|
|
|
return new DataResponse(null, Http::STATUS_UNAUTHORIZED); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
$this->comment = new Comment(); |
|
143
|
|
|
$this->comment->setPollId($this->acl->getPollId()); |
|
144
|
|
|
$this->comment->setUserId($this->acl->getUserId()); |
|
145
|
|
|
$this->comment->setComment($message); |
|
146
|
|
|
$this->comment->setDt(date('Y-m-d H:i:s')); |
|
147
|
|
|
|
|
148
|
|
|
try { |
|
149
|
|
|
$this->comment = $this->commentMapper->insert($this->comment); |
|
150
|
|
|
} catch (\Exception $e) { |
|
151
|
|
|
return new DataResponse($e, Http::STATUS_METHOD_NOT_ALLOWED); |
|
152
|
|
|
} |
|
153
|
|
|
return $this->list($this->acl->getPollId(), $this->acl->getToken()); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* delete |
|
158
|
|
|
* Delete Comment |
|
159
|
|
|
* @PublicPage |
|
160
|
|
|
* @NoCSRFRequired |
|
161
|
|
|
* @NoAdminRequired |
|
162
|
|
|
* @param int $pollId |
|
163
|
|
|
* @param string $message |
|
164
|
|
|
* @param string $token |
|
165
|
|
|
* @return DataResponse |
|
166
|
|
|
*/ |
|
167
|
|
|
public function delete($comment, $token = '') { |
|
168
|
|
|
|
|
169
|
|
|
$this->comment = $this->commentMapper->find($comment['id']); |
|
170
|
|
|
|
|
171
|
|
|
if (\OC::$server->getUserSession()->isLoggedIn()) { |
|
172
|
|
|
$this->acl->setPollId($this->comment->getPollId()); |
|
173
|
|
|
} elseif (!$this->acl->setToken($token)->getTokenIsValid()) { |
|
174
|
|
|
return new DataResponse(null, Http::STATUS_UNAUTHORIZED); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
try { |
|
178
|
|
|
if ($this->comment->getUserId() === $this->acl->getUserId()) { |
|
179
|
|
|
$this->comment = $this->commentMapper->delete($this->comment); |
|
180
|
|
|
} else { |
|
181
|
|
|
return new DataResponse(null, Http::STATUS_UNAUTHORIZED); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
} catch (\Exception $e) { |
|
185
|
|
|
return new DataResponse($e, Http::STATUS_METHOD_NOT_ALLOWED); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
return $this->list($this->acl->getPollId(), $this->acl->getToken()); |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.