Skip to content

Commit 44bfb12

Browse files
authored
Merge pull request #23709 from nextcloud/backport/23702/stable18
[stable18] fix sharer flag on ldap:show-remnants when user owned more than a single share
2 parents cd10235 + a5dbf38 commit 44bfb12

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
@@ -89,7 +89,6 @@ public function __construct($ocName, IConfig $config, IDBConnection $db, UserMap
8989
$this->config = $config;
9090
$this->db = $db;
9191
$this->mapping = $mapping;
92-
$this->fetchDetails();
9392
}
9493

9594
/**
@@ -131,6 +130,9 @@ public function getOCName() {
131130
* @return string
132131
*/
133132
public function getUID() {
133+
if (!isset($this->uid)) {
134+
$this->fetchDetails();
135+
}
134136
return $this->uid;
135137
}
136138

@@ -139,6 +141,9 @@ public function getUID() {
139141
* @return string
140142
*/
141143
public function getDN() {
144+
if (!isset($this->dn)) {
145+
$this->fetchDetails();
146+
}
142147
return $this->dn;
143148
}
144149

@@ -147,6 +152,9 @@ public function getDN() {
147152
* @return string
148153
*/
149154
public function getDisplayName() {
155+
if (!isset($this->displayName)) {
156+
$this->fetchDetails();
157+
}
150158
return $this->displayName;
151159
}
152160

@@ -155,6 +163,9 @@ public function getDisplayName() {
155163
* @return string
156164
*/
157165
public function getEmail() {
166+
if (!isset($this->email)) {
167+
$this->fetchDetails();
168+
}
158169
return $this->email;
159170
}
160171

@@ -163,6 +174,9 @@ public function getEmail() {
163174
* @return string
164175
*/
165176
public function getHomePath() {
177+
if (!isset($this->homePath)) {
178+
$this->fetchDetails();
179+
}
166180
return $this->homePath;
167181
}
168182

@@ -171,6 +185,9 @@ public function getHomePath() {
171185
* @return int
172186
*/
173187
public function getLastLogin() {
188+
if (!isset($this->lastLogin)) {
189+
$this->fetchDetails();
190+
}
174191
return (int)$this->lastLogin;
175192
}
176193

@@ -179,6 +196,9 @@ public function getLastLogin() {
179196
* @return int
180197
*/
181198
public function getDetectedOn() {
199+
if (!isset($this->foundDeleted)) {
200+
$this->fetchDetails();
201+
}
182202
return (int)$this->foundDeleted;
183203
}
184204

@@ -187,6 +207,9 @@ public function getDetectedOn() {
187207
* @return bool
188208
*/
189209
public function getHasActiveShares() {
210+
if (!isset($this->hasActiveShares)) {
211+
$this->fetchDetails();
212+
}
190213
return $this->hasActiveShares;
191214
}
192215

@@ -219,29 +242,22 @@ protected function fetchDetails() {
219242
*/
220243
protected function determineShares() {
221244
$query = $this->db->prepare('
222-
SELECT COUNT(`uid_owner`)
245+
SELECT `uid_owner`
223246
FROM `*PREFIX*share`
224247
WHERE `uid_owner` = ?
225248
', 1);
226249
$query->execute(array($this->ocName));
227-
$sResult = $query->fetchColumn(0);
228-
if((int)$sResult === 1) {
250+
if ($query->rowCount() > 0) {
229251
$this->hasActiveShares = true;
230252
return;
231253
}
232254

233255
$query = $this->db->prepare('
234-
SELECT COUNT(`owner`)
256+
SELECT `owner`
235257
FROM `*PREFIX*share_external`
236258
WHERE `owner` = ?
237259
', 1);
238260
$query->execute(array($this->ocName));
239-
$sResult = $query->fetchColumn(0);
240-
if((int)$sResult === 1) {
241-
$this->hasActiveShares = true;
242-
return;
243-
}
244-
245-
$this->hasActiveShares = false;
261+
$this->hasActiveShares = $query->rowCount() > 0;
246262
}
247263
}
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 <[email protected]>
6+
*
7+
* @author Arthur Schiwon <[email protected]>
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)