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

ShareService::setEmailAddress()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 9
ccs 0
cts 6
cp 0
rs 10
cc 2
nc 2
nop 2
crap 6
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
	 * @throws InvalidShareType
118
	 */
119
	public function add($pollId, $type, $userId, $emailAddress = '') {
120
		if (!$this->acl->set($pollId)->getAllowEdit()) {
121
			throw new NotAuthorizedException;
122
		}
123
124
		switch ($type) {
125
			case Group::TYPE:
126
				$share = new Group($userId);
0 ignored issues
show
Unused Code introduced by
The assignment to $share is dead and can be removed.
Loading history...
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
127
			case Circle::TYPE:
128
				$share = new Circle($userId);
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
129
			case Contact::TYPE:
130
				$share = new Contact($userId);
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
131
			case ContactGroup::TYPE:
132
				$share = new ContactGroup($userId);
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
133
			case User::TYPE:
134
				$share = new User($userId);
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
135
			case Email::TYPE:
136
				$share = new Email($userId, $emailAddress);
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
137
			default:
138
				throw new InvalidShareType('Invalid share type (' . $type . ')');
139
		}
140
141
		$this->share = new Share();
0 ignored issues
show
Unused Code introduced by
$this->share = new OCA\Polls\Db\Share() is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

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