Cleaned up gravatar image importing

This commit is contained in:
Dan Brown 2015-12-14 20:13:32 +00:00
parent 8f7c642f32
commit 93223fcd3d
5 changed files with 17 additions and 15 deletions

View File

@ -4,7 +4,7 @@ namespace BookStack\Http\Controllers;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash; use Illuminate\Http\Response;
use BookStack\Http\Requests; use BookStack\Http\Requests;
use BookStack\Repos\UserRepo; use BookStack\Repos\UserRepo;
use BookStack\Services\SocialAuthService; use BookStack\Services\SocialAuthService;
@ -30,7 +30,6 @@ class UserController extends Controller
/** /**
* Display a listing of the users. * Display a listing of the users.
*
* @return Response * @return Response
*/ */
public function index() public function index()
@ -42,7 +41,6 @@ class UserController extends Controller
/** /**
* Show the form for creating a new user. * Show the form for creating a new user.
*
* @return Response * @return Response
*/ */
public function create() public function create()
@ -53,7 +51,6 @@ class UserController extends Controller
/** /**
* Store a newly created user in storage. * Store a newly created user in storage.
*
* @param Request $request * @param Request $request
* @return Response * @return Response
*/ */
@ -73,13 +70,20 @@ class UserController extends Controller
$user->save(); $user->save();
$user->attachRoleId($request->get('role')); $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'); return redirect('/users');
} }
/** /**
* Show the form for editing the specified user. * Show the form for editing the specified user.
*
* @param int $id * @param int $id
* @param SocialAuthService $socialAuthService * @param SocialAuthService $socialAuthService
* @return Response * @return Response
@ -98,7 +102,6 @@ class UserController extends Controller
/** /**
* Update the specified user in storage. * Update the specified user in storage.
*
* @param Request $request * @param Request $request
* @param int $id * @param int $id
* @return Response * @return Response
@ -148,7 +151,6 @@ class UserController extends Controller
/** /**
* Remove the specified user from storage. * Remove the specified user from storage.
*
* @param int $id * @param int $id
* @return Response * @return Response
*/ */
@ -158,11 +160,13 @@ class UserController extends Controller
return $this->currentUser->id == $id; return $this->currentUser->id == $id;
}); });
$user = $this->userRepo->getById($id); $user = $this->userRepo->getById($id);
// Delete social accounts // Delete social accounts
if($this->userRepo->isOnlyAdmin($user)) { if ($this->userRepo->isOnlyAdmin($user)) {
session()->flash('error', 'You cannot delete the only admin'); session()->flash('error', 'You cannot delete the only admin');
return redirect($user->getEditUrl()); return redirect($user->getEditUrl());
} }
$user->socialAccounts()->delete(); $user->socialAccounts()->delete();
$user->delete(); $user->delete();
return redirect('/users'); return redirect('/users');

View File

@ -16,11 +16,11 @@ class Image extends Model
* Get a thumbnail for this image. * Get a thumbnail for this image.
* @param int $width * @param int $width
* @param int $height * @param int $height
* @param bool|false $hardCrop * @param bool|false $keepRatio
* @return string * @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);
} }
} }

View File

@ -183,15 +183,12 @@ class ImageService
*/ */
public function saveUserGravatar(User $user, $size = 500) public function saveUserGravatar(User $user, $size = 500)
{ {
if (!env('USE_GRAVATAR', false)) return false;
$emailHash = md5(strtolower(trim($user->email))); $emailHash = md5(strtolower(trim($user->email)));
$url = 'http://www.gravatar.com/avatar/' . $emailHash . '?s=' . $size . '&d=identicon'; $url = 'http://www.gravatar.com/avatar/' . $emailHash . '?s=' . $size . '&d=identicon';
$imageName = str_replace(' ', '-', $user->name . '-gravatar.png'); $imageName = str_replace(' ', '-', $user->name . '-gravatar.png');
$image = $this->saveNewFromUrl($url, 'user', $imageName); $image = $this->saveNewFromUrl($url, 'user', $imageName);
$image->created_by = $user->id; $image->created_by = $user->id;
$image->save(); $image->save();
$user->avatar()->associate($image);
$user->save();
return $image; return $image;
} }

View File

@ -146,7 +146,7 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
public function getAvatar($size = 50) public function getAvatar($size = 50)
{ {
if ($this->image_id === 0 || $this->image_id === null) return '/user_avatar.png'; 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);
} }
/** /**

View File

@ -26,5 +26,6 @@
<env name="QUEUE_DRIVER" value="sync"/> <env name="QUEUE_DRIVER" value="sync"/>
<env name="DB_CONNECTION" value="mysql_testing"/> <env name="DB_CONNECTION" value="mysql_testing"/>
<env name="MAIL_PRETEND" value="true"/> <env name="MAIL_PRETEND" value="true"/>
<env name="DISABLE_EXTERNAL_SERVICES" value="true"/>
</php> </php>
</phpunit> </phpunit>