Completed
Pull Request — master (#1128)
by René
05:00
created

ShareController::resolveContactGroup()   A

Complexity

Conditions 3
Paths 9

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 11
ccs 0
cts 10
cp 0
rs 10
cc 3
nc 9
nop 1
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\InvalidUsernameException;
30
use OCA\Polls\Exceptions\InvalidShareType;
31
use OCA\Polls\Exceptions\ShareAlreadyExists;
32
33
34
use OCP\IRequest;
35
use OCP\AppFramework\Controller;
36
use OCP\AppFramework\Http;
37
use OCP\AppFramework\Http\DataResponse;
38
39
use OCA\Polls\DB\Share;
40
use OCA\Polls\Service\MailService;
41
use OCA\Polls\Service\ShareService;
42
use OCA\Polls\Service\SystemService;
43
use OCA\Polls\Model\Circle;
44
use OCA\Polls\Model\ContactGroup;
45
46
class ShareController extends Controller {
47
48
	/** @var MailService */
49
	private $mailService;
50
51
	/** @var ShareService */
52
	private $shareService;
53
54
	/** @var SystemService */
55
	private $systemService;
56
57
	/**
58
	 * ShareController constructor.
59
	 * @param string $appName
60
	 * @param IRequest $request
61
	 * @param MailService $mailService
62
	 * @param ShareService $shareService
63
	 * @param SystemService $systemService
64
	 */
65
	public function __construct(
66
		string $appName,
67
		IRequest $request,
68
		MailService $mailService,
69
		ShareService $shareService,
70
		SystemService $systemService
71
	) {
72
		parent::__construct($appName, $request);
73
		$this->mailService = $mailService;
74
		$this->shareService = $shareService;
75
		$this->systemService = $systemService;
76
	}
77
78
	/**
79
	 * List shares
80
	 * @NoAdminRequired
81
	 * @param int $pollId
82
	 * @return DataResponse
83
	 */
84
	public function list($pollId) {
85
		try {
86
			return new DataResponse(['shares' => $this->shareService->list($pollId)], Http::STATUS_OK);
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 string $type
99
	 * @param string $userId
100
	 * @param string $userEmail
101
	 * @return DataResponse
102
	 */
103
	public function add($pollId, $type, $userId = '', $emailAddress = '') {
104
		try {
105
			return new DataResponse(['share' => $this->shareService->add($pollId, $type, $userId, $emailAddress)], Http::STATUS_CREATED);
106
		} catch (NotAuthorizedException $e) {
107
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
108
		} catch (ShareAlreadyExists $e) {
109
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
110
		}
111
	}
112
113
	/**
114
	 * Get share
115
	 * @NoAdminRequired
116
	 * @param string $token
117
	 * @return DataResponse
118
	 */
119
	public function get($token) {
120
		try {
121
			return new DataResponse(['share' => $this->shareService->get($token)], Http::STATUS_CREATED);
122
		} catch (NotAuthorizedException $e) {
123
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
124
		} catch (\Exception $e) {
125
			return new DataResponse($e, Http::STATUS_CONFLICT);
126
		}
127
	}
128
129
	/**
130
	 * Set email address
131
	 * @NoAdminRequired
132
	 * @PublicPage
133
	 * @param int $pollId
134
	 * @param int $pollId
135
	 * @param string $type
136
	 * @param string $userId
137
	 * @param string $userEmail
138
	 * @return DataResponse
139
	 */
140
	public function setEmailAddress($token, $userEmail) {
141
		try {
142
			return new DataResponse(['share' => $this->shareService->setEmailAddress($token, $userEmail)], Http::STATUS_OK);
143
		} catch (NotAuthorizedException $e) {
144
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
145
		} catch (InvalidShareType $e) {
146
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
147
		} catch (\Exception $e) {
148
			return new DataResponse($e, Http::STATUS_CONFLICT);
149
		}
150
	}
151
152
	/**
153
	 * Create a personal share from a public share
154
	 * or update an email share with the username
155
	 * @NoAdminRequired
156
	 * @PublicPage
157
	 * @param string $token
158
	 * @param string $userName
159
	 * @return DataResponse
160
	 */
161
	public function personal($token, $userName, $emailAddress = '') {
162
		try {
163
			return new DataResponse($this->shareService->personal($token, $userName, $emailAddress), Http::STATUS_CREATED);
164
		} catch (NotAuthorizedException $e) {
165
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
166
		} catch (InvalidUsernameException $e) {
167
			return new DataResponse(['error' => $userName . ' is not valid'], Http::STATUS_CONFLICT);
168
		} catch (DoesNotExistException $e) {
169
			// return forbidden in all not catched error cases
170
			return new DataResponse($e, Http::STATUS_FORBIDDEN);
171
		}
172
	}
173
174
	/**
175
	 * Delete share
176
	 * @NoAdminRequired
177
	 * @param string $token
178
	 * @return DataResponse
179
	 */
180
181
	public function delete($token) {
182
		try {
183
			return new DataResponse($this->shareService->delete($token), Http::STATUS_OK);
184
		} catch (NotAuthorizedException $e) {
185
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
186
		} catch (Exception $e) {
187
			return new DataResponse($e, Http::STATUS_NOT_FOUND);
188
		}
189
	}
190
191
	/**
192
	 * Sent invitation mails for a share
193
	 * @NoAdminRequired
194
	 * @PublicPage
195
	 * @param string $token
196
	 * @return DataResponse
197
	 */
198
	public function sendInvitation($token) {
199
		try {
200
			$sentResult = $this->mailService->sendInvitationMail($token);
201
			$share = $this->shareService->get($token);
202
			return new DataResponse(['share' => $share, 'sentResult' => $sentResult], Http::STATUS_OK);
203
		} catch (Exception $e) {
204
			return new DataResponse(['error' => $e], Http::STATUS_CONFLICT);
205
		}
206
	}
207
208
	/**
209
	 * resolve contact group to individual shares
210
	 * @NoAdminRequired
211
	 * @param string $token
212
	 * @return DataResponse
213
	 */
214
	public function resolveGroup($token) {
215
		$shares = [];
216
		try {
217
			$share = $this->shareService->get($token);
218
			if ($share->getType() === Share::TYPE_CIRCLE) {
219
				foreach ((new Circle($share->getUserId()))->getMembers() as $member) {
220
					try {
221
						$newShare = $this->shareService->add($share->getPollId(), $member->getType(), $member->getId());
222
						$shares[] = $newShare;
223
					} catch (ShareAlreadyExists $e) {
224
						continue;
225
					}
226
				}
227
			} elseif ($share->getType() === Share::TYPE_CONTACTGROUP) {
228
				foreach ((new ContactGroup($share->getUserId()))->getMembers() as $contact) {
229
					try {
230
						$newShare = $this->shareService->add($share->getPollId(), Share::TYPE_CONTACT, $contact->getId(), $contact->getEmailAddress());
231
						$shares[] = $newShare;
232
					} catch (ShareAlreadyExists $e) {
233
						continue;
234
					}
235
				}
236
			}
237
			$this->shareService->delete($token);
238
			return new DataResponse(['shares' => $shares], Http::STATUS_OK);
239
		} catch (Exception $e) {
240
			return new DataResponse(['error' => $e], Http::STATUS_CONFLICT);
241
		}
242
	}
243
}
244