Skip to content

Commit 953dae2

Browse files
committed
issue#135 Allow email login
1 parent 94714ae commit 953dae2

9 files changed

Lines changed: 56 additions & 7 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
- Support for Nextcloud 19
1010
- Argon2id support
1111
- System wide values option
12+
- Allow email login option
1213

1314
## [4.4.1] - 2020-02-02
1415
### Fixed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Here are all currently supported options.
4949
Name | Description | Details
5050
--- | --- | ---
5151
**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.
52+
**Allow email login** | User input at login is considered to be either UID or email. | Optional.<br/>Default: *false*.<br/>Requires: user *Email* column.
5253
**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*.
5354
**Allow providing avatar** | Can user provide its avatar. The value is used when column *Provide avatar* is not set. | Optional.<br/>Default: *false*.
5455
**Case-insensitive username** | Whether user query should be case-sensitive or case-insensitive. | Optional.<br/>Default: *false*.

lib/Backend/UserBackend.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* Nextcloud - user_sql
44
*
5-
* @copyright 2018 Marcin Łojewski <dev@mlojewski.me>
5+
* @copyright 2020 Marcin Łojewski <dev@mlojewski.me>
66
* @author Marcin Łojewski <dev@mlojewski.me>
77
*
88
* This program is free software: you can redistribute it and/or modify
@@ -318,8 +318,14 @@ public function checkPassword(string $uid, string $password)
318318
}
319319

