Skip to content

Commit 01a91f5

Browse files
committed
Adopt user backend to Nextcloud 14 interfaces
1 parent e4d962f commit 01a91f5

2 files changed

Lines changed: 59 additions & 30 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1313
### Changed
1414
- Support for Nextcloud 14 only
1515
- Group backend implementation
16+
- User backend implementation
1617

1718
### Fixed
1819
- Table and column autocomplete in settings panel

lib/Backend/UserBackend.php

Lines changed: 58 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
namespace OCA\UserSQL\Backend;
2323

24-
use OC\User\Backend;
2524
use OCA\UserSQL\Action\EmailSync;
2625
use OCA\UserSQL\Action\IUserAction;
2726
use OCA\UserSQL\Cache;
@@ -35,13 +34,28 @@
3534
use OCP\IConfig;
3635
use OCP\IL10N;
3736
use OCP\ILogger;
37+
use OCP\User\Backend\ABackend;
38+
use OCP\User\Backend\ICheckPasswordBackend;
39+
use OCP\User\Backend\ICountUsersBackend;
40+
use OCP\User\Backend\IGetDisplayNameBackend;
41+
use OCP\User\Backend\IGetHomeBackend;
42+
use OCP\User\Backend\IProvideAvatarBackend;
43+
use OCP\User\Backend\ISetDisplayNameBackend;
44+
use OCP\User\Backend\ISetPasswordBackend;
3845

3946
/**
4047
* The SQL user backend manager.
4148
*
4249
* @author Marcin Łojewski <dev@mlojewski.me>
4350
*/
44-
final class UserBackend extends Backend
51+
final class UserBackend extends ABackend implements
52+
ICheckPasswordBackend,
53+
ICountUsersBackend,
54+
IGetDisplayNameBackend,
55+
IGetHomeBackend,
56+
IProvideAvatarBackend,
57+
ISetDisplayNameBackend,
58+
ISetPasswordBackend
4559
{
4660
/**
4761
* @var string The application name.
@@ -228,7 +242,7 @@ private function getUser($uid)
228242
/**
229243
* @inheritdoc
230244
*/
231-
public function getDisplayName($uid)
245+
public function getDisplayName($uid): string
232246
{
233247
$this->logger->debug(
234248
"Entering getDisplayName($uid)", ["app" => $this->appName]
@@ -258,7 +272,7 @@ public function getDisplayName($uid)
258272
*
259273
* @return string|bool The user ID on success, false otherwise.
260274
*/
261-
public function checkPassword($uid, $password)
275+
public function checkPassword(string $uid, string $password)
262276
{
263277
$this->logger->debug(
264278
"Entering checkPassword($uid, *)", ["app" => $this->appName]
@@ -337,6 +351,10 @@ public function getDisplayNames($search = "", $limit = null, $offset = null)
337351
["app" => $this->appName]
338352
);
339353

354+
if (empty($this->properties[DB::USER_NAME_COLUMN])) {
355+
return false;
356+
}
357+
340358
$users = $this->getUsers($search, $limit, $offset);
341359

342360
$names = [];
@@ -410,12 +428,16 @@ function ($user) {
410428
*
411429
* @return bool TRUE if the password has been set, FALSE otherwise.
412430
*/
413-
public function setPassword($uid, $password)
431+
public function setPassword(string $uid, string $password): bool
414432
{
415433
$this->logger->debug(
416434
"Entering setPassword($uid, *)", ["app" => "user_sql"]
417435
);
418436

437+
if (empty($this->properties[Opt::PASSWORD_CHANGE])) {
438+
return false;
439+
}
440+
419441
$passwordAlgorithm = $this->getPasswordAlgorithm();
420442
if ($passwordAlgorithm === false) {
421443
return false;
@@ -452,12 +474,16 @@ public function setPassword($uid, $password)
452474
/**
453475
* @inheritdoc
454476
*/
455-
public function getHome($uid)
477+
public function getHome(string $uid)
456478
{
457479
$this->logger->debug(
458480
"Entering getHome($uid)", ["app" => $this->appName]
459481
);
460482

483+
if (empty($this->properties[Opt::HOME_MODE])) {
484+
return false;
485+
}
486+
461487
$home = false;
462488
switch ($this->properties[Opt::HOME_MODE]) {
463489
case App::HOME_STATIC:
@@ -487,12 +513,16 @@ public function getHome($uid)
487513
*
488514
* @return bool TRUE if the user can change its avatar, FALSE otherwise.
489515
*/
490-
public function canChangeAvatar($uid)
516+
public function canChangeAvatar(string $uid): bool
491517
{
492518
$this->logger->debug(
493519
"Entering canChangeAvatar($uid)", ["app" => $this->appName]
494520
);
495521

522+
if (empty($this->properties[DB::USER_AVATAR_COLUMN])) {
523+
return false;
524+
}
525+
496526
$user = $this->userRepository->findByUid($uid);
497527
if (!($user instanceof User)) {
498528
return false;
@@ -515,13 +545,17 @@ public function canChangeAvatar($uid)
515545
*
516546
* @return bool TRUE if the password has been set, FALSE otherwise.
517547
*/
518-
public function setDisplayName($uid, $displayName)
548+
public function setDisplayName(string $uid, string $displayName): bool
519549
{
520550
$this->logger->debug(
521551
"Entering setDisplayName($uid, $displayName)",
522552
["app" => $this->appName]
523553
);
524554

555+
if (empty($this->properties[Opt::NAME_CHANGE])) {
556+
return false;
557+
}
558+
525559
$user = $this->userRepository->findByUid($uid);
526560
if (!($user instanceof User)) {
527561
return false;
@@ -541,28 +575,6 @@ public function setDisplayName($uid, $displayName)
541575
return false;
542576
}
543577

544-
/**
545-
* @inheritdoc
546-
*/
547-
public function getSupportedActions()
548-
{
549-
$actions = parent::getSupportedActions();
550-
551-
$actions &= empty($this->properties[DB::USER_NAME_COLUMN])
552-
? ~Backend::GET_DISPLAYNAME : ~0;
553-
$actions &= empty($this->properties[Opt::HOME_MODE])
554-
? ~Backend::GET_HOME : ~0;
555-
$actions &= empty($this->properties[DB::USER_AVATAR_COLUMN])
556-
? ~Backend::PROVIDE_AVATAR : ~0;
557-
$actions &= (!empty($this->properties[DB::USER_NAME_COLUMN])
558-
&& $this->properties[Opt::NAME_CHANGE]) ? ~0
559-
: ~Backend::SET_DISPLAYNAME;
560-
$actions &= $this->properties[Opt::PASSWORD_CHANGE] ? ~0
561-
: ~Backend::SET_PASSWORD;
562-
563-
return $actions;
564-
}
565-
566578
/**
567579
* Check if this backend is correctly set and can be enabled.
568580
*
@@ -580,4 +592,20 @@ public function isConfigured()
580592
&& !empty($this->properties[DB::USER_PASSWORD_COLUMN])
581593
&& !empty($this->properties[Opt::CRYPTO_CLASS]);
582594
}
595+
596+
/**
597+
* @inheritdoc
598+
*/
599+
public function getBackendName()
600+
{
601+
return "User SQL";
602+
}
603+
604+
/**
605+
* @inheritdoc
606+
*/
607+
public function deleteUser($uid)
608+
{
609+
return false;
610+
}
583611
}

0 commit comments

Comments
 (0)