Passed
Pull Request — master (#1128)
by René
04:35
created

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