Completed
Pull Request — master (#1030)
by René
04:05
created

ShareService::add()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 14
c 3
b 0
f 1
dl 0
loc 20
ccs 0
cts 17
cp 0
rs 9.7998
cc 2
nc 2
nop 4
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 Exception;
27
use OCA\Polls\Exceptions\NotAuthorizedException;
28
use OCA\Polls\Exceptions\InvalidUsername;
29
30
use OCP\Security\ISecureRandom;
31
32
use OCA\Polls\Controller\SystemController;
33
use OCA\Polls\Db\ShareMapper;
34
use OCA\Polls\Db\Share;
35
use OCA\Polls\Service\MailService;
36
use OCA\Polls\Model\Acl;
37
38
class ShareService {
39
40
	/** @var SystemController */
41
	private $systemController;
42
43
	/** @var ShareMapper */
44
	private $shareMapper;
45
46
	/** @var Share */
47
	private $share;
48
49
	/** @var MailService */
50
	private $mailService;
51
52
	/** @var Acl */
53
	private $acl;
54
55
	/**
56
	 * ShareController constructor.
57
	 * @param SystemController $systemController
58
	 * @param ShareMapper $shareMapper
59
	 * @param Share $share
60
	 * @param MailService $mailService
61
	 * @param Acl $acl
62
	 */
63
	public function __construct(
64
		SystemController $systemController,
65
		ShareMapper $shareMapper,
66
		Share $share,
67
		MailService $mailService,
68
		Acl $acl
69
	) {
70
		$this->systemController = $systemController;
71
		$this->shareMapper = $shareMapper;
72
		$this->share = $share;
73
		$this->mailService = $mailService;
74
		$this->acl = $acl;
75
	}
76
77
	/**
78
	 * Read all shares of a poll based on the poll id and return list as array
79
	 * @NoAdminRequired
80
	 * @param int $pollId
81
	 * @return array array of Share
82
	 * @throws NotAuthorizedException
83
	 */
84
	public function list($pollId) {
85
		if (!$this->acl->setPollId($pollId)->getAllowEdit()) {
86
			throw new NotAuthorizedException;
87
		}
88
89
		return $this->shareMapper->findByPoll($pollId);
90
	}
91
92
	/**
93
	 * Get share by token
94
	 * @NoAdminRequired
95
	 * @param string $token
96
	 * @return Share
97
	 */
98
	public function get($token) {
99
		return $this->shareMapper->findByToken($token);
100
	}
101
102
	/**
103
	 * Add share
104
	 * @NoAdminRequired
105
	 * @param int $pollId
106
	 * @param string $type
107
	 * @param string $userId
108
	 * @param string $userEmail
109
	 * @return Share
110
	 * @throws NotAuthorizedException
111
	 */
112
	public function add($pollId, $type, $userId, $userEmail = '') {
113
114
		if (!$this->acl->setPollId($pollId)->getAllowEdit()) {
115
			throw new NotAuthorizedException;
116
		}
117
118
		$this->share = new Share();
119
		$this->share->setType($type);
120
		$this->share->setPollId($pollId);
121
		$this->share->setUserId($userId);
122
		$this->share->setUserEmail($userEmail);
123
		$this->share->setInvitationSent(0);
124
		$this->share->setToken(\OC::$server->getSecureRandom()->generate(
125
			16,
126
			ISecureRandom::CHAR_DIGITS .
127
			ISecureRandom::CHAR_LOWER .
128
			ISecureRandom::CHAR_UPPER
129
		));
130
131
		return $this->shareMapper->insert($this->share);
132
	}
133
134
	/**
135
	 * Create a personal share from a public share
136
	 * or update an email share with the username
137
	 * @NoAdminRequired
138
	 * @param string $token
139
	 * @param string $userName
140
	 * @return Share
141
	 * @throws NotAuthorizedException
142
	 * @throws InvalidUsername
143
	 */
144
	public function personal($token, $userName) {
145
		$publicShare = $this->shareMapper->findByToken($token);
146
147
		// Return of validatePublicUsername is a DataResponse
148
		$checkUsername = $this->systemController->validatePublicUsername($publicShare->getPollId(), $userName, $token);
149
150
		// if status is not 200, return DataResponse from validatePublicUsername
151
		if ($checkUsername->getStatus() !== 200) {
152
			throw new InvalidUsername;
153
		}
154
155
		if ($publicShare->getType() === 'public') {
156
157
			$this->share = new Share();
158
			$this->share->setToken(\OC::$server->getSecureRandom()->generate(
159
				16,
160
				ISecureRandom::CHAR_DIGITS .
161
				ISecureRandom::CHAR_LOWER .
162
				ISecureRandom::CHAR_UPPER
163
			));
164
			$this->share->setType('external');
165
			$this->share->setPollId($publicShare->getPollId());
166
			$this->share->setUserId($userName);
167
			$this->share->setUserEmail('');
168
			$this->share->setInvitationSent(time());
169
			return $this->shareMapper->insert($this->share);
170
171
		} elseif ($publicShare->getType() === 'email') {
172
173
			$publicShare->setType('external');
174
			$publicShare->setUserId($userName);
175
			return $this->shareMapper->update($publicShare);
176
177
		} else {
178
			throw new NotAuthorizedException;
179
		}
180
	}
181
182
	/**
183
	 * Delete share
184
	 * remove share
185
	 * @NoAdminRequired
186
	 * @param string $token
187
	 * @return Share
188
	 * @throws NotAuthorizedException
189
	 */
190
191
	public function delete($token) {
192
		$this->share = $this->shareMapper->findByToken($token);
193
		if (!$this->acl->setPollId($this->share->getPollId())->getAllowEdit()) {
194
			throw new NotAuthorizedException;
195
		}
196
197
		$this->shareMapper->delete($this->share);
198
199
		return $this->share;
200
	}
201
}
202