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

Allow features to be available publicly for Twill users #2

Closed
wants to merge 5 commits into from
Closed
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,15 @@ Or in Blade:
```

Don't forget to add the feature flags to your navigation too.

## Allow users logged in Twill option
You can allow your users to have access to a feature in public available domains if they are logged in on Twill. But there are some caveats for it to work:

- Twill must be int he same domain of the web application, meaning that `ADMIN_APP_URL` must be empty or have the same domain as the frontend; or
- The Laravel session domain should set in order for the apps to share the session cookie:

```dotenv
SESSION_DOMAIN=.laravel-twill-project.test
```

Also, make sure your sessions are working fine, when you switch to a shared domain they might break and a browser cookie clear might be needed.
6 changes: 2 additions & 4 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ parameters:

excludePaths:

checkMissingIterableValueType: false

universalObjectCratesClasses:

ignoreErrors:
- identifier: missingType.generics
- identifier: missingType.iterableValue

reportUnmatchedIgnoredErrors: false

checkGenericClassInNonGenericObjectType: false
6 changes: 6 additions & 0 deletions src/Http/Controllers/TwillFeatureFlagController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ class TwillFeatureFlagController extends ModuleController
'sort' => true,
],

'publicly_available_twill_users' => [
'title' => 'Publicly available for Twill users',
'field' => 'publicly_available_twill_users_yes_no',
'sort' => true,
],

'publicly_available_ips' => [
'title' => 'Publicly available to (IPs)',
'field' => 'publicly_available_ips',
Expand Down
8 changes: 7 additions & 1 deletion src/Models/TwillFeatureFlag.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
* @property string $publicly_available_yes_no
* @property string|null $publicly_available_ips
* @property bool $published
* @property bool $publicly_available_twill_users
*/
class TwillFeatureFlag extends Model
{
use HasRevisions;

protected $fillable = ['published', 'title', 'description', 'code', 'publicly_available', 'ip_addresses'];
protected $fillable = ['published', 'title', 'description', 'code', 'publicly_available', 'ip_addresses', 'publicly_available_twill_users'];

/**
* Save the model to the database.
Expand All @@ -39,6 +40,11 @@ public function getPubliclyAvailableYesNoAttribute(): string
return $this->publicly_available ? 'Yes' : '';
}

public function getPubliclyAvailableTwillUsersYesNoAttribute(): string
{
return $this->publicly_available_twill_users ? 'Yes' : '';
}

public function getPubliclyAvailableIpsAttribute(): string|null
{
return $this->ip_addresses ?? null;
Expand Down
32 changes: 26 additions & 6 deletions src/Repositories/TwillFeatureFlagRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function getFeature(string $code): bool
return false;
}

if (blank($featureFlag) || blank($featureFlag?->published) || $featureFlag?->published === false) {
if (blank($featureFlag) || blank($featureFlag->published) || $featureFlag->published === false) {
return false;
}

Expand Down Expand Up @@ -73,11 +73,8 @@ public function featureList(bool $all = false): array

private function isPubliclyAvailableToCurrentUser(TwillFeatureFlag $featureFlag): bool
{
return (new GeolocationService())->currentIpAddressIsOnList(
collect(explode(',', $featureFlag->ip_addresses))
->map(fn($ip) => trim($ip))
->toArray(),
);
return $this->isPubliclyAvailableToIpAddresses($featureFlag) ||
$this->isPubliclyAvailableToTwillUsers($featureFlag);
}

private function bootCache(): void
Expand Down Expand Up @@ -132,4 +129,27 @@ protected function url(array|bool $parsed, string $attribute): string|null

return $parsed[$attribute];
}

public function isPubliclyAvailableToIpAddresses(TwillFeatureFlag $featureFlag): bool
{
if (blank($featureFlag->ip_addresses)) {
return false;
}

return (new GeolocationService())->currentIpAddressIsOnList(
collect(explode(',', $featureFlag->ip_addresses))
->map(fn($ip) => trim($ip))
->toArray(),
);
}

public function isPubliclyAvailableToTwillUsers(TwillFeatureFlag $featureFlag): bool
{
if (!$featureFlag->publicly_available_twill_users) {
return false;
}

// Is the user authenticated on Twill?
return auth('twill_users')->check();
}
}
3 changes: 3 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Str;
use A17\Twill\Facades\TwillCapsules;
use A17\Twill\TwillPackageServiceProvider;
use A17\TwillFeatureFlags\Services\Helpers;

class ServiceProvider extends TwillPackageServiceProvider
{
Expand All @@ -15,6 +16,8 @@ public function boot(): void
{
$this->registerThisCapsule();

Helpers::load();

parent::boot();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Services/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ class Helpers
{
public static function load(): void
{
require __DIR__ . '/../Support/helpers.php';
require_once __DIR__ . '/../Support/helpers.php';
}
}
24 changes: 24 additions & 0 deletions src/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,27 @@ function feature_list(bool $all = false): array
return (new TwillFeatureFlagRepository())->featureList($all);
}
}

if (!function_exists('features_can_be_public_on_twill')) {
function features_can_be_public_on_twill(): bool
{
$sessionDomain = config('session.domain');

$twillAdminAppHost = parse_url(config('twill.admin_app_url') ?? '')['host'] ?? null;

$appDomainHost = parse_url(config('app.url') ?? '')['host'] ?? null;

// If the admin app is not set it probably means Twill is in the same domain of the frontend
if (blank($twillAdminAppHost)) {
return true;
}

// Otherwise the domain must be the same
if ($twillAdminAppHost === $appDomainHost) {
return true;
}

// Otherwise the session domain must be set
return filled($sessionDomain);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('twill_feature_flags', function (Blueprint $table) {
$table->boolean('publicly_available_twill_users')->default(false);
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('twill_feature_flags', function (Blueprint $table) {
$table->dropColumn('publicly_available_twill_users');
});
}
};
7 changes: 7 additions & 0 deletions src/resources/views/admin/form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
'label' => 'Publicly available',
])

@if(features_can_be_public_on_twill())
@formField('checkbox', [
'name' => 'publicly_available_twill_users',
'label' => 'Publicly available for users logged in Twill',
])
@endif

@formField('input', [
'label' => 'IP Addresses',
'name' => 'ip_addresses',
Expand Down
Loading