Passed
Pull Request — master (#1128)
by René
06:43
created

ShareController::resolveGroup()   A

Complexity

Conditions 6
Paths 19

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 17
ccs 0
cts 12
cp 0
rs 9.2222
c 0
b 0
f 0
cc 6
nc 19
nop 1
crap 42
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\MailService;
40
use OCA\Polls\Service\ShareService;
41
use OCA\Polls\Service\SystemService;
42
use OCA\Polls\Model\Circle;
43
use OCA\Polls\Model\ContactGroup;
44
45
class ShareController extends Controller {
46
47
	/** @var MailService */
48
	private $mailService;
49
50
	/** @var ShareService */
51
	private $shareService;
52
53
	/** @var SystemService */
54
	private $systemService;
55
56
	/**
57
	 * ShareController constructor.
58
	 * @param string $appName
59
	 * @param IRequest $request
60
	 * @param MailService $mailService
61
	 * @param ShareService $shareService
62
	 * @param SystemService $systemService
63
	 */
64
	public function __construct(
65
		string $appName,
66
		IRequest $request,
67
		MailService $mailService,
68
		ShareService $shareService,
69
		SystemService $systemService
70
	) {
71
		parent::__construct($appName, $request);
72
		$this->mailService = $mailService;
73
		$this->shareService = $shareService;
74
		$this->systemService = $systemService;
75
	}
76
77
	/**
78
	 * Add share
79
	 * @NoAdminRequired
80
	 * @param int $pollId
81
	 * @param int $pollId
82
	 * @param string $type
83
	 * @param string $userId
84
	 * @param string $userEmail
85
	 * @return DataResponse
86
	 */
87
	public function add($pollId, $type, $userId = '', $emailAddress = '') {
88
		try {
89
			return new DataResponse(['share' => $this->shareService->add($pollId, $type, $userId, $emailAddress)], Http::STATUS_CREATED);
90
		} catch (NotAuthorizedException $e) {
91
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
92
		} catch (\Exception $e) {
93
			return new DataResponse($e, Http::STATUS_CONFLICT);
94
		}
95
	}
96
97
	/**
98
	 * Get share
99
	 * @NoAdminRequired
100
	 * @param string $token
101
	 * @return DataResponse
102
	 */
103
	public function get($token) {
104
		try {
105
			return new DataResponse(['share' => $this->shareService->get($token)], Http::STATUS_CREATED);
106
		} catch (NotAuthorizedException $e) {
107
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
108
		} catch (\Exception $e) {
109
			return new DataResponse($e, Http::STATUS_CONFLICT);
110
		}
111
	}
112
113
	/**
114
	 * Set email address
115
	 * @NoAdminRequired
116
	 * @PublicPage
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 (InvalidUsernameException $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 group to individual shares
194
	 * @NoAdminRequired
195
	 * @param string $token
196
	 * @return DataResponse
197
	 */
198
	public function resolveGroup($token) {
199
		$shares = [];
200
		try {
201
			$share = $this->shareService->get($token);
202
			if ($share->getType() === Share::TYPE_CIRCLE) {
203
				foreach (new Circle($share->getUserId()->getMembers()) as $member) {
204
					$shares[] = $this->shareService->add($share->getPollId(), $member->getType(), $member->getId());
205
				}
206
			} elseif ($share->getType() === Share::TYPE_CONTACTGROUP) {
207
				foreach (new ContactGroup($share->getUserId()->getMembers()) as $contact) {
208
					$shares[] = $this->shareService->add($share->getPollId(), Share::TYPE_CONTACT, $contact->getId(), $contact->getEmailAddress());
209
				}
210
			}
211
212
			return new DataResponse(['shares' => $shares], Http::STATUS_OK);
213
		} catch (Exception $e) {
214
			return new DataResponse(['error' => $e], Http::STATUS_CONFLICT);
215
		}
216
	}
217
}
218