]> BookStack Code Mirror - bookstack/blobdiff - app/Repos/UserRepo.php
Updated user profile image delete to delete all uploads
[bookstack] / app / Repos / UserRepo.php
index 127db9fb59c482aab05198277558bf7b6abdcac6..e21568d63efb4823c528289f4fac3a87a01ef2f9 100644 (file)
@@ -1,9 +1,11 @@
 <?php namespace BookStack\Repos;
 
+use Activity;
+use BookStack\Image;
 use BookStack\Role;
 use BookStack\User;
 use Exception;
-use Setting;
+use Images;
 
 class UserRepo
 {
@@ -86,7 +88,7 @@ class UserRepo
         // Get avatar from gravatar and save
         if (!config('services.disable_services')) {
             try {
-                $avatar = \Images::saveUserGravatar($user);
+                $avatar = Images::saveUserGravatar($user);
                 $user->avatar()->associate($avatar);
                 $user->save();
             } catch (Exception $e) {
@@ -116,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;
     }
@@ -141,11 +143,18 @@ class UserRepo
     /**
      * Remove the given user from storage, Delete all related content.
      * @param User $user
+     * @throws Exception
      */
     public function destroy(User $user)
     {
         $user->socialAccounts()->delete();
         $user->delete();
+        
+        // Delete user profile images
+        $profileImages = $images = Image::where('type', '=', 'user')->where('created_by', '=', $user->id)->get();
+        foreach ($profileImages as $image) {
+            Images::destroyImage($image);
+        }
     }
 
     /**
@@ -157,7 +166,7 @@ class UserRepo
      */
     public function getActivity(User $user, $count = 20, $page = 0)
     {
-        return \Activity::userActivity($user, $count, $page);
+        return Activity::userActivity($user, $count, $page);
     }
 
     /**
@@ -169,13 +178,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);
             })
         ];
@@ -199,9 +208,9 @@ class UserRepo
      * Get the roles in the system that are assignable to a user.
      * @return mixed
      */
-    public function getAssignableRoles()
+    public function getAllRoles()
     {
-        return $this->role->visible();
+        return $this->role->all();
     }
 
     /**
@@ -211,7 +220,7 @@ class UserRepo
      */
     public function getRestrictableRoles()
     {
-        return $this->role->where('hidden', '=', false)->where('system_name', '=', '')->get();
+        return $this->role->where('system_name', '!=', 'admin')->get();
     }
 
 }
\ No newline at end of file