]> BookStack Code Mirror - bookstack/blobdiff - app/Auth/UserRepo.php
Extend /users API endpoint
[bookstack] / app / Auth / UserRepo.php
index 6b7de325998a10e8dbd4a530a475d91d0c1f3458..4444c734c35077a557fcdfe8daf8664b082bfe3d 100644 (file)
@@ -1,6 +1,7 @@
 <?php namespace BookStack\Auth;
 
 use Activity;
+use BookStack\Entities\EntityProvider;
 use BookStack\Entities\Models\Book;
 use BookStack\Entities\Models\Bookshelf;
 use BookStack\Entities\Models\Chapter;
@@ -44,6 +45,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.
      */
@@ -52,20 +61,26 @@ class UserRepo
         return User::query()->with('roles', 'avatar')->orderBy('name', 'asc')->get();
     }
 
+    /**
+     * Get all users as Builder for API
+     */
+    public function getUsersBuilder(int $id = null ) : Builder
+    {
+        $query = User::query()->select(['*'])
+            ->withLastActivityAt()
+            ->with(['roles', 'avatar']);
+        return $query;
+    }
     /**
      * Get all the users with their permissions in a paginated format.
      */
     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']) {
@@ -162,14 +177,20 @@ 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;
     }
 
     /**
      * Remove the given user from storage, Delete all related content.
      * @throws Exception
      */
-    public function destroy(User $user)
+    public function destroy(User $user, ?int $newOwnerId = null)
     {
         $user->socialAccounts()->delete();
         $user->apiTokens()->delete();
@@ -183,6 +204,25 @@ class UserRepo
         foreach ($profileImages as $image) {
             Images::destroy($image);
         }
+
+        if (!empty($newOwnerId)) {
+            $newOwner = User::query()->find($newOwnerId);
+            if (!is_null($newOwner)) {
+                $this->migrateOwnership($user, $newOwner);
+            }
+        }
+    }
+
+    /**
+     * Migrate ownership of items in the system from one user to another.
+     */
+    protected function migrateOwnership(User $fromUser, User $toUser)
+    {
+        $entities = (new EntityProvider)->all();
+        foreach ($entities as $instance) {
+            $instance->newQuery()->where('owned_by', '=', $fromUser->id)
+                ->update(['owned_by' => $toUser->id]);
+        }
     }
 
     /**