3
0
Fork 0
This repository has been archived on 2024-11-14. You can view files and clone it, but cannot push or open issues or pull requests.
ThemeParkPlus-Panel/app/Http/Controllers/Profile/ChangeController.php

119 lines
3.8 KiB
PHP
Raw Permalink Normal View History

2020-02-26 13:59:58 +00:00
<?php
namespace App\Http\Controllers\Profile;
2020-02-26 13:59:58 +00:00
use App\ChangeEmail;
use App\Http\Controllers\Controller;
2021-06-27 19:01:43 +00:00
use App\Mail\EmailChange;
2020-02-26 13:59:58 +00:00
use App\Notifications\SendMailChange;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Str;
class ChangeController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware(['auth', 'verified', '2fa']);
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
return view('profile.change');
2020-02-26 13:59:58 +00:00
}
public function changePassword(Request $request) {
$validator = Validator::make($request->all(), [
'password' => ['required', 'max:255'],
'new_password' => ['required', 'min:6', 'confirmed', 'regex:/[a-z]/', 'regex:/[A-Z]/', 'regex:/[0-9]/', 'regex:/[@$!%*#?&]/'],
'new_confirm_password' => ['required', 'same:new_password']
]);
if(!$validator->passes())
return Redirect::back()->withErrors($validator);
$user = Auth::user();
if(!Hash::check($request->get('password'), $user->password)) {
$validator->getMessageBag()->add('pass_password', 'Wrong user password.');
return Redirect::back()->withErrors($validator);
}
$user->password = Hash::make($request->get('new_password'));
if(!$user->save()) {
$validator->getMessageBag()->add('new_password', 'Unable to change password.');
return Redirect::back()->withErrors($validator);
}
session()->flash('pass_success', 'Successfully changed password.');
return Redirect::back();
}
public function changeEmail(Request $request) {
$validator = Validator::make($request->all(), [
'password' => ['required', 'max:255'],
'new_email' => ['required', 'email', 'unique:users,email', 'max:255'],
'new_confirm_email' => ['required', 'same:new_email']
]);
if(!$validator->passes())
return Redirect::back()->withErrors($validator);
$user = Auth::user();
if(!Hash::check($request->get('password'), $user->password)) {
$validator->getMessageBag()->add('email_password', 'Wrong user password.');
return Redirect::back()->withErrors($validator);
}
$user->email = $request->get('new_email');
$change = ChangeEmail::create([
'user_id' => $user->id,
'email' => $request->get('new_email'),
'token' => Str::random(12)
]);
2021-06-27 19:01:43 +00:00
Mail::to($user->email)->send(new EmailChange($change));
2020-02-26 13:59:58 +00:00
session()->flash('email_success', 'Successfully requested email change');
return Redirect::back();
}
public function verifyEmail($id, $token, $email) {
if(Auth::id() != $id) {
session()->flash('email_error', 'Incorrect email change request: '.$email);
2021-06-27 19:01:43 +00:00
return Redirect::route('change');
2020-02-26 13:59:58 +00:00
}
$user = Auth::user();
$model = ChangeEmail::where([
'user_id' => $user->id,
'token' => $token,
'email' => $email
])->first();
if(empty($model)) {
session()->flash('email_error', 'Unable to change email address to: '.$email);
2021-06-27 19:01:43 +00:00
return Redirect::route('change');
2020-02-26 13:59:58 +00:00
}
$user->email = $email;
$user->save();
$model->delete();
session()->flash('email_success', 'Successfully changed email address to: '.$email);
2021-06-27 19:01:43 +00:00
return Redirect::route('change');
2020-02-26 13:59:58 +00:00
}
}