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

ShareService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 5
dl 0
loc 12
ccs 0
cts 7
cp 0
rs 10
c 1
b 0
f 1
cc 1
nc 1
nop 5
crap 2
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 = '') {
0 ignored issues
show
Unused Code introduced by
The parameter $emailAddress is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

118
	public function add($pollId, $type, $userId, /** @scrutinizer ignore-unused */ $emailAddress = '') {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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);
134
		}
135
		\OC::$server->getLogger()->alert(json_encode($share));
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...
136
137
		$this->share = new Share();
138
		$this->share->setPollId($pollId);
139
		$this->share->setType($share->getType());
140
		$this->share->setUserId($share->getId());
141
		$this->share->setDisplayName($share->getDisplayName());
142
		$this->share->setUserEmail($share->getEmailAddress());
143
		$this->share->setInvitationSent(0);
144
		$this->share->setToken(\OC::$server->getSecureRandom()->generate(
145
			16,
146
			ISecureRandom::CHAR_DIGITS .
147
			ISecureRandom::CHAR_LOWER .
148
			ISecureRandom::CHAR_UPPER
149
		));
150
151
		return $this->shareMapper->insert($this->share);
152
	}
153
154
	/**
155
	 * Set emailAddress to personal share
156
	 * or update an email share with the username
157
	 * @NoAdminRequired
158
	 * @param string $token
159
	 * @param string $emailAddress
160
	 * @return Share
161
	 * @throws InvalidShareType
162
	 */
163
	public function setEmailAddress($token, $emailAddress) {
164
		$this->share = $this->shareMapper->findByToken($token);
165
		if ($this->share->getType() === Share::TYPE_EXTERNAL) {
166
			$this->systemService->validateEmailAddress($emailAddress);
167
			$this->share->setUserEmail($emailAddress);
168
			// TODO: Send confirmation
169
			return $this->shareMapper->update($this->share);
170
		} else {
171
			throw new InvalidShareType('Email address can only be set in external shares.');
172
		}
173
	}
174
175
	/**
176
	 * Create a personal share from a public share
177
	 * or update an email share with the username
178
	 * @NoAdminRequired
179
	 * @param string $token
180
	 * @param string $userName
181
	 * @return Share
182
	 * @throws NotAuthorizedException
183
	 */
184
	public function personal($token, $userName, $emailAddress = '') {
185
		$this->share = $this->shareMapper->findByToken($token);
186
187
		$this->systemService->validatePublicUsername($this->share->getPollId(), $userName, $token);
188
189
		if ($emailAddress) {
190
			$this->systemService->validateEmailAddress($emailAddress);
191
		}
192
193
		if ($this->share->getType() === Share::TYPE_PUBLIC) {
194
			$pollId = $this->share->getPollId();
195
			$this->share = new Share();
196
			$this->share->setToken(\OC::$server->getSecureRandom()->generate(
197
				16,
198
				ISecureRandom::CHAR_DIGITS .
199
				ISecureRandom::CHAR_LOWER .
200
				ISecureRandom::CHAR_UPPER
201
			));
202
			$this->share->setType(Share::TYPE_EXTERNAL);
203
			$this->share->setPollId($pollId);
204
			$this->share->setUserId($userName);
205
			$this->share->setUserEmail($emailAddress);
206
			$this->share->setInvitationSent(time());
207
			$this->shareMapper->insert($this->share);
208
209
			if ($emailAddress) {
210
				$this->mailService->sendInvitationMail($this->share->getToken());
211
			}
212
213
			return $this->share;
214
		} elseif ($this->share->getType() === Share::TYPE_EMAIL) {
215
			$this->share->setType(Share::TYPE_EXTERNAL);
216
			$this->share->setUserId($userName);
217
			$this->share->setUserEmail($emailAddress);
218
			return $this->shareMapper->update($this->share);
219
		} else {
220
			throw new NotAuthorizedException;
221
		}
222
	}
223
224
	/**
225
	 * Delete share
226
	 * remove share
227
	 * @NoAdminRequired
228
	 * @param string $token
229
	 * @return Share
230
	 * @throws NotAuthorizedException
231
	 */
232
233
	public function delete($token) {
234
		$this->share = $this->shareMapper->findByToken($token);
235
		if (!$this->acl->set($this->share->getPollId())->getAllowEdit()) {
236
			throw new NotAuthorizedException;
237
		}
238
239
		$this->shareMapper->delete($this->share);
240
241
		return $this->share;
242
	}
243
}
244