]> BookStack Code Mirror - bookstack/blobdiff - app/Repos/UserRepo.php
#630: Deleting user's profile pics on deleting of user account (#646)
[bookstack] / app / Repos / UserRepo.php
index 22c92f3cea1b8e16f18244eb417be8b040deeaf3..a159606da74ca48ba41c0559839fcf1dd50f05f7 100644 (file)
@@ -3,6 +3,7 @@
 use BookStack\Role;
 use BookStack\User;
 use Exception;
+use BookStack\Services\ImageService;
 
 class UserRepo
 {
@@ -10,6 +11,7 @@ class UserRepo
     protected $user;
     protected $role;
     protected $entityRepo;
+    protected $imageService;
 
     /**
      * UserRepo constructor.
@@ -17,11 +19,12 @@ class UserRepo
      * @param Role $role
      * @param EntityRepo $entityRepo
      */
-    public function __construct(User $user, Role $role, EntityRepo $entityRepo)
+    public function __construct(User $user, Role $role, EntityRepo $entityRepo, ImageService $imageService)
     {
         $this->user = $user;
         $this->role = $role;
         $this->entityRepo = $entityRepo;
+        $this->imageService = $imageService;
     }
 
     /**
@@ -115,9 +118,9 @@ class UserRepo
      */
     public function isOnlyAdmin(User $user)
     {
-        if (!$user->roles->pluck('name')->contains('admin')) return false;
+        if (!$user->hasSystemRole('admin')) return false;
 
-        $adminRole = $this->role->getRole('admin');
+        $adminRole = $this->role->getSystemRole('admin');
         if ($adminRole->users->count() > 1) return false;
         return true;
     }
@@ -145,6 +148,12 @@ class UserRepo
     {
         $user->socialAccounts()->delete();
         $user->delete();
+        
+        // Deleting User profile pics
+        $profilePic = $user->image_id ? $user->avatar->findOrFail($user->image_id) : FALSE;
+        if ($profilePic) {
+            $this->imageService->destroyImage($profilePic);
+        }
     }
 
     /**
@@ -168,13 +177,13 @@ class UserRepo
     public function getRecentlyCreated(User $user, $count = 20)
     {
         return [
-            'pages'    => $this->entityRepo->getRecentlyCreatedPages($count, 0, function ($query) use ($user) {
+            'pages'    => $this->entityRepo->getRecentlyCreated('page', $count, 0, function ($query) use ($user) {
                 $query->where('created_by', '=', $user->id);
             }),
-            'chapters' => $this->entityRepo->getRecentlyCreatedChapters($count, 0, function ($query) use ($user) {
+            'chapters' => $this->entityRepo->getRecentlyCreated('chapter', $count, 0, function ($query) use ($user) {
                 $query->where('created_by', '=', $user->id);
             }),
-            'books'    => $this->entityRepo->getRecentlyCreatedBooks($count, 0, function ($query) use ($user) {
+            'books'    => $this->entityRepo->getRecentlyCreated('book', $count, 0, function ($query) use ($user) {
                 $query->where('created_by', '=', $user->id);
             })
         ];