]> BookStack Code Mirror - bookstack/blob - app/Entities/Managers/EntityContext.php
Add support Windows Authentication via SAML
[bookstack] / app / Entities / Managers / EntityContext.php
1 <?php namespace BookStack\Entities\Managers;
2
3 use BookStack\Entities\Book;
4 use BookStack\Entities\Bookshelf;
5 use Illuminate\Session\Store;
6
7 class EntityContext
8 {
9     protected $session;
10
11     protected $KEY_SHELF_CONTEXT_ID = 'context_bookshelf_id';
12
13     /**
14      * EntityContextManager constructor.
15      */
16     public function __construct(Store $session)
17     {
18         $this->session = $session;
19     }
20
21     /**
22      * Get the current bookshelf context for the given book.
23      */
24     public function getContextualShelfForBook(Book $book): ?Bookshelf
25     {
26         $contextBookshelfId = $this->session->get($this->KEY_SHELF_CONTEXT_ID, null);
27
28         if (!is_int($contextBookshelfId)) {
29             return null;
30         }
31
32         $shelf = Bookshelf::visible()->find($contextBookshelfId);
33         $shelfContainsBook = $shelf && $shelf->contains($book);
34
35         return $shelfContainsBook ? $shelf : null;
36     }
37
38     /**
39      * Store the current contextual shelf ID.
40      * @param int $shelfId
41      */
42     public function setShelfContext(int $shelfId)
43     {
44         $this->session->put($this->KEY_SHELF_CONTEXT_ID, $shelfId);
45     }
46
47     /**
48      * Clear the session stored shelf context id.
49      */
50     public function clearShelfContext()
51     {
52         $this->session->forget($this->KEY_SHELF_CONTEXT_ID);
53     }
54 }