Skip to content

Commit aa18cfb

Browse files
authored
Merge pull request #906 from apps-caraga/apps-caraga-patch-1
Update DbAuthMiddleware.php
2 parents 3051700 + 12b5890 commit aa18cfb

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,7 @@ You can tune the middleware behavior using middleware specific configuration par
698698
- "apiKeyDbAuth.apiKeyColumn": The users table column that holds the API key ("api_key")
699699
- "dbAuth.mode": Set to "optional" if you want to allow anonymous access ("required")
700700
- "dbAuth.usersTable": The table that is used to store the users in ("users")
701+
- "dbAuth.loginTable": The table or view that is used to retrieve the users info for login
701702
- "dbAuth.usernameColumn": The users table column that holds usernames ("username")
702703
- "dbAuth.passwordColumn": The users table column that holds passwords ("password")
703704
- "dbAuth.returnedColumns": The columns returned on successful login, empty means 'all' ("")
@@ -826,6 +827,13 @@ users can freely add, modify or delete any account! The minimal configuration is
826827

827828
Note that this middleware uses session cookies and stores the logged in state on the server.
828829

830+
**Login using views with joined table**
831+
832+
For login operations, it is possible to use a view as the usersTable. Such view can return a filtered result from the users table, e.g., *where active = true* or it may also return a result multiple tables thru a table join. At a minimum, the view should include the ***username*** and ***password***.
833+
834+
However, views with joined tables are not insertable ([see issue 907](https://github.com/mevdschee/php-crud-api/issues/907) ). As a workaround, use the property ***loginTable*** to set a different reference table for login. The **usersTable** will still be set to the normal, insertable users table.
835+
836+
829837
#### Basic authentication
830838

831839
The Basic type supports a file (by default '.htpasswd') that holds the users and their (hashed) passwords separated by a colon (':').

src/Tqdev/PhpCrudApi/Middleware/DbAuthMiddleware.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
5151
$username = isset($body->$usernameFormFieldName) ? $body->$usernameFormFieldName : '';
5252
$password = isset($body->$passwordFormFieldName) ? $body->$passwordFormFieldName : '';
5353
$newPassword = isset($body->$newPasswordFormFieldName) ? $body->$newPasswordFormFieldName : '';
54-
$tableName = $this->getProperty('usersTable', 'users');
54+
if($path ==='login')
55+
$tableName = $this->getProperty('loginTable', 'users'); //add separate property for login as this could be a view joining users table to other table such as roles, details etc. At a minimum, the view output should include the $usernameColumn and $passwordColumn
56+
else
57+
$tableName = $this->getProperty('usersTable', 'users');
5558
$table = $this->reflection->getTable($tableName);
5659
$usernameColumnName = $this->getProperty('usernameColumn', 'username');
5760
$usernameColumn = $table->getColumn($usernameColumnName);

0 commit comments

Comments
 (0)