Skip to content
This repository was archived by the owner on Aug 8, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 72 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,25 @@ The project is currently in development and is considered experimental at this s
The MultiTenant plugin is best to implement when you begin developing your application, but with some work
you should be able to adapt an existing application to use the Plugin.

The plugin currently implements the following multi-tenancy architecures (Strategy).
The plugin currently implements the following multi-tenancy architecures (Strategy):

### Domain Strategy

* Shared Database, Shared Schema
* Single Application Instance
* Subdomain per Tenant

### Session Strategy

* Shared Database, Shared Schema
* Single Application Instance
* Using same domain, the tenant is identified by the session path matched against the model field configured.

### Configuration Strategy

* Shared Database, Shared Schema
* Single Application Instance
* Using same domain, the tenant is identified by a key in the CakePHP configuration storage

### Tenants

Expand All @@ -41,7 +52,9 @@ application. ie signup/register code.

#### 'tenant' Context

'tenant' context represents this tenant. When the user is accessing the application at the subdomain, this is
'tenant' context represents this tenant.

* Domain Strategy: When the user is accessing the application at the subdomain, this is
considered the 'tenant' context.

#### Custom Contexts
Expand Down Expand Up @@ -141,9 +154,9 @@ Add the following to the bottom of your application's config\app.php
*
* ## Options
*
* - `strategy` - 'domain' is currently the only implemented strategy
* - `primaryDomain` - The domain for the main application
* value to false, when dealing with older versions of IE, Chrome Frame or certain web-browsing devices and AJAX
* - `strategy` - 'domain', 'session' or 'configuration'.
* - `primaryDomain` - The domain for the main application. All tenant subdomains finish with this.
* - `primarySubdomains` - Subdomains of the primaryDomain considered also as primary, for example, "www".
* - `model` - The model that represents the tenant, usually 'Accounts'
* - `redirectInactive` - URI to redirect when the tenant is not active or does not exist. This should be a uri at the
* primary domain, usually your signup page or feature pitch page with call-to-action signup button.
Expand All @@ -155,15 +168,18 @@ Add the following to the bottom of your application's config\app.php
*
*/
'MultiTenant' => [
'strategy'=>'domain',
'primaryDomain'=>'www.example.com',
'model'=>[
'className'=>'Accounts',
'field'=>'domain', //field of model that holds subdomain/domain tenants
'conditions'=>['is_active'=>1] //query conditions to match active accounts
'strategy' => 'domain',
'primaryDomain' => 'example.com',
'primarySubdomains => [
'www',
]
'model' => [
'className' => 'Accounts',
'field' => 'domain', //field of model that holds subdomain/domain tenants
'conditions' => ['is_active'=>1] //query conditions to match active accounts
],
'redirectInactive'=>'/register',
'reservedDomains'=>[
'redirectInactive' => '/register',
'reservedDomains' => [
'admin',
'superuser',
'system',
Expand All @@ -172,13 +188,53 @@ Add the following to the bottom of your application's config\app.php
'contextMap' => [
'admin'=>'admin.example.com' //an example of a custom context
],
'scopeBehavior'=>[
'global_value'=>0, //global records are matched by this value
'foreign_key_field'=>'account_id' //the foreign key field that associates records to tenant model
'scopeBehavior' => [
'global_value' => 0, //global records are matched by this value
'foreign_key_field' => 'account_id' //the foreign key field that associates records to tenant model
]
]
```

Here we have a session strategy configuration. When the users logs in
the account_id is stored in the session. Then it's checked against the
Accounts table (id field) where the account is active.
```
'MultiTenant' => [
'strategy' => [
'session' => [
'path' => 'Auth.User.account_id'
]
],
'primaryDomain' => 'www.example.com',
'model' => [
'className' => 'Accounts',
'field' => 'id', //field of model that holds subdomain/domain tenants
'conditions' => ['is_active' => 1] //query conditions to match active accounts
],
'redirectInactive' => '/register',
'scopeBehavior' => [
'global_value' => 1, //global records are matched by this value
'foreign_key_field' => 'account_id' //the foreign key field that associates records to tenant model
]
]
```

Here you can see a *configuration* strategy:
```
'MultiTenant' => [
'strategy' => [
'configuration' => [
'tenant_key' => 'account_id'
]
],
...

// ...then, somewhere in your code, you can change the tenant
// simply writing to the configuration key.
Configure::write('account_id', 34);
...
```

Note: don't forget to add the , to the bottom config section when pasting the above configuration. A syntax error in config\app.php is a silent failure (blank page).

## Usage
Expand Down
Loading