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

[ADVISING-1011]: Enforce Subscription Rules for Knowledge Management #318

Merged
merged 7 commits into from
Dec 4, 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
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,16 @@
namespace Assist\KnowledgeBase\Policies;

use App\Models\User;
use App\Enums\Feature;
use Illuminate\Auth\Access\Response;
use App\Concerns\FeatureAccessEnforcedPolicyBefore;
use Assist\KnowledgeBase\Models\KnowledgeBaseCategory;
use App\Policies\Contracts\FeatureAccessEnforcedPolicy;

class KnowledgeBaseCategoryPolicy
class KnowledgeBaseCategoryPolicy implements FeatureAccessEnforcedPolicy
{
use FeatureAccessEnforcedPolicyBefore;

public function viewAny(User $user): Response
{
return $user->canOrElse(
Expand Down Expand Up @@ -97,4 +102,9 @@ public function forceDelete(User $user, KnowledgeBaseCategory $knowledgeBaseCate
denyResponse: 'You do not have permissions to permanently delete this knowledge base category.'
);
}

protected function requiredFeatures(): array
{
return [Feature::KnowledgeManagement];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,16 @@
namespace Assist\KnowledgeBase\Policies;

use App\Models\User;
use App\Enums\Feature;
use Illuminate\Auth\Access\Response;
use Assist\KnowledgeBase\Models\KnowledgeBaseItem;
use App\Concerns\FeatureAccessEnforcedPolicyBefore;
use App\Policies\Contracts\FeatureAccessEnforcedPolicy;

class KnowledgeBaseItemPolicy
class KnowledgeBaseItemPolicy implements FeatureAccessEnforcedPolicy
{
use FeatureAccessEnforcedPolicyBefore;

public function viewAny(User $user): Response
{
return $user->canOrElse(
Expand Down Expand Up @@ -97,4 +102,9 @@ public function forceDelete(User $user, KnowledgeBaseItem $knowledgeBaseItem): R
denyResponse: 'You do not have permissions to force delete this knowledge base item.'
);
}

protected function requiredFeatures(): array
{
return [Feature::KnowledgeManagement];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,16 @@
namespace Assist\KnowledgeBase\Policies;

use App\Models\User;
use App\Enums\Feature;
use Illuminate\Auth\Access\Response;
use App\Concerns\FeatureAccessEnforcedPolicyBefore;
use Assist\KnowledgeBase\Models\KnowledgeBaseQuality;
use App\Policies\Contracts\FeatureAccessEnforcedPolicy;

class KnowledgeBaseQualityPolicy
class KnowledgeBaseQualityPolicy implements FeatureAccessEnforcedPolicy
{
use FeatureAccessEnforcedPolicyBefore;

public function viewAny(User $user): Response
{
return $user->canOrElse(
Expand Down Expand Up @@ -97,4 +102,9 @@ public function forceDelete(User $user, KnowledgeBaseQuality $knowledgeBaseQuali
denyResponse: 'You do not have permission to permanently delete this knowledge base category.'
);
}

protected function requiredFeatures(): array
{
return [Feature::KnowledgeManagement];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,16 @@
namespace Assist\KnowledgeBase\Policies;

use App\Models\User;
use App\Enums\Feature;
use Illuminate\Auth\Access\Response;
use App\Concerns\FeatureAccessEnforcedPolicyBefore;
use Assist\KnowledgeBase\Models\KnowledgeBaseStatus;
use App\Policies\Contracts\FeatureAccessEnforcedPolicy;

class KnowledgeBaseStatusPolicy
class KnowledgeBaseStatusPolicy implements FeatureAccessEnforcedPolicy
{
use FeatureAccessEnforcedPolicyBefore;

public function viewAny(User $user): Response
{
return $user->canOrElse(
Expand Down Expand Up @@ -97,4 +102,9 @@ public function forceDelete(User $user, KnowledgeBaseStatus $knowledgeBaseStatus
denyResponse: 'You do not have permission to permanently delete this knowledge base status.'
);
}

protected function requiredFeatures(): array
{
return [Feature::KnowledgeManagement];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
*/

use App\Models\User;
use App\Settings\LicenseSettings;

use function Pest\Laravel\actingAs;
use function Pest\Livewire\livewire;
Expand Down Expand Up @@ -82,3 +83,44 @@

assertDatabaseHas(KnowledgeBaseCategory::class, $request->toArray());
});

test('CreateKnowledgeBaseCategory is gated with proper feature access control', function () {
$settings = app(LicenseSettings::class);

$settings->data->addons->knowledgeManagement = false;

$settings->save();

$user = User::factory()->create();

$user->givePermissionTo('knowledge_base_category.view-any');
$user->givePermissionTo('knowledge_base_category.create');

actingAs($user)
->get(
KnowledgeBaseCategoryResource::getUrl('create')
)->assertForbidden();

livewire(KnowledgeBaseCategoryResource\Pages\CreateKnowledgeBaseCategory::class)
->assertForbidden();

$settings->data->addons->knowledgeManagement = true;

$settings->save();

actingAs($user)
->get(
KnowledgeBaseCategoryResource::getUrl('create')
)->assertSuccessful();

$request = collect(CreateKnowledgeBaseCategoryRequestFactory::new()->create());

livewire(KnowledgeBaseCategoryResource\Pages\CreateKnowledgeBaseCategory::class)
->fillForm($request->toArray())
->call('create')
->assertHasNoFormErrors();

assertCount(1, KnowledgeBaseCategory::all());

assertDatabaseHas(KnowledgeBaseCategory::class, $request->toArray());
});
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
*/

use App\Models\User;
use App\Settings\LicenseSettings;

use function Pest\Laravel\actingAs;
use function Pest\Livewire\livewire;
Expand Down Expand Up @@ -89,3 +90,52 @@

assertEquals($request['name'], $knowledgeBaseCategory->fresh()->name);
});

test('EditKnowledgeBaseCategory is gated with proper feature access control', function () {
$settings = app(LicenseSettings::class);

$settings->data->addons->knowledgeManagement = false;

$settings->save();

$user = User::factory()->create();

$user->givePermissionTo('knowledge_base_category.view-any');
$user->givePermissionTo('knowledge_base_category.*.update');

$knowledgeBaseCategory = KnowledgeBaseCategory::factory()->create();

actingAs($user)
->get(
KnowledgeBaseCategoryResource::getUrl('edit', [
'record' => $knowledgeBaseCategory,
])
)->assertForbidden();

livewire(KnowledgeBaseCategoryResource\Pages\EditKnowledgeBaseCategory::class, [
'record' => $knowledgeBaseCategory->getRouteKey(),
])
->assertForbidden();

$settings->data->addons->knowledgeManagement = true;

$settings->save();

actingAs($user)
->get(
KnowledgeBaseCategoryResource::getUrl('edit', [
'record' => $knowledgeBaseCategory,
])
)->assertSuccessful();

$request = collect(EditKnowledgeBaseCategoryRequestFactory::new()->create());

livewire(KnowledgeBaseCategoryResource\Pages\EditKnowledgeBaseCategory::class, [
'record' => $knowledgeBaseCategory->getRouteKey(),
])
->fillForm($request->toArray())
->call('save')
->assertHasNoFormErrors();

assertEquals($request['name'], $knowledgeBaseCategory->fresh()->name);
});
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
*/

use App\Models\User;
use App\Settings\LicenseSettings;

use function Pest\Laravel\actingAs;

Expand Down Expand Up @@ -62,3 +63,29 @@
KnowledgeBaseCategoryResource::getUrl('index')
)->assertSuccessful();
});

test('ListKnowledgeBaseCategory is gated with proper feature access control', function () {
$settings = app(LicenseSettings::class);

$settings->data->addons->knowledgeManagement = false;

$settings->save();

$user = User::factory()->create();

$user->givePermissionTo('knowledge_base_category.view-any');

actingAs($user)
->get(
KnowledgeBaseCategoryResource::getUrl('index')
)->assertForbidden();

$settings->data->addons->knowledgeManagement = true;

$settings->save();

actingAs($user)
->get(
KnowledgeBaseCategoryResource::getUrl('index')
)->assertSuccessful();
});
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
*/

use App\Models\User;
use App\Settings\LicenseSettings;

use function Pest\Laravel\actingAs;

Expand Down Expand Up @@ -68,3 +69,36 @@
])
)->assertSuccessful();
});

