parent
df0e06045f
commit
904e29955d
@ -0,0 +1,61 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\Auth; |
||||||
|
|
||||||
|
|
||||||
|
use App\Http\Controllers\Controller; |
||||||
|
use App\Models\EmailConfirmation; |
||||||
|
use Illuminate\Http\Request; |
||||||
|
use Illuminate\Support\Collection; |
||||||
|
|
||||||
|
class ConfirmEmailController extends Controller |
||||||
|
{ |
||||||
|
public function resendConfirmation() |
||||||
|
{ |
||||||
|
$user = user(); |
||||||
|
/** @var EmailConfirmation[]|Collection $ec */ |
||||||
|
$ec = $user->emailConfirmations()->valid()->get(); |
||||||
|
$email = ''; |
||||||
|
|
||||||
|
if ($ec->count() == 0) { |
||||||
|
user()->sendEmailConfirmationLink($email = $user->email); |
||||||
|
} else { |
||||||
|
user()->sendEmailConfirmationLink($email = $ec[0]->email); |
||||||
|
} |
||||||
|
|
||||||
|
flash()->success("A new confirmation link was sent to your e-mail $email"); // not important, will fade |
||||||
|
|
||||||
|
return back(); |
||||||
|
} |
||||||
|
|
||||||
|
public function confirmEmailAndLogin(Request $request) |
||||||
|
{ |
||||||
|
$input = $this->validate($request, [ |
||||||
|
'token' => 'string|required', |
||||||
|
]); |
||||||
|
|
||||||
|
$ec = EmailConfirmation::where('token', $input->token)->valid()->first(); |
||||||
|
if (!$ec) abort(400, "Invalid or expired token."); |
||||||
|
|
||||||
|
$u = $ec->user; |
||||||
|
if (!$u) abort(400, "User account does not exist."); |
||||||
|
|
||||||
|
if ($ec->email) $u->email = $ec->email; |
||||||
|
$wasConfirmed = $u->confirmed; |
||||||
|
$u->confirmed = true; |
||||||
|
$u->save(); |
||||||
|
$ec->delete(); |
||||||
|
|
||||||
|
\Auth::login($u, true); |
||||||
|
|
||||||
|
if ($wasConfirmed) { |
||||||
|
// user just changed an e-mail |
||||||
|
flash()->success("Your new e-mail $ec->email was confirmed!")->important(); |
||||||
|
} else { |
||||||
|
flash()->success("Your e-mail $ec->email was confirmed, your account is now active!")->important(); |
||||||
|
} |
||||||
|
|
||||||
|
return redirect(route('profile.view', $u->name)); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,24 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace App\Http\Middleware; |
||||||
|
|
||||||
|
use Closure; |
||||||
|
|
||||||
|
class ActivatedAccount |
||||||
|
{ |
||||||
|
/** |
||||||
|
* Handle an incoming request. |
||||||
|
* |
||||||
|
* @param \Illuminate\Http\Request $request |
||||||
|
* @param \Closure $next |
||||||
|
* @return mixed |
||||||
|
*/ |
||||||
|
public function handle($request, Closure $next) |
||||||
|
{ |
||||||
|
if (guest() || !user()->confirmed) { |
||||||
|
abort(403, "Only users with active accounts can do this."); |
||||||
|
} |
||||||
|
|
||||||
|
return $next($request); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,75 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace App\Notifications; |
||||||
|
|
||||||
|
use App\Models\User; |
||||||
|
use Illuminate\Bus\Queueable; |
||||||
|
use Illuminate\Notifications\Notification; |
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue; |
||||||
|
use Illuminate\Notifications\Messages\MailMessage; |
||||||
|
use Lang; |
||||||
|
|
||||||
|
class ConfirmEmail extends Notification |
||||||
|
{ |
||||||
|
/** |
||||||
|
* The email confirm token. |
||||||
|
* |
||||||
|
* @var string |
||||||
|
*/ |
||||||
|
public $token; |
||||||
|
|
||||||
|
/** |
||||||
|
* The new e-mail |
||||||
|
* |
||||||
|
* @var string |
||||||
|
*/ |
||||||
|
public $newEmail; |
||||||
|
|
||||||
|
/** |
||||||
|
* Create a new notification instance. |
||||||
|
* |
||||||
|
* @return void |
||||||
|
*/ |
||||||
|
public function __construct($newEmail, $token) |
||||||
|
{ |
||||||
|
$this->newEmail = $newEmail; |
||||||
|
$this->token = $token; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Get the notification's delivery channels. |
||||||
|
* |
||||||
|
* @param mixed $notifiable |
||||||
|
* @return array |
||||||
|
*/ |
||||||
|
public function via($notifiable) |
||||||
|
{ |
||||||
|
return ['mail']; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Get the mail representation of the notification. |
||||||
|
* |
||||||
|
* @param mixed $notifiable |
||||||
|
* @return \Illuminate\Notifications\Messages\MailMessage |
||||||
|
*/ |
||||||
|
public function toMail($notifiable) |
||||||
|
{ |
||||||
|
/** @var User $notifiable */ |
||||||
|
$notifiable->email = $this->newEmail; // hack to send to the new e-mail |
||||||
|
|
||||||
|
return (new MailMessage) |
||||||
|
->subject('E-Mail Confirmation') |
||||||
|
|
||||||
|
->line( |
||||||
|
'Please confirm that you just requested using this e-mail with the datatable.directory |
||||||
|
account '.$notifiable->handle.'. It will be used to login, to recover forgotten passwords, |
||||||
|
and for sending notifications. You can always opt out of e-mail notifications in the settings.') |
||||||
|
|
||||||
|
->action('Confirm E-Mail', url(config('app.url').route('confirm-email', ['token' => $this->token], false))) |
||||||
|
|
||||||
|
->line( |
||||||
|
'If you did not request this action, no further action is required. The confirmation link |
||||||
|
will expire in a few hours.'); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue