lasthourcloud/app/Http/Controllers/Controller.php

152 lines
5.9 KiB
PHP
Raw Normal View History

2023-03-17 14:33:48 +00:00
<?php
namespace App\Http\Controllers;
2024-01-07 15:23:41 +00:00
use App\Events\TestEvent;
2023-06-12 10:00:01 +00:00
use App\Models\TeamInvitation;
use App\Models\User;
2024-01-07 15:23:41 +00:00
use App\Providers\RouteServiceProvider;
2023-03-17 14:33:48 +00:00
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Auth\EmailVerificationRequest;
2023-03-17 14:33:48 +00:00
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;
2023-03-17 14:33:48 +00:00
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;
2024-01-07 15:23:41 +00:00
use Laravel\Fortify\Fortify;
use Laravel\Fortify\Contracts\FailedPasswordResetLinkRequestResponse;
use Laravel\Fortify\Contracts\SuccessfulPasswordResetLinkRequestResponse;
use Illuminate\Support\Facades\Password;
2023-03-17 14:33:48 +00:00
class Controller extends BaseController
{
use AuthorizesRequests, ValidatesRequests;
2023-06-07 13:08:35 +00:00
2024-01-07 15:23:41 +00:00
public function realtime_test() {
if (auth()->user()?->currentTeam()->id !== 0) {
return redirect(RouteServiceProvider::HOME);
}
TestEvent::dispatch();
return 'Look at your other tab.';
}
public function verify() {
return view('auth.verify-email');
}
public function email_verify(EmailVerificationRequest $request) {
$request->fulfill();
2024-01-07 15:23:41 +00:00
$name = request()->user()?->name;
// send_internal_notification("User {$name} verified their email address.");
2024-01-07 15:23:41 +00:00
return redirect(RouteServiceProvider::HOME);
}
public function forgot_password(Request $request) {
2024-01-07 15:23:41 +00:00
if (is_transactional_emails_active()) {
$arrayOfRequest = $request->only(Fortify::email());
$request->merge([
2024-01-07 15:23:41 +00:00
'email' => Str::lower($arrayOfRequest['email']),
]);
$type = set_transanctional_email_settings();
if (!$type) {
return response()->json(['message' => 'Transactional emails are not active'], 400);
}
$request->validate([Fortify::email() => 'required|email']);
2024-01-07 15:23:41 +00:00
$status = Password::broker(config('fortify.passwords'))->sendResetLink(
$request->only(Fortify::email())
2024-01-07 15:23:41 +00:00
);
if ($status == Password::RESET_LINK_SENT) {
return app(SuccessfulPasswordResetLinkRequestResponse::class, ['status' => $status]);
}
if ($status == Password::RESET_THROTTLED) {
return response('Already requested a password reset in the past minutes.', 400);
}
return app(FailedPasswordResetLinkRequestResponse::class, ['status' => $status]);
}
return response()->json(['message' => 'Transactional emails are not active'], 400);
}
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.');
}
2024-01-07 15:23:41 +00:00
public function accept_invitation()
2023-06-12 10:00:01 +00:00
{
try {
$resetPassword = request()->query('reset-password');
$invitationUuid = request()->route('uuid');
$invitation = TeamInvitation::whereUuid($invitationUuid)->firstOrFail();
2023-06-12 10:00:01 +00:00
$user = User::whereEmail($invitation->email)->firstOrFail();
2023-09-15 09:19:36 +00:00
$invitationValid = $invitation->isValid();
if ($invitationValid) {
if ($resetPassword) {
$user->update([
'password' => Hash::make($invitationUuid),
'force_password_reset' => true
]);
}
if ($user->teams()->where('team_id', $invitation->team->id)->exists()) {
$invitation->delete();
return redirect()->route('team.index');
}
2023-06-12 10:00:01 +00:00
$user->teams()->attach($invitation->team->id, ['role' => $invitation->role]);
$invitation->delete();
if (auth()->user()?->id !== $user->id) {
return redirect()->route('login');
}
refreshSession($invitation->team);
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
}
}
2024-01-07 15:23:41 +00:00
public function revoke_invitation()
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
}
}
}