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