Skip to content

Commit 0905096

Browse files
committed
Issue#74 Case (in)sensitive login
1 parent 9ab6df0 commit 0905096

8 files changed

Lines changed: 29 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
99
- Whirlpool hash algorithm
1010
- 'Prepend salt' toggle
1111
- Drupal 7 hash algorithm
12+
- Case-insensitive username option
1213
### Fixed
1314
- Error when 'Display name' not set
1415
- Encoding of iteration for 'Extended DES (Crypt)'

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Name | Description | Details
4949
--- | --- | ---
5050
**Allow display name change** | With this option enabled user can change its display name. The display name change is propagated to the database. | Optional.<br/>Default: false.<br/>Requires: user *Display name* column.
5151
**Allow password change** | Can user change its password. The password change is propagated to the database. See [Hash algorithms](#hash-algorithms). | Optional.<br/>Default: false.
52+
**Case-insensitive username** | Whether user query should be case-sensitive or case-insensitive. | Optional.<br/>Default: false.
5253
**Use cache** | Use database query results cache. The cache can be cleared any time with the *Clear cache* button click. | Optional.<br/>Default: false.
5354
**Hash algorithm** | How users passwords are stored in the database. See [Hash algorithms](#hash-algorithms). | Mandatory.
5455
**Email sync** | Sync e-mail address with the Nextcloud.<br/>- *None* - Disables this feature. This is the default option.<br/>- *Synchronise only once* - Copy the e-mail address to the Nextcloud preferences if its not set.<br/>- *Nextcloud always wins* - Always copy the e-mail address to the database. This updates the user table.<br/>- *SQL always wins* - Always copy the e-mail address to the Nextcloud preferences. | Optional.<br/>Default: *None*.<br/>Requires: user *Email* column.

lib/Backend/UserBackend.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,11 +292,13 @@ public function checkPassword(string $uid, string $password)
292292
return false;
293293
}
294294

295-
$user = $this->userRepository->findByUid($uid);
296-
if (!($user instanceof User)) {
295+
$caseSensitive = empty($this->properties[Opt::CASE_INSENSITIVE_USERNAME]);
296+
$user = $this->userRepository->findByUid($uid, $caseSensitive);
297+
if (!($user instanceof User) || ($caseSensitive && $user->uid !== $uid)) {
297298
return false;
298299
}
299300

301+
$uid = $user->uid;
300302
$password = $this->addSalt($user, $password);
301303

302304
$isCorrect = $passwordAlgorithm->checkPassword(

lib/Constant/Opt.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
*/
2929
final class Opt
3030
{
31+
const CASE_INSENSITIVE_USERNAME = "opt.case_insensitive_username";
3132
const CRYPTO_CLASS = "opt.crypto_class";
3233
const EMAIL_SYNC = "opt.email_sync";
3334
const HOME_LOCATION = "opt.home_location";

lib/Constant/Query.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ final class Query
3535
const FIND_GROUP_USERS = "find_group_users";
3636
const FIND_GROUPS = "find_groups";
3737
const FIND_USER = "find_user";
38+
const FIND_USER_CASE_INSENSITIVE = "find_user_case_insensitive";
3839
const FIND_USER_GROUPS = "find_user_groups";
3940
const FIND_USERS = "find_users";
4041
const UPDATE_DISPLAY_NAME = "update_display_name";

lib/Query/QueryProvider.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ private function loadQueries()
144144
"FROM $user " .
145145
"WHERE $uUID = :$uidParam",
146146

147+
Query::FIND_USER_CASE_INSENSITIVE =>
148+
"SELECT $userColumns, $uPassword AS password " .
149+
"FROM $user " .
150+
"WHERE lower($uUID) = lower(:$uidParam)",
151+
147152
Query::FIND_USER_GROUPS =>
148153
"SELECT $groupColumns " .
149154
"FROM $group, $userGroup " .

lib/Repository/UserRepository.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,26 @@ public function __construct(DataQuery $dataQuery)
5353
}
5454

5555
/**
56-
* Get a user entity object.
56+
* Get an user entity object.
5757
*
58-
* @param string $uid The user ID.
58+
* @param string $uid The user ID.
59+
* @param bool $caseSensitive TRUE for case sensitive search,
60+
* FALSE for case insensitive search.
5961
*
6062
* @return User The user entity, NULL if it does not exists or
6163
* FALSE on failure.
6264
*/
63-
public function findByUid($uid)
65+
public function findByUid($uid, $caseSensitive = true)
6466
{
65-
return $this->dataQuery->queryEntity(
66-
Query::FIND_USER, User::class, [Query::UID_PARAM => $uid]
67-
);
67+
if ($caseSensitive) {
68+
return $this->dataQuery->queryEntity(
69+
Query::FIND_USER, User::class, [Query::UID_PARAM => $uid]
70+
);
71+
} else {
72+
return $this->dataQuery->queryEntity(
73+
Query::FIND_USER_CASE_INSENSITIVE, User::class, [Query::UID_PARAM => $uid]
74+
);
75+
}
6876
}
6977

7078
/**

templates/admin.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ function print_select_options(
109109
<p class="settings-hint"><?php p($l->t("Here are all currently supported options.")); ?></p>
110110
<fieldset><?php
111111
print_checkbox_input($l, "opt-name_change", "Allow display name change", $_["opt.name_change"]);
112-
print_checkbox_input($l, "opt-password_change", "Allow password change", $_["opt.password_change"]); ?>
112+
print_checkbox_input($l, "opt-password_change", "Allow password change", $_["opt.password_change"]);
113+
print_checkbox_input($l, "opt-case_insensitive_username", "Case-insensitive username", $_["opt.case_insensitive_username"]); ?>
113114
<div class="button-right"><?php
114115
print_checkbox_input($l, "opt-use_cache", "Use cache", $_["opt.use_cache"], false); ?>
115116
<input type="submit" id="user_sql-clear_cache" value="<?php p($l->t("Clear cache")); ?>">

0 commit comments

Comments
 (0)