Completed
Pull Request — master (#31651)
by Thomas
12:23
created

LockMapper::getLockByToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @author Thomas Müller <[email protected]>
5
 * @copyright Copyright (c) 2018, ownCloud GmbH
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 */
20
21
namespace OC\Lock\Persistent;
22
23
use OCP\AppFramework\Db\Entity;
24
use OCP\AppFramework\Db\Mapper;
25
use OCP\AppFramework\Utility\ITimeFactory;
26
use OCP\IDBConnection;
27
28
class LockMapper extends Mapper {
29
	/** @var ITimeFactory */
30
	private $timeFactory;
31
32
	public function __construct(IDBConnection $db, ITimeFactory $timeFactory) {
33
		parent::__construct($db, 'persistent_locks', null);
34
		$this->timeFactory = $timeFactory;
35
	}
36
37
	/**
38
	 * Selects all locks from the database for the given storage and path.
39
	 * Also parent folders are returned and in case $returnChildLocks is true all
40
	 * children locks as well.
41
	 *
42
	 * @param int $storageId
43
	 * @param string $internalPath
44
	 * @param bool $returnChildLocks
45
	 * @return Lock[]
46
	 */
47
	public function getLocksByPath(int $storageId, string $internalPath, bool $returnChildLocks) : array {
48
		$query = $this->db->getQueryBuilder();
49
		$pathPattern = $this->db->escapeLikeParameter($internalPath) . '%';
50
51
		$query->select(['id', 'owner', 'timeout', 'created_at', 'token', 'token', 'scope', 'depth', 'file_id', 'path', 'owner_account_id'])
52
			->from($this->getTableName(), 'l')
53
			->join('l', 'filecache', 'f', $query->expr()->eq('l.file_id', 'f.fileid'))
54
			->where($query->expr()->eq('storage', $query->createNamedParameter($storageId)))
55
			->andWhere($query->expr()->gt('created_at', $query->createFunction('(' . $query->createNamedParameter($this->timeFactory->getTime()) . ' - `timeout`)')));
56
57
		if ($returnChildLocks) {
58
			$query->andWhere($query->expr()->like('f.path', $query->createNamedParameter($pathPattern)));
59
		} else {
60
			$query->andWhere($query->expr()->eq('f.path', $query->createNamedParameter($internalPath)));
61
		}
62
63
		// We need to check locks for every part in the uri.
64
		$uriParts = \explode('/', $internalPath);
65
66
		// We already covered the last part of the uri
67
		\array_pop($uriParts);
68
69
		$currentPath = '';
70
		foreach ($uriParts as $part) {
71
			if ($currentPath) {
72
				$currentPath .= '/';
73
			}
74
			$currentPath .= $part;
75
			$query->orWhere(
76
				$query->expr()->andX(
77
					// TODO: think about parent locks for depth 1
78
					$query->expr()->neq('depth', $query->createNamedParameter(0)),
79
					$query->expr()->eq('path', $query->createNamedParameter($currentPath))
0 ignored issues
show
Unused Code introduced by
The call to IExpressionBuilder::andX() has too many arguments starting with $query->expr()->eq('path...arameter($currentPath)).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
80
				)
81
			);
82
		}
83
84
		return $this->findEntities($query->getSQL(), $query->getParameters());
85
	}
86
87
	/**
88
	 * @param int $fileId
89
	 * @param string $token
90
	 * @return bool
91
	 */
92 View Code Duplication
	public function deleteByFileIdAndToken(int $fileId, string $token) : bool {
93
		$query = $this->db->getQueryBuilder();
94
95
		$rowCount = $query->delete($this->getTableName())
96
			->where($query->expr()->eq('file_id', $query->createNamedParameter($fileId)))
97
			->andWhere($query->expr()->eq('token_hash', $query->createNamedParameter(\md5($token))))
98
			->execute();
99
100
		return $rowCount === 1;
101
	}
102
103
	/**
104
	 * @param string $token
105
	 * @return Lock
106
	 * @throws \OCP\AppFramework\Db\DoesNotExistException
107
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
108
	 */
109
	public function getLockByToken(string $token) : Lock {
110
		$query = $this->db->getQueryBuilder();
111
112
		$query->select(['id', 'owner', 'timeout', 'created_at', 'token', 'token_hash', 'scope', 'depth', 'file_id', 'owner_account_id'])
113
			->from($this->getTableName(), 'l')
114
			->where($query->expr()->eq('token_hash', $query->createNamedParameter(\md5($token))));
115
116
		return $this->findEntity($query->getSQL(), $query->getParameters());
117
	}
118
119 View Code Duplication
	public function insert(Entity $entity) {
120
		if (!$entity instanceof Lock) {
121
			throw new \InvalidArgumentException('Wrong entity type used');
122
		}
123
		if (\md5($entity->getToken()) !== $entity->getTokenHash()) {
124
			throw new \InvalidArgumentException('token_hash does not match the token of the lock');
125
		}
126
		return parent::insert($entity);
127
	}
128
129 View Code Duplication
	public function update(Entity $entity) {
130
		if (!$entity instanceof Lock) {
131
			throw new \InvalidArgumentException('Wrong entity type used');
132
		}
133
		if (\md5($entity->getToken()) !== $entity->getTokenHash()) {
134
			throw new \InvalidArgumentException('token_hash does not match the token of the lock');
135
		}
136
		return parent::update($entity);
137
	}
138
139
	public function cleanup() {
140
		$query = $this->db->getQueryBuilder();
141
		$query->delete($this->getTableName())
142
			->where($query->expr()->lt('created_at',
143
				$query->createFunction('(' . $query->createNamedParameter($this->timeFactory->getTime()) . ' - `timeout`)')))
144
			->execute();
145
	}
146
}
147