Skip to content

Commit

Permalink
Allow selecting users that are allowed to see the feature flag enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonio Ribeiro committed Jun 8, 2024
1 parent b599ae2 commit c1b78b5
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 17 deletions.
Binary file modified docs/screenshot02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 38 additions & 1 deletion src/Models/TwillFeatureFlag.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use A17\Twill\Models\Model;
use Illuminate\Support\Str;
use A17\Twill\Models\Behaviors\HasRelated;
use A17\Twill\Models\Behaviors\HasRevisions;
use Illuminate\Foundation\Auth\User as AuthenticatableContract;

/**
* @property string $code
Expand All @@ -18,9 +20,18 @@
*/
class TwillFeatureFlag extends Model
{
use HasRelated;
use HasRevisions;

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

/**
* Save the model to the database.
Expand Down Expand Up @@ -49,4 +60,30 @@ public function getPubliclyAvailableIpsAttribute(): string|null
{
return $this->ip_addresses ?? null;
}

public function userIsPubliclyAllowed(AuthenticatableContract|null $user): bool
{
if ($user === null) {
return false;
}

/** @phpstan-ignore-next-line */
if ($user->published === false) {
return false;
}

/** @phpstan-ignore-next-line */
if ($user->isSuperAdmin()) {
return true;
}

$allowedUsers = $this->getRelated('allowed_twill_users');

if ($allowedUsers->isEmpty()) {
return true;
}

/** @phpstan-ignore-next-line */
return $allowedUsers->pluck('email')->contains($user->email);
}
}
12 changes: 9 additions & 3 deletions src/Repositories/TwillFeatureFlagRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class TwillFeatureFlagRepository extends ModuleRepository
{
use HandleRevisions;

protected $relatedBrowsers = [
'allowed_twill_users' => ['moduleName' => 'users', 'relation' => 'allowed_twill_users'],
];

public function __construct(TwillFeatureFlag $model = null)
{
$this->bootCache();
Expand Down Expand Up @@ -53,7 +57,8 @@ public function getFeature(string $code): bool

private function isRealProduction(): bool
{
return (new Collection(config('app.domains.publicly_available')))->contains(request()->getHost());
return app()->environment('production') &&
(new Collection(config('app.domains.publicly_available')))->contains(request()->getHost());
}

public function featureList(bool $all = false): array
Expand Down Expand Up @@ -149,7 +154,8 @@ public function isPubliclyAvailableToTwillUsers(TwillFeatureFlag $featureFlag):
return false;
}

// Is the user authenticated on Twill?
return auth('twill_users')->check();
$auth = auth('twill_users');

return $auth->check() && $featureFlag->userIsPubliclyAllowed($auth->user());
}
}
39 changes: 26 additions & 13 deletions src/resources/views/admin/form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@
'type' => 'text'
])

@formField('input', [
'label' => 'Description',
'name' => 'description',
'rows' => 4,
'type' => 'textarea'
])

@formField('checkbox', [
'name' => 'publicly_available',
'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 All @@ -29,10 +29,23 @@
'translated' => false,
])

@formField('input', [
'label' => 'Description',
'name' => 'description',
'rows' => 4,
'type' => 'textarea'
])
@if (features_can_be_public_on_twill())
@formField('checkbox', [
'name' => 'publicly_available_twill_users',
'label' => 'Publicly available for users logged in Twill',
])

@formConnectedFields([
'fieldName' => 'publicly_available_twill_users',
'fieldValues' => true,
])
@formField('browser', [
'moduleName' => 'users',
'name' => 'allowed_twill_users',
'label' => 'Allowed users',
'note' => 'If no users are selected, all users will be allowed',
'max' => 999,
])
@endformConnectedFields
@endif
@stop

0 comments on commit c1b78b5

Please sign in to comment.