]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/BookshelfController.php
Made delete permissions a requirement for move operations
[bookstack] / app / Http / Controllers / BookshelfController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use Activity;
4 use BookStack\Auth\UserRepo;
5 use BookStack\Entities\Bookshelf;
6 use BookStack\Entities\Repos\EntityRepo;
7 use BookStack\Entities\ExportService;
8 use Illuminate\Http\Request;
9 use Illuminate\Http\Response;
10 use Views;
11
12 class BookshelfController extends Controller
13 {
14
15     protected $entityRepo;
16     protected $userRepo;
17     protected $exportService;
18
19     /**
20      * BookController constructor.
21      * @param \BookStack\Entities\Repos\EntityRepo $entityRepo
22      * @param UserRepo $userRepo
23      * @param \BookStack\Entities\ExportService $exportService
24      */
25     public function __construct(EntityRepo $entityRepo, UserRepo $userRepo, ExportService $exportService)
26     {
27         $this->entityRepo = $entityRepo;
28         $this->userRepo = $userRepo;
29         $this->exportService = $exportService;
30         parent::__construct();
31     }
32
33     /**
34      * Display a listing of the book.
35      * @return Response
36      */
37     public function index()
38     {
39         $shelves = $this->entityRepo->getAllPaginated('bookshelf', 18);
40         $recents = $this->signedIn ? $this->entityRepo->getRecentlyViewed('bookshelf', 4, 0) : false;
41         $popular = $this->entityRepo->getPopular('bookshelf', 4, 0);
42         $new = $this->entityRepo->getRecentlyCreated('bookshelf', 4, 0);
43         $shelvesViewType = setting()->getUser($this->currentUser, 'bookshelves_view_type', config('app.views.bookshelves', 'grid'));
44
45         $this->setPageTitle(trans('entities.shelves'));
46         return view('shelves/index', [
47             'shelves' => $shelves,
48             'recents' => $recents,
49             'popular' => $popular,
50             'new' => $new,
51             'shelvesViewType' => $shelvesViewType
52         ]);
53     }
54
55     /**
56      * Show the form for creating a new bookshelf.
57      * @return Response
58      */
59     public function create()
60     {
61         $this->checkPermission('bookshelf-create-all');
62         $books = $this->entityRepo->getAll('book', false, 'update');
63         $this->setPageTitle(trans('entities.shelves_create'));
64         return view('shelves/create', ['books' => $books]);
65     }
66
67     /**
68      * Store a newly created bookshelf in storage.
69      * @param  Request $request
70      * @return Response
71      */
72     public function store(Request $request)
73     {
74         $this->checkPermission('bookshelf-create-all');
75         $this->validate($request, [
76             'name' => 'required|string|max:255',
77             'description' => 'string|max:1000',
78         ]);
79
80         $bookshelf = $this->entityRepo->createFromInput('bookshelf', $request->all());
81         $this->entityRepo->updateShelfBooks($bookshelf, $request->get('books', ''));
82         Activity::add($bookshelf, 'bookshelf_create');
83
84         return redirect($bookshelf->getUrl());
85     }
86
87
88     /**
89      * Display the specified bookshelf.
90      * @param String $slug
91      * @return Response
92      * @throws \BookStack\Exceptions\NotFoundException
93      */
94     public function show(string $slug)
95     {
96         $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
97         $this->checkOwnablePermission('book-view', $bookshelf);
98
99         $books = $this->entityRepo->getBookshelfChildren($bookshelf);
100         Views::add($bookshelf);
101
102         $this->setPageTitle($bookshelf->getShortName());
103         return view('shelves/show', [
104             'shelf' => $bookshelf,
105             'books' => $books,
106             'activity' => Activity::entityActivity($bookshelf, 20, 0)
107         ]);
108     }
109
110     /**
111      * Show the form for editing the specified bookshelf.
112      * @param $slug
113      * @return Response
114      * @throws \BookStack\Exceptions\NotFoundException
115      */
116     public function edit(string $slug)
117     {
118         $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
119         $this->checkOwnablePermission('bookshelf-update', $bookshelf);
120
121         $shelfBooks = $this->entityRepo->getBookshelfChildren($bookshelf);
122         $shelfBookIds = $shelfBooks->pluck('id');
123         $books = $this->entityRepo->getAll('book', false, 'update');
124         $books = $books->filter(function ($book) use ($shelfBookIds) {
125              return !$shelfBookIds->contains($book->id);
126         });
127
128         $this->setPageTitle(trans('entities.shelves_edit_named', ['name' => $bookshelf->getShortName()]));
129         return view('shelves/edit', [
130             'shelf' => $bookshelf,
131             'books' => $books,
132             'shelfBooks' => $shelfBooks,
133         ]);
134     }
135
136
137     /**
138      * Update the specified bookshelf in storage.
139      * @param  Request $request
140      * @param string $slug
141      * @return Response
142      * @throws \BookStack\Exceptions\NotFoundException
143      */
144     public function update(Request $request, string $slug)
145     {
146         $shelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
147         $this->checkOwnablePermission('bookshelf-update', $shelf);
148         $this->validate($request, [
149             'name' => 'required|string|max:255',
150             'description' => 'string|max:1000',
151         ]);
152
153          $shelf = $this->entityRepo->updateFromInput('bookshelf', $shelf, $request->all());
154          $this->entityRepo->updateShelfBooks($shelf, $request->get('books', ''));
155          Activity::add($shelf, 'bookshelf_update');
156
157          return redirect($shelf->getUrl());
158     }
159
160
161     /**
162      * Shows the page to confirm deletion
163      * @param $slug
164      * @return \Illuminate\View\View
165      * @throws \BookStack\Exceptions\NotFoundException
166      */
167     public function showDelete(string $slug)
168     {
169         $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
170         $this->checkOwnablePermission('bookshelf-delete', $bookshelf);
171
172         $this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $bookshelf->getShortName()]));
173         return view('shelves/delete', ['shelf' => $bookshelf]);
174     }
175
176     /**
177      * Remove the specified bookshelf from storage.
178      * @param string $slug
179      * @return Response
180      * @throws \BookStack\Exceptions\NotFoundException
181      * @throws \Throwable
182      */
183     public function destroy(string $slug)
184     {
185         $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
186         $this->checkOwnablePermission('bookshelf-delete', $bookshelf);
187         Activity::addMessage('bookshelf_delete', 0, $bookshelf->name);
188         $this->entityRepo->destroyBookshelf($bookshelf);
189         return redirect('/shelves');
190     }
191
192     /**
193      * Show the Restrictions view.
194      * @param $slug
195      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
196      * @throws \BookStack\Exceptions\NotFoundException
197      */
198     public function showRestrict(string $slug)
199     {
200         $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug);
201         $this->checkOwnablePermission('restrictions-manage', $bookshelf);
202
203         $roles = $this->userRepo->getRestrictableRoles();
204         return view('shelves.restrictions', [
205             'shelf' => $bookshelf,
206             'roles' => $roles
207         ]);
208     }
209
210     /**
211      * Set the restrictions for this bookshelf.
212      * @param $slug
213      * @param Request $request
214      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
215      * @throws \BookStack\Exceptions\NotFoundException
216      */
217     public function restrict(string $slug, Request $request)
218     {
219         $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug);
220         $this->checkOwnablePermission('restrictions-manage', $bookshelf);
221
222         $this->entityRepo->updateEntityPermissionsFromRequest($request, $bookshelf);
223         session()->flash('success', trans('entities.shelves_permissions_updated'));
224         return redirect($bookshelf->getUrl());
225     }
226
227     /**
228      * Copy the permissions of a bookshelf to the child books.
229      * @param string $slug
230      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
231      * @throws \BookStack\Exceptions\NotFoundException
232      */
233     public function copyPermissions(string $slug)
234     {
235         $bookshelf = $this->entityRepo->getBySlug('bookshelf', $slug);
236         $this->checkOwnablePermission('restrictions-manage', $bookshelf);
237
238         $updateCount = $this->entityRepo->copyBookshelfPermissions($bookshelf);
239         session()->flash('success', trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
240         return redirect($bookshelf->getUrl());
241     }
242 }