use Activity;
+use BookStack\Services\RestrictionService;
use Illuminate\Support\Str;
use BookStack\Chapter;
{
protected $chapter;
+ protected $restrictionService;
/**
* ChapterRepo constructor.
- * @param $chapter
+ * @param Chapter $chapter
+ * @param RestrictionService $restrictionService
*/
- public function __construct(Chapter $chapter)
+ public function __construct(Chapter $chapter, RestrictionService $restrictionService)
{
$this->chapter = $chapter;
+ $this->restrictionService = $restrictionService;
+ }
+
+ /**
+ * Base query for getting chapters, Takes restrictions into account.
+ * @return mixed
+ */
+ private function chapterQuery()
+ {
+ return $this->restrictionService->enforceChapterRestrictions($this->chapter, 'view');
}
/**
*/
public function idExists($id)
{
- return $this->chapter->where('id', '=', $id)->count() > 0;
+ return $this->chapterQuery()->where('id', '=', $id)->count() > 0;
}
/**
*/
public function getById($id)
{
- return $this->chapter->findOrFail($id);
+ return $this->chapterQuery()->findOrFail($id);
}
/**
*/
public function getAll()
{
- return $this->chapter->all();
+ return $this->chapterQuery()->all();
}
/**
*/
public function getBySlug($slug, $bookId)
{
- $chapter = $this->chapter->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
+ $chapter = $this->chapterQuery()->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
if ($chapter === null) abort(404);
return $chapter;
}
public function getBySearch($term, $whereTerms = [], $count = 20, $paginationAppends = [])
{
$terms = explode(' ', $term);
- $chapters = $this->chapter->fullTextSearchQuery(['name', 'description'], $terms, $whereTerms)
+ $chapters = $this->restrictionService->enforceChapterRestrictions($this->chapter->fullTextSearchQuery(['name', 'description'], $terms, $whereTerms))
->paginate($count)->appends($paginationAppends);
$words = join('|', explode(' ', preg_quote(trim($term), '/')));
foreach ($chapters as $chapter) {