Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend interfaces #8935

Merged
merged 9 commits into from
Mar 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,15 @@
'OCP\\Template' => $baseDir . '/lib/public/Template.php',
'OCP\\User' => $baseDir . '/lib/public/User.php',
'OCP\\UserInterface' => $baseDir . '/lib/public/UserInterface.php',
'OCP\\User\\Backend\\ABackend' => $baseDir . '/lib/public/User/Backend/ABackend.php',
'OCP\\User\\Backend\\ICheckPasswordBackend' => $baseDir . '/lib/public/User/Backend/ICheckPasswordBackend.php',
'OCP\\User\\Backend\\ICountUsersBackend' => $baseDir . '/lib/public/User/Backend/ICountUsersBackend.php',
'OCP\\User\\Backend\\ICreateUserBackend' => $baseDir . '/lib/public/User/Backend/ICreateUserBackend.php',
'OCP\\User\\Backend\\IGetDisplayNameBackend' => $baseDir . '/lib/public/User/Backend/IGetDisplayNameBackend.php',
'OCP\\User\\Backend\\IGetHomeBackend' => $baseDir . '/lib/public/User/Backend/IGetHomeBackend.php',
'OCP\\User\\Backend\\IProvideAvatarBackend' => $baseDir . '/lib/public/User/Backend/IProvideAvatarBackend.php',
'OCP\\User\\Backend\\ISetDisplayNameBackend' => $baseDir . '/lib/public/User/Backend/ISetDisplayNameBackend.php',
'OCP\\User\\Backend\\ISetPasswordBackend' => $baseDir . '/lib/public/User/Backend/ISetPasswordBackend.php',
'OCP\\Util' => $baseDir . '/lib/public/Util.php',
'OCP\\WorkflowEngine\\ICheck' => $baseDir . '/lib/public/WorkflowEngine/ICheck.php',
'OCP\\WorkflowEngine\\IManager' => $baseDir . '/lib/public/WorkflowEngine/IManager.php',
Expand Down
9 changes: 9 additions & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,15 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OCP\\Template' => __DIR__ . '/../../..' . '/lib/public/Template.php',
'OCP\\User' => __DIR__ . '/../../..' . '/lib/public/User.php',
'OCP\\UserInterface' => __DIR__ . '/../../..' . '/lib/public/UserInterface.php',
'OCP\\User\\Backend\\ABackend' => __DIR__ . '/../../..' . '/lib/public/User/Backend/ABackend.php',
'OCP\\User\\Backend\\ICheckPasswordBackend' => __DIR__ . '/../../..' . '/lib/public/User/Backend/ICheckPasswordBackend.php',
'OCP\\User\\Backend\\ICountUsersBackend' => __DIR__ . '/../../..' . '/lib/public/User/Backend/ICountUsersBackend.php',
'OCP\\User\\Backend\\ICreateUserBackend' => __DIR__ . '/../../..' . '/lib/public/User/Backend/ICreateUserBackend.php',
'OCP\\User\\Backend\\IGetDisplayNameBackend' => __DIR__ . '/../../..' . '/lib/public/User/Backend/IGetDisplayNameBackend.php',
'OCP\\User\\Backend\\IGetHomeBackend' => __DIR__ . '/../../..' . '/lib/public/User/Backend/IGetHomeBackend.php',
'OCP\\User\\Backend\\IProvideAvatarBackend' => __DIR__ . '/../../..' . '/lib/public/User/Backend/IProvideAvatarBackend.php',
'OCP\\User\\Backend\\ISetDisplayNameBackend' => __DIR__ . '/../../..' . '/lib/public/User/Backend/ISetDisplayNameBackend.php',
'OCP\\User\\Backend\\ISetPasswordBackend' => __DIR__ . '/../../..' . '/lib/public/User/Backend/ISetPasswordBackend.php',
'OCP\\Util' => __DIR__ . '/../../..' . '/lib/public/Util.php',
'OCP\\WorkflowEngine\\ICheck' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/ICheck.php',
'OCP\\WorkflowEngine\\IManager' => __DIR__ . '/../../..' . '/lib/public/WorkflowEngine/IManager.php',
Expand Down
46 changes: 30 additions & 16 deletions lib/private/User/Database.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
Expand Down Expand Up @@ -57,16 +58,29 @@
namespace OC\User;

use OC\Cache\CappedMemoryCache;
use OC\DB\QueryBuilder\Literal;
use OCP\IUserBackend;
use OCP\User\Backend\ABackend;
use OCP\User\Backend\ICheckPasswordBackend;
use OCP\User\Backend\ICountUsersBackend;
use OCP\User\Backend\ICreateUserBackend;
use OCP\User\Backend\IGetDisplayNameBackend;
use OCP\User\Backend\IGetHomeBackend;
use OCP\User\Backend\ISetDisplayNameBackend;
use OCP\User\Backend\ISetPasswordBackend;
use OCP\Util;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\GenericEvent;

/**
* Class for user management in a SQL Database (e.g. MySQL, SQLite)
*/
class Database extends Backend implements IUserBackend {
class Database extends ABackend
implements ICreateUserBackend,
ISetPasswordBackend,
ISetDisplayNameBackend,
IGetDisplayNameBackend,
ICheckPasswordBackend,
IGetHomeBackend,
ICountUsersBackend {
/** @var CappedMemoryCache */
private $cache;

Expand All @@ -93,13 +107,13 @@ public function __construct($eventDispatcher = null) {
* Creates a new user. Basic checking of username is done in OC_User
* itself, not in its subclasses.
*/
public function createUser($uid, $password) {
public function createUser(string $uid, string $password): bool {
if (!$this->userExists($uid)) {
$event = new GenericEvent($password);
$this->eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event);
$query = \OC_DB::prepare('INSERT INTO `*PREFIX*users` ( `uid`, `password` ) VALUES( ?, ? )');
try {
$result = $query->execute(array($uid, \OC::$server->getHasher()->hash($password)));
$result = $query->execute([$uid, \OC::$server->getHasher()->hash($password)]);
} catch (\Exception $e) {
$result = false;
}
Expand All @@ -124,7 +138,7 @@ public function createUser($uid, $password) {
public function deleteUser($uid) {
// Delete user-group-relation
$query = \OC_DB::prepare('DELETE FROM `*PREFIX*users` WHERE `uid` = ?');
$result = $query->execute(array($uid));
$result = $query->execute([$uid]);

if (isset($this->cache[$uid])) {
unset($this->cache[$uid]);
Expand All @@ -142,12 +156,12 @@ public function deleteUser($uid) {
*
* Change the password of a user
*/
public function setPassword($uid, $password) {
public function setPassword(string $uid, string $password): bool {
if ($this->userExists($uid)) {
$event = new GenericEvent($password);
$this->eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event);
$query = \OC_DB::prepare('UPDATE `*PREFIX*users` SET `password` = ? WHERE `uid` = ?');
$result = $query->execute(array(\OC::$server->getHasher()->hash($password), $uid));
$result = $query->execute([\OC::$server->getHasher()->hash($password), $uid]);

return $result ? true : false;
}
Expand All @@ -164,10 +178,10 @@ public function setPassword($uid, $password) {
*
* Change the display name of a user
*/
public function setDisplayName($uid, $displayName) {
public function setDisplayName(string $uid, string $displayName): bool {
if ($this->userExists($uid)) {
$query = \OC_DB::prepare('UPDATE `*PREFIX*users` SET `displayname` = ? WHERE LOWER(`uid`) = LOWER(?)');
$query->execute(array($displayName, $uid));
$query->execute([$displayName, $uid]);
$this->cache[$uid]['displayname'] = $displayName;

return true;
Expand All @@ -182,7 +196,7 @@ public function setDisplayName($uid, $displayName) {
* @param string $uid user ID of the user
* @return string display name
*/
public function getDisplayName($uid) {
public function getDisplayName($uid): string {
$this->loadUser($uid);
return empty($this->cache[$uid]['displayname']) ? $uid : $this->cache[$uid]['displayname'];
}
Expand Down Expand Up @@ -235,9 +249,9 @@ public function getDisplayNames($search = '', $limit = null, $offset = null) {
* Check if the password is correct without logging in the user
* returns the user id or false
*/
public function checkPassword($uid, $password) {
public function checkPassword(string $uid, string $password) {
$query = \OC_DB::prepare('SELECT `uid`, `password` FROM `*PREFIX*users` WHERE LOWER(`uid`) = LOWER(?)');
$result = $query->execute(array($uid));
$result = $query->execute([$uid]);

$row = $result->fetchRow();
if ($row) {
Expand Down Expand Up @@ -271,7 +285,7 @@ private function loadUser($uid) {
}

$query = \OC_DB::prepare('SELECT `uid`, `displayname` FROM `*PREFIX*users` WHERE LOWER(`uid`) = LOWER(?)');
$result = $query->execute(array($uid));
$result = $query->execute([$uid]);

if ($result === false) {
Util::writeLog('core', \OC_DB::getErrorMessage(), Util::ERROR);
Expand Down Expand Up @@ -326,9 +340,9 @@ public function userExists($uid) {
* @param string $uid the username
* @return string|false
*/
public function getHome($uid) {
public function getHome(string $uid) {
if ($this->userExists($uid)) {
return \OC::$server->getConfig()->getSystemValue("datadirectory", \OC::$SERVERROOT . "/data") . '/' . $uid;
return \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/' . $uid;
}

return false;
Expand Down
72 changes: 72 additions & 0 deletions lib/public/User/Backend/ABackend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2018 Roeland Jago Douma <roeland@famdouma.nl>
*
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCP\User\Backend;

use OC\User\Backend;
use OCP\IUserBackend;
use OCP\UserInterface;

/**
* @since 14.0.0
*/
abstract class ABackend implements IUserBackend, UserInterface {

/**
* @deprecated 14.0.0
*
* @param int $actions The action to check for
* @return bool
*/
public function implementsActions($actions): bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason there is no int type hinted?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes because this inherits from the old interfaces. And there is no typehint there.
Adding the typehint will make any userbackend do 💥 (including LDAP).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay

$implements = 0;

if ($this instanceof ICreateUserBackend) {
$implements |= Backend::CREATE_USER;
}
if ($this instanceof ISetPasswordBackend) {
$implements |= Backend::SET_PASSWORD;
}
if ($this instanceof ICheckPasswordBackend) {
$implements |= Backend::CHECK_PASSWORD;
}
if ($this instanceof IGetHomeBackend) {
$implements |= Backend::GET_HOME;
}
if ($this instanceof IGetDisplayNameBackend) {
$implements |= Backend::GET_DISPLAYNAME;
}
if ($this instanceof ISetDisplayNameBackend) {
$implements |= Backend::SET_DISPLAYNAME;
}
if ($this instanceof IProvideAvatarBackend) {
$implements |= Backend::PROVIDE_AVATAR;
}
if ($this instanceof ICountUsersBackend) {
$implements |= Backend::COUNT_USERS;
}

return (bool)($actions & $implements);
}
}
39 changes: 39 additions & 0 deletions lib/public/User/Backend/ICheckPasswordBackend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2018 Roeland Jago Douma <roeland@famdouma.nl>
*
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCP\User\Backend;

/**
* @since 14.0.0
*/
interface ICheckPasswordBackend {
/**
* @since 14.0.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you document the return values and usage for all those methods as well? (just take the ones from the DatabaseBackend

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

*
* @param string $uid The username
* @param string $password The password
* @return string|bool The uid on success false on failure
*/
public function checkPassword(string $loginName, string $password);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to type hint the success case and expect an exception on failure?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it would. But lets do baby steps. First get this in and then have an issue/PR to discuss further steps and improvements. Because if we change all that we also have to change all the logic that is calling this etc.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay 👍

}
38 changes: 38 additions & 0 deletions lib/public/User/Backend/ICountUsersBackend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2018 Roeland Jago Douma <roeland@famdouma.nl>
*
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCP\User\Backend;

/**
* @since 14.0.0
*/
interface ICountUsersBackend {

/**
* @since 14.0.0
*
* @return int|bool The number of users on success false on failure
*/
public function countUsers();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same exception handling here.

}
40 changes: 40 additions & 0 deletions lib/public/User/Backend/ICreateUserBackend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2018 Roeland Jago Douma <roeland@famdouma.nl>
*
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCP\User\Backend;

/**
* @since 14.0.0
*/
interface ICreateUserBackend {

/**
* @since 14.0.0
*
* @param string $uid The username of the user to create
* @param string $password The password of the new user
* @return bool
*/
public function createUser(string $uid, string $password): bool;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expect it to succeed or to throw an exception. So there should not be any return value.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't change that without chaning the whole backend logic as well. It will happen but baby steps.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay.

}
39 changes: 39 additions & 0 deletions lib/public/User/Backend/IGetDisplayNameBackend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2018 Roeland Jago Douma <roeland@famdouma.nl>
*
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCP\User\Backend;

/**
* @since 14.0.0
*/
interface IGetDisplayNameBackend {

/**
* @since 14.0.0
*
* @param string $uid user ID of the user
* @return string display name
*/
public function getDisplayName($uid): string;
}
Loading