use Activity;
+use BookStack\Exceptions\NotFoundException;
+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();
}
/**
* @param $slug
* @param $bookId
* @return mixed
+ * @throws NotFoundException
*/
public function getBySlug($slug, $bookId)
{
- $chapter = $this->chapter->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
- if ($chapter === null) abort(404);
+ $chapter = $this->chapterQuery()->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
+ if ($chapter === null) throw new NotFoundException('Chapter not found');
return $chapter;
}
+ /**
+ * Get the child items for a chapter
+ * @param Chapter $chapter
+ */
+ public function getChildren(Chapter $chapter)
+ {
+ return $this->restrictionService->enforcePageRestrictions($chapter->pages())->get();
+ }
+
/**
* Create a new chapter from request input.
* @param $input
}
Activity::removeEntity($chapter);
$chapter->views()->delete();
+ $chapter->restrictions()->delete();
$chapter->delete();
}
if (!empty($term)) {
$terms = array_merge($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) {
return $chapter;
}
+ /**
+ * Updates pages restrictions from a request
+ * @param $request
+ * @param $chapter
+ */
+ public function updateRestrictionsFromRequest($request, $chapter)
+ {
+ // TODO - extract into shared repo
+ $chapter->restricted = $request->has('restricted') && $request->get('restricted') === 'true';
+ $chapter->restrictions()->delete();
+ if ($request->has('restrictions')) {
+ foreach($request->get('restrictions') as $roleId => $restrictions) {
+ foreach ($restrictions as $action => $value) {
+ $chapter->restrictions()->create([
+ 'role_id' => $roleId,
+ 'action' => strtolower($action)
+ ]);
+ }
+ }
+ }
+ $chapter->save();
+ }
+
}
\ No newline at end of file