Completed
Pull Request — master (#794)
by René
04:12
created

CommentController::delete()   A

Complexity

Conditions 6
Paths 11

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 11
c 0
b 0
f 0
nc 11
nop 1
dl 0
loc 19
ccs 0
cts 16
cp 0
crap 42
rs 9.2222
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 $mapper;
51
	private $logger;
52
53
	private $groupManager;
54
	private $pollMapper;
55
	private $anonymizer;
56
	private $acl;
57
58
	/**
59
	 * CommentController constructor.
60
	 * @param string $appName
61
	 * @param $UserId
62
	 * @param CommentMapper $mapper
63
	 * @param IGroupManager $groupManager
64
	 * @param PollMapper $pollMapper
65
	 * @param AnonymizeService $anonymizer
66
	 * @param Acl $acl
67
	 */
68
69
	public function __construct(
70
		string $appName,
71
		$userId,
72
		IRequest $request,
73
		ILogger $logger,
74
		CommentMapper $mapper,
75
		IGroupManager $groupManager,
76
		PollMapper $pollMapper,
77
		AnonymizeService $anonymizer,
78
		Acl $acl
79
	) {
80
		parent::__construct($appName, $request);
81
		$this->userId = $userId;
82
		$this->mapper = $mapper;
83
		$this->logger = $logger;
84
		$this->groupManager = $groupManager;
85
		$this->pollMapper = $pollMapper;
86
		$this->anonymizer = $anonymizer;
87
		$this->acl = $acl;
88
	}
89
90
91
	/**
92
	 * get
93
	 * Read all comments of a poll based on the poll id and return list as array
94
	 * @NoAdminRequired
95
	 * @param integer $pollId
96
	 * @return DataResponse
97
	 */
98
	public function get($pollId) {
99
100
		try {
101
			if (!$this->acl->getFoundByToken()) {
102
				$this->acl->setPollId($pollId);
103
			}
104
105
			if (!$this->acl->getAllowSeeUsernames()) {
106
				$this->anonymizer->set($pollId, $this->acl->getUserId());
107
				return new DataResponse((array) $this->anonymizer->getComments(), Http::STATUS_OK);
108
			} else {
109
				return new DataResponse((array) $this->mapper->findByPoll($pollId), Http::STATUS_OK);
110
			}
111
112
		} catch (DoesNotExistException $e) {
113
			return new DataResponse($e, Http::STATUS_NOT_FOUND);
114
		}
115
116
	}
117
118
	/**
119
	 * getByToken
120
	 * Read all comments of a poll based on a share token and return list as array
121
	 * @NoAdminRequired
122
	 * @NoCSRFRequired
123
	 * @PublicPage
124
	 * @param string $token
125
	 * @return DataResponse
126
	 */
127
	public function getByToken($token) {
128
129
		try {
130
			$this->acl->setToken($token);
131
		} catch (DoesNotExistException $e) {
132
			return new DataResponse($e, Http::STATUS_NOT_FOUND);
133
		}
134
135
		return $this->get($this->acl->getPollId());
136
137
	}
138
139
	/**
140
	 * write
141
	 * Write a new comment to the db and returns the new comment as array
142
	 * @NoAdminRequired
143
	 * @NoCSRFRequired
144
	 * @param int $pollId
145
	 * @param string $userId
146
	 * @param string $message
147
	 * @return DataResponse
148
	 */
149
	public function write($pollId, $userId, $message) {
150
		$this->logger->alert('write');
151
		if (!\OC::$server->getUserSession()->isLoggedIn() && !$this->acl->getFoundByToken()) {
152
			$this->logger->alert('not allowed ' . json_encode(\OC::$server->getUserSession()->isLoggedIn()));
153
			return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
154
		}
155
156
		if (!$this->acl->getFoundByToken()) {
157
			$this->acl->setPollId($pollId);
158
		}
159
160
		if ($this->acl->getAllowComment()) {
161
			$comment = new Comment();
162
			$comment->setPollId($pollId);
163
			$comment->setUserId($userId);
164
			$comment->setComment($message);
165
			$comment->setDt(date('Y-m-d H:i:s'));
166
167
168
			try {
169
				$comment = $this->mapper->insert($comment);
170
			} catch (\Exception $e) {
171
				$this->logger->alert('conflict ' . json_encode($e));
172
				return new DataResponse($e, Http::STATUS_CONFLICT);
173
			}
174
		} else {
175
			$this->logger->alert('unauthorized ');
176
			return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
177
		}
178
179
180
		$this->logger->alert('ok '. json_encode($comment));
181
		return new DataResponse($comment, Http::STATUS_OK);
182
183
	}
184
185
	/**
186
	 * writeByToken
187
	 * @NoAdminRequired
188
	 * @PublicPage
189
	 * @NoCSRFRequired
190
	 * @param Array $option
191
	 * @param string $setTo
192
	 * @param string $token
193
	 * @return DataResponse
194
	 */
195
	public function writeByToken($token, $message) {
196
197
		try {
198
			$this->acl->setToken($token);
199
			return $this->write($this->acl->getPollId(), $this->acl->getUserId(), $message);
200
201
		} catch (DoesNotExistException $e) {
202
			return new DataResponse($e, Http::STATUS_NOT_FOUND);
203
		}
204
205
206
	}
207
208
209
	/**
210
	 * delete
211
	 * Delete Comment
212
	 * @NoAdminRequired
213
	 * @param int $pollId
214
	 * @param string $message
215
	 * @return DataResponse
216
	 */
217
	public function delete($comment) {
218
		if (!\OC::$server->getUserSession()->isLoggedIn() && !$this->acl->getFoundByToken()) {
219
			return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
220
		}
221
222
		if (!$this->acl->getFoundByToken()) {
223
			$this->acl->setPollId($comment['pollId']);
224
		}
225
226
		try {
227
			if ( $comment['userId'] === $this->acl->getUserId() ) {
228
					$comment = $this->mapper->find($comment['id']);
229
					$comment = $this->mapper->delete($comment);
230
			}
231
		} catch (\Exception $e) {
232
			return new DataResponse($e, Http::STATUS_CONFLICT);
233
		}
234
235
		return new DataResponse(['comment' => $comment], Http::STATUS_OK);
236
237
	}
238
239
	/**
240
	 * writeByToken
241
	 * @NoAdminRequired
242
	 * @PublicPage
243
	 * @NoCSRFRequired
244
	 * @param Array $option
245
	 * @param string $setTo
246
	 * @param string $token
247
	 * @return DataResponse
248
	 */
249
	public function deleteByToken($token, $comment) {
250
251
		try {
252
			$this->acl->setToken($token);
253
			return $this->delete($comment);
254
255
		} catch (DoesNotExistException $e) {
256
			return new DataResponse($e, Http::STATUS_NOT_FOUND);
257
		}
258
259
260
261
	}
262
263
}
264