Adopt user backend to Nextcloud 14 interfaces

This commit is contained in:
Marcin Łojewski
2018-06-30 23:20:46 +02:00
parent e4d962f139
commit 01a91f54ce
2 changed files with 59 additions and 30 deletions

View File

@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Changed ### Changed
- Support for Nextcloud 14 only - Support for Nextcloud 14 only
- Group backend implementation - Group backend implementation
- User backend implementation
### Fixed ### Fixed
- Table and column autocomplete in settings panel - Table and column autocomplete in settings panel

View File

@@ -21,7 +21,6 @@
namespace OCA\UserSQL\Backend; namespace OCA\UserSQL\Backend;
use OC\User\Backend;
use OCA\UserSQL\Action\EmailSync; use OCA\UserSQL\Action\EmailSync;
use OCA\UserSQL\Action\IUserAction; use OCA\UserSQL\Action\IUserAction;
use OCA\UserSQL\Cache; use OCA\UserSQL\Cache;
@@ -35,13 +34,28 @@ use OCA\UserSQL\Repository\UserRepository;
use OCP\IConfig; use OCP\IConfig;
use OCP\IL10N; use OCP\IL10N;
use OCP\ILogger; use OCP\ILogger;
use OCP\User\Backend\ABackend;
use OCP\User\Backend\ICheckPasswordBackend;
use OCP\User\Backend\ICountUsersBackend;
use OCP\User\Backend\IGetDisplayNameBackend;
use OCP\User\Backend\IGetHomeBackend;
use OCP\User\Backend\IProvideAvatarBackend;
use OCP\User\Backend\ISetDisplayNameBackend;
use OCP\User\Backend\ISetPasswordBackend;
/** /**
* The SQL user backend manager. * The SQL user backend manager.
* *
* @author Marcin Łojewski <dev@mlojewski.me> * @author Marcin Łojewski <dev@mlojewski.me>
*/ */
final class UserBackend extends Backend final class UserBackend extends ABackend implements
ICheckPasswordBackend,
ICountUsersBackend,
IGetDisplayNameBackend,
IGetHomeBackend,
IProvideAvatarBackend,
ISetDisplayNameBackend,
ISetPasswordBackend
{ {
/** /**
* @var string The application name. * @var string The application name.
@@ -228,7 +242,7 @@ final class UserBackend extends Backend
/** /**
* @inheritdoc * @inheritdoc
*/ */
public function getDisplayName($uid) public function getDisplayName($uid): string
{ {
$this->logger->debug( $this->logger->debug(
"Entering getDisplayName($uid)", ["app" => $this->appName] "Entering getDisplayName($uid)", ["app" => $this->appName]
@@ -258,7 +272,7 @@ final class UserBackend extends Backend
* *
* @return string|bool The user ID on success, false otherwise. * @return string|bool The user ID on success, false otherwise.
*/ */
public function checkPassword($uid, $password) public function checkPassword(string $uid, string $password)
{ {
$this->logger->debug( $this->logger->debug(
"Entering checkPassword($uid, *)", ["app" => $this->appName] "Entering checkPassword($uid, *)", ["app" => $this->appName]
@@ -337,6 +351,10 @@ final class UserBackend extends Backend
["app" => $this->appName] ["app" => $this->appName]
); );
if (empty($this->properties[DB::USER_NAME_COLUMN])) {
return false;
}
$users = $this->getUsers($search, $limit, $offset); $users = $this->getUsers($search, $limit, $offset);
$names = []; $names = [];
@@ -410,12 +428,16 @@ final class UserBackend extends Backend
* *
* @return bool TRUE if the password has been set, FALSE otherwise. * @return bool TRUE if the password has been set, FALSE otherwise.
*/ */
public function setPassword($uid, $password) public function setPassword(string $uid, string $password): bool
{ {
$this->logger->debug( $this->logger->debug(
"Entering setPassword($uid, *)", ["app" => "user_sql"] "Entering setPassword($uid, *)", ["app" => "user_sql"]
); );
if (empty($this->properties[Opt::PASSWORD_CHANGE])) {
return false;
}
$passwordAlgorithm = $this->getPasswordAlgorithm(); $passwordAlgorithm = $this->getPasswordAlgorithm();
if ($passwordAlgorithm === false) { if ($passwordAlgorithm === false) {
return false; return false;
@@ -452,12 +474,16 @@ final class UserBackend extends Backend
/** /**
* @inheritdoc * @inheritdoc
*/ */
public function getHome($uid) public function getHome(string $uid)
{ {
$this->logger->debug( $this->logger->debug(
"Entering getHome($uid)", ["app" => $this->appName] "Entering getHome($uid)", ["app" => $this->appName]
); );
if (empty($this->properties[Opt::HOME_MODE])) {
return false;
}
$home = false; $home = false;
switch ($this->properties[Opt::HOME_MODE]) { switch ($this->properties[Opt::HOME_MODE]) {
case App::HOME_STATIC: case App::HOME_STATIC:
@@ -487,12 +513,16 @@ final class UserBackend extends Backend
* *
* @return bool TRUE if the user can change its avatar, FALSE otherwise. * @return bool TRUE if the user can change its avatar, FALSE otherwise.
*/ */
public function canChangeAvatar($uid) public function canChangeAvatar(string $uid): bool
{ {
$this->logger->debug( $this->logger->debug(
"Entering canChangeAvatar($uid)", ["app" => $this->appName] "Entering canChangeAvatar($uid)", ["app" => $this->appName]
); );
if (empty($this->properties[DB::USER_AVATAR_COLUMN])) {
return false;
}
$user = $this->userRepository->findByUid($uid); $user = $this->userRepository->findByUid($uid);
if (!($user instanceof User)) { if (!($user instanceof User)) {
return false; return false;
@@ -515,13 +545,17 @@ final class UserBackend extends Backend
* *
* @return bool TRUE if the password has been set, FALSE otherwise. * @return bool TRUE if the password has been set, FALSE otherwise.
*/ */
public function setDisplayName($uid, $displayName) public function setDisplayName(string $uid, string $displayName): bool
{ {
$this->logger->debug( $this->logger->debug(
"Entering setDisplayName($uid, $displayName)", "Entering setDisplayName($uid, $displayName)",
["app" => $this->appName] ["app" => $this->appName]
); );
if (empty($this->properties[Opt::NAME_CHANGE])) {
return false;
}
$user = $this->userRepository->findByUid($uid); $user = $this->userRepository->findByUid($uid);
if (!($user instanceof User)) { if (!($user instanceof User)) {
return false; return false;
@@ -541,28 +575,6 @@ final class UserBackend extends Backend
return false; return false;
} }
/**
* @inheritdoc
*/
public function getSupportedActions()
{
$actions = parent::getSupportedActions();
$actions &= empty($this->properties[DB::USER_NAME_COLUMN])
? ~Backend::GET_DISPLAYNAME : ~0;
$actions &= empty($this->properties[Opt::HOME_MODE])
? ~Backend::GET_HOME : ~0;
$actions &= empty($this->properties[DB::USER_AVATAR_COLUMN])
? ~Backend::PROVIDE_AVATAR : ~0;
$actions &= (!empty($this->properties[DB::USER_NAME_COLUMN])
&& $this->properties[Opt::NAME_CHANGE]) ? ~0
: ~Backend::SET_DISPLAYNAME;
$actions &= $this->properties[Opt::PASSWORD_CHANGE] ? ~0
: ~Backend::SET_PASSWORD;
return $actions;
}
/** /**
* Check if this backend is correctly set and can be enabled. * Check if this backend is correctly set and can be enabled.
* *
@@ -580,4 +592,20 @@ final class UserBackend extends Backend
&& !empty($this->properties[DB::USER_PASSWORD_COLUMN]) && !empty($this->properties[DB::USER_PASSWORD_COLUMN])
&& !empty($this->properties[Opt::CRYPTO_CLASS]); && !empty($this->properties[Opt::CRYPTO_CLASS]);
} }
/**
* @inheritdoc
*/
public function getBackendName()
{
return "User SQL";
}
/**
* @inheritdoc
*/
public function deleteUser($uid)
{
return false;
}
} }