test('ViewKnowledgeBaseCategory is gated with proper feature access control', function () {
$settings = app(LicenseSettings::class);

$settings->data->addons->knowledgeManagement = false;

$settings->save();

$user = User::factory()->create();

$user->givePermissionTo('knowledge_base_category.view-any');
$user->givePermissionTo('knowledge_base_category.*.view');

$knowledgeBaseCategory = KnowledgeBaseCategory::factory()->create();

actingAs($user)
->get(
KnowledgeBaseCategoryResource::getUrl('view', [
'record' => $knowledgeBaseCategory,
])
)->assertForbidden();

$settings->data->addons->knowledgeManagement = true;

$settings->save();

actingAs($user)
->get(
KnowledgeBaseCategoryResource::getUrl('view', [
'record' => $knowledgeBaseCategory,
])
)->assertSuccessful();
});
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
*/

use App\Models\User;
use App\Settings\LicenseSettings;

use function Pest\Laravel\actingAs;
use function Pest\Livewire\livewire;
Expand Down Expand Up @@ -86,3 +87,48 @@

expect($knowledgeBaseItem->division->pluck('id')->toArray())->toEqual($request['division']);
});

test('CreateKnowledgeBaseItem is gated with proper feature access control', function () {
$settings = app(LicenseSettings::class);

$settings->data->addons->knowledgeManagement = false;

$settings->save();

$user = User::factory()->create();

actingAs($user)
->get(
KnowledgeBaseItemResource::getUrl('create')
)->assertForbidden();

$user->givePermissionTo('knowledge_base_item.view-any');
$user->givePermissionTo('knowledge_base_item.create');

livewire(KnowledgeBaseItemResource\Pages\CreateKnowledgeBaseItem::class)
->assertForbidden();

$settings->data->addons->knowledgeManagement = true;

$settings->save();

actingAs($user)
->get(
KnowledgeBaseItemResource::getUrl('create')
)->assertSuccessful();

$request = collect(CreateKnowledgeBaseItemRequestFactory::new()->create());

livewire(KnowledgeBaseItemResource\Pages\CreateKnowledgeBaseItem::class)
->fillForm($request->toArray())
->call('create')
->assertHasNoFormErrors();

assertCount(1, KnowledgeBaseItem::all());

assertDatabaseHas(KnowledgeBaseItem::class, $request->except('division')->toArray());

$knowledgeBaseItem = KnowledgeBaseItem::first();

expect($knowledgeBaseItem->division->pluck('id')->toArray())->toEqual($request['division']);
});
Loading
Loading