Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/allow non model tenant #2

Merged
merged 7 commits into from
Dec 28, 2023
Merged
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
9 changes: 6 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ jobs:
steps:
- uses: actions/checkout@v2

- uses: symfonycorp/security-checker-action@v4

- name: Setup PHP, with composer and extensions
uses: shivammathur/setup-php@v2
with:
php-version: 8.1
php-version: 8.0
extensions: mbstring, xml, hash, ctype, iconv, curl

- name: Get composer cache directory
Expand All @@ -36,11 +34,16 @@ jobs:
- name: Install Composer dependencies
run: composer install -n

- uses: symfonycorp/security-checker-action@v4

- name: Validate composer.lock
run: composer validate --strict

- name: Run pint
run: vendor/bin/pint --test

- name: Run pest
run: vendor/bin/pest

- name: Run Phpstan
run: vendor/bin/phpstan --no-progress --debug --memory-limit=1G
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
/.vscode
/.phpstorm.meta.php
clover.xml
/build
composer.lock
38 changes: 22 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Laravel Multitenancy
[![Latest Stable Version](https://poser.pugx.org/acdphp/laravel-multitenancy/v)](https://packagist.org/packages/acdphp/laravel-multitenancy)
[![License](http://poser.pugx.org/acdphp/laravel-multitenancy/license)](https://packagist.org/packages/acdphp/laravel-multitenancy)

Laravel multi-tenancy model scoping and automatic tenancy assignment.

Expand All @@ -9,11 +8,7 @@ Laravel multi-tenancy model scoping and automatic tenancy assignment.
composer require acdphp/laravel-multitenancy
```

## Modifying config
- By default, it will look for `\App\Models\Company::class` as `tenant_class` that has `id` as `tenant_primary_key`. Relatively, it will use `company_id` column of the models that belongs to a tenant as `tenant_ref_key`. If this isn't the case, you may override the config by publishing it.
```sh
php artisan vendor:publish --provider="Acdphp\Multitenancy\TenancyServiceProvider"
```
This package will resolve the current tenant based on the resolved authenticated user's tenant key value.

## Model Usage
```php
Expand All @@ -25,6 +20,19 @@ class YourModel extends Model
}
```

## Manually setting the tenant
- In the registration, for example, tenancy isn't set because it's a non-authenticated endpoint. The tenant has to be manually assigned.
```php
use Acdphp\Multitenancy\Facades\Tenancy;

// Create company and set as tenant
$company = Company::create(...);
Tenancy::setTenantId($company->id);

// Then proceed to create a user
User::create(...);
```

## Bypassing Scope
- Sometimes, it's needed to bypass scoping when accessing a model that belongs to a tenant but on public endpoints. Example when logging in.
```php
Expand All @@ -51,17 +59,15 @@ Tenancy::bypassCreating();
Route::middleware(['tenancy.creating.bypass'])->post('your-route', ...);
```

## Manually setting tenant
- In the registration, for example, tenancy isn't set because it's a non-authenticated endpoint. The tenant has to be manually assigned using setTenant.
```php
use Acdphp\Multitenancy\Facades\Tenancy;

// Create company and set as tenant
$company = Company::create(...);
Tenancy::setTenant($company);
## Modifying config
- Publish config
```sh
php artisan vendor:publish --provider="Acdphp\Multitenancy\TenancyServiceProvider"
```

// Then proceed to create a user
User::create(...);
- Change column name to look for tenancy in models.
```php
'tenant_ref_key' => 'company_id',
```

## License
Expand Down
42 changes: 38 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,24 @@
"php": "^8.0"
},
"require-dev": {
"laravel/pint": "^1.13",
"laravel/pint": "^1.5",
"phpstan/phpstan": "^1.10",
"nunomaduro/larastan": "^2.6",
"larastan/larastan": "^2.7",
"ekino/phpstan-banned-code": "^1.0",
"orchestra/testbench": "^8.10"
"orchestra/testbench": "^7.38",
"pestphp/pest": "^1.23",
"pestphp/pest-plugin-laravel": "^1.4"
},
"autoload": {
"psr-4": {
"Acdphp\\Multitenancy\\": "src"
"Acdphp\\Multitenancy\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Acdphp\\Multitenancy\\Tests\\": "tests/",
"Workbench\\App\\": "workbench/app/",
"Workbench\\Database\\Factories\\": "workbench/database/factories/"
}
},
"extra": {
Expand All @@ -34,5 +43,30 @@
"Tenancy": "Acdphp\\Multitenancy\\Facades\\Tenancy"
}
}
},
"scripts": {
"post-autoload-dump": [
"@clear",
"@prepare"
],
"clear": "@php vendor/bin/testbench package:purge-skeleton --ansi",
"prepare": "@php vendor/bin/testbench package:discover --ansi",
"build": "@php vendor/bin/testbench workbench:build --ansi",
"serve": [
"@build",
"@php vendor/bin/testbench serve"
],
"lint": [
"@php vendor/bin/pint",
"@php vendor/bin/phpstan analyse"
],
"test": [
"@php vendor/bin/pest"
]
},
"config": {
"allow-plugins": {
"pestphp/pest-plugin": true
}
}
}
Loading
Loading