mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-08-26 04:05:57 +00:00
Updated user profile image delete to delete all uploads
Also moved test and made more comprehensive
This commit is contained in:
parent
35e00ddb95
commit
ead4b14d94
@ -1,9 +1,11 @@
|
|||||||
<?php namespace BookStack\Repos;
|
<?php namespace BookStack\Repos;
|
||||||
|
|
||||||
|
use Activity;
|
||||||
|
use BookStack\Image;
|
||||||
use BookStack\Role;
|
use BookStack\Role;
|
||||||
use BookStack\User;
|
use BookStack\User;
|
||||||
use Exception;
|
use Exception;
|
||||||
use BookStack\Services\ImageService;
|
use Images;
|
||||||
|
|
||||||
class UserRepo
|
class UserRepo
|
||||||
{
|
{
|
||||||
@ -11,7 +13,6 @@ class UserRepo
|
|||||||
protected $user;
|
protected $user;
|
||||||
protected $role;
|
protected $role;
|
||||||
protected $entityRepo;
|
protected $entityRepo;
|
||||||
protected $imageService;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* UserRepo constructor.
|
* UserRepo constructor.
|
||||||
@ -19,12 +20,11 @@ class UserRepo
|
|||||||
* @param Role $role
|
* @param Role $role
|
||||||
* @param EntityRepo $entityRepo
|
* @param EntityRepo $entityRepo
|
||||||
*/
|
*/
|
||||||
public function __construct(User $user, Role $role, EntityRepo $entityRepo, ImageService $imageService)
|
public function __construct(User $user, Role $role, EntityRepo $entityRepo)
|
||||||
{
|
{
|
||||||
$this->user = $user;
|
$this->user = $user;
|
||||||
$this->role = $role;
|
$this->role = $role;
|
||||||
$this->entityRepo = $entityRepo;
|
$this->entityRepo = $entityRepo;
|
||||||
$this->imageService = $imageService;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -88,7 +88,7 @@ class UserRepo
|
|||||||
// Get avatar from gravatar and save
|
// Get avatar from gravatar and save
|
||||||
if (!config('services.disable_services')) {
|
if (!config('services.disable_services')) {
|
||||||
try {
|
try {
|
||||||
$avatar = \Images::saveUserGravatar($user);
|
$avatar = Images::saveUserGravatar($user);
|
||||||
$user->avatar()->associate($avatar);
|
$user->avatar()->associate($avatar);
|
||||||
$user->save();
|
$user->save();
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
@ -143,16 +143,17 @@ class UserRepo
|
|||||||
/**
|
/**
|
||||||
* Remove the given user from storage, Delete all related content.
|
* Remove the given user from storage, Delete all related content.
|
||||||
* @param User $user
|
* @param User $user
|
||||||
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function destroy(User $user)
|
public function destroy(User $user)
|
||||||
{
|
{
|
||||||
$user->socialAccounts()->delete();
|
$user->socialAccounts()->delete();
|
||||||
$user->delete();
|
$user->delete();
|
||||||
|
|
||||||
// Deleting User profile pics
|
// Delete user profile images
|
||||||
$profilePic = $user->image_id ? $user->avatar->findOrFail($user->image_id) : FALSE;
|
$profileImages = $images = Image::where('type', '=', 'user')->where('created_by', '=', $user->id)->get();
|
||||||
if ($profilePic) {
|
foreach ($profileImages as $image) {
|
||||||
$this->imageService->destroyImage($profilePic);
|
Images::destroyImage($image);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -165,7 +166,7 @@ class UserRepo
|
|||||||
*/
|
*/
|
||||||
public function getActivity(User $user, $count = 20, $page = 0)
|
public function getActivity(User $user, $count = 20, $page = 0)
|
||||||
{
|
{
|
||||||
return \Activity::userActivity($user, $count, $page);
|
return Activity::userActivity($user, $count, $page);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -236,6 +236,7 @@ class ImageService extends UploadService
|
|||||||
* Destroys an Image object along with its files and thumbnails.
|
* Destroys an Image object along with its files and thumbnails.
|
||||||
* @param Image $image
|
* @param Image $image
|
||||||
* @return bool
|
* @return bool
|
||||||
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function destroyImage(Image $image)
|
public function destroyImage(Image $image)
|
||||||
{
|
{
|
||||||
|
@ -184,4 +184,28 @@ class ImageTest extends TestCase
|
|||||||
$this->assertTrue($testImageData === $uploadedImageData, "Uploaded image file data does not match our test image as expected");
|
$this->assertTrue($testImageData === $uploadedImageData, "Uploaded image file data does not match our test image as expected");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function test_user_images_deleted_on_user_deletion()
|
||||||
|
{
|
||||||
|
$editor = $this->getEditor();
|
||||||
|
$this->actingAs($editor);
|
||||||
|
|
||||||
|
$imageName = 'profile.png';
|
||||||
|
$relPath = $this->getTestImagePath('gallery', $imageName);
|
||||||
|
$this->deleteImage($relPath);
|
||||||
|
|
||||||
|
$file = $this->getTestImage($imageName);
|
||||||
|
$this->call('POST', '/images/user/upload', [], [], ['file' => $file], []);
|
||||||
|
$this->call('POST', '/images/user/upload', [], [], ['file' => $file], []);
|
||||||
|
|
||||||
|
$profileImages = Image::where('type', '=', 'user')->where('created_by', '=', $editor->id)->get();
|
||||||
|
$this->assertTrue($profileImages->count() === 2, "Found profile images does not match upload count");
|
||||||
|
|
||||||
|
$userDelete = $this->asAdmin()->delete("/settings/users/{$editor->id}");
|
||||||
|
$userDelete->assertStatus(302);
|
||||||
|
$this->assertDatabaseMissing('images', [
|
||||||
|
'type' => 'user',
|
||||||
|
'created_by' => $editor->id
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -116,18 +116,4 @@ class UserProfileTest extends BrowserKitTest
|
|||||||
->pageHasElement('.featured-image-container');
|
->pageHasElement('.featured-image-container');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_user_delete()
|
|
||||||
{
|
|
||||||
$newUser = $this->getNewBlankUser();
|
|
||||||
$this->actingAs($newUser);
|
|
||||||
$this->asAdmin()->visit('/settings/users/' . $newUser->id . '/delete')
|
|
||||||
->see('Delete User')
|
|
||||||
->press('Confirm')
|
|
||||||
->seePageIs('/settings/users/')
|
|
||||||
->see('USERS')->see('ADD NEW USER');
|
|
||||||
|
|
||||||
$this->dontSeeInDatabase('images', [
|
|
||||||
'id' => $newUser->image_id
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user