1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-11-04 13:40:44 +00:00
Automatic multi-tenancy for Laravel. No code changes needed. https://tenancyforlaravel.com
  • PHP 99.2%
  • Nushell 0.3%
  • Dockerfile 0.3%
Find a file
Samuel Stancl c1f32bc439
[MINOR BC] Allowlist and validate tenant DB connection config keys (#1503)
> Minor breaking change: if you have custom `tenancy_db_*` keys on your
tenants, you will need to override your Tenant model's `database()` to
return `parent::database()->allow(['your', 'extra', 'keys'])`,
potentially with custom validation (`->validate(['your', 'keys'],
bool|Closure)` where the second argument specifies `true` for default
validation logic, `false` for **no validation**, or a custom Closure
returning a boolean or throwing an exception).

`HasDatabase::database()` passed every `tenancy_db_*` attribute into the
tenant connection config. Since the `Tenant` model is unguarded, an app
that mass-assigns request data into a tenant let the requester set keys
like `url`, `read` or `write`, which repoint the connection, or
`isolation_level`, `charset` and `collation`, which `MySqlConnector`
interpolates into SQL on connect.

`DatabaseConfig` now only accepts `host`, `port`, `charset`,
`collation`, `username` and `password`. Any other key throws an
exception naming the key and the tenant. Values are validated with
`validateParameter()`, except usernames and passwords, which the PDO
drivers pass to the server separately or escape before use. `host` uses
the default character list plus `.:` and `port` only allows digits.

Other keys can be allowed by overriding `database()` in the Tenant
model:

```php
// Tenant
public function database(): DatabaseConfig
{
    return parent::database()
        ->allow(['sslmode', 'options']) // '*' allows any key
        ->validate('options', false); // true = default rules, false = none, or a closure
}
```

`allow()` accepts `string|string[]`, `validate()` accepts
`(string|string[], bool|Closure)` where the first argument is either one
or multiple key names and the second argument is the validator — true
for the default rules, false for **no validation**, or a Closure for
custom rules.

`allow('*')` makes all keys allowed. `validate('*', $validator)`
specifies the validator used for keys that don't have *their own*
validator specified.

Allowed keys use the default validation unless `validate()` says
otherwise.

Also fixes `$harden` ignoring `tenancy_db_name` when it's a custom
column.

See the _Customizing databases_ page in v4 docs.

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 03:47:02 -08:00
.github ci: move php-cs-fixer to dev dependencies instead of a global dep (#1506) 2026-10-14 00:29:38 -07:00
art Update artwork 2020-06-09 15:36:47 +02:00
assets Test that a failing migration stops the tenant creation pipeline (#1499) 2026-11-02 21:25:19 -08:00
doctum wip 2023-04-14 17:23:19 +02:00
extensions [4.x] Add DisallowSqliteAttach feature (#1283) 2025-01-04 15:34:37 +01:00
src [MINOR BC] Allowlist and validate tenant DB connection config keys (#1503) 2026-11-03 03:47:02 -08:00
tests [MINOR BC] Allowlist and validate tenant DB connection config keys (#1503) 2026-11-03 03:47:02 -08:00
typedefs parallel commands: core # autodetect, bugfixes, improved output 2024-09-27 23:02:03 +02:00
.gitattributes misc: update .gitattributes and .nvim.lua 2025-10-13 16:14:15 +02:00
.gitignore gitignore workspaces 2026-10-11 22:32:44 -07:00
.nvim.lua misc: update .gitattributes and .nvim.lua 2025-10-13 16:14:15 +02:00
.php-cs-fixer.php [4.x] Cloning: addTenantParameter(bool), domain(string|null) (#1374) 2025-07-29 17:17:32 +02:00
AGENTS.md claude.md: clarify master branch, add AGENTS.md symlink 2026-10-13 20:50:14 -07:00
CLAUDE.md claude.md: wait after restarting mysql 2026-11-02 23:32:19 -08:00
composer.json ci: move php-cs-fixer to dev dependencies instead of a global dep (#1506) 2026-10-14 00:29:38 -07:00
CONTRIBUTING.md contributing.md: minor wording improvement 2026-11-02 02:29:07 -08:00
docker-compose-m1.override.yml use mysql:8 in docker 2025-01-21 15:23:17 +01:00
docker-compose.yml docker: change mssql env yaml syntax 2026-05-01 15:55:06 +02:00
Dockerfile PHP 8.5 support 2025-10-20 01:44:24 +02:00
DONATIONS.md update md files 2020-06-08 22:42:13 +02:00
INTERNAL.md [4.x] Add DisallowSqliteAttach feature (#1283) 2025-01-04 15:34:37 +01:00
LICENSE Initial commit 2019-01-17 22:24:12 +01:00
phpstan.neon Load spatie/invade's PHPStan extension 2026-10-22 02:50:30 -07:00
phpunit.xml Misc test fixes (#44) 2024-04-06 19:17:34 +02:00
README.md pending tenants: minor cleanup 2025-10-29 22:54:53 +01:00
static_properties.nu util: add static_properties.nu, more portable shebangs, PHP 8.5 beta 2025-09-22 00:20:33 +02:00
SUPPORT.md Discord link 2022-06-13 19:16:35 +02:00
t t, test: fix tput cols logic 2026-11-03 02:12:02 -08:00
test t, test: fix tput cols logic 2026-11-03 02:12:02 -08:00

Tenancy for Laravel logo

Laravel 11.x Latest Stable Version GitHub Actions CI status Donate

Tenancy for Laravel — stancl/tenancy

Automatic multi-tenancy for your Laravel app.

You won't have to change a thing in your application's code.

  • ✔️ No model traits to change database connection
  • ✔️ No replacing of Laravel classes (Cache, Storage, ...) with tenancy-aware classes
  • ✔️ Built-in tenant identification based on hostname (including second level domains)

Documentation

Documentation can be found here: https://v4.tenancyforlaravel.com

Need help?

Credits