Made registration gravatar/email requests fail gracefully
* Extracted any email confirmation text into langs. * Added new notification on confirmation email send fail. Closes #187
Showing
6 changed files
with
53 additions
and
15 deletions
| ... | @@ -8,6 +8,7 @@ use BookStack\Repos\UserRepo; | ... | @@ -8,6 +8,7 @@ use BookStack\Repos\UserRepo; |
| 8 | use BookStack\Services\EmailConfirmationService; | 8 | use BookStack\Services\EmailConfirmationService; |
| 9 | use BookStack\Services\SocialAuthService; | 9 | use BookStack\Services\SocialAuthService; |
| 10 | use BookStack\User; | 10 | use BookStack\User; |
| 11 | +use Exception; | ||
| 11 | use Illuminate\Http\Request; | 12 | use Illuminate\Http\Request; |
| 12 | use Illuminate\Http\Response; | 13 | use Illuminate\Http\Response; |
| 13 | use Validator; | 14 | use Validator; |
| ... | @@ -56,7 +57,6 @@ class RegisterController extends Controller | ... | @@ -56,7 +57,6 @@ class RegisterController extends Controller |
| 56 | $this->userRepo = $userRepo; | 57 | $this->userRepo = $userRepo; |
| 57 | $this->redirectTo = baseUrl('/'); | 58 | $this->redirectTo = baseUrl('/'); |
| 58 | $this->redirectPath = baseUrl('/'); | 59 | $this->redirectPath = baseUrl('/'); |
| 59 | - $this->username = config('auth.method') === 'standard' ? 'email' : 'username'; | ||
| 60 | parent::__construct(); | 60 | parent::__construct(); |
| 61 | } | 61 | } |
| 62 | 62 | ||
| ... | @@ -158,7 +158,13 @@ class RegisterController extends Controller | ... | @@ -158,7 +158,13 @@ class RegisterController extends Controller |
| 158 | 158 | ||
| 159 | if (setting('registration-confirmation') || setting('registration-restrict')) { | 159 | if (setting('registration-confirmation') || setting('registration-restrict')) { |
| 160 | $newUser->save(); | 160 | $newUser->save(); |
| 161 | - $this->emailConfirmationService->sendConfirmation($newUser); | 161 | + |
| 162 | + try { | ||
| 163 | + $this->emailConfirmationService->sendConfirmation($newUser); | ||
| 164 | + } catch (Exception $e) { | ||
| 165 | + session()->flash('error', trans('auth.email_confirm_send_error')); | ||
| 166 | + } | ||
| 167 | + | ||
| 162 | return redirect('/register/confirm'); | 168 | return redirect('/register/confirm'); |
| 163 | } | 169 | } |
| 164 | 170 | ||
| ... | @@ -189,7 +195,7 @@ class RegisterController extends Controller | ... | @@ -189,7 +195,7 @@ class RegisterController extends Controller |
| 189 | $user->email_confirmed = true; | 195 | $user->email_confirmed = true; |
| 190 | $user->save(); | 196 | $user->save(); |
| 191 | auth()->login($user); | 197 | auth()->login($user); |
| 192 | - session()->flash('success', 'Your email has been confirmed!'); | 198 | + session()->flash('success', trans('auth.email_confirm_success')); |
| 193 | $this->emailConfirmationService->deleteConfirmationsByUser($user); | 199 | $this->emailConfirmationService->deleteConfirmationsByUser($user); |
| 194 | return redirect($this->redirectPath); | 200 | return redirect($this->redirectPath); |
| 195 | } | 201 | } |
| ... | @@ -215,8 +221,16 @@ class RegisterController extends Controller | ... | @@ -215,8 +221,16 @@ class RegisterController extends Controller |
| 215 | 'email' => 'required|email|exists:users,email' | 221 | 'email' => 'required|email|exists:users,email' |
| 216 | ]); | 222 | ]); |
| 217 | $user = $this->userRepo->getByEmail($request->get('email')); | 223 | $user = $this->userRepo->getByEmail($request->get('email')); |
| 224 | + | ||
| 225 | + try { | ||
| 226 | + $this->emailConfirmationService->sendConfirmation($user); | ||
| 227 | + } catch (Exception $e) { | ||
| 228 | + session()->flash('error', trans('auth.email_confirm_send_error')); | ||
| 229 | + return redirect('/register/confirm'); | ||
| 230 | + } | ||
| 231 | + | ||
| 218 | $this->emailConfirmationService->sendConfirmation($user); | 232 | $this->emailConfirmationService->sendConfirmation($user); |
| 219 | - session()->flash('success', 'Confirmation email resent, Please check your inbox.'); | 233 | + session()->flash('success', trans('auth.email_confirm_resent')); |
| 220 | return redirect('/register/confirm'); | 234 | return redirect('/register/confirm'); |
| 221 | } | 235 | } |
| 222 | 236 | ... | ... |
| ... | @@ -3,6 +3,7 @@ | ... | @@ -3,6 +3,7 @@ |
| 3 | namespace BookStack\Http\Controllers; | 3 | namespace BookStack\Http\Controllers; |
| 4 | 4 | ||
| 5 | use BookStack\Activity; | 5 | use BookStack\Activity; |
| 6 | +use Exception; | ||
| 6 | use Illuminate\Http\Request; | 7 | use Illuminate\Http\Request; |
| 7 | 8 | ||
| 8 | use Illuminate\Http\Response; | 9 | use Illuminate\Http\Response; |
| ... | @@ -100,9 +101,14 @@ class UserController extends Controller | ... | @@ -100,9 +101,14 @@ class UserController extends Controller |
| 100 | 101 | ||
| 101 | // Get avatar from gravatar and save | 102 | // Get avatar from gravatar and save |
| 102 | if (!config('services.disable_services')) { | 103 | if (!config('services.disable_services')) { |
| 103 | - $avatar = \Images::saveUserGravatar($user); | 104 | + try { |
| 104 | - $user->avatar()->associate($avatar); | 105 | + $avatar = \Images::saveUserGravatar($user); |
| 105 | - $user->save(); | 106 | + $user->avatar()->associate($avatar); |
| 107 | + $user->save(); | ||
| 108 | + } catch (Exception $e) { | ||
| 109 | + \Log::error('Failed to save user gravatar image'); | ||
| 110 | + } | ||
| 111 | + | ||
| 106 | } | 112 | } |
| 107 | 113 | ||
| 108 | return redirect('/settings/users'); | 114 | return redirect('/settings/users'); | ... | ... |
| ... | @@ -38,11 +38,12 @@ class ConfirmEmail extends Notification | ... | @@ -38,11 +38,12 @@ class ConfirmEmail extends Notification |
| 38 | */ | 38 | */ |
| 39 | public function toMail($notifiable) | 39 | public function toMail($notifiable) |
| 40 | { | 40 | { |
| 41 | + $appName = ['appName' => setting('app-name')]; | ||
| 41 | return (new MailMessage) | 42 | return (new MailMessage) |
| 42 | - ->subject('Confirm your email on ' . session('app-name')) | 43 | + ->subject(trans('auth.email_confirm_subject', $appName)) |
| 43 | - ->greeting('Thanks for joining ' . setting('app-name') . '!') | 44 | + ->greeting(trans('auth.email_confirm_greeting', $appName)) |
| 44 | - ->line('Please confirm your email address by clicking the button below:') | 45 | + ->line(trans('auth.email_confirm_text')) |
| 45 | - ->action('Confirm Email', baseUrl('/register/confirm/' . $this->token)); | 46 | + ->action(trans('auth.email_confirm_action'), baseUrl('/register/confirm/' . $this->token)); |
| 46 | } | 47 | } |
| 47 | 48 | ||
| 48 | } | 49 | } | ... | ... |
| ... | @@ -2,6 +2,7 @@ | ... | @@ -2,6 +2,7 @@ |
| 2 | 2 | ||
| 3 | use BookStack\Role; | 3 | use BookStack\Role; |
| 4 | use BookStack\User; | 4 | use BookStack\User; |
| 5 | +use Exception; | ||
| 5 | use Setting; | 6 | use Setting; |
| 6 | 7 | ||
| 7 | class UserRepo | 8 | class UserRepo |
| ... | @@ -84,9 +85,14 @@ class UserRepo | ... | @@ -84,9 +85,14 @@ class UserRepo |
| 84 | 85 | ||
| 85 | // Get avatar from gravatar and save | 86 | // Get avatar from gravatar and save |
| 86 | if (!config('services.disable_services')) { | 87 | if (!config('services.disable_services')) { |
| 87 | - $avatar = \Images::saveUserGravatar($user); | 88 | + try { |
| 88 | - $user->avatar()->associate($avatar); | 89 | + $avatar = \Images::saveUserGravatar($user); |
| 89 | - $user->save(); | 90 | + $user->avatar()->associate($avatar); |
| 91 | + $user->save(); | ||
| 92 | + } catch (Exception $e) { | ||
| 93 | + $user->save(); | ||
| 94 | + \Log::error('Failed to save user gravatar image'); | ||
| 95 | + } | ||
| 90 | } | 96 | } |
| 91 | 97 | ||
| 92 | return $user; | 98 | return $user; | ... | ... |
| ... | @@ -213,7 +213,7 @@ class ImageService | ... | @@ -213,7 +213,7 @@ class ImageService |
| 213 | public function saveUserGravatar(User $user, $size = 500) | 213 | public function saveUserGravatar(User $user, $size = 500) |
| 214 | { | 214 | { |
| 215 | $emailHash = md5(strtolower(trim($user->email))); | 215 | $emailHash = md5(strtolower(trim($user->email))); |
| 216 | - $url = 'http://www.gravatar.com/avatar/' . $emailHash . '?s=' . $size . '&d=identicon'; | 216 | + $url = 'https://www.gravatar.com/avatar/' . $emailHash . '?s=' . $size . '&d=identicon'; |
| 217 | $imageName = str_replace(' ', '-', $user->name . '-gravatar.png'); | 217 | $imageName = str_replace(' ', '-', $user->name . '-gravatar.png'); |
| 218 | $image = $this->saveNewFromUrl($url, 'user', $imageName); | 218 | $image = $this->saveNewFromUrl($url, 'user', $imageName); |
| 219 | $image->created_by = $user->id; | 219 | $image->created_by = $user->id; | ... | ... |
| ... | @@ -12,4 +12,15 @@ return [ | ... | @@ -12,4 +12,15 @@ return [ |
| 12 | */ | 12 | */ |
| 13 | 'failed' => 'These credentials do not match our records.', | 13 | 'failed' => 'These credentials do not match our records.', |
| 14 | 'throttle' => 'Too many login attempts. Please try again in :seconds seconds.', | 14 | 'throttle' => 'Too many login attempts. Please try again in :seconds seconds.', |
| 15 | + | ||
| 16 | + /** | ||
| 17 | + * Email Confirmation Text | ||
| 18 | + */ | ||
| 19 | + 'email_confirm_subject' => 'Confirm your email on :appName', | ||
| 20 | + 'email_confirm_greeting' => 'Thanks for joining :appName!', | ||
| 21 | + 'email_confirm_text' => 'Please confirm your email address by clicking the button below:', | ||
| 22 | + 'email_confirm_action' => 'Confirm Email', | ||
| 23 | + 'email_confirm_send_error' => 'Email confirmation required but the system could not send the email. Contact the admin to ensure email is set up correctly.', | ||
| 24 | + 'email_confirm_success' => 'Your email has been confirmed!', | ||
| 25 | + 'email_confirm_resent' => 'Confirmation email resent, Please check your inbox.', | ||
| 15 | ]; | 26 | ]; |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or sign in to post a comment