From 93223fcd3d08b3255566ca6a7626d3e6cf3a25e9 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Mon, 14 Dec 2015 20:13:32 +0000 Subject: [PATCH] Cleaned up gravatar image importing --- app/Http/Controllers/UserController.php | 20 ++++++++++++-------- app/Image.php | 6 +++--- app/Services/ImageService.php | 3 --- app/User.php | 2 +- phpunit.xml | 1 + 5 files changed, 17 insertions(+), 15 deletions(-) diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index c3f08a2f8..46b6bf22e 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -4,7 +4,7 @@ namespace BookStack\Http\Controllers; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Hash; +use Illuminate\Http\Response; use BookStack\Http\Requests; use BookStack\Repos\UserRepo; use BookStack\Services\SocialAuthService; @@ -30,7 +30,6 @@ class UserController extends Controller /** * Display a listing of the users. - * * @return Response */ public function index() @@ -42,7 +41,6 @@ class UserController extends Controller /** * Show the form for creating a new user. - * * @return Response */ public function create() @@ -53,7 +51,6 @@ class UserController extends Controller /** * Store a newly created user in storage. - * * @param Request $request * @return Response */ @@ -73,13 +70,20 @@ class UserController extends Controller $user->save(); $user->attachRoleId($request->get('role')); + + // Get avatar from gravatar and save + if (!env('DISABLE_EXTERNAL_SERVICES', false)) { + $avatar = \Images::saveUserGravatar($user); + $user->avatar()->associate($avatar); + $user->save(); + } + return redirect('/users'); } /** * Show the form for editing the specified user. - * * @param int $id * @param SocialAuthService $socialAuthService * @return Response @@ -98,7 +102,6 @@ class UserController extends Controller /** * Update the specified user in storage. - * * @param Request $request * @param int $id * @return Response @@ -148,7 +151,6 @@ class UserController extends Controller /** * Remove the specified user from storage. - * * @param int $id * @return Response */ @@ -158,11 +160,13 @@ class UserController extends Controller return $this->currentUser->id == $id; }); $user = $this->userRepo->getById($id); + // Delete social accounts - if($this->userRepo->isOnlyAdmin($user)) { + if ($this->userRepo->isOnlyAdmin($user)) { session()->flash('error', 'You cannot delete the only admin'); return redirect($user->getEditUrl()); } + $user->socialAccounts()->delete(); $user->delete(); return redirect('/users'); diff --git a/app/Image.php b/app/Image.php index 651c618e6..3ac084d8f 100644 --- a/app/Image.php +++ b/app/Image.php @@ -16,11 +16,11 @@ class Image extends Model * Get a thumbnail for this image. * @param int $width * @param int $height - * @param bool|false $hardCrop + * @param bool|false $keepRatio * @return string */ - public function getThumb($width, $height, $hardCrop = false) + public function getThumb($width, $height, $keepRatio = false) { - return Images::getThumbnail($this, $width, $height, $hardCrop); + return Images::getThumbnail($this, $width, $height, $keepRatio); } } diff --git a/app/Services/ImageService.php b/app/Services/ImageService.php index 57293209c..3f3094103 100644 --- a/app/Services/ImageService.php +++ b/app/Services/ImageService.php @@ -183,15 +183,12 @@ class ImageService */ public function saveUserGravatar(User $user, $size = 500) { - if (!env('USE_GRAVATAR', false)) return false; $emailHash = md5(strtolower(trim($user->email))); $url = 'http://www.gravatar.com/avatar/' . $emailHash . '?s=' . $size . '&d=identicon'; $imageName = str_replace(' ', '-', $user->name . '-gravatar.png'); $image = $this->saveNewFromUrl($url, 'user', $imageName); $image->created_by = $user->id; $image->save(); - $user->avatar()->associate($image); - $user->save(); return $image; } diff --git a/app/User.php b/app/User.php index bf2b14ac4..12b502adc 100644 --- a/app/User.php +++ b/app/User.php @@ -146,7 +146,7 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon public function getAvatar($size = 50) { if ($this->image_id === 0 || $this->image_id === null) return '/user_avatar.png'; - return $this->avatar->getThumb($size, $size, true); + return $this->avatar->getThumb($size, $size, false); } /** diff --git a/phpunit.xml b/phpunit.xml index 0884937af..d86aacd00 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -26,5 +26,6 @@ +