<?php namespace BookStack\Auth;
use Activity;
-use BookStack\Entities\Book;
-use BookStack\Entities\Bookshelf;
-use BookStack\Entities\Chapter;
-use BookStack\Entities\Page;
+use BookStack\Entities\Models\Book;
+use BookStack\Entities\Models\Bookshelf;
+use BookStack\Entities\Models\Chapter;
+use BookStack\Entities\Models\Page;
use BookStack\Exceptions\NotFoundException;
use BookStack\Exceptions\UserUpdateException;
use BookStack\Uploads\Image;
use Exception;
use Illuminate\Database\Eloquent\Builder;
+use Illuminate\Pagination\LengthAwarePaginator;
use Images;
use Log;
}
/**
- * @param string $email
- * @return User|null
+ * Get a user by their email address.
*/
- public function getByEmail($email)
+ public function getByEmail(string $email): ?User
{
return $this->user->where('email', '=', $email)->first();
}
/**
* Get all the users with their permissions in a paginated format.
- * @param int $count
- * @param $sortData
- * @return Builder|static
*/
- public function getAllUsersPaginatedAndSorted($count, $sortData)
+ public function getAllUsersPaginatedAndSorted(int $count, array $sortData): LengthAwarePaginator
{
- $query = $this->user->with('roles', 'avatar')->orderBy($sortData['sort'], $sortData['order']);
+ $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 = $this->user->with(['roles', 'avatar', 'latestActivity'])
+ ->orderBy($sort, $sortData['order']);
if ($sortData['search']) {
$term = '%' . $sortData['search'] . '%';
/**
* Creates a new user and attaches a role to them.
- * @param array $data
- * @param boolean $verifyEmail
- * @return User
*/
- public function registerNew(array $data, $verifyEmail = false)
+ public function registerNew(array $data, bool $emailConfirmed = false): User
{
- $user = $this->create($data, $verifyEmail);
- $this->attachDefaultRole($user);
+ $user = $this->create($data, $emailConfirmed);
+ $user->attachDefaultRole();
$this->downloadAndAssignUserAvatar($user);
return $user;
}
- /**
- * Give a user the default role. Used when creating a new user.
- * @param User $user
- */
- public function attachDefaultRole(User $user)
- {
- $roleId = setting('registration-role');
- if ($roleId !== false && $user->roles()->where('id', '=', $roleId)->count() === 0) {
- $user->attachRoleId($roleId);
- }
- }
-
/**
* Assign a user to a system-level role.
* @param User $user
/**
* Create a new basic instance of user.
- * @param array $data
- * @param boolean $verifyEmail
- * @return User
*/
- public function create(array $data, $verifyEmail = false)
+ public function create(array $data, bool $emailConfirmed = false): User
{
return $this->user->forceCreate([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
- 'email_confirmed' => $verifyEmail
+ 'email_confirmed' => $emailConfirmed,
+ 'external_auth_id' => $data['external_auth_id'] ?? '',
]);
}
public function destroy(User $user)
{
$user->socialAccounts()->delete();
+ $user->apiTokens()->delete();
$user->delete();
// Delete user profile images
*/
public function getAllRoles()
{
- return $this->role->newQuery()->orderBy('name', 'asc')->get();
+ return $this->role->newQuery()->orderBy('display_name', 'asc')->get();
}
/**