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

Feature tenant aware Refactored #32

Merged
merged 4 commits into from
Mar 10, 2024
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.DS_Store
.idea
.phpunit*
.phpunit.*
.vscode
build
composer.lock
Expand Down
5 changes: 2 additions & 3 deletions config/api-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
'panel_prefix' => true,
],
'tenancy' => [
'enabled' => false,
'enabled' => false,
'awareness' => false,
],

]
];
3 changes: 1 addition & 2 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
Route::prefix('api')
->name('api.')
->group(function () {
if (! ApiService::isTenancyEnabled() && ApiService::tenancyAwareness()) {
if (!ApiService::isTenancyEnabled() && ApiService::tenancyAwareness()) {
throw new InvalidTenancyConfiguration('Tenancy awereness is enabled. But, Tenancy is disabled.');
}

foreach (Filament::getPanels() as $key => $panel) {
try {
$apiServicePlugin = $panel->getPlugin('api-service');
Expand Down
2 changes: 1 addition & 1 deletion src/Concerns/HasTenancy.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ public static function isTenancyEnabled(): bool

public static function tenancyAwareness(): bool
{
return config('api-service.tenancy.awereness', false);
return config('api-service.tenancy.awareness', false);
}
}
2 changes: 2 additions & 0 deletions src/Http/Handlers.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Handlers
{
use HasHandlerTenantScope;
use HttpResponse;
use HasHandlerTenantScope;

public static ?string $uri = '/';
public static string $method = 'get';
public static ?string $resource = null;
Expand Down
10 changes: 6 additions & 4 deletions src/Traits/HasHandlerTenantScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ protected static function getTenantOwnershipRelationship(Model $record): Relatio
{
$relationshipName = static::getTenantOwnershipRelationshipName();

if (! $record->isRelation($relationshipName)) {
if (!$record->isRelation($relationshipName)) {

$resourceClass = static::class;
$recordClass = $record::class;

Expand Down Expand Up @@ -59,7 +60,7 @@ protected static function scopeEloquentQueryToTenant(QueryBuilder $query, ?Model
) {
if (auth()->check()) {

$query = match (true) {
$query = match (true) {
$tenantOwnershipRelationship instanceof MorphTo => $query->whereMorphedTo(
$tenantOwnershipRelationshipName,
$tenant,
Expand All @@ -78,13 +79,14 @@ protected static function scopeEloquentQueryToTenant(QueryBuilder $query, ?Model

if (
ApiService::isTenancyEnabled() &&
! ApiService::tenancyAwareness() &&
!ApiService::tenancyAwareness() &&
static::isScopedToTenant()
) {

if (auth()->check()) {

$query = match (true) {
$query = match (true) {

$tenantOwnershipRelationship instanceof MorphTo => $query
->where($tenantModel->getRelationWithoutConstraints($tenantOwnershipRelationshipName)->getMorphType(), $tenantModel->getMorphClass())
->whereIn($tenantModel->getRelationWithoutConstraints($tenantOwnershipRelationshipName)->getForeignKeyName(), request()->user()->{Str::plural($tenantOwnershipRelationshipName)}->pluck($tenantModel->getKeyName())->toArray()),
Expand Down