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;
|
2024-01-11 07:52:30 +00:00
|
|
|
use Illuminate\Foundation\Auth\EmailVerificationRequest;
|
2023-03-17 14:33:48 +00:00
|
|
|
use Illuminate\Foundation\Validation\ValidatesRequests;
|
2024-01-11 07:52:30 +00:00
|
|
|
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;
|
2023-09-06 10:07:34 +00:00
|
|
|
use Illuminate\Support\Facades\Crypt;
|
|
|
|
use Illuminate\Support\Facades\Hash;
|
2024-06-10 20:43:34 +00:00
|
|
|
use Illuminate\Support\Facades\Password;
|
2023-09-15 09:19:36 +00:00
|
|
|
use Illuminate\Support\Str;
|
2024-01-07 15:23:41 +00:00
|
|
|
use Laravel\Fortify\Contracts\FailedPasswordResetLinkRequestResponse;
|
|
|
|
use Laravel\Fortify\Contracts\SuccessfulPasswordResetLinkRequestResponse;
|
2024-06-10 20:43:34 +00:00
|
|
|
use Laravel\Fortify\Fortify;
|
2023-03-17 14:33:48 +00:00
|
|
|
|
|
|
|
class Controller extends BaseController
|
|
|
|
{
|
|
|
|
use AuthorizesRequests, ValidatesRequests;
|
2023-06-07 13:08:35 +00:00
|
|
|
|
2024-06-10 20:43:34 +00:00
|
|
|
public function realtime_test()
|
|
|
|
{
|
2024-01-07 15:23:41 +00:00
|
|
|
if (auth()->user()?->currentTeam()->id !== 0) {
|
|
|
|
return redirect(RouteServiceProvider::HOME);
|
|
|
|
}
|
|
|
|
TestEvent::dispatch();
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-01-07 15:23:41 +00:00
|
|
|
return 'Look at your other tab.';
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
|
|
|
public function verify()
|
|
|
|
{
|
2024-01-07 15:23:41 +00:00
|
|
|
return view('auth.verify-email');
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
|
|
|
public function email_verify(EmailVerificationRequest $request)
|
|
|
|
{
|
2024-01-11 07:52:30 +00:00
|
|
|
$request->fulfill();
|
2024-01-07 15:23:41 +00:00
|
|
|
$name = request()->user()?->name;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-05-06 10:39:47 +00:00
|
|
|
// send_internal_notification("User {$name} verified their email address.");
|
2024-01-07 15:23:41 +00:00
|
|
|
return redirect(RouteServiceProvider::HOME);
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
|
|
|
public function forgot_password(Request $request)
|
|
|
|
{
|
2024-01-07 15:23:41 +00:00
|
|
|
if (is_transactional_emails_active()) {
|
2024-01-11 07:52:30 +00:00
|
|
|
$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();
|
2024-06-10 20:43:34 +00:00
|
|
|
if (! $type) {
|
2024-01-07 15:23:41 +00:00
|
|
|
return response()->json(['message' => 'Transactional emails are not active'], 400);
|
|
|
|
}
|
2024-01-11 07:52:30 +00:00
|
|
|
$request->validate([Fortify::email() => 'required|email']);
|
2024-01-07 15:23:41 +00:00
|
|
|
$status = Password::broker(config('fortify.passwords'))->sendResetLink(
|
2024-01-11 07:52:30 +00:00
|
|
|
$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);
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-01-07 15:23:41 +00:00
|
|
|
return app(FailedPasswordResetLinkRequestResponse::class, ['status' => $status]);
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-01-07 15:23:41 +00:00
|
|
|
return response()->json(['message' => 'Transactional emails are not active'], 400);
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-09-06 10:07:34 +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();
|
2024-06-10 20:43:34 +00:00
|
|
|
if (! $user) {
|
2023-09-06 10:07:34 +00:00
|
|
|
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();
|
|
|
|
}
|
2023-12-13 14:22:37 +00:00
|
|
|
if (is_null(data_get($user, 'email_verified_at'))) {
|
2023-10-26 11:00:40 +00:00
|
|
|
$user->email_verified_at = now();
|
|
|
|
$user->save();
|
|
|
|
}
|
2023-09-06 10:07:34 +00:00
|
|
|
Auth::login($user);
|
|
|
|
session(['currentTeam' => $team]);
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-09-06 10:07:34 +00:00
|
|
|
return redirect()->route('dashboard');
|
|
|
|
}
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-09-06 10:07:34 +00:00
|
|
|
return redirect()->route('login')->with('error', 'Invalid credentials.');
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2024-01-07 15:23:41 +00:00
|
|
|
public function accept_invitation()
|
2023-06-12 10:00:01 +00:00
|
|
|
{
|
|
|
|
try {
|
2023-12-13 14:22:37 +00:00
|
|
|
$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) {
|
2023-12-13 14:22:37 +00:00
|
|
|
if ($resetPassword) {
|
|
|
|
$user->update([
|
|
|
|
'password' => Hash::make($invitationUuid),
|
2024-06-10 20:43:34 +00:00
|
|
|
'force_password_reset' => true,
|
2023-12-13 14:22:37 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
if ($user->teams()->where('team_id', $invitation->team->id)->exists()) {
|
|
|
|
$invitation->delete();
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-12-13 14:22:37 +00:00
|
|
|
return redirect()->route('team.index');
|
|
|
|
}
|
2023-06-12 10:00:01 +00:00
|
|
|
$user->teams()->attach($invitation->team->id, ['role' => $invitation->role]);
|
|
|
|
$invitation->delete();
|
2023-12-13 14:22:37 +00:00
|
|
|
if (auth()->user()?->id !== $user->id) {
|
|
|
|
return redirect()->route('login');
|
|
|
|
}
|
|
|
|
refreshSession($invitation->team);
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-08-30 14:01:38 +00:00
|
|
|
return redirect()->route('team.index');
|
2023-06-12 10:00:01 +00:00
|
|
|
} else {
|
|
|
|
abort(401);
|
|
|
|
}
|
2023-09-15 13:34:25 +00:00
|
|
|
} 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-08-08 09:51:36 +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();
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-08-30 14:01:38 +00:00
|
|
|
return redirect()->route('team.index');
|
2023-09-15 13:34:25 +00:00
|
|
|
} catch (\Throwable $e) {
|
2023-09-11 15:36:30 +00:00
|
|
|
throw $e;
|
2023-06-12 10:00:01 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
}
|