X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/05666efda9a51f245d23fc33e65cd598180e97da..refs/pull/479/head:/app/Repos/UserRepo.php diff --git a/app/Repos/UserRepo.php b/app/Repos/UserRepo.php index b4931bdff..c3546a442 100644 --- a/app/Repos/UserRepo.php +++ b/app/Repos/UserRepo.php @@ -2,7 +2,7 @@ use BookStack\Role; use BookStack\User; -use Setting; +use Exception; class UserRepo { @@ -51,6 +51,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 +84,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; @@ -142,13 +168,13 @@ class UserRepo public function getRecentlyCreated(User $user, $count = 20) { return [ - 'pages' => $this->entityRepo->getRecentlyCreatedPages($count, 0, function ($query) use ($user) { + 'pages' => $this->entityRepo->getRecentlyCreated('page', $count, 0, function ($query) use ($user) { $query->where('created_by', '=', $user->id); }), - 'chapters' => $this->entityRepo->getRecentlyCreatedChapters($count, 0, function ($query) use ($user) { + 'chapters' => $this->entityRepo->getRecentlyCreated('chapter', $count, 0, function ($query) use ($user) { $query->where('created_by', '=', $user->id); }), - 'books' => $this->entityRepo->getRecentlyCreatedBooks($count, 0, function ($query) use ($user) { + 'books' => $this->entityRepo->getRecentlyCreated('book', $count, 0, function ($query) use ($user) { $query->where('created_by', '=', $user->id); }) ]; @@ -172,9 +198,9 @@ class UserRepo * Get the roles in the system that are assignable to a user. * @return mixed */ - public function getAssignableRoles() + public function getAllRoles() { - return $this->role->visible(); + return $this->role->all(); } /** @@ -184,7 +210,7 @@ class UserRepo */ public function getRestrictableRoles() { - return $this->role->where('hidden', '=', false)->where('system_name', '=', '')->get(); + return $this->role->where('system_name', '!=', 'admin')->get(); } } \ No newline at end of file