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

[ADVAPP-223]: Refactor premium features to require an available CRM license. #449

Merged
merged 8 commits into from
Jan 17, 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
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,19 @@

return [
'model' => [
'form' => [
'application' => [
'*',
],
'application_field' => [
'*',
],
'application_submission' => [
'*',
],
'application_step' => [
'*',
],
'application_authentication' => [
'*',
],
],
Expand Down
24 changes: 20 additions & 4 deletions app-modules/application/src/Policies/ApplicationPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,29 @@
use App\Enums\Feature;
use App\Models\Authenticatable;
use Illuminate\Auth\Access\Response;
use App\Concerns\PerformsFeatureChecks;
use App\Concerns\PerformsLicenseChecks;
use AdvisingApp\Application\Models\Application;
use App\Concerns\FeatureAccessEnforcedPolicyBefore;
use App\Policies\Contracts\FeatureAccessEnforcedPolicy;
use AdvisingApp\Authorization\Enums\LicenseType;
use App\Policies\Contracts\PerformsChecksBeforeAuthorization;

class ApplicationPolicy implements FeatureAccessEnforcedPolicy
class ApplicationPolicy implements PerformsChecksBeforeAuthorization
{
use FeatureAccessEnforcedPolicyBefore;
use PerformsLicenseChecks;
use PerformsFeatureChecks;

public function before(Authenticatable $authenticatable): ?Response
{
if (! is_null($response = $this->hasAnyLicense($authenticatable, [LicenseType::RetentionCrm, LicenseType::RecruitmentCrm]))) {
return $response;
}

if (! is_null($response = $this->hasFeatures())) {
return $response;
}

return null;
}

public function viewAny(Authenticatable $authenticatable): Response
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use function Pest\Laravel\actingAs;
use function Pest\Livewire\livewire;

use AdvisingApp\Authorization\Enums\LicenseType;
use AdvisingApp\Application\Filament\Resources\ApplicationResource;

// TODO: Write CreateApplication tests
Expand All @@ -50,7 +51,7 @@
// Permission Tests

test('CreateApplication is gated with proper access control', function () {
$user = User::factory()->create();
$user = User::factory()->licensed(LicenseType::cases())->create();

actingAs($user)
->get(
Expand Down Expand Up @@ -78,7 +79,7 @@

$settings->save();

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

actingAs($user)
->get(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
use function Pest\Livewire\livewire;

use AdvisingApp\Application\Models\Application;
use AdvisingApp\Authorization\Enums\LicenseType;
use AdvisingApp\Application\Filament\Resources\ApplicationResource;
use AdvisingApp\Application\Database\Seeders\ApplicationSubmissionStateSeeder;

Expand All @@ -57,7 +58,7 @@
test('EditApplication is gated with proper access control', function () {
seed(ApplicationSubmissionStateSeeder::class);

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

$application = Application::factory()->create();

Expand Down Expand Up @@ -95,7 +96,7 @@

$settings->save();

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

$user->givePermissionTo('application.view-any');
$user->givePermissionTo('application.*.update');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

use function Pest\Laravel\actingAs;

use AdvisingApp\Authorization\Enums\LicenseType;
use AdvisingApp\Application\Filament\Resources\ApplicationResource;

// TODO: Write ListApplications tests
Expand All @@ -49,7 +50,7 @@
// Permission Tests

test('ListApplications is gated with proper access control', function () {
$user = User::factory()->create();
$user = User::factory()->licensed(LicenseType::cases())->create();

actingAs($user)
->get(
Expand All @@ -71,7 +72,7 @@

$settings->save();

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

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

Expand Down
24 changes: 20 additions & 4 deletions app-modules/form/src/Policies/FormPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,28 @@
use App\Models\Authenticatable;
use AdvisingApp\Form\Models\Form;
use Illuminate\Auth\Access\Response;
use App\Concerns\FeatureAccessEnforcedPolicyBefore;
use App\Policies\Contracts\FeatureAccessEnforcedPolicy;
use App\Concerns\PerformsFeatureChecks;
use App\Concerns\PerformsLicenseChecks;
use AdvisingApp\Authorization\Enums\LicenseType;
use App\Policies\Contracts\PerformsChecksBeforeAuthorization;

class FormPolicy implements FeatureAccessEnforcedPolicy
class FormPolicy implements PerformsChecksBeforeAuthorization
{
use FeatureAccessEnforcedPolicyBefore;
use PerformsLicenseChecks;
use PerformsFeatureChecks;

public function before(Authenticatable $authenticatable): ?Response
{
if (! is_null($response = $this->hasAnyLicense($authenticatable, [LicenseType::RetentionCrm, LicenseType::RecruitmentCrm]))) {
return $response;
}

if (! is_null($response = $this->hasFeatures())) {
return $response;
}

return null;
}

public function viewAny(Authenticatable $authenticatable): Response
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
use Filament\Actions\Contracts\HasActions;
use Illuminate\Database\Eloquent\Collection;
use Filament\Forms\Concerns\InteractsWithForms;
use AdvisingApp\Authorization\Enums\LicenseType;
use Filament\Actions\Concerns\InteractsWithActions;
use AdvisingApp\InAppCommunication\Enums\ConversationType;
use AdvisingApp\IntegrationTwilio\Actions\GetTwilioApiKey;
Expand Down Expand Up @@ -87,6 +88,10 @@ public static function canAccess(): bool
/** @var User $user */
$user = auth()->user();

if (! $user->hasAnyLicense([LicenseType::RetentionCrm, LicenseType::RecruitmentCrm])) {
return false;
}

return Gate::check(Feature::RealtimeChat->getGateName()) && $user->can('in-app-communication.realtime-chat.access');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,29 @@
use App\Enums\Feature;
use App\Models\Authenticatable;
use Illuminate\Auth\Access\Response;
use App\Concerns\FeatureAccessEnforcedPolicyBefore;
use App\Policies\Contracts\FeatureAccessEnforcedPolicy;
use App\Concerns\PerformsFeatureChecks;
use App\Concerns\PerformsLicenseChecks;
use AdvisingApp\Authorization\Enums\LicenseType;
use AdvisingApp\KnowledgeBase\Models\KnowledgeBaseCategory;
use App\Policies\Contracts\PerformsChecksBeforeAuthorization;

class KnowledgeBaseCategoryPolicy implements FeatureAccessEnforcedPolicy
class KnowledgeBaseCategoryPolicy implements PerformsChecksBeforeAuthorization
{
use FeatureAccessEnforcedPolicyBefore;
use PerformsLicenseChecks;
use PerformsFeatureChecks;

public function before(Authenticatable $authenticatable): ?Response
{
if (! is_null($response = $this->hasAnyLicense($authenticatable, [LicenseType::RetentionCrm, LicenseType::RecruitmentCrm]))) {
return $response;
}

if (! is_null($response = $this->hasFeatures())) {
return $response;
}

return null;
}

public function viewAny(Authenticatable $authenticatable): Response
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,29 @@
use App\Enums\Feature;
use App\Models\Authenticatable;
use Illuminate\Auth\Access\Response;
use App\Concerns\FeatureAccessEnforcedPolicyBefore;
use App\Concerns\PerformsFeatureChecks;
use App\Concerns\PerformsLicenseChecks;
use AdvisingApp\Authorization\Enums\LicenseType;
use AdvisingApp\KnowledgeBase\Models\KnowledgeBaseItem;
use App\Policies\Contracts\FeatureAccessEnforcedPolicy;
use App\Policies\Contracts\PerformsChecksBeforeAuthorization;

class KnowledgeBaseItemPolicy implements FeatureAccessEnforcedPolicy
class KnowledgeBaseItemPolicy implements PerformsChecksBeforeAuthorization
{
use FeatureAccessEnforcedPolicyBefore;
use PerformsLicenseChecks;
use PerformsFeatureChecks;

public function before(Authenticatable $authenticatable): ?Response
{
if (! is_null($response = $this->hasAnyLicense($authenticatable, [LicenseType::RetentionCrm, LicenseType::RecruitmentCrm]))) {
return $response;
}

if (! is_null($response = $this->hasFeatures())) {
return $response;
}

return null;
}

public function viewAny(Authenticatable $authenticatable): Response
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,29 @@
use App\Enums\Feature;
use App\Models\Authenticatable;
use Illuminate\Auth\Access\Response;
use App\Concerns\FeatureAccessEnforcedPolicyBefore;
use App\Policies\Contracts\FeatureAccessEnforcedPolicy;
use App\Concerns\PerformsFeatureChecks;
use App\Concerns\PerformsLicenseChecks;
use AdvisingApp\Authorization\Enums\LicenseType;
use AdvisingApp\KnowledgeBase\Models\KnowledgeBaseQuality;
use App\Policies\Contracts\PerformsChecksBeforeAuthorization;

class KnowledgeBaseQualityPolicy implements FeatureAccessEnforcedPolicy
class KnowledgeBaseQualityPolicy implements PerformsChecksBeforeAuthorization
{
use FeatureAccessEnforcedPolicyBefore;
use PerformsLicenseChecks;
use PerformsFeatureChecks;

public function before(Authenticatable $authenticatable): ?Response
{
if (! is_null($response = $this->hasAnyLicense($authenticatable, [LicenseType::RetentionCrm, LicenseType::RecruitmentCrm]))) {
return $response;
}

if (! is_null($response = $this->hasFeatures())) {
return $response;
}

return null;
}

public function viewAny(Authenticatable $authenticatable): Response
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,29 @@
use App\Enums\Feature;
use App\Models\Authenticatable;
use Illuminate\Auth\Access\Response;
use App\Concerns\FeatureAccessEnforcedPolicyBefore;
use App\Policies\Contracts\FeatureAccessEnforcedPolicy;
use App\Concerns\PerformsFeatureChecks;
use App\Concerns\PerformsLicenseChecks;
use AdvisingApp\Authorization\Enums\LicenseType;
use AdvisingApp\KnowledgeBase\Models\KnowledgeBaseStatus;
use App\Policies\Contracts\PerformsChecksBeforeAuthorization;

class KnowledgeBaseStatusPolicy implements FeatureAccessEnforcedPolicy
class KnowledgeBaseStatusPolicy implements PerformsChecksBeforeAuthorization
{
use FeatureAccessEnforcedPolicyBefore;
use PerformsLicenseChecks;
use PerformsFeatureChecks;

public function before(Authenticatable $authenticatable): ?Response
{
if (! is_null($response = $this->hasAnyLicense($authenticatable, [LicenseType::RetentionCrm, LicenseType::RecruitmentCrm]))) {
return $response;
}

if (! is_null($response = $this->hasFeatures())) {
return $response;
}

return null;
}

public function viewAny(Authenticatable $authenticatable): Response
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use function PHPUnit\Framework\assertCount;
use function Pest\Laravel\assertDatabaseHas;

use AdvisingApp\Authorization\Enums\LicenseType;
use AdvisingApp\KnowledgeBase\Models\KnowledgeBaseCategory;
use AdvisingApp\KnowledgeBase\Filament\Resources\KnowledgeBaseCategoryResource;
use AdvisingApp\KnowledgeBase\Tests\KnowledgeBaseCategory\RequestFactories\CreateKnowledgeBaseCategoryRequestFactory;
Expand All @@ -54,7 +55,7 @@
// Permission Tests

test('CreateKnowledgeBaseCategory is gated with proper access control', function () {
$user = User::factory()->create();
$user = User::factory()->licensed(LicenseType::cases())->create();

actingAs($user)
->get(
Expand Down Expand Up @@ -91,7 +92,7 @@

$settings->save();

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

$user->givePermissionTo('knowledge_base_category.view-any');
$user->givePermissionTo('knowledge_base_category.create');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use function Pest\Livewire\livewire;
use function PHPUnit\Framework\assertEquals;

use AdvisingApp\Authorization\Enums\LicenseType;
use AdvisingApp\KnowledgeBase\Models\KnowledgeBaseCategory;
use AdvisingApp\KnowledgeBase\Filament\Resources\KnowledgeBaseCategoryResource;
use AdvisingApp\KnowledgeBase\Tests\KnowledgeBaseCategory\RequestFactories\EditKnowledgeBaseCategoryRequestFactory;
Expand All @@ -53,7 +54,7 @@
// Permission Tests

test('EditKnowledgeBaseCategory is gated with proper access control', function () {
$user = User::factory()->create();
$user = User::factory()->licensed(LicenseType::cases())->create();

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

Expand Down Expand Up @@ -98,7 +99,7 @@

$settings->save();

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

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