Skip to content

Commit

Permalink
Merge pull request #34 from BRACKETS-by-TRIAD/laravel-7
Browse files Browse the repository at this point in the history
Laravel 7
  • Loading branch information
RichardDominik authored Apr 29, 2020
2 parents 87d8f36 + 2a6a193 commit 4d6db5f
Show file tree
Hide file tree
Showing 15 changed files with 692 additions and 11 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"spatie/laravel-permission": "^3.0"
},
"require-dev": {
"orchestra/testbench": "~4.0",
"orchestra/testbench": "~4.0|~5.0",
"mockery/mockery": "^1.0",
"phpunit/phpunit": "^8.5"
},
Expand Down
17 changes: 17 additions & 0 deletions resources/lang/en/resets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| Reset Passwords Lines
|--------------------------------------------------------------------------
|
*/

'email' => [
'line' => 'You are receiving this email because we received a password reset request for your account.',
'action' => 'Reset Password',
'notRequested' => 'If you did not request a password reset, no further action is required.',
],
];
6 changes: 6 additions & 0 deletions resources/lang/sk/activations.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@
'user' => 'Nenašli sme používateľa s danými prihlasovacími údajmi. Používateľ je už zrejme aktívny.',
'disabled' => 'Aktivácia nie je možná.',

'email' => [
'line' => 'Tento email ste dostali pretože sme zaznamenali požiadavku o aktiváciu vašeho účtu.',
'action' => 'Aktivovať účet',
'notRequested' => 'Ak ste nepožiadali o aktiváciu vašeho účtú môžete túto správu ignorovať.',
],

];
18 changes: 18 additions & 0 deletions resources/lang/sk/resets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| Reset Passwords Lines
|--------------------------------------------------------------------------
|
*/

'email' => [
'line' => 'Tento email ste dostali pretože sme zaznamenali požiadavku o resetovanie hesla vašeho účtu.',
'action' => 'Resetovať heslo',
'notRequested' => 'Ak ste nepožiadali o resetovanie hesla môžete túto správu ignorovať.',
],

];
2 changes: 1 addition & 1 deletion src/Http/Controllers/Auth/ActivationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Brackets\AdminAuth\Activation\Contracts\CanActivate as CanActivateContract;
use Brackets\AdminAuth\Activation\Facades\Activation;
use Brackets\AdminAuth\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\RedirectsUsers;
use Brackets\AdminAuth\Traits\RedirectsUsers;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
Expand Down
3 changes: 2 additions & 1 deletion src/Http/Controllers/Auth/ForgotPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace Brackets\AdminAuth\Http\Controllers\Auth;

use Brackets\AdminAuth\Http\Controllers\Controller;
use Brackets\AdminAuth\Traits\SendsPasswordResetEmails;
use Illuminate\Contracts\Auth\PasswordBroker as PasswordBrokerContract;
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Controllers/Auth/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Brackets\AdminAuth\Http\Controllers\Auth;

use Brackets\AdminAuth\Http\Controllers\Controller;
use Brackets\AdminAuth\Traits\AuthenticatesUsers;
use Illuminate\Contracts\Auth\StatefulGuard;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Controllers/Auth/ResetPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace Brackets\AdminAuth\Http\Controllers\Auth;

use Brackets\AdminAuth\Http\Controllers\Controller;
use Brackets\AdminAuth\Traits\ResetsPasswords;
use Illuminate\Contracts\Auth\CanResetPassword;
use Illuminate\Contracts\Auth\PasswordBroker as PasswordBrokerContract;
use Illuminate\Contracts\Auth\StatefulGuard;
use Illuminate\Contracts\View\Factory;
use Illuminate\Foundation\Auth\ResetsPasswords;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
Expand Down
6 changes: 3 additions & 3 deletions src/Notifications/ActivationNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public function toMail($notifiable): MailMessage
{
//TODO change to template?
return (new MailMessage)
->line('You are receiving this email because we received an activation request for your account.')
->action('Activate your account', route('brackets/admin-auth::admin/activation/activate', $this->token))
->line('If you did not request an activation, no further action is required.');
->line(trans('brackets/admin-auth::activations.email.line'))
->action(trans('brackets/admin-auth::activations.email.action'), route('brackets/admin-auth::admin/activation/activate', $this->token))
->line(trans('brackets/admin-auth::activations.email.notRequested'));
}
}
6 changes: 3 additions & 3 deletions src/Notifications/ResetPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public function toMail($notifiable): MailMessage
{
//TODO change to template?
return (new MailMessage)
->line('You are receiving this email because we received a password reset request for your account.')
->action('Reset Password', route('brackets/admin-auth::admin/password/showResetForm', $this->token))
->line('If you did not request a password reset, no further action is required.');
->line(trans('brackets/admin-auth::resets.email.line'))
->action(trans('brackets/admin-auth::resets.email.action'), route('brackets/admin-auth::admin/password/showResetForm', $this->token))
->line(trans('brackets/admin-auth::resets.email.notRequested'));
}
}
199 changes: 199 additions & 0 deletions src/Traits/AuthenticatesUsers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
<?php

namespace Brackets\AdminAuth\Traits;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\ValidationException;

trait AuthenticatesUsers
{
use RedirectsUsers, ThrottlesLogins;

/**
* 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)) {
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->filled('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\Response
*/
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 Response('', 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)
{
//
}

/**
* 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')],
]);
}

/**
* Get the login username to be used by the controller.
*
* @return string
*/
public function username()
{
return 'email';
}

/**
* Log the user out of the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
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 Response('', 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();
}
}
20 changes: 20 additions & 0 deletions src/Traits/RedirectsUsers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Brackets\AdminAuth\Traits;

trait RedirectsUsers
{
/**
* Get the post register / login redirect path.
*
* @return string
*/
public function redirectPath()
{
if (method_exists($this, 'redirectTo')) {
return $this->redirectTo();
}

return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
}
}
Loading

0 comments on commit 4d6db5f

Please sign in to comment.