From 09050966128380e7d429325c241c1ef95b0eb756 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20=C5=81ojewski?= Date: Sun, 28 Oct 2018 17:40:12 +0100 Subject: [PATCH] Issue#74 Case (in)sensitive login --- CHANGELOG.md | 1 + README.md | 1 + lib/Backend/UserBackend.php | 6 ++++-- lib/Constant/Opt.php | 1 + lib/Constant/Query.php | 1 + lib/Query/QueryProvider.php | 5 +++++ lib/Repository/UserRepository.php | 20 ++++++++++++++------ templates/admin.php | 3 ++- 8 files changed, 29 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ebbb5d9..5bf7521 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Whirlpool hash algorithm - 'Prepend salt' toggle - Drupal 7 hash algorithm +- Case-insensitive username option ### Fixed - Error when 'Display name' not set - Encoding of iteration for 'Extended DES (Crypt)' diff --git a/README.md b/README.md index f99d4fb..386e583 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ Name | Description | Details --- | --- | --- **Allow display name change** | With this option enabled user can change its display name. The display name change is propagated to the database. | Optional.
Default: false.
Requires: user *Display name* column. **Allow password change** | Can user change its password. The password change is propagated to the database. See [Hash algorithms](#hash-algorithms). | Optional.
Default: false. +**Case-insensitive username** | Whether user query should be case-sensitive or case-insensitive. | Optional.
Default: false. **Use cache** | Use database query results cache. The cache can be cleared any time with the *Clear cache* button click. | Optional.
Default: false. **Hash algorithm** | How users passwords are stored in the database. See [Hash algorithms](#hash-algorithms). | Mandatory. **Email sync** | Sync e-mail address with the Nextcloud.
- *None* - Disables this feature. This is the default option.
- *Synchronise only once* - Copy the e-mail address to the Nextcloud preferences if its not set.
- *Nextcloud always wins* - Always copy the e-mail address to the database. This updates the user table.
- *SQL always wins* - Always copy the e-mail address to the Nextcloud preferences. | Optional.
Default: *None*.
Requires: user *Email* column. diff --git a/lib/Backend/UserBackend.php b/lib/Backend/UserBackend.php index a5d3b3a..e26f708 100644 --- a/lib/Backend/UserBackend.php +++ b/lib/Backend/UserBackend.php @@ -292,11 +292,13 @@ final class UserBackend extends ABackend implements return false; } - $user = $this->userRepository->findByUid($uid); - if (!($user instanceof User)) { + $caseSensitive = empty($this->properties[Opt::CASE_INSENSITIVE_USERNAME]); + $user = $this->userRepository->findByUid($uid, $caseSensitive); + if (!($user instanceof User) || ($caseSensitive && $user->uid !== $uid)) { return false; } + $uid = $user->uid; $password = $this->addSalt($user, $password); $isCorrect = $passwordAlgorithm->checkPassword( diff --git a/lib/Constant/Opt.php b/lib/Constant/Opt.php index fe6449a..75b0da6 100644 --- a/lib/Constant/Opt.php +++ b/lib/Constant/Opt.php @@ -28,6 +28,7 @@ namespace OCA\UserSQL\Constant; */ final class Opt { + const CASE_INSENSITIVE_USERNAME = "opt.case_insensitive_username"; const CRYPTO_CLASS = "opt.crypto_class"; const EMAIL_SYNC = "opt.email_sync"; const HOME_LOCATION = "opt.home_location"; diff --git a/lib/Constant/Query.php b/lib/Constant/Query.php index 35eceb7..8cbff2c 100644 --- a/lib/Constant/Query.php +++ b/lib/Constant/Query.php @@ -35,6 +35,7 @@ final class Query const FIND_GROUP_USERS = "find_group_users"; const FIND_GROUPS = "find_groups"; const FIND_USER = "find_user"; + const FIND_USER_CASE_INSENSITIVE = "find_user_case_insensitive"; const FIND_USER_GROUPS = "find_user_groups"; const FIND_USERS = "find_users"; const UPDATE_DISPLAY_NAME = "update_display_name"; diff --git a/lib/Query/QueryProvider.php b/lib/Query/QueryProvider.php index 06cd02b..cd027d0 100644 --- a/lib/Query/QueryProvider.php +++ b/lib/Query/QueryProvider.php @@ -144,6 +144,11 @@ class QueryProvider implements \ArrayAccess "FROM $user " . "WHERE $uUID = :$uidParam", + Query::FIND_USER_CASE_INSENSITIVE => + "SELECT $userColumns, $uPassword AS password " . + "FROM $user " . + "WHERE lower($uUID) = lower(:$uidParam)", + Query::FIND_USER_GROUPS => "SELECT $groupColumns " . "FROM $group, $userGroup " . diff --git a/lib/Repository/UserRepository.php b/lib/Repository/UserRepository.php index 91438ea..9841cf0 100644 --- a/lib/Repository/UserRepository.php +++ b/lib/Repository/UserRepository.php @@ -53,18 +53,26 @@ class UserRepository } /** - * Get a user entity object. + * Get an user entity object. * - * @param string $uid The user ID. + * @param string $uid The user ID. + * @param bool $caseSensitive TRUE for case sensitive search, + * FALSE for case insensitive search. * * @return User The user entity, NULL if it does not exists or * FALSE on failure. */ - public function findByUid($uid) + public function findByUid($uid, $caseSensitive = true) { - return $this->dataQuery->queryEntity( - Query::FIND_USER, User::class, [Query::UID_PARAM => $uid] - ); + if ($caseSensitive) { + return $this->dataQuery->queryEntity( + Query::FIND_USER, User::class, [Query::UID_PARAM => $uid] + ); + } else { + return $this->dataQuery->queryEntity( + Query::FIND_USER_CASE_INSENSITIVE, User::class, [Query::UID_PARAM => $uid] + ); + } } /** diff --git a/templates/admin.php b/templates/admin.php index 64c4301..b722b8b 100644 --- a/templates/admin.php +++ b/templates/admin.php @@ -109,7 +109,8 @@ function print_select_options(

t("Here are all currently supported options.")); ?>

+ print_checkbox_input($l, "opt-password_change", "Allow password change", $_["opt.password_change"]); + print_checkbox_input($l, "opt-case_insensitive_username", "Case-insensitive username", $_["opt.case_insensitive_username"]); ?>
">