Completed
Pull Request — master (#479)
by Maxence
02:08
created

TokensRequest::updateSinglePassword()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
/**
3
 * Circles - Bring cloud-users closer together.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2017
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
28
namespace OCA\Circles\Db;
29
30
31
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
32
use OCA\Circles\Exceptions\TokenDoesNotExistException;
33
use OCA\Circles\Model\Member;
34
use OCA\Circles\Model\SharesToken;
35
36
37
/**
38
 * Class TokensRequest
39
 *
40
 * @package OCA\Circles\Db
41
 */
42
class TokensRequest extends TokensRequestBuilder {
43
44
45
	/**
46
	 * @param string $token
47
	 *
48
	 * @return SharesToken
49
	 * @throws TokenDoesNotExistException
50
	 */
51 View Code Duplication
	public function getByToken(string $token) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
		$qb = $this->getTokensSelectSql();
53
		$this->limitToToken($qb, $token);
54
55
		$cursor = $qb->execute();
56
		$data = $cursor->fetch();
57
		$cursor->closeCursor();
58
		if ($data === false) {
59
			throw new TokenDoesNotExistException('Unknown share token');
60
		}
61
62
		return $this->parseTokensSelectSql($data);
63
	}
64
65
66
	/**
67
	 * @param string $shareId
68
	 * @param string $circleId
69
	 * @param string $email
70
	 *
71
	 * @return SharesToken
72
	 * @throws TokenDoesNotExistException
73
	 */
74
	public function getTokenFromMember(string $shareId, string $circleId, string $email) {
75
		$qb = $this->getTokensSelectSql();
76
		$this->limitToShareId($qb, $shareId);
77
		$this->limitToUserId($qb, $email);
78
		$this->limitToCircleId($qb, $circleId);
79
80
		$cursor = $qb->execute();
81
		$data = $cursor->fetch();
82
		$cursor->closeCursor();
83
		if ($data === false) {
84
			throw new TokenDoesNotExistException('Unknown share token');
85
		}
86
87
		return $this->parseTokensSelectSql($data);
88
	}
89
90
91
	/**
92
	 * @param Member $member
93
	 *
94
	 * @return SharesToken[]
95
	 */
96 View Code Duplication
	public function getTokensFromMember(Member $member) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
		$qb = $this->getTokensSelectSql();
98
		$this->limitToUserId($qb, $member->getUserId());
99
		$this->limitToCircleId($qb, $member->getCircleId());
100
101
		$shares = [];
102
		$cursor = $qb->execute();
103
		while ($data = $cursor->fetch()) {
104
			$shares[] = $this->parseTokensSelectSql($data);
105
		}
106
		$cursor->closeCursor();
107
108
		return $shares;
109
	}
110
111
112
	/**
113
	 * @param Member $member
114
	 * @param int $shareId
115
	 * @param string $password
116
	 *
117
	 * @return SharesToken
118
	 * @throws TokenDoesNotExistException
119
	 */
120
	public function generateTokenForMember(Member $member, int $shareId, string $password = ''): SharesToken {
121
		try {
122
			$token = $this->miscService->token(15);
123
124
			if ($password !== '') {
125
				$hasher = \OC::$server->getHasher();
126
				$password = $hasher->hash($password);
127
			}
128
129
			$qb = $this->getTokensInsertSql();
130
			$qb->setValue('circle_id', $qb->createNamedParameter($member->getCircleId()))
131
			   ->setValue('user_id', $qb->createNamedParameter($member->getUserId()))
132
			   ->setValue('share_id', $qb->createNamedParameter($shareId))
133
			   ->setValue('member_id', $qb->createNamedParameter($member->getMemberId()))
134
			   ->setValue('token', $qb->createNamedParameter($token))
135
			   ->setValue('password', $qb->createNamedParameter($password));
136
137
			$qb->execute();
138
		} catch (UniqueConstraintViolationException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
Bug introduced by
The class Doctrine\DBAL\Exception\...raintViolationException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
139
		}
140
141
		return $this->getTokenFromMember($shareId, $member->getCircleId(), $member->getUserId());
142
	}
143
144
145
	/**
146
	 * @param int $shareId
147
	 */
148
	public function removeTokenByShareId(int $shareId) {
149
		$qb = $this->getTokensDeleteSql();
150
		$this->limitToShareId($qb, $shareId);
151
152
		$qb->execute();
153
	}
154
155
156
	/**
157
	 * @param Member $member
158
	 */
159
	public function removeTokensFromMember(Member $member) {
160
		$qb = $this->getTokensDeleteSql();
161
		$this->limitToCircleId($qb, $member->getCircleId());
162
		$this->limitToUserId($qb, $member->getUserId());
163
164
		$qb->execute();
165
	}
166
167
168
	public function updateSinglePassword(string $circleId, string $password) {
169
		$qb = $this->getTokensUpdateSql();
170
171
		if ($password !== '') {
172
			$hasher = \OC::$server->getHasher();
173
			$password = $hasher->hash($password);
174
		}
175
176
		$this->limitToCircleId($qb, $circleId);
177
		$qb->set('password', $qb->createNamedParameter($password));
178
179
		$qb->execute();
180
	}
181
182
}
183
184