primo upload
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\Http\Controllers\Web\Auth;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Vanguard\Http\Controllers\Controller;
|
||||
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;
|
||||
|
||||
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');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the response for a successful password reset link.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param string $response
|
||||
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
|
||||
*/
|
||||
protected function sendResetLinkResponse(Request $request, $response)
|
||||
{
|
||||
return back()->with('success', trans($response));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\Http\Controllers\Web\Auth;
|
||||
|
||||
use Auth;
|
||||
use Authy;
|
||||
use Illuminate\Contracts\Container\BindingResolutionException;
|
||||
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\User\UserRepository;
|
||||
use Vanguard\Services\Auth\ThrottlesLogins;
|
||||
use Vanguard\Services\Auth\TwoFactor\Contracts\Authenticatable;
|
||||
|
||||
class LoginController extends Controller
|
||||
{
|
||||
use ThrottlesLogins;
|
||||
|
||||
public function __construct(private UserRepository $users)
|
||||
{
|
||||
$this->middleware('guest')->except('logout');
|
||||
$this->middleware('auth')->only('logout');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the application login form.
|
||||
*
|
||||
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
||||
*/
|
||||
public function show()
|
||||
{
|
||||
return view('auth.login', [
|
||||
'socialProviders' => config('auth.social.providers')
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param LoginRequest $request
|
||||
* @return RedirectResponse|Response
|
||||
* @throws BindingResolutionException
|
||||
*/
|
||||
public function login(LoginRequest $request)
|
||||
{
|
||||
// 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 = 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 login 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(__('Your account is banned by administrator.'));
|
||||
}
|
||||
|
||||
Auth::login($user, setting('remember_me') && $request->get('remember'));
|
||||
|
||||
return $this->authenticated($request, $throttles, $user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the response after the user was authenticated.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param bool $throttles
|
||||
* @param $user
|
||||
* @return RedirectResponse|Response
|
||||
*/
|
||||
protected function authenticated(Request $request, $throttles, $user)
|
||||
{
|
||||
if ($throttles) {
|
||||
$this->clearLoginAttempts($request);
|
||||
}
|
||||
|
||||
if (setting('2fa.enabled') && Authy::isEnabled($user)) {
|
||||
return $this->logoutAndRedirectToTokenPage($request, $user);
|
||||
}
|
||||
|
||||
event(new LoggedIn);
|
||||
|
||||
if ($request->has('to')) {
|
||||
return redirect()->to($request->get('to'));
|
||||
}
|
||||
|
||||
return redirect()->intended();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param Authenticatable $user
|
||||
* @return RedirectResponse
|
||||
*/
|
||||
protected function logoutAndRedirectToTokenPage(Request $request, Authenticatable $user)
|
||||
{
|
||||
Auth::logout();
|
||||
|
||||
$request->session()->put('auth.2fa.id', $user->id);
|
||||
|
||||
return redirect()->route('auth.token');
|
||||
}
|
||||
|
||||
/**
|
||||
* Log the user out of the application.
|
||||
*
|
||||
* @return RedirectResponse|\Illuminate\Routing\Redirector
|
||||
*/
|
||||
public function logout()
|
||||
{
|
||||
event(new LoggedOut);
|
||||
|
||||
Auth::logout();
|
||||
|
||||
return redirect('login');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\Http\Controllers\Web\Auth;
|
||||
|
||||
use Illuminate\Auth\Events\Registered;
|
||||
use Vanguard\Http\Controllers\Controller;
|
||||
use Vanguard\Http\Requests\Auth\RegisterRequest;
|
||||
use Vanguard\Repositories\Role\RoleRepository;
|
||||
use Vanguard\Repositories\User\UserRepository;
|
||||
use Vanguard\Support\Enum\UserStatus;
|
||||
|
||||
class RegisterController extends Controller
|
||||
{
|
||||
public function __construct(private UserRepository $users)
|
||||
{
|
||||
$this->middleware('registration')->only('show', 'register');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the application registration form.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function show()
|
||||
{
|
||||
return view('auth.register', [
|
||||
'socialProviders' => config('auth.social.providers')
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle a registration request for the application.
|
||||
*
|
||||
* @param RegisterRequest $request
|
||||
* @param RoleRepository $roles
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function register(RegisterRequest $request, RoleRepository $roles)
|
||||
{
|
||||
$user = $this->users->create(
|
||||
array_merge($request->validFormData(), ['role_id' => $roles->findByName('User')->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,61 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\Http\Controllers\Web\Auth;
|
||||
|
||||
use Illuminate\Auth\Events\PasswordReset;
|
||||
use Illuminate\Support\Str;
|
||||
use Vanguard\Http\Controllers\Controller;
|
||||
use Illuminate\Foundation\Auth\ResetsPasswords;
|
||||
|
||||
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');
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the given user's password.
|
||||
*
|
||||
* @param \Illuminate\Contracts\Auth\CanResetPassword $user
|
||||
* @param string $password
|
||||
* @return void
|
||||
*/
|
||||
protected function resetPassword($user, $password)
|
||||
{
|
||||
$user->password = $password;
|
||||
|
||||
$user->setRememberToken(Str::random(60));
|
||||
|
||||
$user->save();
|
||||
|
||||
event(new PasswordReset($user));
|
||||
|
||||
$this->guard()->login($user);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\Http\Controllers\Web\Auth;
|
||||
|
||||
use Auth;
|
||||
use Authy;
|
||||
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;
|
||||
|
||||
class SocialAuthController extends Controller
|
||||
{
|
||||
public function __construct(private UserRepository $users, private SocialManager $socialManager)
|
||||
{
|
||||
$this->middleware('guest');
|
||||
}
|
||||
|
||||
/**
|
||||
* Redirect user to specified provider in order to complete the authentication process.
|
||||
*
|
||||
* @param $provider
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
*/
|
||||
public function redirectToProvider($provider)
|
||||
{
|
||||
if (strtolower($provider) == 'facebook') {
|
||||
return Socialite::driver('facebook')->with(['auth_type' => 'rerequest'])->redirect();
|
||||
}
|
||||
|
||||
return Socialite::driver($provider)->redirect();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle response authentication provider.
|
||||
*
|
||||
* @param $provider
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
*/
|
||||
public function handleProviderCallback($provider)
|
||||
{
|
||||
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.
|
||||
*
|
||||
* @param $provider
|
||||
* @return SocialUser
|
||||
*/
|
||||
private function getUserFromProvider($provider)
|
||||
{
|
||||
return Socialite::driver($provider)->user();
|
||||
}
|
||||
|
||||
/**
|
||||
* Log provided user in and redirect him to intended page.
|
||||
*
|
||||
* @param $user
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
*/
|
||||
private function loginAndRedirect($user)
|
||||
{
|
||||
if ($user->isBanned()) {
|
||||
return redirect()->to('login')
|
||||
->withErrors(__('Your account is banned by administrator.'));
|
||||
}
|
||||
|
||||
if (setting('2fa.enabled') && Authy::isEnabled($user)) {
|
||||
session()->put('auth.2fa.id', $user->id);
|
||||
return redirect()->route('auth.token');
|
||||
}
|
||||
|
||||
Auth::login($user);
|
||||
|
||||
event(new LoggedIn);
|
||||
|
||||
return redirect()->intended('/');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\Http\Controllers\Web\Auth;
|
||||
|
||||
use Auth;
|
||||
use Authy;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Vanguard\Events\User\LoggedIn;
|
||||
use Vanguard\Http\Controllers\Controller;
|
||||
use Vanguard\Repositories\User\UserRepository;
|
||||
use Vanguard\Services\Auth\ThrottlesLogins;
|
||||
|
||||
class TwoFactorTokenController extends Controller
|
||||
{
|
||||
use ThrottlesLogins;
|
||||
|
||||
public function __construct(private UserRepository $users)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Show Two-Factor Token form.
|
||||
* @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
|
||||
*/
|
||||
public function show()
|
||||
{
|
||||
return session('auth.2fa.id') ? view('auth.token') : redirect('login');
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle Two-Factor token form submission.
|
||||
*
|
||||
* @param Request $request
|
||||
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
||||
* @throws \Illuminate\Validation\ValidationException
|
||||
*/
|
||||
public function update(Request $request)
|
||||
{
|
||||
$this->validate($request, ['token' => 'required']);
|
||||
|
||||
if (! session('auth.2fa.id')) {
|
||||
return redirect('login');
|
||||
}
|
||||
|
||||
$user = $this->users->find(
|
||||
$request->session()->pull('auth.2fa.id')
|
||||
);
|
||||
|
||||
if (! $user) {
|
||||
throw new NotFoundHttpException;
|
||||
}
|
||||
|
||||
if (! Authy::tokenIsValid($user, $request->token)) {
|
||||
return redirect()->to('login')
|
||||
->withErrors(__('2FA Token is invalid!'));
|
||||
}
|
||||
|
||||
Auth::login($user);
|
||||
|
||||
event(new LoggedIn);
|
||||
|
||||
return redirect()->intended('/');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\Http\Controllers\Web\Auth;
|
||||
|
||||
use Vanguard\Http\Controllers\Controller;
|
||||
use Illuminate\Foundation\Auth\VerifiesEmails;
|
||||
|
||||
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