TRF Certest first commit
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\Http\Controllers\Web\Auth;
|
||||
|
||||
use Illuminate\Contracts\Auth\PasswordBroker;
|
||||
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Vanguard\Http\Controllers\Controller;
|
||||
|
||||
class ForgotPasswordController extends Controller
|
||||
{
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Password Reset Controller
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| This controller is responsible for handling password reset emails and
|
||||
| includes a trait which assists in sending these notifications from
|
||||
| your application to your users. Feel free to explore this trait.
|
||||
|
|
||||
*/
|
||||
|
||||
use SendsPasswordResetEmails;
|
||||
|
||||
/**
|
||||
* Create a new controller instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('guest');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function sendResetLinkResponse(Request $request, $response): RedirectResponse
|
||||
{
|
||||
return back()->with('success', trans($response));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function sendResetLinkFailedResponse(Request $request, $response): RedirectResponse
|
||||
{
|
||||
$messages = ['email' => trans($response)];
|
||||
$httpResponse = back()->withInput($request->only('email'));
|
||||
|
||||
return $response === PasswordBroker::INVALID_USER
|
||||
? $httpResponse->withSuccess($messages)
|
||||
: $httpResponse->withErrors($messages);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\Http\Controllers\Web\Auth;
|
||||
|
||||
use Auth;
|
||||
use Illuminate\Contracts\Auth\Authenticatable as BaseAuthenticatable;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Vanguard\Events\User\LoggedIn;
|
||||
use Vanguard\Events\User\LoggedOut;
|
||||
use Vanguard\Http\Controllers\Controller;
|
||||
use Vanguard\Http\Requests\Auth\LoginRequest;
|
||||
use Vanguard\Repositories\Session\SessionRepository;
|
||||
use Vanguard\Repositories\User\UserRepository;
|
||||
use Vanguard\Services\Auth\ThrottlesLogins;
|
||||
use Vanguard\User;
|
||||
|
||||
class LoginController extends Controller
|
||||
{
|
||||
use ThrottlesLogins;
|
||||
|
||||
public function __construct(private readonly UserRepository $users)
|
||||
{
|
||||
$this->middleware('guest')->except('logout');
|
||||
$this->middleware('auth')->only('logout');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the application login form.
|
||||
*/
|
||||
public function show(): View
|
||||
{
|
||||
return view('auth.login', [
|
||||
'socialProviders' => config('auth.social.providers'),
|
||||
]);
|
||||
}
|
||||
|
||||
public function login(LoginRequest $request, SessionRepository $sessions): Response|RedirectResponse
|
||||
{
|
||||
// In case that request throttling is enabled, we have to check if user can perform this request.
|
||||
// We'll key this by the username and the IP address of the client making these requests into this application.
|
||||
$throttles = (bool) setting('throttle_enabled');
|
||||
|
||||
//Redirect URL that can be passed as hidden field.
|
||||
$to = $request->has('to') ? '?to='.$request->get('to') : '';
|
||||
|
||||
if ($throttles && $this->hasTooManyLoginAttempts($request)) {
|
||||
return $this->sendLockoutResponse($request);
|
||||
}
|
||||
|
||||
$credentials = $request->getCredentials();
|
||||
|
||||
if (! Auth::validate($credentials)) {
|
||||
// If the login attempt was unsuccessful we will increment the number of attempts
|
||||
// to log in and redirect the user back to the login form. Of course, when this
|
||||
// user surpasses their maximum number of attempts they will get locked out.
|
||||
if ($throttles) {
|
||||
$this->incrementLoginAttempts($request);
|
||||
}
|
||||
|
||||
return redirect()->to('login'.$to)
|
||||
->withErrors(trans('auth.failed'));
|
||||
}
|
||||
|
||||
$user = Auth::getProvider()->retrieveByCredentials($credentials);
|
||||
|
||||
if ($user->isBanned()) {
|
||||
return redirect()->to('login'.$to)
|
||||
->withErrors(trans('auth.banned'));
|
||||
}
|
||||
|
||||
$maxSessions = setting('max_active_sessions');
|
||||
if ($maxSessions && $sessions->getActiveSessionsCount($user->id) >= $maxSessions) {
|
||||
return redirect()->to('login'.$to)
|
||||
->withErrors(trans('auth.max_sessions_reached'));
|
||||
}
|
||||
|
||||
Auth::login($user, setting('remember_me') && $request->get('remember'));
|
||||
|
||||
return $this->authenticated($request, $throttles, $user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the response after the user was authenticated.
|
||||
*/
|
||||
protected function authenticated(
|
||||
Request $request,
|
||||
bool $throttles,
|
||||
BaseAuthenticatable $user,
|
||||
): Response|RedirectResponse {
|
||||
if ($throttles) {
|
||||
$this->clearLoginAttempts($request);
|
||||
}
|
||||
|
||||
$redirectPage = $request->get('to');
|
||||
|
||||
if (setting('2fa.enabled') && $user->twoFactorEnabled()) {
|
||||
return $this->logoutAndRedirectToTokenPage($request, $user, $redirectPage);
|
||||
}
|
||||
|
||||
event(new LoggedIn);
|
||||
|
||||
if ($redirectPage) {
|
||||
return redirect()->to($redirectPage);
|
||||
}
|
||||
|
||||
// Reindirizza in base al ruolo
|
||||
if ($user->hasRole('Admin')) {
|
||||
return redirect()->to('userarea/admin.php');
|
||||
} elseif ($user->hasRole('User')) {
|
||||
return redirect()->to('userarea/index.php');
|
||||
}
|
||||
|
||||
// Se il ruolo non è specificato, reindirizza alla home predefinita
|
||||
return redirect()->intended('/');
|
||||
|
||||
}
|
||||
|
||||
protected function logoutAndRedirectToTokenPage(Request $request, $user, ?string $redirectPage): RedirectResponse
|
||||
{
|
||||
Auth::logout();
|
||||
|
||||
$request->session()->put('auth.2fa.id', $user->id);
|
||||
|
||||
if ($redirectPage) {
|
||||
$request->session()->put('auth.redirect_to', $redirectPage);
|
||||
}
|
||||
|
||||
return redirect()->route('auth.token');
|
||||
}
|
||||
|
||||
/**
|
||||
* Log the user out of the application.
|
||||
*/
|
||||
public function logout(): RedirectResponse
|
||||
{
|
||||
event(new LoggedOut);
|
||||
|
||||
Auth::logout();
|
||||
|
||||
return redirect('login');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\Http\Controllers\Web\Auth;
|
||||
|
||||
use Illuminate\Auth\Events\Registered;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Vanguard\Http\Controllers\Controller;
|
||||
use Vanguard\Http\Requests\Auth\RegisterRequest;
|
||||
use Vanguard\Repositories\Role\RoleRepository;
|
||||
use Vanguard\Repositories\User\UserRepository;
|
||||
use Vanguard\Role;
|
||||
|
||||
class RegisterController extends Controller
|
||||
{
|
||||
public function __construct(private readonly UserRepository $users)
|
||||
{
|
||||
$this->middleware('registration')->only('show', 'register');
|
||||
}
|
||||
|
||||
public function show(): View
|
||||
{
|
||||
return view('auth.register', [
|
||||
'socialProviders' => config('auth.social.providers'),
|
||||
]);
|
||||
}
|
||||
|
||||
public function register(RegisterRequest $request, RoleRepository $roles): RedirectResponse
|
||||
{
|
||||
$user = $this->users->create(
|
||||
array_merge(
|
||||
$request->validFormData(),
|
||||
['role_id' => $roles->findByName(Role::DEFAULT_USER_ROLE)->id],
|
||||
)
|
||||
);
|
||||
|
||||
event(new Registered($user));
|
||||
|
||||
$message = setting('reg_email_confirmation')
|
||||
? __('Your account is created successfully! Please confirm your email.')
|
||||
: __('Your account is created successfully!');
|
||||
|
||||
\Auth::login($user);
|
||||
|
||||
return redirect('/')->with('success', $message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\Http\Controllers\Web\Auth;
|
||||
|
||||
use Illuminate\Auth\Events\PasswordReset;
|
||||
use Illuminate\Contracts\Auth\CanResetPassword;
|
||||
use Illuminate\Foundation\Auth\ResetsPasswords;
|
||||
use Illuminate\Support\Str;
|
||||
use Vanguard\Http\Controllers\Controller;
|
||||
|
||||
class ResetPasswordController extends Controller
|
||||
{
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Password Reset Controller
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| This controller is responsible for handling password reset requests
|
||||
| and uses a simple trait to include this behavior. You're free to
|
||||
| explore this trait and override any methods you wish to tweak.
|
||||
|
|
||||
*/
|
||||
|
||||
use ResetsPasswords;
|
||||
|
||||
/**
|
||||
* Where to redirect users after resetting their password.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $redirectTo = '/';
|
||||
|
||||
/**
|
||||
* Create a new controller instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('guest');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function resetPassword(CanResetPassword $user, string $password)
|
||||
{
|
||||
$user->password = $password;
|
||||
|
||||
$user->setRememberToken(Str::random(60));
|
||||
|
||||
$user->save();
|
||||
|
||||
event(new PasswordReset($user));
|
||||
|
||||
$this->guard()->login($user);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\Http\Controllers\Web\Auth;
|
||||
|
||||
use Auth;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Laravel\Socialite\Contracts\User as SocialUser;
|
||||
use Socialite;
|
||||
use Vanguard\Events\User\LoggedIn;
|
||||
use Vanguard\Http\Controllers\Controller;
|
||||
use Vanguard\Repositories\User\UserRepository;
|
||||
use Vanguard\Services\Auth\Social\SocialManager;
|
||||
use Vanguard\User;
|
||||
|
||||
class SocialAuthController extends Controller
|
||||
{
|
||||
public function __construct(private readonly UserRepository $users, private readonly SocialManager $socialManager)
|
||||
{
|
||||
$this->middleware('guest');
|
||||
}
|
||||
|
||||
/**
|
||||
* Redirect user to specified provider in order to complete the authentication process.
|
||||
*/
|
||||
public function redirectToProvider(string $provider): RedirectResponse
|
||||
{
|
||||
if (strtolower($provider) == 'facebook') {
|
||||
return Socialite::driver('facebook')->with(['auth_type' => 'rerequest'])->redirect();
|
||||
}
|
||||
|
||||
return Socialite::driver($provider)->redirect();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle response authentication provider.
|
||||
*/
|
||||
public function handleProviderCallback(string $provider): RedirectResponse
|
||||
{
|
||||
if (request()->get('error')) {
|
||||
return redirect('login')
|
||||
->withErrors(__('Something went wrong during the authentication process. Please try again.'));
|
||||
}
|
||||
|
||||
$socialUser = $this->getUserFromProvider($provider);
|
||||
|
||||
$user = $this->users->findBySocialId($provider, $socialUser->getId());
|
||||
|
||||
if (! $user) {
|
||||
if (! setting('reg_enabled')) {
|
||||
return redirect('login')
|
||||
->withErrors(__('Only users who already created an account can log in.'));
|
||||
}
|
||||
|
||||
if (! $socialUser->getEmail()) {
|
||||
return redirect('login')
|
||||
->withErrors(__('You have to provide your email address.'));
|
||||
}
|
||||
|
||||
$user = $this->socialManager->associate($socialUser, $provider);
|
||||
|
||||
event(new \Illuminate\Auth\Events\Registered($user));
|
||||
}
|
||||
|
||||
return $this->loginAndRedirect($user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user from authentication provider.
|
||||
*/
|
||||
private function getUserFromProvider(string $provider): SocialUser
|
||||
{
|
||||
return Socialite::driver($provider)->user();
|
||||
}
|
||||
|
||||
/**
|
||||
* Log provided user in and redirect him to intended page.
|
||||
*/
|
||||
private function loginAndRedirect(User $user): RedirectResponse
|
||||
{
|
||||
if ($user->isBanned()) {
|
||||
return redirect()->to('login')
|
||||
->withErrors(__('Your account is banned by administrator.'));
|
||||
}
|
||||
|
||||
if (setting('2fa.enabled') && $user->twoFactorEnabled()) {
|
||||
session()->put('auth.2fa.id', $user->id);
|
||||
|
||||
return redirect()->route('auth.token');
|
||||
}
|
||||
|
||||
Auth::login($user);
|
||||
|
||||
event(new LoggedIn);
|
||||
|
||||
return redirect()->intended('/');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\Http\Controllers\Web\Auth;
|
||||
|
||||
use Auth;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Vanguard\Events\User\LoggedIn;
|
||||
use Vanguard\Http\Controllers\Controller;
|
||||
use Vanguard\Http\Requests\TwoFactor\TwoFactorLoginRequest;
|
||||
use Vanguard\Repositories\User\UserRepository;
|
||||
use Vanguard\Services\Auth\ThrottlesLogins;
|
||||
|
||||
class TwoFactorTokenController extends Controller
|
||||
{
|
||||
use ThrottlesLogins;
|
||||
|
||||
public function __construct(private readonly UserRepository $users)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Show Two-Factor Token form.
|
||||
*/
|
||||
public function show(): View|RedirectResponse
|
||||
{
|
||||
return session('auth.2fa.id') ? view('auth.token') : redirect('login');
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle Two-Factor token form submission.
|
||||
*/
|
||||
public function update(TwoFactorLoginRequest $request): RedirectResponse
|
||||
{
|
||||
$this->validate($request, ['code' => 'required']);
|
||||
|
||||
if (! session('auth.2fa.id')) {
|
||||
return redirect('login');
|
||||
}
|
||||
|
||||
$user = $this->users->find(
|
||||
$request->session()->pull('auth.2fa.id')
|
||||
);
|
||||
|
||||
if (!$user) {
|
||||
throw new NotFoundHttpException;
|
||||
}
|
||||
|
||||
$customRedirect = $request->session()->pull('auth.redirect_to') ?: '';
|
||||
|
||||
if (!$request->hasValidCode($user)) {
|
||||
return redirect()->to('login' . ($customRedirect ? "?to={$customRedirect}" : ''))
|
||||
->withErrors(trans('auth.2fa.invalid_token'));
|
||||
}
|
||||
|
||||
Auth::login($user);
|
||||
|
||||
event(new LoggedIn);
|
||||
|
||||
return redirect()->intended($customRedirect ?: '/');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\Http\Controllers\Web\Auth;
|
||||
|
||||
use Illuminate\Foundation\Auth\VerifiesEmails;
|
||||
use Vanguard\Http\Controllers\Controller;
|
||||
|
||||
class VerificationController extends Controller
|
||||
{
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Email Verification Controller
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| This controller is responsible for handling email verification for any
|
||||
| user that recently registered with the application. Emails may also
|
||||
| be re-sent if the user didn't receive the original email message.
|
||||
|
|
||||
*/
|
||||
|
||||
use VerifiesEmails;
|
||||
|
||||
/**
|
||||
* Where to redirect users after verification.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $redirectTo = '/';
|
||||
|
||||
/**
|
||||
* Create a new controller instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('auth');
|
||||
$this->middleware('signed')->only('verify');
|
||||
$this->middleware('throttle:6,1')->only('verify', 'resend');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user