153 lines
5.6 KiB
PHP
153 lines
5.6 KiB
PHP
<?php
|
|
|
|
use App\Models\User;
|
|
use Flux\Flux;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Support\Facades\Gate;
|
|
use Livewire\Attributes\Computed;
|
|
use Livewire\Attributes\Title;
|
|
use Livewire\Component;
|
|
use Livewire\WithPagination;
|
|
|
|
new #[Title('Lifetime access')] class extends Component
|
|
{
|
|
use WithPagination;
|
|
|
|
private const PER_PAGE = 10;
|
|
|
|
public function mount(): void
|
|
{
|
|
Gate::authorize('accessAdmin');
|
|
}
|
|
|
|
public string $search = '';
|
|
|
|
public function updatedSearch(): void
|
|
{
|
|
$this->resetPage();
|
|
}
|
|
|
|
/**
|
|
* Get the users whose lifetime access status can be toggled, eager-loading
|
|
* their current organization so we can show and flip its lifetime access
|
|
* flag without extra queries.
|
|
*
|
|
* @return LengthAwarePaginator<User>
|
|
*/
|
|
#[Computed]
|
|
public function users(): LengthAwarePaginator
|
|
{
|
|
return $this->filteredUserQuery()
|
|
->with('currentOrganization')
|
|
->orderBy('name')
|
|
->paginate(self::PER_PAGE);
|
|
}
|
|
|
|
/**
|
|
* @return Builder<User>
|
|
*/
|
|
private function filteredUserQuery(): Builder
|
|
{
|
|
return User::query()
|
|
->when(
|
|
$this->search !== '',
|
|
fn (Builder $query) => $query->where(fn (Builder $query) => $query
|
|
->where('name', 'like', '%'.$this->search.'%')
|
|
->orWhere('email', 'like', '%'.$this->search.'%'))
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Flip the lifetime access flag on the given user's current organization.
|
|
* Every user has a personal organization, so this is the organization that
|
|
* controls whether they can use the app without a subscription.
|
|
*/
|
|
public function toggleLifetimeAccess(int $userId): void
|
|
{
|
|
Gate::authorize('accessAdmin');
|
|
|
|
$user = User::query()->findOrFail($userId);
|
|
$organization = $user->currentOrganization;
|
|
|
|
if ($organization === null) {
|
|
Flux::toast(variant: 'danger', text: __('This user has no organization.'));
|
|
|
|
return;
|
|
}
|
|
|
|
$organization->update(['lifetime_access' => ! $organization->lifetime_access]);
|
|
|
|
Flux::toast(
|
|
variant: 'success',
|
|
text: $organization->lifetime_access
|
|
? __('Lifetime access enabled for :name.', ['name' => $user->name])
|
|
: __('Lifetime access disabled for :name.', ['name' => $user->name]),
|
|
);
|
|
}
|
|
}; ?>
|
|
|
|
<div class="flex h-full w-full flex-1 flex-col gap-6">
|
|
<div>
|
|
<flux:heading size="xl">{{ __('Lifetime access') }}</flux:heading>
|
|
<flux:text class="mt-1">{{ __('Grant or revoke perpetual access so users can use the app without a subscription.') }}</flux:text>
|
|
</div>
|
|
|
|
<flux:input
|
|
size="sm"
|
|
type="search"
|
|
icon="magnifying-glass"
|
|
wire:model.live.debounce.300ms="search"
|
|
placeholder="{{ __('Search users…') }}"
|
|
class="w-full sm:w-64"
|
|
data-test="lifetime-access-search-input"
|
|
/>
|
|
|
|
<flux:card>
|
|
<flux:table :paginate="$this->users">
|
|
<flux:table.columns>
|
|
<flux:table.column>{{ __('Name') }}</flux:table.column>
|
|
<flux:table.column>{{ __('Email') }}</flux:table.column>
|
|
<flux:table.column width="5rem">{{ __('Lifetime access') }}</flux:table.column>
|
|
<flux:table.column align="end" width="5rem">{{ __('Actions') }}</flux:table.column>
|
|
</flux:table.columns>
|
|
|
|
<flux:table.rows>
|
|
@forelse ($this->users as $user)
|
|
<?php
|
|
$organization = $user->currentOrganization;
|
|
$hasLifetime = $organization?->lifetime_access === true;
|
|
?>
|
|
<flux:table.row :key="$user->id" data-test="lifetime-access-user-row-{{ $user->id }}">
|
|
<flux:table.cell>{{ $user->name }}</flux:table.cell>
|
|
<flux:table.cell class="text-zinc-500">{{ $user->email }}</flux:table.cell>
|
|
<flux:table.cell data-test="lifetime-access-status-{{ $user->id }}">
|
|
@if ($hasLifetime)
|
|
<flux:badge size="sm" color="green">{{ __('Lifetime') }}</flux:badge>
|
|
@else
|
|
<flux:badge size="sm" color="zinc">{{ __('None') }}</flux:badge>
|
|
@endif
|
|
</flux:table.cell>
|
|
<flux:table.cell class="py-0">
|
|
<flux:button
|
|
size="sm"
|
|
variant="{{ $hasLifetime ? 'danger' : 'primary' }}"
|
|
icon="{{ $hasLifetime ? 'x-mark' : 'check' }}"
|
|
:title="$hasLifetime ? __('Disable lifetime access') : __('Enable lifetime access')"
|
|
wire:click="toggleLifetimeAccess({{ $user->id }})"
|
|
data-test="toggle-lifetime-access-{{ $user->id }}"
|
|
/>
|
|
</flux:table.cell>
|
|
</flux:table.row>
|
|
@empty
|
|
<flux:table.row>
|
|
<flux:table.cell colspan="4" align="center" class="py-10">
|
|
<flux:text>{{ __('No users match your search.') }}</flux:text>
|
|
</flux:table.cell>
|
|
</flux:table.row>
|
|
@endforelse
|
|
</flux:table.rows>
|
|
</flux:table>
|
|
</flux:card>
|
|
</div>
|