Passed
Push — master ( 18ab32...6cb597 )
by René
04:13
created

PublicController::validateEmailAddress()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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