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\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)) {
session()->flash('error', 'You cannot delete the only admin');
return redirect($user->getEditUrl());
}
$user->socialAccounts()->delete();
$user->delete();
return redirect('/users');

View File

@ -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);
}
}

View File

@ -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;
}

View File

@ -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);
}
/**

View File

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