You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
243 lines
7.2 KiB
243 lines
7.2 KiB
2 years ago
|
<?php
|
||
|
|
||
|
namespace App\Http\Controllers\Auth;
|
||
|
|
||
|
use Illuminate\Http\JsonResponse;
|
||
|
use App\Http\Controllers\Controller;
|
||
|
use App\Models\ExpirarContrasena;
|
||
|
use App\Providers\RouteServiceProvider;
|
||
|
use Illuminate\Foundation\Auth\AuthenticatesUsers;
|
||
|
use Illuminate\Foundation\Auth\RedirectsUsers;
|
||
|
use Illuminate\Foundation\Auth\ThrottlesLogins;
|
||
|
use Illuminate\Support\Facades\Auth;
|
||
|
use Illuminate\Validation\ValidationException;
|
||
|
use Illuminate\Http\Request;
|
||
|
use Illuminate\Support\Carbon;
|
||
|
use App\Models\ExpirarContrasenas;
|
||
|
use Illuminate\Support\Facades\Redirect;
|
||
|
|
||
|
class LoginController extends Controller
|
||
|
{
|
||
|
use RedirectsUsers, ThrottlesLogins;
|
||
|
|
||
|
protected $redirectTo = '/inicio';
|
||
|
|
||
|
/**
|
||
|
* Show the application's login form.
|
||
|
*
|
||
|
* @return \Illuminate\View\View
|
||
|
*/
|
||
|
public function showLoginForm()
|
||
|
{
|
||
|
return view('auth.login');
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Handle a login request to the application.
|
||
|
*
|
||
|
* @param \Illuminate\Http\Request $request
|
||
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Http\JsonResponse
|
||
|
*
|
||
|
* @throws \Illuminate\Validation\ValidationException
|
||
|
*/
|
||
|
public function login(Request $request)
|
||
|
{
|
||
|
$this->validateLogin($request);
|
||
|
|
||
|
// If the class is using the ThrottlesLogins trait, we can automatically throttle
|
||
|
// the login attempts for this application. We'll key this by the username and
|
||
|
// the IP address of the client making these requests into this application.
|
||
|
if (method_exists($this, 'hasTooManyLoginAttempts') &&
|
||
|
$this->hasTooManyLoginAttempts($request)) {
|
||
|
$this->fireLockoutEvent($request);
|
||
|
|
||
|
return $this->sendLockoutResponse($request);
|
||
|
}
|
||
|
|
||
|
if ($this->attemptLogin($request)) {
|
||
|
if ($request->hasSession()) {
|
||
|
$request->session()->put('auth.password_confirmed_at', time());
|
||
|
}
|
||
|
|
||
|
return $this->sendLoginResponse($request);
|
||
|
}
|
||
|
|
||
|
// 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.
|
||
|
$this->incrementLoginAttempts($request);
|
||
|
|
||
|
return $this->sendFailedLoginResponse($request);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Validate the user login request.
|
||
|
*
|
||
|
* @param \Illuminate\Http\Request $request
|
||
|
* @return void
|
||
|
*
|
||
|
* @throws \Illuminate\Validation\ValidationException
|
||
|
*/
|
||
|
protected function validateLogin(Request $request)
|
||
|
{
|
||
|
$request->validate([
|
||
|
$this->username() => 'required|string',
|
||
|
'password' => 'required|string',
|
||
|
]);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Attempt to log the user into the application.
|
||
|
*
|
||
|
* @param \Illuminate\Http\Request $request
|
||
|
* @return bool
|
||
|
*/
|
||
|
protected function attemptLogin(Request $request)
|
||
|
{
|
||
|
return $this->guard()->attempt(
|
||
|
$this->credentials($request), $request->boolean('remember')
|
||
|
);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Get the needed authorization credentials from the request.
|
||
|
*
|
||
|
* @param \Illuminate\Http\Request $request
|
||
|
* @return array
|
||
|
*/
|
||
|
protected function credentials(Request $request)
|
||
|
{
|
||
|
return $request->only($this->username(), 'password');
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Send the response after the user was authenticated.
|
||
|
*
|
||
|
* @param \Illuminate\Http\Request $request
|
||
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
|
||
|
*/
|
||
|
protected function sendLoginResponse(Request $request)
|
||
|
{
|
||
|
$request->session()->regenerate();
|
||
|
|
||
|
$this->clearLoginAttempts($request);
|
||
|
|
||
|
if ($response = $this->authenticated($request, $this->guard()->user())) {
|
||
|
return $response;
|
||
|
}
|
||
|
|
||
|
return $request->wantsJson()
|
||
|
? new JsonResponse([], 204)
|
||
|
: redirect()->intended($this->redirectPath());
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* The user has been authenticated.
|
||
|
*
|
||
|
* @param \Illuminate\Http\Request $request
|
||
|
* @param mixed $user
|
||
|
* @return mixed
|
||
|
*/
|
||
|
protected function authenticated(Request $request, $user)
|
||
|
{
|
||
|
if($user->activo != 1){
|
||
|
|
||
|
return redirect()->route('login')->with('message', ['type'=> 'error', 'text' => 'Tu acceso ha sido desactivado o eliminado.', 'title' => '¡Error!']);
|
||
|
// abort(401);
|
||
|
}else{
|
||
|
|
||
|
$expirar = ExpirarContrasena::where('fk_id_users',$user->id)->get()->toArray();
|
||
|
// dd($expirar);
|
||
|
if($expirar){
|
||
|
$dias = Carbon::parse($expirar[0]['ultima_actualizacion'])->addDays($expirar[0]['dias_expiracion']);
|
||
|
//prueba
|
||
|
// if($dias->lessThan(Carbon::now())){
|
||
|
// auth()->logout();
|
||
|
// return redirect()->route('contrasena_expirada', ['password_expired_id' => $user->id])->with('message', ['type'=> 'error', 'text' => 'Tu contraseña ah expirado, favor de actualizar su contraseña..', 'title' => '¡Error!']);
|
||
|
// }
|
||
|
if($expirar[0]['dias_expiracion'] == 0){
|
||
|
auth()->logout();
|
||
|
return redirect()->route('contrasena_expirada', ['password_expired_id' => $user->id])->with('message', ['type'=> 'error', 'text' => 'Tu contraseña ah expirado, favor de actualizar su contraseña..', 'title' => '¡Error!']);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return redirect()->intended($this->redirectPath());
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Get the failed login response instance.
|
||
|
*
|
||
|
* @param \Illuminate\Http\Request $request
|
||
|
* @return \Symfony\Component\HttpFoundation\Response
|
||
|
*
|
||
|
* @throws \Illuminate\Validation\ValidationException
|
||
|
*/
|
||
|
protected function sendFailedLoginResponse(Request $request)
|
||
|
{
|
||
|
throw ValidationException::withMessages([
|
||
|
// $this->username() => [trans('auth.failed')],
|
||
|
$this->username() => ['No se encontraron coincidencias con el usuario y la contraseña.'],
|
||
|
]);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Get the login username to be used by the controller.
|
||
|
*
|
||
|
* @return string
|
||
|
*/
|
||
|
public function username()
|
||
|
{
|
||
|
return 'name';
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Log the user out of the application.
|
||
|
*
|
||
|
* @param \Illuminate\Http\Request $request
|
||
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
|
||
|
*/
|
||
|
public function logout(Request $request)
|
||
|
{
|
||
|
$this->guard()->logout();
|
||
|
|
||
|
$request->session()->invalidate();
|
||
|
|
||
|
$request->session()->regenerateToken();
|
||
|
|
||
|
if ($response = $this->loggedOut($request)) {
|
||
|
return $response;
|
||
|
}
|
||
|
|
||
|
return $request->wantsJson()
|
||
|
? new JsonResponse([], 204)
|
||
|
: redirect('/');
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* The user has logged out of the application.
|
||
|
*
|
||
|
* @param \Illuminate\Http\Request $request
|
||
|
* @return mixed
|
||
|
*/
|
||
|
protected function loggedOut(Request $request)
|
||
|
{
|
||
|
//
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Get the guard to be used during authentication.
|
||
|
*
|
||
|
* @return \Illuminate\Contracts\Auth\StatefulGuard
|
||
|
*/
|
||
|
protected function guard()
|
||
|
{
|
||
|
return Auth::guard();
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
}
|