Passed
Pull Request — master (#1339)
by René
05:25 queued 01:34
created

PublicController::getVotes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
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 OCP\IRequest;
27
use OCP\IURLGenerator;
28
use OCP\AppFramework\Controller;
29
use OCP\AppFramework\Http;
30
use OCP\AppFramework\Http\DataResponse;
31
use OCP\AppFramework\Http\TemplateResponse;
32
use OCP\AppFramework\Http\Template\PublicTemplateResponse;
33
34
35
36
use OCA\Polls\Db\Share;
37
use OCA\Polls\Db\Poll;
38
use OCA\Polls\Model\Acl;
39
use OCA\Polls\Service\CommentService;
40
use OCA\Polls\Service\MailService;
41
use OCA\Polls\Service\OptionService;
42
use OCA\Polls\Service\PollService;
43
use OCA\Polls\Service\ShareService;
44
use OCA\Polls\Service\SubscriptionService;
45
use OCA\Polls\Service\VoteService;
46
use OCA\Polls\Service\SystemService;
47
48
class PublicController extends Controller {
49
50
	/** @var IURLGenerator */
51
	private $urlGenerator;
52
53
	/** @var Acl */
54
	private $acl;
55
56
	/** @var CommentService */
57
	private $commentService;
58
59
	/** @var OptionService */
60
	private $optionService;
61
62
	/** @var MailService */
63
	private $mailService;
64
65
	/** @var PollService */
66
	private $pollService;
67
68
	/** @var Poll */
69
	private $poll;
70
71
	/** @var ShareService */
72
	private $shareService;
73
74
	/** @var SubscriptionService */
75
	private $subscriptionService;
76
77
	/** @var Share */
78
	private $share;
79
80
	/** @var VoteService */
81
	private $voteService;
82
83
	/** @var SystemService */
84
	private $systemService;
85
86
	use ResponseHandle;
87
88
	public function __construct(
89
		string $appName,
90
		IRequest $request,
91
		IURLGenerator $urlGenerator,
92
		Acl $acl,
93
		CommentService $commentService,
94
		MailService $mailService,
95
		OptionService $optionService,
96
		PollService $pollService,
97
		Poll $poll,
98
		ShareService $shareService,
99
		Share $share,
100
		SubscriptionService $subscriptionService,
101
		VoteService $voteService,
102
		SystemService $systemService
103
	) {
104
		parent::__construct($appName, $request);
105
		$this->urlGenerator = $urlGenerator;
106
		$this->acl = $acl;
107
		$this->commentService = $commentService;
108
		$this->mailService = $mailService;
109
		$this->optionService = $optionService;
110
		$this->pollService = $pollService;
111
		$this->poll = $poll;
112
		$this->shareService = $shareService;
113
		$this->share = $share;
114
		$this->subscriptionService = $subscriptionService;
115
		$this->voteService = $voteService;
116
		$this->systemService = $systemService;
117
	}
118
119
	/**
120
	 * @PublicPage
121
	 * @NoAdminRequired
122
	 * @NoCSRFRequired
123
	 * @return TemplateResponse|PublicTemplateResponse
124
	 */
125
	public function votePage() {
126
		if (\OC::$server->getUserSession()->isLoggedIn()) {
127
			return new TemplateResponse('polls', 'polls.tmpl', [
128
				'urlGenerator' => $this->urlGenerator]);
129
		} else {
130
			return new PublicTemplateResponse('polls', 'polls.tmpl', [
131
				'urlGenerator' => $this->urlGenerator]);
132
		}
133
	}
134
135
	/**
136
	 * get complete poll via token
137
	 * @PublicPage
138
	 * @NoAdminRequired
139
	 */
140
	public function getPoll(string $token): DataResponse {
141
		return $this->response(function () use ($token) {
142
			$this->share = $this->shareService->get($token, true);
143
			$this->acl->setShare($this->share);
144
			$this->poll = $this->pollService->get($this->share->getPollId());
145
			return [
146
				'acl' => $this->acl,
147
				'poll' => $this->poll,
148
			];
149
		});
150
	}
151
152
	/**
153
	 * Get share
154
	 * @PublicPage
155
	 * @NoAdminRequired
156
	 */
157
	public function getShare(string $token): DataResponse {
158
		return $this->response(function () use ($token) {
159
			return ['share' =>$this->shareService->get($token, true)];
160
		});
161
	}
162
163
	/**
164
	 * Get Comments
165
	 * @NoAdminRequired
166
	 * @PublicPage
167
	 */
168
	public function getComments(string $token): DataResponse {
169
		return $this->response(function () use ($token) {
170
			return ['comments'=> $this->commentService->list(null, $token)];
171
		});
172
	}
173
174
	/**
175
	 * Get votes
176
	 * @NoAdminRequired
177
	 * @PublicPage
178
	 */
179
	public function getVotes(string $token): DataResponse {
180
		return $this->response(function () use ($token) {
181
			return ['votes'=> $this->voteService->list(null, $token)];
182
		});
183
	}
184
185
	/**
186
	 * Get options
187
	 * @NoAdminRequired
188
	 * @PublicPage
189
	 */
190
	public function getOptions(string $token): DataResponse {
191
		return $this->response(function () use ($token) {
192
			return ['options'=> $this->optionService->list(null, $token)];
193
		});
194
	}
195
196
	/**
197
	 * Get subscription status
198
	 * @PublicPage
199
	 * @NoAdminRequired
200
	 */
201
	public function getSubscription(string $token): DataResponse {
202
		return $this->response(function () use ($token) {
203
			return ['subscribed' => $this->subscriptionService->get(0, $token)];
204
		});
205
	}
206
207
	/**
208
	 * Set Vote
209
	 * @PublicPage
210
	 * @NoAdminRequired
211
	 */
212
	public function setVote(int $optionId, string $setTo, string $token): DataResponse {
213
		return $this->response(function () use ($optionId, $setTo, $token) {
214
			return ['vote' =>$this->voteService->set($optionId, $setTo, $token)];
215
		});
216
	}
217
218
	/**
219
	 * Write a new comment to the db and returns the new comment as array
220
	 * @NoAdminRequired
221
	 * @PublicPage
222
	 */
223
	public function addComment(string $token, string $message): DataResponse {
224
		return $this->response(function () use ($token, $message) {
225
			return ['comment'=> $this->commentService->add(null, $token, $message)];
226
		});
227
	}
228
229
	/**
230
	 * Delete Comment
231
	 * @NoAdminRequired
232
	 * @PublicPage
233
	 */
234
	public function deleteComment(int $commentId, string $token): DataResponse {
235
		return $this->responseDeleteTolerant(function () use ($commentId, $token) {
236
			return ['comment'=> $this->commentService->delete($commentId, $token)];
237
		});
238
	}
239
240
	/**
241
	 * subscribe
242
	 * @PublicPage
243
	 * @NoAdminRequired
244
	 */
245
	public function subscribe(string $token): DataResponse {
246
		return $this->response(function () use ($token) {
247
			return ['subscribed' => $this->subscriptionService->set(0, $token, true)];
248
		});
249
	}
250
251
	/**
252
	 * Unsubscribe
253
	 * @PublicPage
254
	 * @NoAdminRequired
255
	 */
256
	public function unsubscribe(string $token): DataResponse {
257
		return $this->response(function () use ($token) {
258
			return ['subscribed' => $this->subscriptionService->set(0, $token, false)];
259
		});
260
	}
261
262
	/**
263
	 * Validate it the user name is reservrd
264
	 * return false, if this username already exists as a user or as
265
	 * a participant of the poll
266
	 * @NoAdminRequired
267
	 * @PublicPage
268
	 */
269
	public function validatePublicUsername(string $userName, string $token): DataResponse {
270
		try {
271
			return new DataResponse(['result' => $this->systemService->validatePublicUsername($userName, $token), 'name' => $userName], Http::STATUS_OK);
272
		} catch (\Exception $e) {
273
			return new DataResponse(['message' => $e->getMessage()], Http::STATUS_CONFLICT);
274
		}
275
	}
276
277
	/**
278
	 * Validate email address (simple validation)
279
	 * @NoAdminRequired
280
	 * @PublicPage
281
	 */
282
	public function validateEmailAddress(string $emailAddress): DataResponse {
283
		try {
284
			return new DataResponse(['result' => $this->systemService->validateEmailAddress($emailAddress), 'emailAddress' => $emailAddress], Http::STATUS_OK);
285
		} catch (\Exception $e) {
286
			return new DataResponse(['message' => $e->getMessage()], Http::STATUS_CONFLICT);
287
		}
288
	}
289
290
	/**
291
	 * Create a personal share from a public share
292
	 * or update an email share with the username
293
	 * @NoAdminRequired
294
	 * @PublicPage
295
	 */
296
	public function register(string $token, string $userName, string $emailAddress = ''): DataResponse {
297
		return $this->responseCreate(function () use ($token, $userName, $emailAddress) {
298
			return ['share' => $this->shareService->personal($token, $userName, $emailAddress)];
299
		});
300
	}
301
302
	/**
303
	 * Sent invitation mails for a share
304
	 * Additionally send notification via notifications
305
	 * @NoAdminRequired
306
	 * @PublicPage
307
	 */
308
	public function resendInvitation(string $token): DataResponse {
309
		return $this->response(function () use ($token) {
310
			return ['share' => $this->mailService->resendInvitation($token)];
311
		});
312
	}
313
}
314