]> BookStack Code Mirror - bookstack/blobdiff - app/Auth/UserRepo.php
Reviewed PR to add import user avatars va LDAP
[bookstack] / app / Auth / UserRepo.php
index 6fb5dfa0fb48af6467a24c19bf360ca15a3d913e..15ce2cbc9bb28d2b9aaf52208cc4fc0d488a358d 100644 (file)
@@ -8,13 +8,11 @@ use BookStack\Entities\Models\Chapter;
 use BookStack\Entities\Models\Page;
 use BookStack\Exceptions\NotFoundException;
 use BookStack\Exceptions\UserUpdateException;
-use BookStack\Uploads\Image;
 use BookStack\Uploads\UserAvatars;
 use Exception;
 use Illuminate\Database\Eloquent\Builder;
 use Illuminate\Database\Eloquent\Collection;
 use Illuminate\Pagination\LengthAwarePaginator;
-use Images;
 use Log;
 
 class UserRepo
@@ -45,6 +43,14 @@ class UserRepo
         return User::query()->findOrFail($id);
     }
 
+    /**
+     * Get a user by their slug.
+     */
+    public function getBySlug(string $slug): User
+    {
+        return User::query()->where('slug', '=', $slug)->firstOrFail();
+    }
+
     /**
      * Get all the users with their permissions.
      */
@@ -59,14 +65,10 @@ class UserRepo
     public function getAllUsersPaginatedAndSorted(int $count, array $sortData): LengthAwarePaginator
     {
         $sort = $sortData['sort'];
-        if ($sort === 'latest_activity') {
-            $sort = \BookStack\Actions\Activity::query()->select('created_at')
-                ->whereColumn('activities.user_id', 'users.id')
-                ->latest()
-                ->take(1);
-        }
 
-        $query = User::query()->with(['roles', 'avatar', 'latestActivity'])
+        $query = User::query()->select(['*'])
+            ->withLastActivityAt()
+            ->with(['roles', 'avatar'])
             ->orderBy($sort, $sortData['order']);
 
         if ($sortData['search']) {
@@ -163,7 +165,13 @@ class UserRepo
             'email_confirmed' => $emailConfirmed,
             'external_auth_id' => $data['external_auth_id'] ?? '',
         ];
-        return User::query()->forceCreate($details);
+
+        $user = new User();
+        $user->forceFill($details);
+        $user->refreshSlug();
+        $user->save();
+
+        return $user;
     }
 
     /**
@@ -174,16 +182,11 @@ class UserRepo
     {
         $user->socialAccounts()->delete();
         $user->apiTokens()->delete();
+        $user->favourites()->delete();
         $user->delete();
         
         // Delete user profile images
-        $profileImages = Image::query()->where('type', '=', 'user')
-            ->where('uploaded_to', '=', $user->id)
-            ->get();
-
-        foreach ($profileImages as $image) {
-            Images::destroy($image);
-        }
+        $this->userAvatar->destroyAllForUser($user);
 
         if (!empty($newOwnerId)) {
             $newOwner = User::query()->find($newOwnerId);