Completed
Pull Request — master (#1038)
by René
06:20
created

ShareController::add()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 10
cc 3
nc 3
nop 4
crap 12
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
use OCA\Polls\Exceptions\NotAuthorizedException;
29
use OCA\Polls\Exceptions\InvalidUsername;
30
use OCA\Polls\Exceptions\InvalidShareType;
31
32
33
use OCP\IRequest;
34
use OCP\AppFramework\Controller;
35
use OCP\AppFramework\Http;
36
use OCP\AppFramework\Http\DataResponse;
37
38
use OCA\Polls\Service\ShareService;
39
use OCA\Polls\Service\MailService;
40
use OCA\Polls\Service\SystemService;
41
42
class ShareController extends Controller {
43
44
	/** @var ShareService */
45
	private $shareService;
46
47
	/** @var MailService */
48
	private $mailService;
49
50
	/** @var SystemService */
51
	private $systemService;
52
53
	/**
54
	 * ShareController constructor.
55
	 * @param string $appName
56
	 * @param IRequest $request
57
	 * @param MailService $mailService
58
	 * @param ShareService $shareService
59
	 * @param SystemService $systemService
60
	 */
61
	public function __construct(
62
		string $appName,
63
		IRequest $request,
64
		MailService $mailService,
65
		ShareService $shareService,
66
		SystemService $systemService
67
	) {
68
		parent::__construct($appName, $request);
69
		$this->shareService = $shareService;
70
		$this->mailService = $mailService;
71
		$this->systemService = $systemService;
72
	}
73
74
	/**
75
	 * Add share
76
	 * @NoAdminRequired
77
	 * @param int $pollId
78
	 * @param int $pollId
79
	 * @param string $type
80
	 * @param string $userId
81
	 * @param string $userEmail
82
	 * @return DataResponse
83
	 */
84
	public function add($pollId, $type, $userId = '', $userEmail = '') {
85
		try {
86
			return new DataResponse(['share' => $this->shareService->add($pollId, $type, $userId, $userEmail)], Http::STATUS_CREATED);
87
		} catch (NotAuthorizedException $e) {
88
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
89
		} catch (\Exception $e) {
90
			return new DataResponse($e, Http::STATUS_CONFLICT);
91
		}
92
	}
93
94
	/**
95
	 * Add share
96
	 * @NoAdminRequired
97
	 * @param int $pollId
98
	 * @param int $pollId
99
	 * @param string $type
100
	 * @param string $userId
101
	 * @param string $userEmail
102
	 * @return DataResponse
103
	 */
104
	public function get($token) {
105
		try {
106
			return new DataResponse(['share' => $this->shareService->get($token)], Http::STATUS_CREATED);
107
		} catch (NotAuthorizedException $e) {
108
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
109
		} catch (\Exception $e) {
110
			return new DataResponse($e, Http::STATUS_CONFLICT);
111
		}
112
	}
113
114
	/**
115
	 * Add share
116
	 * @NoAdminRequired
117
	 * @param int $pollId
118
	 * @param int $pollId
119
	 * @param string $type
120
	 * @param string $userId
121
	 * @param string $userEmail
122
	 * @return DataResponse
123
	 */
124
	public function setEmailAddress($token, $userEmail) {
125
		try {
126
			return new DataResponse(['share' => $this->shareService->setEmailAddress($token, $userEmail)], Http::STATUS_OK);
127
		} catch (NotAuthorizedException $e) {
128
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
129
		} catch (InvalidShareType $e) {
130
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
131
		} catch (\Exception $e) {
132
			return new DataResponse($e, Http::STATUS_CONFLICT);
133
		}
134
	}
135
136
	/**
137
	 * Create a personal share from a public share
138
	 * or update an email share with the username
139
	 * @NoAdminRequired
140
	 * @PublicPage
141
	 * @param string $token
142
	 * @param string $userName
143
	 * @return DataResponse
144
	 */
145
	public function personal($token, $userName, $emailAddress = '') {
146
		try {
147
			return new DataResponse($this->shareService->personal($token, $userName, $emailAddress), Http::STATUS_CREATED);
148
		} catch (NotAuthorizedException $e) {
149
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
150
		} catch (InvalidUsername $e) {
151
			return new DataResponse(['error' => $userName . ' is not valid'], Http::STATUS_CONFLICT);
152
		} catch (DoesNotExistException $e) {
153
			// return forbidden in all not catched error cases
154
			return new DataResponse($e, Http::STATUS_FORBIDDEN);
155
		}
156
	}
157
158
	/**
159
	 * Delete share
160
	 * @NoAdminRequired
161
	 * @param string $token
162
	 * @return DataResponse
163
	 */
164
165
	public function delete($token) {
166
		try {
167
			return new DataResponse($this->shareService->delete($token), Http::STATUS_OK);
168
		} catch (NotAuthorizedException $e) {
169
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
170
		} catch (Exception $e) {
171
			return new DataResponse($e, Http::STATUS_NOT_FOUND);
172
		}
173
	}
174
175
	/**
176
	 * Sent invitation mails for a share
177
	 * @NoAdminRequired
178
	 * @PublicPage
179
	 * @param string $token
180
	 * @return DataResponse
181
	 */
182
	public function sendInvitation($token) {
183
		try {
184
			$sentResult = $this->mailService->sendInvitationMail($token);
185
			$share = $this->shareService->get($token);
186
			return new DataResponse(['share' => $share, 'sentResult' => $sentResult], Http::STATUS_OK);
187
		} catch (Exception $e) {
188
			return new DataResponse(['error' => $e], Http::STATUS_CONFLICT);
189
		}
190
	}
191
192
	/**
193
	 * resolve Contact groupe to individual shares
194
	 * @NoAdminRequired
195
	 * @param string $token
196
	 * @return DataResponse
197
	 */
198
	public function resolveContactGroup($token) {
199
		$shares = [];
200
		try {
201
			$share = $this->shareService->get($token);
202
			foreach ($this->systemService->getContactsGroupMembers($share->getUserId()) as $member) {
203
				$shares[] = $this->shareService->add($share->getpollId(), 'contact', $member['user'], $member['emailAddress']);
204
			}
205
206
			return new DataResponse(['shares' => $shares], Http::STATUS_OK);
207
		} catch (Exception $e) {
208
			return new DataResponse(['error' => $e], Http::STATUS_CONFLICT);
209
		}
210
	}
211
}
212