X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/e6ea53b3e68932deb2a38359af8cbc34d10735b0..refs/pull/2734/head:/app/Auth/UserRepo.php diff --git a/app/Auth/UserRepo.php b/app/Auth/UserRepo.php index 6fb5dfa0f..4444c734c 100644 --- a/app/Auth/UserRepo.php +++ b/app/Auth/UserRepo.php @@ -45,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. */ @@ -53,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']) { @@ -163,7 +177,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; } /**