Skip to content

Commit a45580b

Browse files
committed
multi tenant support by default
closes #11
1 parent ec9f3c7 commit a45580b

File tree

3 files changed

+30
-18
lines changed

3 files changed

+30
-18
lines changed

src/templates/public/api.php

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,58 @@
11
<?php
22

3-
// Dependencies
3+
// Import dependencies
44
use Tqdev\PhpCrudApi\Api;
55
use Tqdev\PhpCrudApi\Config\Config;
66
use Tqdev\PhpCrudApi\RequestFactory;
77
use Tqdev\PhpCrudApi\ResponseUtils;
88
require('vendor/autoload.php');
99

10-
// Credentials
10+
// Import and define credentials
1111
@include('credentials.php');
1212
@define('MYSQL_HOST', 'mysql');
1313
@define('MYSQL_DATABASE', 'development');
1414
@define('MYSQL_USERNAME', 'root');
1515
@define('MYSQL_PASSWORD', 'root');
1616

17-
// User ID
18-
session_start();
19-
define('USERID', isset($_SESSION['user']['id']) ? $_SESSION['user']['id'] : 0);
20-
2117
// Configuration
2218
$config = new Config([
2319

24-
// Debug Mode
20+
// Debug mode
2521
'debug' => MYSQL_DATABASE === 'development',
2622

27-
// Database Credentials
23+
// Credentials
2824
'address' => MYSQL_HOST,
2925
'database' => MYSQL_DATABASE,
3026
'username' => MYSQL_USERNAME,
3127
'password' => MYSQL_PASSWORD,
3228

33-
// Database Authentication
34-
'middlewares' => 'dbAuth,authorization',
29+
// Middlewares
30+
'middlewares' => 'dbAuth,authorization,multiTenancy',
31+
32+
// Database authentication
3533
'dbAuth.mode' => 'optional',
3634
'dbAuth.registerUser' => '1',
35+
'dbAuth.passwordLength' => '3',
36+
37+
// Database Authorization
3738
'authorization.tableHandler' => function ($operation, $tableName) {
39+
40+
// No access to the users table
3841
if ($tableName === 'users') return false;
42+
43+
// Access to all other tables
3944
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+
4256
]);
4357

4458
// Initialization

src/templates/schema.sql

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
CREATE TABLE IF NOT EXISTS `users` (
2-
`id` INTEGER(4) NOT NULL PRIMARY KEY AUTO_INCREMENT,
2+
`id` INTEGER(8) NOT NULL PRIMARY KEY AUTO_INCREMENT,
33
`username` VARCHAR(255) NOT NULL,
44
`password` VARCHAR(255) NOT NULL
55
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
66

77
CREATE TABLE IF NOT EXISTS `tasks` (
8-
`id` INTEGER(4) NOT NULL PRIMARY KEY AUTO_INCREMENT,
9-
`title` VARCHAR(255) NOT NULL,
8+
`id` INTEGER(8) NOT NULL PRIMARY KEY AUTO_INCREMENT,
9+
`userId` INTEGER(8) NOT NULL DEFAULT 0,
10+
`title` VARCHAR(255) NOT NULL DEFAULT "",
1011
`done` TINYINT(1) NOT NULL DEFAULT 0
1112
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

src/templates/testdata.sql

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
1-
INSERT IGNORE INTO `users` (`id`, `username`, `password`)
2-
VALUES (1, "root", "cm9vdA==");
3-
41
INSERT IGNORE INTO `tasks` (`id`, `title`, `done`)
52
VALUES (1, "First Task", 1), (2, "Second Task", 0), (3, "Third Task", 1);

0 commit comments

Comments
 (0)