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

ShareService::add()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 33
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 27
c 3
b 0
f 1
dl 0
loc 33
ccs 0
cts 20
cp 0
rs 8.4444
cc 8
nc 8
nop 4
crap 72
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\Service;
25
26
use OCA\Polls\Exceptions\NotAuthorizedException;
27
use OCA\Polls\Exceptions\InvalidShareType;
28
29
use OCP\Security\ISecureRandom;
30
31
use OCA\Polls\Db\ShareMapper;
32
use OCA\Polls\Db\Share;
33
use OCA\Polls\Model\Acl;
34
use OCA\Polls\Model\Circle;
35
use OCA\Polls\Model\Contact;
36
use OCA\Polls\Model\ContactGroup;
37
use OCA\Polls\Model\Email;
38
use OCA\Polls\Model\Group;
39
use OCA\Polls\Model\User;
40
41
class ShareService {
42
43
	/** @var SystemService */
44
	private $systemService;
45
46
	/** @var ShareMapper */
47
	private $shareMapper;
48
49
	/** @var Share */
50
	private $share;
51
52
	/** @var MailService */
53
	private $mailService;
54
55
	/** @var Acl */
56
	private $acl;
57
58
	/**
59
	 * ShareController constructor.
60
	 * @param SystemService $systemService
61
	 * @param ShareMapper $shareMapper
62
	 * @param Share $share
63
	 * @param MailService $mailService
64
	 * @param Acl $acl
65
	 */
66
	public function __construct(
67
		SystemService $systemService,
68
		ShareMapper $shareMapper,
69
		Share $share,
70
		MailService $mailService,
71
		Acl $acl
72
	) {
73
		$this->systemService = $systemService;
74
		$this->shareMapper = $shareMapper;
75
		$this->share = $share;
76
		$this->mailService = $mailService;
77
		$this->acl = $acl;
78
	}
79
80
	/**
81
	 * Read all shares of a poll based on the poll id and return list as array
82
	 * @NoAdminRequired
83
	 * @param int $pollId
84
	 * @return array array of Share
85
	 * @throws NotAuthorizedException
86
	 */
87
	public function list($pollId, $token) {
88
		if ($token) {
89
			return [$this->get($token)];
90
		}
91
92
		if (!$this->acl->set($pollId)->getAllowEdit()) {
93
			throw new NotAuthorizedException;
94
		}
95
96
		return $this->shareMapper->findByPoll($pollId);
97
	}
98
99
	/**
100
	 * Get share by token
101
	 * @NoAdminRequired
102
	 * @param string $token
103
	 * @return Share
104
	 */
105
	public function get($token) {
106
		$this->share = $this->shareMapper->findByToken($token);
107
		return $this->share;
108
	}
109
110
	/**
111
	 * Add share
112
	 * @NoAdminRequired
113
	 * @param int $pollId
114
	 * @param array $user
115
	 * @return Share
116
	 * @throws NotAuthorizedException
117
	 */
118
	public function add($pollId, $type, $userId, $emailAddress = '') {
119
		if (!$this->acl->set($pollId)->getAllowEdit()) {
120
			throw new NotAuthorizedException;
121
		}
122
		if ($type === Group::TYPE) {
123
			$share = new Group($userId);
124
		} elseif ($type === Circle::TYPE) {
125
			$share = new Circle($userId);
126
		} elseif ($type === Contact::TYPE) {
127
			$share = new Contact($userId);
128
		} elseif ($type === ContactGroup::TYPE) {
129
			$share = new ContactGroup($userId);
130
		} elseif ($type === User::TYPE) {
131
			$share = new User($userId);
132
		} elseif ($type === Email::TYPE) {
133
			$share = new Email($userId, $emailAddress);
134
		}
135
136
		$this->share = new Share();
137
		$this->share->setPollId($pollId);
138
		$this->share->setType($share->getType());
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $share does not seem to be defined for all execution paths leading up to this point.
Loading history...
139
		$this->share->setUserId($share->getId());
140
		$this->share->setDisplayName($share->getDisplayName());
141
		$this->share->setUserEmail($share->getEmailAddress());
142
		$this->share->setInvitationSent(0);
143
		$this->share->setToken(\OC::$server->getSecureRandom()->generate(
144
			16,
145
			ISecureRandom::CHAR_DIGITS .
146
			ISecureRandom::CHAR_LOWER .
147
			ISecureRandom::CHAR_UPPER
148
		));
149
150
		return $this->shareMapper->insert($this->share);
151
	}
152
153
	/**
154
	 * Set emailAddress to personal share
155
	 * or update an email share with the username
156
	 * @NoAdminRequired
157
	 * @param string $token
158
	 * @param string $emailAddress
159
	 * @return Share
160
	 * @throws InvalidShareType
161
	 */
162
	public function setEmailAddress($token, $emailAddress) {
163
		$this->share = $this->shareMapper->findByToken($token);
164
		if ($this->share->getType() === Share::TYPE_EXTERNAL) {
165
			$this->systemService->validateEmailAddress($emailAddress);
166
			$this->share->setUserEmail($emailAddress);
167
			// TODO: Send confirmation
168
			return $this->shareMapper->update($this->share);
169
		} else {
170
			throw new InvalidShareType('Email address can only be set in external shares.');
171
		}
172
	}
173
174
	/**
175
	 * Create a personal share from a public share
176
	 * or update an email share with the username
177
	 * @NoAdminRequired
178
	 * @param string $token
179
	 * @param string $userName
180
	 * @return Share
181
	 * @throws NotAuthorizedException
182
	 */
183
	public function personal($token, $userName, $emailAddress = '') {
184
		$this->share = $this->shareMapper->findByToken($token);
185
186
		$this->systemService->validatePublicUsername($this->share->getPollId(), $userName, $token);
187
188
		if ($emailAddress) {
189
			$this->systemService->validateEmailAddress($emailAddress);
190
		}
191
192
		if ($this->share->getType() === Share::TYPE_PUBLIC) {
193
			$pollId = $this->share->getPollId();
194
			$this->share = new Share();
195
			$this->share->setToken(\OC::$server->getSecureRandom()->generate(
196
				16,
197
				ISecureRandom::CHAR_DIGITS .
198
				ISecureRandom::CHAR_LOWER .
199
				ISecureRandom::CHAR_UPPER
200
			));
201
			$this->share->setType(Share::TYPE_EXTERNAL);
202
			$this->share->setPollId($pollId);
203
			$this->share->setUserId($userName);
204
			$this->share->setUserEmail($emailAddress);
205
			$this->share->setInvitationSent(time());
206
			$this->shareMapper->insert($this->share);
207
208
			if ($emailAddress) {
209
				$this->mailService->sendInvitationMail($this->share->getToken());
210
			}
211
212
			return $this->share;
213
		} elseif ($this->share->getType() === Share::TYPE_EMAIL) {
214
			$this->share->setType(Share::TYPE_EXTERNAL);
215
			$this->share->setUserId($userName);
216
			$this->share->setUserEmail($emailAddress);
217
			return $this->shareMapper->update($this->share);
218
		} else {
219
			throw new NotAuthorizedException;
220
		}
221
	}
222
223
	/**
224
	 * Delete share
225
	 * remove share
226
	 * @NoAdminRequired
227
	 * @param string $token
228
	 * @return Share
229
	 * @throws NotAuthorizedException
230
	 */
231
232
	public function delete($token) {
233
		$this->share = $this->shareMapper->findByToken($token);
234
		if (!$this->acl->set($this->share->getPollId())->getAllowEdit()) {
235
			throw new NotAuthorizedException;
236
		}
237
238
		$this->shareMapper->delete($this->share);
239
240
		return $this->share;
241
	}
242
}
243