]> 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 15813b3e18b4e204d4a6e45e84cab56477754f7b..a159606da74ca48ba41c0559839fcf1dd50f05f7 100644 (file)
@@ -2,7 +2,8 @@
 
 use BookStack\Role;
 use BookStack\User;
-use Setting;
+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;
     }
 
     /**
@@ -51,6 +54,27 @@ class UserRepo
         return $this->user->with('roles', 'avatar')->orderBy('name', 'asc')->get();
     }
 
+    /**
+     * Get all the users with their permissions in a paginated format.
+     * @param int $count
+     * @param $sortData
+     * @return \Illuminate\Database\Eloquent\Builder|static
+     */
+    public function getAllUsersPaginatedAndSorted($count = 20, $sortData)
+    {
+        $query = $this->user->with('roles', 'avatar')->orderBy($sortData['sort'], $sortData['order']);
+
+        if ($sortData['search']) {
+            $term = '%' . $sortData['search'] . '%';
+            $query->where(function($query) use ($term) {
+                $query->where('name', 'like', $term)
+                    ->orWhere('email', 'like', $term);
+            });
+        }
+
+        return $query->paginate($count);
+    }
+
     /**
      * Creates a new user and attaches a role to them.
      * @param array $data
@@ -63,9 +87,14 @@ class UserRepo
 
         // Get avatar from gravatar and save
         if (!config('services.disable_services')) {
-            $avatar = \Images::saveUserGravatar($user);
-            $user->avatar()->associate($avatar);
-            $user->save();
+            try {
+                $avatar = \Images::saveUserGravatar($user);
+                $user->avatar()->associate($avatar);
+                $user->save();
+            } catch (Exception $e) {
+                $user->save();
+                \Log::error('Failed to save user gravatar image');
+            }
         }
 
         return $user;
@@ -77,7 +106,7 @@ class UserRepo
      */
     public function attachDefaultRole($user)
     {
-        $roleId = Setting::get('registration-role');
+        $roleId = setting('registration-role');
         if ($roleId === false) $roleId = $this->role->first()->id;
         $user->attachRoleId($roleId);
     }
@@ -89,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;
     }
@@ -106,7 +135,8 @@ class UserRepo
         return $this->user->forceCreate([
             'name'     => $data['name'],
             'email'    => $data['email'],
-            'password' => bcrypt($data['password'])
+            'password' => bcrypt($data['password']),
+            'email_confirmed' => false
         ]);
     }
 
@@ -118,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);
+        }
     }
 
     /**
@@ -141,12 +177,15 @@ class UserRepo
     public function getRecentlyCreated(User $user, $count = 20)
     {
         return [
-            'pages' => $this->entityRepo->page->where('created_by', '=', $user->id)->orderBy('created_at', 'desc')
-                ->take($count)->get(),
-            'chapters' => $this->entityRepo->chapter->where('created_by', '=', $user->id)->orderBy('created_at', 'desc')
-                ->take($count)->get(),
-            'books' => $this->entityRepo->book->where('created_by', '=', $user->id)->orderBy('created_at', 'desc')
-                ->take($count)->get()
+            'pages'    => $this->entityRepo->getRecentlyCreated('page', $count, 0, function ($query) use ($user) {
+                $query->where('created_by', '=', $user->id);
+            }),
+            'chapters' => $this->entityRepo->getRecentlyCreated('chapter', $count, 0, function ($query) use ($user) {
+                $query->where('created_by', '=', $user->id);
+            }),
+            'books'    => $this->entityRepo->getRecentlyCreated('book', $count, 0, function ($query) use ($user) {
+                $query->where('created_by', '=', $user->id);
+            })
         ];
     }
 
@@ -158,10 +197,29 @@ class UserRepo
     public function getAssetCounts(User $user)
     {
         return [
-            'pages' => $this->entityRepo->page->where('created_by', '=', $user->id)->count(),
+            'pages'    => $this->entityRepo->page->where('created_by', '=', $user->id)->count(),
             'chapters' => $this->entityRepo->chapter->where('created_by', '=', $user->id)->count(),
-            'books' => $this->entityRepo->book->where('created_by', '=', $user->id)->count(),
+            'books'    => $this->entityRepo->book->where('created_by', '=', $user->id)->count(),
         ];
     }
 
+    /**
+     * Get the roles in the system that are assignable to a user.
+     * @return mixed
+     */
+    public function getAllRoles()
+    {
+        return $this->role->all();
+    }
+
+    /**
+     * Get all the roles which can be given restricted access to
+     * other entities in the system.
+     * @return mixed
+     */
+    public function getRestrictableRoles()
+    {
+        return $this->role->where('system_name', '!=', 'admin')->get();
+    }
+
 }
\ No newline at end of file