refactor is_instance_admin

This commit is contained in:
Andras Bacsai 2023-08-14 16:56:13 +02:00
parent f8506f9d20
commit 952d335789
9 changed files with 29 additions and 16 deletions

View File

@ -59,7 +59,7 @@ public function dashboard()
public function settings() public function settings()
{ {
if (auth()->user()->isInstanceAdmin()) { if (is_instance_admin()) {
$settings = InstanceSettings::get(); $settings = InstanceSettings::get();
$database = StandalonePostgresql::whereName('coolify-db')->first(); $database = StandalonePostgresql::whereName('coolify-db')->first();
if ($database) { if ($database) {

View File

@ -12,7 +12,7 @@ class ServerController extends Controller
public function new_server() public function new_server()
{ {
if (!is_cloud()) { if (!is_cloud() || is_instance_admin()) {
return view('server.create', [ return view('server.create', [
'limit_reached' => false, 'limit_reached' => false,
'private_keys' => PrivateKey::ownedByCurrentTeam()->get(), 'private_keys' => PrivateKey::ownedByCurrentTeam()->get(),

View File

@ -10,16 +10,20 @@ class SubscriptionValid
{ {
public function handle(Request $request, Closure $next): Response public function handle(Request $request, Closure $next): Response
{ {
$is_instance_admin = auth()->user()?->isInstanceAdmin();
if (!auth()->user() || !is_cloud()) { if (!auth()->user() || !is_cloud()) {
if ($request->path() === 'subscription' && !$is_instance_admin) { if ($request->path() === 'subscription') {
return redirect('/'); return redirect('/');
} else { } else {
return $next($request); return $next($request);
} }
} }
if (is_subscription_active() && $request->path() === 'subscription' && !$is_instance_admin) { $is_instance_admin = is_instance_admin();
if ($is_instance_admin) {
return $next($request);
}
if (is_subscription_active() && $request->path() === 'subscription') {
return redirect('/'); return redirect('/');
} }
if (is_subscription_in_grace_period()) { if (is_subscription_in_grace_period()) {

View File

@ -29,6 +29,11 @@ function generate_readme_file(string $name, string $updated_at): string
return "Resource name: $name\nLatest Deployment Date: $updated_at"; return "Resource name: $name\nLatest Deployment Date: $updated_at";
} }
function is_instance_admin()
{
return auth()->user()?->isInstanceAdmin();
}
function general_error_handler(Throwable|null $err = null, $that = null, $isJson = false, $customErrorMessage = null): mixed function general_error_handler(Throwable|null $err = null, $that = null, $isJson = false, $customErrorMessage = null): mixed
{ {
try { try {

View File

@ -46,19 +46,21 @@ function getEndDate()
function is_subscription_active() function is_subscription_active()
{ {
$team = auth()->user()?->currentTeam(); $team = auth()->user()?->currentTeam();
if (!$team) { if (!$team) {
return false; return false;
} }
if (is_instance_admin()) {
return true;
}
$subscription = $team?->subscription; $subscription = $team?->subscription;
if (!$subscription) { if (!$subscription) {
return false; return false;
} }
$is_active = $subscription->lemon_status === 'active'; $is_active = $subscription->lemon_status === 'active';
$is_instance_admin = auth()->user()->isInstanceAdmin();
ray($is_instance_admin);
return $is_active || $is_instance_admin; return $is_active;
} }
function is_subscription_in_grace_period() function is_subscription_in_grace_period()
{ {
@ -66,13 +68,15 @@ function is_subscription_in_grace_period()
if (!$team) { if (!$team) {
return false; return false;
} }
if (is_instance_admin()) {
return true;
}
$subscription = $team?->subscription; $subscription = $team?->subscription;
if (!$subscription) { if (!$subscription) {
return false; return false;
} }
$is_instance_admin = auth()->user()->isInstanceAdmin();
$is_still_grace_period = $subscription->lemon_ends_at && $is_still_grace_period = $subscription->lemon_ends_at &&
Carbon::parse($subscription->lemon_ends_at) > Carbon::now(); Carbon::parse($subscription->lemon_ends_at) > Carbon::now();
return $is_still_grace_period || $is_instance_admin; return $is_still_grace_period;
} }

View File

@ -25,7 +25,7 @@
<body> <body>
@livewireScripts @livewireScripts
<x-toaster-hub /> <x-toaster-hub />
@if (auth()->user()->isInstanceAdmin() || is_subscription_in_grace_period()) @if (is_instance_admin() || is_subscription_in_grace_period())
<div class="fixed top-3 left-4" id="vue"> <div class="fixed top-3 left-4" id="vue">
<magic-bar></magic-bar> <magic-bar></magic-bar>
</div> </div>

View File

@ -51,7 +51,7 @@ class="{{ request()->is('command-center') ? 'text-warning icon' : 'icon' }}" vie
</a> </a>
</li> </li>
<div class="flex-1"></div> <div class="flex-1"></div>
@if (auth()->user()->isInstanceAdmin()) @if (is_instance_admin())
<livewire:upgrade /> <livewire:upgrade />
@endif @endif
<li title="Profile"> <li title="Profile">
@ -79,7 +79,7 @@ class="{{ request()->is('command-center') ? 'text-warning icon' : 'icon' }}" vie
</svg> </svg>
</a> </a>
</li> </li>
@if (auth()->user()->isInstanceAdmin()) @if (is_instance_admin())
<li title="Settings" class="mt-auto"> <li title="Settings" class="mt-auto">
<a class="hover:bg-transparent" @if (!request()->is('settings')) href="/settings" @endif> <a class="hover:bg-transparent" @if (!request()->is('settings')) href="/settings" @endif>
<svg xmlns="http://www.w3.org/2000/svg" <svg xmlns="http://www.w3.org/2000/svg"

View File

@ -16,7 +16,7 @@
<x-forms.button type="submit"> <x-forms.button type="submit">
Save Save
</x-forms.button> </x-forms.button>
@if (auth()->user()->isInstanceAdmin()) @if (is_instance_admin())
<x-forms.button wire:click='copyFromInstanceSettings'> <x-forms.button wire:click='copyFromInstanceSettings'>
Copy from Instance Settings Copy from Instance Settings
</x-forms.button> </x-forms.button>

View File

@ -26,7 +26,7 @@
<h3 class="pb-4">Invite a new member</h3> <h3 class="pb-4">Invite a new member</h3>
@else @else
<h3>Invite a new member</h3> <h3>Invite a new member</h3>
@if (auth()->user()->isInstanceAdmin()) @if (is_instance_admin())
<div class="pb-4 text-xs text-warning">You need to configure <a href="/settings/emails" <div class="pb-4 text-xs text-warning">You need to configure <a href="/settings/emails"
class="underline text-warning">Transactional class="underline text-warning">Transactional
Emails</a> Emails</a>