From a70ad437adb41ca38b2bebf6bb61d3c67dbe7758 Mon Sep 17 00:00:00 2001 From: DariusIII Date: Tue, 13 Nov 2018 21:54:10 +0100 Subject: [PATCH] Use User model for email queue where possible --- Changelog | 1 + app/Http/Controllers/Admin/UserController.php | 17 ++--- .../Auth/ForgotPasswordController.php | 23 ++++--- .../Auth/ResetPasswordController.php | 69 ++++++++++--------- app/Http/Controllers/ProfileController.php | 8 ++- app/Jobs/SendAccountChangedEmail.php | 10 +-- app/Jobs/SendAccountDeletedEmail.php | 7 +- app/Jobs/SendAccountExpiredEmail.php | 10 +-- app/Jobs/SendPasswordForgottenEmail.php | 7 +- app/Jobs/SendPasswordResetEmail.php | 21 ++++-- app/Models/User.php | 2 +- 11 files changed, 98 insertions(+), 77 deletions(-) diff --git a/Changelog b/Changelog index 6847f2cf4..dd877ef96 100755 --- a/Changelog +++ b/Changelog @@ -1,4 +1,5 @@ 2018-11-13 DariusIII + * Chg: Use User model for email queue where possible * Chg: Update assets * Chg: Update nesbot/carbon to version 1.34.4 * Chg: Use laravel queue for sending emails diff --git a/app/Http/Controllers/Admin/UserController.php b/app/Http/Controllers/Admin/UserController.php index a8d60453a..e361e1c84 100644 --- a/app/Http/Controllers/Admin/UserController.php +++ b/app/Http/Controllers/Admin/UserController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers\Admin; +use App\Jobs\SendAccountDeletedEmail; use App\Models\User; use App\Models\Invitation; use Illuminate\Http\Request; @@ -140,23 +141,23 @@ class UserController extends BasePageController $ret = User::signUp($request->input('username'), $request->input('password'), $request->input('email'), '', $request->input('notes'), $invites, '', true, $request->input('role')); $this->smarty->assign('role', $request->input('role')); } else { - $ret = User::updateUser($request->input('id'), $request->input('username'), $request->input('email'), $request->input('grabs'), $request->input('role'), $request->input('notes'), $request->input('invites'), ($request->has('movieview') ? 1 : 0), ($request->has('musicview') ? 1 : 0), ($request->has('gameview') ? 1 : 0), ($request->has('xxxview') ? 1 : 0), ($request->has('consoleview') ? 1 : 0), ($request->has('bookview') ? 1 : 0)); + $editedUser = User::find($request->input('id')); + $ret = User::updateUser($editedUser->id, $request->input('username'), $request->input('email'), $request->input('grabs'), $request->input('role'), $request->input('notes'), $request->input('invites'), ($request->has('movieview') ? 1 : 0), ($request->has('musicview') ? 1 : 0), ($request->has('gameview') ? 1 : 0), ($request->has('xxxview') ? 1 : 0), ($request->has('consoleview') ? 1 : 0), ($request->has('bookview') ? 1 : 0)); if ($request->input('password') !== null) { - User::updatePassword($request->input('id'), $request->input('password')); + User::updatePassword($editedUser->id, $request->input('password')); } if ($request->input('rolechangedate') !== null) { - User::updateUserRoleChangeDate($request->input('id'), $request->input('rolechangedate')); + User::updateUserRoleChangeDate($editedUser->id, $request->input('rolechangedate')); } if ($request->input('role') !== null) { $roleName = Role::query()->where('id', $request->input('role'))->value('name'); if ($roleName === 'Disabled') { - $blockedUser = User::find($request->input('id')); - if (env('FIREWALL_ENABLED') === true && \Firewall::isBlacklisted($blockedUser->host) === false) { - \Firewall::blacklist($blockedUser->host); + if (env('FIREWALL_ENABLED') === true && \Firewall::isBlacklisted($editedUser->host) === false) { + \Firewall::blacklist($editedUser->host); } } - $email = $request->input('email') ?? $request->input('email'); - SendAccountChangedEmail::dispatch($email, $request->input('id')); + $editedUser->refresh(); + SendAccountChangedEmail::dispatch($editedUser); } } diff --git a/app/Http/Controllers/Auth/ForgotPasswordController.php b/app/Http/Controllers/Auth/ForgotPasswordController.php index b8732b31a..f5b1feb0c 100644 --- a/app/Http/Controllers/Auth/ForgotPasswordController.php +++ b/app/Http/Controllers/Auth/ForgotPasswordController.php @@ -60,18 +60,19 @@ class ForgotPasswordController extends Controller if ($ret === null) { app('smarty.view')->assign('error', 'The email or apikey are not recognised.'); $sent = true; + } else { + // + // Generate a forgottenpassword guid, store it in the user table + // + $guid = \Token::random(32); + User::updatePassResetGuid($ret['id'], $guid); + // + // Send the email + // + $resetLink = url('/').'/resetpassword?guid='.$guid; + SendPasswordForgottenEmail::dispatch($ret, $resetLink); + $sent = true; } - // - // Generate a forgottenpassword guid, store it in the user table - // - $guid = \Token::random(32); - User::updatePassResetGuid($ret['id'], $guid); - // - // Send the email - // - $resetLink = url('/').'/resetpassword?guid='.$guid; - SendPasswordForgottenEmail::dispatch($ret['email'], $resetLink); - $sent = true; } $theme = Settings::settingValue('site.main.style'); diff --git a/app/Http/Controllers/Auth/ResetPasswordController.php b/app/Http/Controllers/Auth/ResetPasswordController.php index 8d89d65c3..2554c95db 100644 --- a/app/Http/Controllers/Auth/ResetPasswordController.php +++ b/app/Http/Controllers/Auth/ResetPasswordController.php @@ -49,47 +49,52 @@ class ResetPasswordController extends Controller */ public function reset(Request $request) { + $error = ''; + $confirmed = ''; + $onscreen = ''; if (! $request->has('guid')) { - app('smarty.view')->assign('error', 'No reset code provided.'); + $error = 'No reset code provided.'; } $ret = User::getByPassResetGuid($request->input('guid')); if ($ret === null) { - app('smarty.view')->assign('error', 'Bad reset code provided.'); + $error = 'Bad reset code provided.'; + } else { + + // + // reset the password, inform the user, send out the email + // + User::updatePassResetGuid($ret['id'], ''); + $newpass = User::generatePassword(); + User::updatePassword($ret['id'], $newpass); + + $onscreen = 'Your password has been reset to '.$newpass.' and sent to your e-mail address.'; + SendPasswordResetEmail::dispatch($ret, $newpass); + $confirmed = true; } - // - // reset the password, inform the user, send out the email - // - User::updatePassResetGuid($ret['id'], ''); - $newpass = User::generatePassword(); - User::updatePassword($ret['id'], $newpass); + $theme = Settings::settingValue('site.main.style'); - $onscreen = 'Your password has been reset to '.$newpass.' and sent to your e-mail address.'; - SendPasswordResetEmail::dispatch($ret['email'], $ret['id'], $newpass); - app('smarty.view')->assign('notice', $onscreen); - $confirmed = true; + $title = 'Forgotten Password'; + $meta_title = 'Forgotten Password'; + $meta_keywords = 'forgotten,password,signup,registration'; + $meta_description = 'Forgotten Password'; - $theme = Settings::settingValue('site.main.style'); + $content = app('smarty.view')->fetch($theme.'/forgottenpassword.tpl'); - $title = 'Forgotten Password'; - $meta_title = 'Forgotten Password'; - $meta_keywords = 'forgotten,password,signup,registration'; - $meta_description = 'Forgotten Password'; - - $content = app('smarty.view')->fetch($theme.'/forgottenpassword.tpl'); - - app('smarty.view')->assign( - [ - 'content' => $content, - 'title' => $title, - 'meta_title' => $meta_title, - 'meta_keywords' => $meta_keywords, - 'meta_description' => $meta_description, - 'email' => $ret['email'], - 'confirmed' => $confirmed, - ] - ); - app('smarty.view')->display($theme.'/basepage.tpl'); + app('smarty.view')->assign( + [ + 'content' => $content, + 'title' => $title, + 'meta_title' => $meta_title, + 'meta_keywords' => $meta_keywords, + 'meta_description' => $meta_description, + 'email' => $ret['email'], + 'confirmed' => $confirmed, + 'error' => $error, + 'notice' => $onscreen, + ] + ); + app('smarty.view')->display($theme.'/basepage.tpl'); } } diff --git a/app/Http/Controllers/ProfileController.php b/app/Http/Controllers/ProfileController.php index ad53c7ba0..8e22007cf 100644 --- a/app/Http/Controllers/ProfileController.php +++ b/app/Http/Controllers/ProfileController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers; +use App\Jobs\SendAccountDeletedEmail; use App\Models\User; use Blacklight\NZBGet; use Blacklight\SABnzbd; @@ -360,9 +361,10 @@ class ProfileController extends BasePageController $this->setPrefs(); $userId = $request->input('id'); - if ($userId !== null && $this->userdata->hasRole('Admin') === false && (int) $userId === Auth::id()) { - User::deleteUser($userId); - + if ($userId !== null && (int) $userId === $this->userdata->id && $this->userdata->hasRole('Admin') === false ) { + $user = User::find($userId); + SendAccountDeletedEmail::dispatch($user); + User::deleteUser($user->id); return redirect('login'); } diff --git a/app/Jobs/SendAccountChangedEmail.php b/app/Jobs/SendAccountChangedEmail.php index 296bac4cd..352d33d3d 100644 --- a/app/Jobs/SendAccountChangedEmail.php +++ b/app/Jobs/SendAccountChangedEmail.php @@ -3,6 +3,7 @@ namespace App\Jobs; use App\Mail\AccountChange; +use App\Models\User; use Illuminate\Bus\Queueable; use Illuminate\Support\Facades\Mail; use Illuminate\Queue\SerializesModels; @@ -21,13 +22,12 @@ class SendAccountChangedEmail implements ShouldQueue /** * Create a new job instance. * - * @param $email - * @param $id + * @param \App\Models\User $user */ - public function __construct($email, $id) + public function __construct(User $user) { - $this->email = $email; - $this->id = $id; + $this->email = $user->email; + $this->id = $user->id; } /** diff --git a/app/Jobs/SendAccountDeletedEmail.php b/app/Jobs/SendAccountDeletedEmail.php index a5936d5e4..3ea4fdd1c 100644 --- a/app/Jobs/SendAccountDeletedEmail.php +++ b/app/Jobs/SendAccountDeletedEmail.php @@ -4,6 +4,7 @@ namespace App\Jobs; use App\Models\Settings; use App\Mail\AccountDeleted; +use App\Models\User; use Illuminate\Bus\Queueable; use Illuminate\Support\Facades\Mail; use Illuminate\Queue\SerializesModels; @@ -20,11 +21,11 @@ class SendAccountDeletedEmail implements ShouldQueue /** * Create a new job instance. * - * @param $userId + * @param \App\Models\User $user */ - public function __construct($userId) + public function __construct(User $user) { - $this->userId = $userId; + $this->userId = $user->id; } /** diff --git a/app/Jobs/SendAccountExpiredEmail.php b/app/Jobs/SendAccountExpiredEmail.php index 5aaa826f8..b0d1609b6 100644 --- a/app/Jobs/SendAccountExpiredEmail.php +++ b/app/Jobs/SendAccountExpiredEmail.php @@ -3,6 +3,7 @@ namespace App\Jobs; use App\Mail\AccountExpired; +use App\Models\User; use Illuminate\Bus\Queueable; use Illuminate\Support\Facades\Mail; use Illuminate\Queue\SerializesModels; @@ -21,13 +22,12 @@ class SendAccountExpiredEmail implements ShouldQueue /** * Create a new job instance. * - * @param $email - * @param $userId + * @param \App\Models\User $user */ - public function __construct($email, $userId) + public function __construct(User $user) { - $this->email = $email; - $this->userId = $userId; + $this->email = $user->email; + $this->userId = $user->id; } /** diff --git a/app/Jobs/SendPasswordForgottenEmail.php b/app/Jobs/SendPasswordForgottenEmail.php index 832e4d518..bbccdc8f5 100644 --- a/app/Jobs/SendPasswordForgottenEmail.php +++ b/app/Jobs/SendPasswordForgottenEmail.php @@ -2,6 +2,7 @@ namespace App\Jobs; +use App\Models\User; use Illuminate\Bus\Queueable; use App\Mail\ForgottenPassword; use Illuminate\Support\Facades\Mail; @@ -21,12 +22,12 @@ class SendPasswordForgottenEmail implements ShouldQueue /** * Create a new job instance. * - * @param $email + * @param \App\Models\User $user * @param $resetLink */ - public function __construct($email, $resetLink) + public function __construct(User $user, $resetLink) { - $this->email = $email; + $this->email = $user->email; $this->resetLink = $resetLink; } diff --git a/app/Jobs/SendPasswordResetEmail.php b/app/Jobs/SendPasswordResetEmail.php index 62d8b73ba..0683a6288 100644 --- a/app/Jobs/SendPasswordResetEmail.php +++ b/app/Jobs/SendPasswordResetEmail.php @@ -3,6 +3,7 @@ namespace App\Jobs; use App\Mail\PasswordReset; +use App\Models\User; use Illuminate\Bus\Queueable; use Illuminate\Support\Facades\Mail; use Illuminate\Queue\SerializesModels; @@ -14,23 +15,31 @@ class SendPasswordResetEmail implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; + /** + * @var string + */ private $email; + /** + * @var int + */ private $userId; + /** + * @var string + */ private $newPass; /** * Create a new job instance. * - * @param $email - * @param $userId - * @param $newPass + * @param \App\Models\User $user + * @param string $newPass */ - public function __construct($email, $userId, $newPass) + public function __construct(User $user, $newPass) { - $this->email = $email; - $this->userId = $userId; + $this->email = $user->email; + $this->userId = $user->id; $this->newPass = $newPass; } diff --git a/app/Models/User.php b/app/Models/User.php index 8360193ae..6366c0654 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -423,7 +423,7 @@ class User extends Authenticatable $user = self::find($u['id']); $user->update(['roles_id' => self::ROLE_USER, 'rolechangedate' => null]); $user->syncRoles('User'); - SendAccountExpiredEmail::dispatch($u['email'], $u['id']); + SendAccountExpiredEmail::dispatch($user); } return self::SUCCESS;