lasthourcloud/app/Http/Controllers/Controller.php

177 lines
5.1 KiB
PHP
Raw Normal View History

2023-03-17 14:33:48 +00:00
<?php
namespace App\Http\Controllers;
2023-06-07 13:08:35 +00:00
use App\Models\InstanceSettings;
use App\Models\Project;
use App\Models\S3Storage;
2023-06-07 13:08:35 +00:00
use App\Models\Server;
2023-08-11 14:13:53 +00:00
use App\Models\StandalonePostgresql;
2023-06-12 10:00:01 +00:00
use App\Models\TeamInvitation;
use App\Models\User;
2023-08-15 12:11:38 +00:00
use App\Models\Waitlist;
2023-03-17 14:33:48 +00:00
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
2023-08-08 15:28:36 +00:00
use Throwable;
2023-03-17 14:33:48 +00:00
class Controller extends BaseController
{
use AuthorizesRequests, ValidatesRequests;
2023-06-07 13:08:35 +00:00
2023-08-15 12:11:38 +00:00
public function waitlist() {
$waiting_in_line = Waitlist::whereVerified(true)->count();
return view('auth.waitlist', [
'waiting_in_line' => $waiting_in_line,
]);
}
2023-07-14 09:27:08 +00:00
public function subscription()
2023-06-22 12:48:47 +00:00
{
if (!is_cloud()) {
2023-07-14 10:09:56 +00:00
abort(404);
}
2023-08-24 14:14:09 +00:00
return view('subscription.show', [
2023-08-11 18:48:52 +00:00
'settings' => InstanceSettings::get(),
2023-07-14 09:27:08 +00:00
]);
2023-06-22 12:48:47 +00:00
}
2023-07-14 10:09:56 +00:00
public function license()
{
if (!is_cloud()) {
2023-07-14 10:09:56 +00:00
abort(404);
}
return view('settings.license', [
2023-08-11 18:48:52 +00:00
'settings' => InstanceSettings::get(),
2023-07-14 10:09:56 +00:00
]);
}
2023-08-15 12:11:38 +00:00
public function force_passoword_reset() {
return view('auth.force-password-reset');
}
2023-06-07 13:08:35 +00:00
public function dashboard()
{
$projects = Project::ownedByCurrentTeam()->get();
$servers = Server::ownedByCurrentTeam()->get();
2023-08-07 13:31:42 +00:00
$s3s = S3Storage::ownedByCurrentTeam()->get();
2023-06-07 13:08:35 +00:00
$resources = 0;
foreach ($projects as $project) {
$resources += $project->applications->count();
2023-08-07 16:46:40 +00:00
$resources += $project->postgresqls->count();
2023-06-07 13:08:35 +00:00
}
return view('dashboard', [
'servers' => $servers->count(),
'projects' => $projects->count(),
'resources' => $resources,
2023-08-07 13:31:42 +00:00
's3s' => $s3s,
2023-06-07 13:08:35 +00:00
]);
}
2023-08-22 15:44:49 +00:00
public function boarding() {
if (currentTeam()->boarding || is_dev()) {
return view('boarding');
} else {
return redirect()->route('dashboard');
}
}
2023-06-07 13:08:35 +00:00
public function settings()
{
2023-08-22 15:44:49 +00:00
if (isInstanceAdmin()) {
2023-06-07 13:08:35 +00:00
$settings = InstanceSettings::get();
2023-08-11 14:13:53 +00:00
$database = StandalonePostgresql::whereName('coolify-db')->first();
if ($database) {
$s3s = S3Storage::whereTeamId(0)->get();
}
2023-06-07 20:07:26 +00:00
return view('settings.configuration', [
2023-08-11 14:13:53 +00:00
'settings' => $settings,
'database' => $database,
's3s' => $s3s ?? [],
2023-06-07 13:08:35 +00:00
]);
} else {
return redirect()->route('dashboard');
}
}
2023-06-09 13:55:21 +00:00
public function team()
{
$invitations = [];
2023-07-13 20:03:27 +00:00
if (auth()->user()->isAdminFromSession()) {
2023-08-22 15:44:49 +00:00
$invitations = TeamInvitation::whereTeamId(currentTeam()->id)->get();
2023-06-09 13:55:21 +00:00
}
return view('team.show', [
'invitations' => $invitations,
]);
}
public function storages()
{
2023-08-07 13:31:42 +00:00
$s3 = S3Storage::ownedByCurrentTeam()->get();
return view('team.storages.all', [
's3' => $s3,
]);
}
public function storages_show()
{
2023-08-07 13:31:42 +00:00
$storage = S3Storage::ownedByCurrentTeam()->whereUuid(request()->storage_uuid)->firstOrFail();
return view('team.storages.show', [
'storage' => $storage,
]);
}
2023-07-13 20:03:27 +00:00
public function members()
{
$invitations = [];
if (auth()->user()->isAdminFromSession()) {
2023-08-22 15:44:49 +00:00
$invitations = TeamInvitation::whereTeamId(currentTeam()->id)->get();
2023-07-13 20:03:27 +00:00
}
return view('team.members', [
'invitations' => $invitations,
]);
}
2023-06-15 07:15:41 +00:00
public function acceptInvitation()
2023-06-12 10:00:01 +00:00
{
try {
$invitation = TeamInvitation::whereUuid(request()->route('uuid'))->firstOrFail();
$user = User::whereEmail($invitation->email)->firstOrFail();
if (is_null(auth()->user())) {
return redirect()->route('login');
}
if (auth()->user()->id !== $user->id) {
abort(401);
}
2023-06-15 07:15:41 +00:00
$createdAt = $invitation->created_at;
$diff = $createdAt->diffInMinutes(now());
2023-06-12 10:00:01 +00:00
if ($diff <= config('constants.invitation.link.expiration')) {
$user->teams()->attach($invitation->team->id, ['role' => $invitation->role]);
$invitation->delete();
return redirect()->route('team.show');
} else {
$invitation->delete();
abort(401);
}
2023-08-08 15:28:36 +00:00
} catch (Throwable $th) {
2023-06-12 10:00:01 +00:00
throw $th;
}
}
2023-06-15 07:15:41 +00:00
public function revokeInvitation()
2023-06-12 10:00:01 +00:00
{
try {
$invitation = TeamInvitation::whereUuid(request()->route('uuid'))->firstOrFail();
$user = User::whereEmail($invitation->email)->firstOrFail();
if (is_null(auth()->user())) {
return redirect()->route('login');
}
if (auth()->user()->id !== $user->id) {
abort(401);
}
$invitation->delete();
return redirect()->route('team.show');
2023-08-08 15:28:36 +00:00
} catch (Throwable $th) {
2023-06-12 10:00:01 +00:00
throw $th;
}
}
}