Skip to content

Commit 9007a8a

Browse files
authored
Merge pull request #23708 from nextcloud/backport/23702/stable19
[stable19] fix sharer flag on ldap:show-remnants when user owned more than a single share
2 parents 238435f + 9c5bd23 commit 9007a8a

File tree

2 files changed

+123
-12
lines changed

2 files changed

+123
-12
lines changed

apps/user_ldap/lib/User/OfflineUser.php

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ public function __construct($ocName, IConfig $config, IDBConnection $db, UserMap
9090
$this->config = $config;
9191
$this->db = $db;
9292
$this->mapping = $mapping;
93-
$this->fetchDetails();
9493
}
9594

9695
/**
@@ -132,6 +131,9 @@ public function getOCName() {
132131
* @return string
133132
*/
134133
public function getUID() {
134+
if (!isset($this->uid)) {
135+
$this->fetchDetails();
136+
}
135137
return $this->uid;
136138
}
137139

@@ -140,6 +142,9 @@ public function getUID() {
140142
* @return string
141143
*/
142144
public function getDN() {
145+
if (!isset($this->dn)) {
146+
$this->fetchDetails();
147+
}
143148
return $this->dn;
144149
}
145150

@@ -148,6 +153,9 @@ public function getDN() {
148153
* @return string
149154
*/
150155
public function getDisplayName() {
156+
if (!isset($this->displayName)) {
157+
$this->fetchDetails();
158+
}
151159
return $this->displayName;
152160
}
153161

@@ -156,6 +164,9 @@ public function getDisplayName() {
156164
* @return string
157165
*/
158166
public function getEmail() {
167+
if (!isset($this->email)) {
168+
$this->fetchDetails();
169+
}
159170
return $this->email;
160171
}
161172

@@ -164,6 +175,9 @@ public function getEmail() {
164175
* @return string
165176
*/
166177
public function getHomePath() {
178+
if (!isset($this->homePath)) {
179+
$this->fetchDetails();
180+
}
167181
return $this->homePath;
168182
}
169183

@@ -172,6 +186,9 @@ public function getHomePath() {
172186
* @return int
173187
*/
174188
public function getLastLogin() {
189+
if (!isset($this->lastLogin)) {
190+
$this->fetchDetails();
191+
}
175192
return (int)$this->lastLogin;
176193
}
177194

@@ -180,6 +197,9 @@ public function getLastLogin() {
180197
* @return int
181198
*/
182199
public function getDetectedOn() {
200+
if (!isset($this->foundDeleted)) {
201+
$this->fetchDetails();
202+
}
183203
return (int)$this->foundDeleted;
184204
}
185205

@@ -188,6 +208,9 @@ public function getDetectedOn() {
188208
* @return bool
189209
*/
190210
public function getHasActiveShares() {
211+
if (!isset($this->hasActiveShares)) {
212+
$this->fetchDetails();
213+
}
191214
return $this->hasActiveShares;
192215
}
193216

@@ -220,29 +243,22 @@ protected function fetchDetails() {
220243
*/
221244
protected function determineShares() {
222245
$query = $this->db->prepare('
223-
SELECT COUNT(`uid_owner`)
246+
SELECT `uid_owner`
224247
FROM `*PREFIX*share`
225248
WHERE `uid_owner` = ?
226249
', 1);
227250
$query->execute([$this->ocName]);
228-
$sResult = $query->fetchColumn(0);
229-
if ((int)$sResult === 1) {
251+
if ($query->rowCount() > 0) {
230252
$this->hasActiveShares = true;
231253
return;
232254
}
233255

234256
$query = $this->db->prepare('
235-
SELECT COUNT(`owner`)
257+
SELECT `owner`
236258
FROM `*PREFIX*share_external`
237259
WHERE `owner` = ?
238260
', 1);
239261
$query->execute([$this->ocName]);
240-
$sResult = $query->fetchColumn(0);
241-
if ((int)$sResult === 1) {
242-
$this->hasActiveShares = true;
243-
return;
244-
}
245-
246-
$this->hasActiveShares = false;
262+
$this->hasActiveShares = $query->rowCount() > 0;
247263
}
248264
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* @copyright Copyright (c) 2020 Arthur Schiwon <blizzz@arthur-schiwon.de>
6+
*
7+
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
8+
*
9+
* @license GNU AGPL version 3 or any later version
10+
*
11+
* This program is free software: you can redistribute it and/or modify
12+
* it under the terms of the GNU Affero General Public License as
13+
* published by the Free Software Foundation, either version 3 of the
14+
* License, or (at your option) any later version.
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Affero General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Affero General Public License
22+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23+
*
24+
*/
25+
26+
namespace OCA\User_LDAP\Tests\User;
27+
28+
use Doctrine\DBAL\Driver\Statement;
29+
use OCA\User_LDAP\Mapping\UserMapping;
30+
use OCA\User_LDAP\User\OfflineUser;
31+
use OCP\IConfig;
32+
use OCP\IDBConnection;
33+
use Test\TestCase;
34+
35+
class OfflineUserTest extends TestCase {
36+
37+
/** @var OfflineUser */
38+
protected $offlineUser;
39+
/** @var UserMapping|\PHPUnit\Framework\MockObject\MockObject */
40+
protected $mapping;
41+
/** @var string */
42+
protected $uid;
43+
/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
44+
protected $config;
45+
/** @var IDBConnection|\PHPUnit\Framework\MockObject\MockObject */
46+
protected $dbc;
47+
48+
public function setUp(): void {
49+
$this->uid = 'deborah';
50+
$this->config = $this->createMock(IConfig::class);
51+
$this->dbc = $this->createMock(IDBConnection::class);
52+
$this->mapping = $this->createMock(UserMapping::class);
53+
54+
$this->offlineUser = new OfflineUser(
55+
$this->uid,
56+
$this->config,
57+
$this->dbc,
58+
$this->mapping
59+
);
60+
}
61+
62+
public function shareOwnerProvider(): array {
63+
// tests for none, one, many
64+
return [
65+
[ 0, 0, false],
66+
[ 1, 0, true],
67+
[ 0, 1, true],
68+
[ 1, 1, true],
69+
[ 2, 0, true],
70+
[ 0, 2, true],
71+
[ 2, 2, true],
72+
];
73+
}
74+
75+
/**
76+
* @dataProvider shareOwnerProvider
77+
*/
78+
public function testHasActiveShares(int $internalOwnerships, int $externalOwnerships, bool $expected) {
79+
$queryMock = $this->createMock(Statement::class);
80+
$queryMock->expects($this->atLeastOnce())
81+
->method('execute');
82+
$queryMock->expects($this->atLeastOnce())
83+
->method('rowCount')
84+
->willReturnOnConsecutiveCalls(
85+
$internalOwnerships > 0 ? 1 : 0,
86+
$externalOwnerships > 0 ? 1 : 0
87+
);
88+
89+
$this->dbc->expects($this->atLeastOnce())
90+
->method('prepare')
91+
->willReturn($queryMock);
92+
93+
$this->assertSame($expected, $this->offlineUser->getHasActiveShares());
94+
}
95+
}

0 commit comments

Comments
 (0)