lasthourcloud/app/Http/Controllers/Controller.php

178 lines
5.5 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\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;
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-09-15 09:19:36 +00:00
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Hash;
2023-09-15 09:19:36 +00:00
use Illuminate\Support\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)) {
2023-09-15 09:19:36 +00:00
$invitation = TeamInvitation::whereEmail($email);
if ($invitation->exists()) {
$team = $invitation->first()->team;
$user->teams()->attach($team->id, ['role' => $invitation->first()->role]);
$invitation->delete();
} else {
$team = $user->teams()->first();
}
if (is_null(data_get($user, 'email_verified_at'))){
$user->email_verified_at = now();
$user->save();
}
Auth::login($user);
session(['currentTeam' => $team]);
return redirect()->route('dashboard');
}
}
return redirect()->route('login')->with('error', 'Invalid credentials.');
}
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) {
2023-12-11 14:23:41 +00:00
if ($database->status !== 'running') {
$database->status = 'running';
$database->save();
}
2023-08-11 14:13:53 +00:00
$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 (auth()->user()->id !== $user->id) {
abort(401);
}
2023-09-15 09:19:36 +00:00
$invitationValid = $invitation->isValid();
if ($invitationValid) {
2023-06-12 10:00:01 +00:00
$user->teams()->attach($invitation->team->id, ['role' => $invitation->role]);
2023-09-15 09:19:36 +00:00
refreshSession($invitation->team);
2023-06-12 10:00:01 +00:00
$invitation->delete();
2023-08-30 14:01:38 +00:00
return redirect()->route('team.index');
2023-06-12 10:00:01 +00:00
} else {
abort(401);
}
} catch (\Throwable $e) {
2023-09-15 09:19:36 +00:00
ray($e->getMessage());
2023-09-11 15:36:30 +00:00
throw $e;
2023-06-12 10:00:01 +00:00
}
}
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');
} catch (\Throwable $e) {
2023-09-11 15:36:30 +00:00
throw $e;
2023-06-12 10:00:01 +00:00
}
}
}