Completed
Pull Request — master (#362)
by Maxence
02:16
created

GSSharesRequest::getForUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 2
nc 2
nop 1
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 daita\MySmallPhpTools\Traits\TStringTools;
32
use OCA\Circles\Model\GlobalScale\GSShare;
33
use OCA\Circles\Model\Member;
34
use OCP\DB\QueryBuilder\IQueryBuilder;
35
36
37
/**
38
 * Class GSSharesRequest
39
 *
40
 * @package OCA\Circles\Db
41
 */
42
class GSSharesRequest extends GSSharesRequestBuilder {
43
44
45
	use TStringTools;
46
47
48
	/**
49
	 * @param GSShare $gsShare
50
	 */
51
	public function create(GSShare $gsShare): void {
52
		$hash = $this->token();
53
		$qb = $this->getGSSharesInsertSql();
54
		$qb->setValue('circle_id', $qb->createNamedParameter($gsShare->getCircleId()))
55
		   ->setValue('owner', $qb->createNamedParameter($gsShare->getOwner()))
56
		   ->setValue('instance', $qb->createNamedParameter($gsShare->getInstance()))
57
		   ->setValue('token', $qb->createNamedParameter($gsShare->getToken()))
58
		   ->setValue('parent', $qb->createNamedParameter($gsShare->getParent()))
59
		   ->setValue('mountpoint', $qb->createNamedParameter($gsShare->getMountPoint()))
60
		   ->setValue('mountpoint_hash', $qb->createNamedParameter($hash));
61
		$qb->execute();
62
	}
63
64
65
	/**
66
	 * @param string $userId
67
	 *
68
	 * @return GSShare[]
69
	 */
70
	public function getForUser(string $userId): array {
71
		$qb = $this->getGSSharesSelectSql();
72
73
		$this->joinMembership($qb, $userId);
74
75
76
		$shares = [];
77
		$cursor = $qb->execute();
78
		while ($data = $cursor->fetch()) {
79
			$shares[] = $this->parseGSSharesSelectSql($data);
80
		}
81
		$cursor->closeCursor();
82
83
		return $shares;
84
	}
85
86
87
	/**
88
	 * @param Member $member
89
	 */
90
	public function removeGSSharesFromMember(Member $member) {
91
		$qb = $this->getGSSharesDeleteSql();
92
		$this->limitToCircleId($qb, $member->getCircleId());
93
		$this->limitToInstance($qb, $member->getInstance());
94
		$this->limitToOwner($qb, $member->getUserId());
95
96
		$qb->execute();
97
	}
98
99
100
	/**
101
	 * @param IQueryBuilder $qb
102
	 * @param string $userId
103
	 */
104
	private function joinMembership(IQueryBuilder $qb, string $userId) {
105
		$qb->from(CoreRequestBuilder::TABLE_MEMBERS, 'm');
106
107
		$expr = $qb->expr();
108
		$andX = $expr->andX();
109
110
		$andX->add($expr->eq('m.user_id', $qb->createNamedParameter($userId)));
111
		$andX->add($expr->eq('m.instance', $qb->createNamedParameter('')));
112
		$andX->add($expr->gt('m.level', $qb->createNamedParameter(0)));
113
		$andX->add($expr->eq('m.user_type', $qb->createNamedParameter(Member::TYPE_USER)));
114
		$andX->add($expr->eq('m.circle_id', 'gsh.circle_id'));
115
116
		$qb->andWhere($andX);
117
	}
118
119
}
120
121