Skip to content

Commit 059d923

Browse files
committed
Merge branch 'palmtown-master' into develop
2 parents 953dae2 + c7e73f2 commit 059d923

10 files changed

Lines changed: 75 additions & 40 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1010
- Argon2id support
1111
- System wide values option
1212
- Allow email login option
13+
- UID user table column
1314

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

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ The definition of user table. The table containing user accounts.
6969
Name | Description | Details
7070
--- | --- | ---
7171
**Table name** | The table name. | Mandatory for user backend.
72-
**Username** | Username column. | Mandatory for user backend.
72+
**UID** | UID column. | Mandatory for user backend.
73+
**Username** | Username column. | Optional.
7374
**Email** | E-mail column. | Mandatory for *Email sync* option.
7475
**Quota** | Quota column. | Mandatory for *Quota sync* option.
7576
**Home** | Home path column. | Mandatory for `Query` *Home sync* option.
@@ -119,7 +120,8 @@ If you don't have any database model yet you can use below tables (MySQL):
119120
```
120121
CREATE TABLE sql_user
121122
(
122-
username VARCHAR(16) PRIMARY KEY,
123+
uid INT PRIMARY KEY AUTO_INCREMENT,
124+
username VARCHAR(16) NOT NULL UNIQUE,
123125
display_name TEXT NULL,
124126
email TEXT NULL,
125127
quota TEXT NULL,
@@ -244,3 +246,4 @@ Since version 4.0.0 the whole core implementation has been rewritten.
244246
* Andreas Boehler for releasing the first version of this application
245247
* Johan Hendriks provided his user_postfixadmin
246248
* Ed Wildgoose for fixing possible SQL injection vulnerability
249+
* Brandon Lee for implementing feature to separate uid from username resolving issues #108 & #121

js/settings.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ user_sql.adminSettingsUI = function () {
119119
);
120120

121121
autocomplete(
122-
"#db-table-user-column-uid, #db-table-user-column-email, #db-table-user-column-quota, #db-table-user-column-home, #db-table-user-column-password, #db-table-user-column-name, #db-table-user-column-active, #db-table-user-column-disabled, #db-table-user-column-avatar, #db-table-user-column-salt",
122+
"#db-table-user-column-uid, #db-table-user-column-username, #db-table-user-column-email, #db-table-user-column-quota, #db-table-user-column-home, #db-table-user-column-password, #db-table-user-column-name, #db-table-user-column-active, #db-table-user-column-disabled, #db-table-user-column-avatar, #db-table-user-column-salt",
123123
"/apps/user_sql/settings/autocomplete/table/user"
124124
);
125125

lib/Backend/UserBackend.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -301,15 +301,15 @@ public function getDisplayName($uid): string
301301
* Check if the user's password is correct then return its ID or
302302
* FALSE on failure.
303303
*
304-
* @param string $uid The user ID.
304+
* @param string $username The username.
305305
* @param string $password The password.
306306
*
307307
* @return string|bool The user ID on success, false otherwise.
308308
*/
309-
public function checkPassword(string $uid, string $password)
309+
public function checkPassword(string $username, string $password)
310310
{
311311
$this->logger->debug(
312-
"Entering checkPassword($uid, *)", ["app" => $this->appName]
312+
"Entering checkPassword($username, *)", ["app" => $this->appName]
313313
);
314314

315315
$passwordAlgorithm = $this->getPasswordAlgorithm();
@@ -320,9 +320,9 @@ public function checkPassword(string $uid, string $password)
320320
$caseSensitive = empty($this->properties[Opt::CASE_INSENSITIVE_USERNAME]);
321321
$emailLogin = !empty($this->properties[Opt::EMAIL_LOGIN]);
322322
if ($emailLogin) {
323-
$user = $this->userRepository->findByUidOrEmail($uid, $caseSensitive);
323+
$user = $this->userRepository->findByUsernameOrEmail($username, $caseSensitive);
324324
} else {
325-
$user = $this->userRepository->findByUid($uid, $caseSensitive);
325+
$user = $this->userRepository->findByUsername($username, $caseSensitive);
326326
}
327327

328328
if (!($user instanceof User)) {

lib/Constant/DB.php

Lines changed: 2 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
@@ -55,4 +55,5 @@ final class DB
5555
const USER_QUOTA_COLUMN = "db.table.user.column.quota";
5656
const USER_SALT_COLUMN = "db.table.user.column.salt";
5757
const USER_UID_COLUMN = "db.table.user.column.uid";
58+
const USER_USERNAME_COLUMN = "db.table.user.column.username";
5859
}

lib/Constant/Query.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ final class Query
3434
const FIND_GROUP = "find_group";
3535
const FIND_GROUP_USERS = "find_group_users";
3636
const FIND_GROUPS = "find_groups";
37-
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";
40-
const FIND_USER_CASE_INSENSITIVE = "find_user_case_insensitive";
37+
const FIND_USER_BY_UID = "find_user_by_uid";
38+
const FIND_USER_BY_USERNAME = "find_user_by_username";
39+
const FIND_USER_BY_USERNAME_CASE_INSENSITIVE = "find_user_by_username_case_insensitive";
40+
const FIND_USER_BY_USERNAME_OR_EMAIL = "find_user_by_username_or_email";
41+
const FIND_USER_BY_USERNAME_OR_EMAIL_CASE_INSENSITIVE = "find_user_by_username_or_email_case_insensitive";
4142
const FIND_USER_GROUPS = "find_user_groups";
4243
const FIND_USERS = "find_users";
4344
const UPDATE_DISPLAY_NAME = "update_display_name";
@@ -52,4 +53,5 @@ final class Query
5253
const QUOTA_PARAM = "quota";
5354
const SEARCH_PARAM = "search";
5455
const UID_PARAM = "uid";
56+
const USERNAME_PARAM = "username";
5557
}

lib/Model/User.php

Lines changed: 6 additions & 2 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
@@ -29,9 +29,13 @@
2929
class User
3030
{
3131
/**
32-
* @var string The UID (username).
32+
* @var mixed The UID.
3333
*/
3434
public $uid;
35+
/**
36+
* @var string The user's username (login name).
37+
*/
38+
public $username;
3539
/**
3640
* @var string The user's email address.
3741
*/

lib/Query/QueryProvider.php

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,19 @@ private function loadQueries()
6464

6565
$gAdmin = $this->properties[DB::GROUP_ADMIN_COLUMN];
6666
$gGID = $this->properties[DB::GROUP_GID_COLUMN];
67-
$gName = $this->properties[DB::GROUP_NAME_COLUMN];
67+
$gName = $this->properties[DB::GROUP_NAME_COLUMN] || $this->properties[DB::GROUP_GID_COLUMN];
6868

6969
$uActive = $this->properties[DB::USER_ACTIVE_COLUMN];
7070
$uAvatar = $this->properties[DB::USER_AVATAR_COLUMN];
7171
$uDisabled = $this->properties[DB::USER_DISABLED_COLUMN];
7272
$uEmail = $this->properties[DB::USER_EMAIL_COLUMN];
7373
$uHome = $this->properties[DB::USER_HOME_COLUMN];
74-
$uName = $this->properties[DB::USER_NAME_COLUMN];
74+
$uName = $this->properties[DB::USER_NAME_COLUMN] || $this->properties[DB::USER_USERNAME_COLUMN] || $this->properties[DB::USER_UID_COLUMN];
7575
$uPassword = $this->properties[DB::USER_PASSWORD_COLUMN];
7676
$uQuota = $this->properties[DB::USER_QUOTA_COLUMN];
7777
$uSalt = $this->properties[DB::USER_SALT_COLUMN];
7878
$uUID = $this->properties[DB::USER_UID_COLUMN];
79+
$uUsername = $this->properties[DB::USER_USERNAME_COLUMN] || $this->properties[DB::USER_UID_COLUMN];
7980

8081
$ugGID = $this->properties[DB::USER_GROUP_GID_COLUMN];
8182
$ugUID = $this->properties[DB::USER_GROUP_UID_COLUMN];
@@ -87,16 +88,18 @@ private function loadQueries()
8788
$quotaParam = Query::QUOTA_PARAM;
8889
$searchParam = Query::SEARCH_PARAM;
8990
$uidParam = Query::UID_PARAM;
91+
$usernameParam = Query::USERNAME_PARAM;
9092

9193
$reverseActiveOpt = $this->properties[Opt::REVERSE_ACTIVE];
9294

9395
$groupColumns
9496
= "g.$gGID AS gid, " .
95-
(empty($gName) ? "g." . $gGID : "g." . $gName) . " AS name, " .
97+
"g.$gName AS name, " .
9698
(empty($gAdmin) ? "false" : "g." . $gAdmin) . " AS admin";
9799
$userColumns
98100
= "u.$uUID AS uid, " .
99-
(empty($uName) ? "u." . $uUID : "u." . $uName) . " AS name, " .
101+
"u.$uUsername AS username, " .
102+
"u.$uName AS name, " .
100103
(empty($uEmail) ? "null" : "u." . $uEmail) . " AS email, " .
101104
(empty($uQuota) ? "null" : "u." . $uQuota) . " AS quota, " .
102105
(empty($uHome) ? "null" : "u." . $uHome) . " AS home, " .
@@ -134,8 +137,7 @@ private function loadQueries()
134137
"SELECT ug.$ugUID AS uid " .
135138
"FROM $userGroup ug " .
136139
"WHERE ug.$ugGID = :$gidParam " .
137-
"AND ug.$ugUID " .
138-
"LIKE :$searchParam " .
140+
"AND ug.$ugUID LIKE :$searchParam " .
139141
"ORDER BY ug.$ugUID",
140142

141143
Query::FIND_GROUPS =>
@@ -145,28 +147,34 @@ private function loadQueries()
145147
(empty($gName) ? "" : "OR g.$gName LIKE :$searchParam ") .
146148
"ORDER BY g.$gGID",
147149

148-
Query::FIND_USER =>
149-
"SELECT $userColumns, u.$uPassword AS password " .
150+
Query::FIND_USER_BY_UID =>
151+
"SELECT $userColumns " .
150152
"FROM $user u " .
151153
"WHERE u.$uUID = :$uidParam " .
152154
(empty($uDisabled) ? "" : "AND NOT u.$uDisabled"),
153155

154-
Query::FIND_USER_BY_UID_OR_EMAIL =>
156+
Query::FIND_USER_BY_USERNAME =>
157+
"SELECT $userColumns, u.$uPassword AS password " .
158+
"FROM $user u " .
159+
"WHERE u.$uUsername = :$usernameParam " .
160+
(empty($uDisabled) ? "" : "AND NOT u.$uDisabled"),
161+
162+
Query::FIND_USER_BY_USERNAME_CASE_INSENSITIVE =>
155163
"SELECT $userColumns, u.$uPassword AS password " .
156164
"FROM $user u " .
157-
"WHERE u.$uUID = :$uidParam OR u.$uEmail = :$emailParam " .
165+
"WHERE lower(u.$uUsername) = lower(:$usernameParam) " .
158166
(empty($uDisabled) ? "" : "AND NOT u.$uDisabled"),
159167

160-
Query::FIND_USER_BY_UID_OR_EMAIL_CASE_INSENSITIVE =>
168+
Query::FIND_USER_BY_USERNAME_OR_EMAIL =>
161169
"SELECT $userColumns, u.$uPassword AS password " .
162170
"FROM $user u " .
163-
"WHERE lower(u.$uUID) = lower(:$uidParam) OR lower(u.$uEmail) = lower(:$emailParam) " .
171+
"WHERE u.$uUsername = :$usernameParam OR u.$uEmail = :$emailParam " .
164172
(empty($uDisabled) ? "" : "AND NOT u.$uDisabled"),
165173

166-
Query::FIND_USER_CASE_INSENSITIVE =>
174+
Query::FIND_USER_BY_USERNAME_OR_EMAIL_CASE_INSENSITIVE =>
167175
"SELECT $userColumns, u.$uPassword AS password " .
168176
"FROM $user u " .
169-
"WHERE lower(u.$uUID) = lower(:$uidParam) " .
177+
"WHERE lower(u.$uUsername) = lower(:$usernameParam) OR lower(u.$uEmail) = lower(:$emailParam) " .
170178
(empty($uDisabled) ? "" : "AND NOT u.$uDisabled"),
171179

172180
Query::FIND_USER_GROUPS =>

lib/Repository/UserRepository.php

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,47 +55,62 @@ public function __construct(DataQuery $dataQuery)
5555
/**
5656
* Get an user entity object.
5757
*
58-
* @param string $uid The user ID.
58+
* @param mixed $uid The user ID.
59+
*
60+
* @return User The user entity, NULL if it does not exists or
61+
* FALSE on failure.
62+
*/
63+
public function findByUid($uid)
64+
{
65+
return $this->dataQuery->queryEntity(
66+
Query::FIND_USER_BY_UID, User::class, [Query::UID_PARAM => $uid]
67+
);
68+
}
69+
70+
/**
71+
* Get an user entity object.
72+
*
73+
* @param string $username The username.
5974
* @param bool $caseSensitive TRUE for case sensitive search,
6075
* FALSE for case insensitive search.
6176
*
6277
* @return User The user entity, NULL if it does not exists or
6378
* FALSE on failure.
6479
*/
65-
public function findByUid($uid, $caseSensitive = true)
80+
public function findByUsername($username, $caseSensitive = true)
6681
{
6782
if ($caseSensitive) {
6883
return $this->dataQuery->queryEntity(
69-
Query::FIND_USER, User::class, [Query::UID_PARAM => $uid]
84+
Query::FIND_USER_BY_USERNAME, User::class, [Query::USERNAME_PARAM => $username]
7085
);
7186
} else {
7287
return $this->dataQuery->queryEntity(
73-
Query::FIND_USER_CASE_INSENSITIVE, User::class, [Query::UID_PARAM => $uid]
88+
Query::FIND_USER_BY_USERNAME_CASE_INSENSITIVE, User::class, [Query::USERNAME_PARAM => $username]
7489
);
7590
}
7691
}
7792

7893
/**
7994
* Get an user entity object.
8095
*
81-
* @param string $query The user ID or email address.
96+
* @param string $query The username or email address.
8297
* @param bool $caseSensitive TRUE for case sensitive search,
8398
* FALSE for case insensitive search.
8499
*
85100
* @return User The user entity, NULL if it does not exists or
86101
* FALSE on failure.
87102
*/
88-
public function findByUidOrEmail($query, $caseSensitive = true)
103+
public function findByUsernameOrEmail($query, $caseSensitive = true)
89104
{
90105
if ($caseSensitive) {
91106
return $this->dataQuery->queryEntity(
92-
Query::FIND_USER_BY_UID_OR_EMAIL, User::class,
93-
[Query::UID_PARAM => $query, Query::EMAIL_PARAM => $query]
107+
Query::FIND_USER_BY_USERNAME_OR_EMAIL, User::class,
108+
[Query::USERNAME_PARAM => $query, Query::EMAIL_PARAM => $query]
94109
);
95110
} else {
96111
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]
112+
Query::FIND_USER_BY_USERNAME_OR_EMAIL_CASE_INSENSITIVE, User::class,
113+
[Query::USERNAME_PARAM => $query, Query::EMAIL_PARAM => $query]
99114
);
100115
}
101116
}

templates/admin.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ function print_select_options(
154154
print_text_input($l, "db-table-user", "Table name", $_["db.table.user"]); ?>
155155
<h3><?php p($l->t("Columns")); ?></h3>
156156
<?php
157-
print_text_input($l, "db-table-user-column-uid", "Username", $_["db.table.user.column.uid"]);
157+
print_text_input($l, "db-table-user-column-uid", "UID", $_["db.table.user.column.uid"]);
158+
print_text_input($l, "db-table-user-column-username", "Username", $_["db.table.user.column.username"]);
158159
print_text_input($l, "db-table-user-column-email", "Email", $_["db.table.user.column.email"]);
159160
print_text_input($l, "db-table-user-column-quota", "Quota", $_["db.table.user.column.quota"]);
160161
print_text_input($l, "db-table-user-column-home", "Home", $_["db.table.user.column.home"]);

0 commit comments

Comments
 (0)