|
1 | 1 | <?php
|
2 | 2 |
|
3 |
| -// Dependencies |
| 3 | +// Import dependencies |
4 | 4 | use Tqdev\PhpCrudApi\Api;
|
5 | 5 | use Tqdev\PhpCrudApi\Config\Config;
|
6 | 6 | use Tqdev\PhpCrudApi\RequestFactory;
|
7 | 7 | use Tqdev\PhpCrudApi\ResponseUtils;
|
8 | 8 | require('vendor/autoload.php');
|
9 | 9 |
|
10 |
| -// Credentials |
| 10 | +// Import and define credentials |
11 | 11 | @include('credentials.php');
|
12 | 12 | @define('MYSQL_HOST', 'mysql');
|
13 | 13 | @define('MYSQL_DATABASE', 'development');
|
14 | 14 | @define('MYSQL_USERNAME', 'root');
|
15 | 15 | @define('MYSQL_PASSWORD', 'root');
|
16 | 16 |
|
17 |
| -// User ID |
18 |
| -session_start(); |
19 |
| -define('USERID', isset($_SESSION['user']['id']) ? $_SESSION['user']['id'] : 0); |
20 |
| - |
21 | 17 | // Configuration
|
22 | 18 | $config = new Config([
|
23 | 19 |
|
24 |
| - // Debug Mode |
| 20 | + // Debug mode |
25 | 21 | 'debug' => MYSQL_DATABASE === 'development',
|
26 | 22 |
|
27 |
| - // Database Credentials |
| 23 | + // Credentials |
28 | 24 | 'address' => MYSQL_HOST,
|
29 | 25 | 'database' => MYSQL_DATABASE,
|
30 | 26 | 'username' => MYSQL_USERNAME,
|
31 | 27 | 'password' => MYSQL_PASSWORD,
|
32 | 28 |
|
33 |
| - // Database Authentication |
34 |
| - 'middlewares' => 'dbAuth,authorization', |
| 29 | + // Middlewares |
| 30 | + 'middlewares' => 'dbAuth,authorization,multiTenancy', |
| 31 | + |
| 32 | + // Database authentication |
35 | 33 | 'dbAuth.mode' => 'optional',
|
36 | 34 | 'dbAuth.registerUser' => '1',
|
| 35 | + 'dbAuth.passwordLength' => '3', |
| 36 | + |
| 37 | + // Database Authorization |
37 | 38 | 'authorization.tableHandler' => function ($operation, $tableName) {
|
| 39 | + |
| 40 | + // No access to the users table |
38 | 41 | if ($tableName === 'users') return false;
|
| 42 | + |
| 43 | + // Access to all other tables |
39 | 44 | return true;
|
40 |
| - } |
41 |
| - |
| 45 | + |
| 46 | + }, |
| 47 | + |
| 48 | + // Multi Tenancy |
| 49 | + 'multiTenancy.handler' => function ($operation, $tableName) { |
| 50 | + |
| 51 | + // For all tables, limit access to the current user |
| 52 | + return ['userId' => $_SESSION['user']['id'] ?? 0]; |
| 53 | + |
| 54 | + }, |
| 55 | + |
42 | 56 | ]);
|
43 | 57 |
|
44 | 58 | // Initialization
|
|
0 commit comments