lasthourcloud/app/Http/Controllers/Controller.php

180 lines
5.2 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-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;
use Auth;
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;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Http;
2023-08-08 15:28:36 +00:00
use Throwable;
use Str;
2023-03-17 14:33:48 +00:00
class Controller extends BaseController
{
use AuthorizesRequests, ValidatesRequests;
2023-06-07 13:08:35 +00:00
public function link()
{
$token = request()->get('token');
if ($token) {
$decrypted = Crypt::decryptString($token);
$email = Str::of($decrypted)->before('@@@');
$password = Str::of($decrypted)->after('@@@');
$user = User::whereEmail($email)->first();
if (!$user) {
return redirect()->route('login');
}
if (Hash::check($password, $user->password)) {
Auth::login($user);
$team = $user->teams()->first();
session(['currentTeam' => $team]);
return redirect()->route('dashboard');
}
}
return redirect()->route('login')->with('error', 'Invalid credentials.');
}
2023-07-14 09:27:08 +00:00
public function subscription()
2023-06-22 12:48:47 +00:00
{
2023-08-31 07:56:37 +00:00
if (!isCloud()) {
2023-07-14 10:09:56 +00:00
abort(404);
}
2023-08-30 14:01:38 +00:00
return view('subscription.index', [
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()
{
2023-08-31 07:56:37 +00:00
if (!isCloud()) {
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
]);
}
public function force_passoword_reset()
{
2023-08-15 12:11:38 +00:00
return view('auth.force-password-reset');
}
public function boarding()
{
if (currentTeam()->boarding || isDev()) {
2023-08-22 15:44:49 +00:00
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
}
2023-08-30 14:01:38 +00:00
return view('team.index', [
2023-06-09 13:55:21 +00:00
'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();
2023-08-30 14:01:38 +00:00
return redirect()->route('team.index');
2023-06-12 10:00:01 +00:00
} 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();
2023-08-30 14:01:38 +00:00
return redirect()->route('team.index');
2023-08-08 15:28:36 +00:00
} catch (Throwable $th) {
2023-06-12 10:00:01 +00:00
throw $th;
}
}
}