320320
$caseSensitive = empty($this->properties[Opt::CASE_INSENSITIVE_USERNAME]);
321-
$user = $this->userRepository->findByUid($uid, $caseSensitive);
322-
if (!($user instanceof User) || ($caseSensitive && $user->uid !== $uid)) {
321+
$emailLogin = !empty($this->properties[Opt::EMAIL_LOGIN]);
322+
if ($emailLogin) {
323+
$user = $this->userRepository->findByUidOrEmail($uid, $caseSensitive);
324+
} else {
325+
$user = $this->userRepository->findByUid($uid, $caseSensitive);
326+
}
327+
328+
if (!($user instanceof User)) {
323329
return false;
324330
}
325331

lib/Constant/Opt.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ final class Opt
3434
const CRYPTO_PARAM_0 = "opt.crypto_param_0";
3535
const CRYPTO_PARAM_1 = "opt.crypto_param_1";
3636
const CRYPTO_PARAM_2 = "opt.crypto_param_2";
37+
const EMAIL_LOGIN = "opt.email_login";
3738
const EMAIL_SYNC = "opt.email_sync";
3839
const HOME_LOCATION = "opt.home_location";
3940
const HOME_MODE = "opt.home_mode";

lib/Constant/Query.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* Nextcloud - user_sql
44
*
5-
* @copyright 2018 Marcin Łojewski <dev@mlojewski.me>
5+
* @copyright 2020 Marcin Łojewski <dev@mlojewski.me>
66
* @author Marcin Łojewski <dev@mlojewski.me>
77
*
88
* This program is free software: you can redistribute it and/or modify
@@ -35,6 +35,8 @@ 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_BY_UID_OR_EMAIL = "find_user_by_uid_or_email";
39+
const FIND_USER_BY_UID_OR_EMAIL_CASE_INSENSITIVE = "find_user_by_uid_or_email_case_insensitive";
3840
const FIND_USER_CASE_INSENSITIVE = "find_user_case_insensitive";
3941
const FIND_USER_GROUPS = "find_user_groups";
4042
const FIND_USERS = "find_users";

lib/Properties.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ private function isBooleanParam($param)
174174
{
175175
return in_array(
176176
$param, [
177-
Opt::APPEND_SALT, Opt::CASE_INSENSITIVE_USERNAME,
177+
Opt::APPEND_SALT, Opt::CASE_INSENSITIVE_USERNAME, Opt::EMAIL_LOGIN,
178178
Opt::NAME_CHANGE, Opt::PASSWORD_CHANGE, Opt::PREPEND_SALT,
179179
Opt::PROVIDE_AVATAR, Opt::REVERSE_ACTIVE, Opt::SAFE_STORE,
180180
Opt::USE_CACHE

lib/Query/QueryProvider.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* Nextcloud - user_sql
44
*
5-
* @copyright 2018 Marcin Łojewski <dev@mlojewski.me>
5+
* @copyright 2020 Marcin Łojewski <dev@mlojewski.me>
66
* @author Marcin Łojewski <dev@mlojewski.me>
77
*
88
* This program is free software: you can redistribute it and/or modify
@@ -151,6 +151,18 @@ private function loadQueries()
151151
"WHERE u.$uUID = :$uidParam " .
152152
(empty($uDisabled) ? "" : "AND NOT u.$uDisabled"),
153153

154+
Query::FIND_USER_BY_UID_OR_EMAIL =>
155+
"SELECT $userColumns, u.$uPassword AS password " .
156+
"FROM $user u " .
157+
"WHERE u.$uUID = :$uidParam OR u.$uEmail = :$emailParam " .
158+
(empty($uDisabled) ? "" : "AND NOT u.$uDisabled"),
159+
160+
Query::FIND_USER_BY_UID_OR_EMAIL_CASE_INSENSITIVE =>
161+
"SELECT $userColumns, u.$uPassword AS password " .
162+
"FROM $user u " .
163+
"WHERE lower(u.$uUID) = lower(:$uidParam) OR lower(u.$uEmail) = lower(:$emailParam) " .
164+
(empty($uDisabled) ? "" : "AND NOT u.$uDisabled"),
165+
154166
Query::FIND_USER_CASE_INSENSITIVE =>
155167
"SELECT $userColumns, u.$uPassword AS password " .
156168
"FROM $user u " .

lib/Repository/UserRepository.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* Nextcloud - user_sql
44
*
5-
* @copyright 2018 Marcin Łojewski <dev@mlojewski.me>
5+
* @copyright 2020 Marcin Łojewski <dev@mlojewski.me>
66
* @author Marcin Łojewski <dev@mlojewski.me>
77
*
88
* This program is free software: you can redistribute it and/or modify
@@ -75,6 +75,31 @@ public function findByUid($uid, $caseSensitive = true)
7575
}
7676
}
7777

78+
/**
79+
* Get an user entity object.
80+
*
81+
* @param string $query The user ID or email address.
82+
* @param bool $caseSensitive TRUE for case sensitive search,
83+
* FALSE for case insensitive search.
84+
*
85+
* @return User The user entity, NULL if it does not exists or
86+
* FALSE on failure.
87+
*/
88+
public function findByUidOrEmail($query, $caseSensitive = true)
89+
{
90+
if ($caseSensitive) {
91+
return $this->dataQuery->queryEntity(
92+
Query::FIND_USER_BY_UID_OR_EMAIL, User::class,
93+
[Query::UID_PARAM => $query, Query::EMAIL_PARAM => $query]
94+
);
95+
} else {
96+
return $this->dataQuery->queryEntity(
97+
Query::FIND_USER_BY_UID_OR_EMAIL_CASE_INSENSITIVE, User::class,
98+
[Query::UID_PARAM => $query, Query::EMAIL_PARAM => $query]
99+
);
100+
}
101+
}
102+
78103
/**
79104
* Get an array of user entity objects.
80105
*

templates/admin.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ function print_select_options(
110110
<p class="settings-hint"><?php p($l->t("Here are all currently supported options.")); ?></p>
111111
<fieldset><?php
112112
print_checkbox_input($l, "opt-name_change", "Allow display name change", $_["opt.name_change"]);
113+
print_checkbox_input($l, "opt-email_login", "Allow email login", $_["opt.email_login"]);
113114
print_checkbox_input($l, "opt-password_change", "Allow password change", $_["opt.password_change"]);
114115
print_checkbox_input($l, "opt-provide_avatar", "Allow providing avatar", $_["opt.provide_avatar"]);
115116
print_checkbox_input($l, "opt-case_insensitive_username", "Case-insensitive username", $_["opt.case_insensitive_username"]);

0 commit comments

Comments
 (0)