]> BookStack Code Mirror - bookstack/blob - app/Services/PermissionService.php
Copied book content, Added create routes
[bookstack] / app / Services / PermissionService.php
1 <?php namespace BookStack\Services;
2
3 use BookStack\Book;
4 use BookStack\Chapter;
5 use BookStack\Entity;
6 use BookStack\EntityPermission;
7 use BookStack\JointPermission;
8 use BookStack\Ownable;
9 use BookStack\Page;
10 use BookStack\Role;
11 use BookStack\User;
12 use Illuminate\Database\Connection;
13 use Illuminate\Database\Eloquent\Builder;
14 use Illuminate\Database\Query\Builder as QueryBuilder;
15 use Illuminate\Support\Collection;
16
17 class PermissionService
18 {
19
20     protected $currentAction;
21     protected $isAdminUser;
22     protected $userRoles = false;
23     protected $currentUserModel = false;
24
25     public $book;
26     public $chapter;
27     public $page;
28
29     protected $db;
30
31     protected $jointPermission;
32     protected $role;
33     protected $entityPermission;
34
35     protected $entityCache;
36
37     /**
38      * PermissionService constructor.
39      * @param JointPermission $jointPermission
40      * @param EntityPermission $entityPermission
41      * @param Connection $db
42      * @param Book $book
43      * @param Chapter $chapter
44      * @param Page $page
45      * @param Role $role
46      */
47     public function __construct(JointPermission $jointPermission, EntityPermission $entityPermission, Connection $db, Book $book, Chapter $chapter, Page $page, Role $role)
48     {
49         $this->db = $db;
50         $this->jointPermission = $jointPermission;
51         $this->entityPermission = $entityPermission;
52         $this->role = $role;
53         $this->book = $book;
54         $this->chapter = $chapter;
55         $this->page = $page;
56         // TODO - Update so admin still goes through filters
57     }
58
59     /**
60      * Set the database connection
61      * @param Connection $connection
62      */
63     public function setConnection(Connection $connection)
64     {
65         $this->db = $connection;
66     }
67
68     /**
69      * Prepare the local entity cache and ensure it's empty
70      * @param Entity[] $entities
71      */
72     protected function readyEntityCache($entities = [])
73     {
74         $this->entityCache = [];
75
76         foreach ($entities as $entity) {
77             $type = $entity->getType();
78             if (!isset($this->entityCache[$type])) {
79                 $this->entityCache[$type] = collect();
80             }
81             $this->entityCache[$type]->put($entity->id, $entity);
82         }
83     }
84
85     /**
86      * Get a book via ID, Checks local cache
87      * @param $bookId
88      * @return Book
89      */
90     protected function getBook($bookId)
91     {
92         if (isset($this->entityCache['book']) && $this->entityCache['book']->has($bookId)) {
93             return $this->entityCache['book']->get($bookId);
94         }
95
96         $book = $this->book->find($bookId);
97         if ($book === null) {
98             $book = false;
99         }
100
101         return $book;
102     }
103
104     /**
105      * Get a chapter via ID, Checks local cache
106      * @param $chapterId
107      * @return Book
108      */
109     protected function getChapter($chapterId)
110     {
111         if (isset($this->entityCache['chapter']) && $this->entityCache['chapter']->has($chapterId)) {
112             return $this->entityCache['chapter']->get($chapterId);
113         }
114
115         $chapter = $this->chapter->find($chapterId);
116         if ($chapter === null) {
117             $chapter = false;
118         }
119
120         return $chapter;
121     }
122
123     /**
124      * Get the roles for the current user;
125      * @return array|bool
126      */
127     protected function getRoles()
128     {
129         if ($this->userRoles !== false) {
130             return $this->userRoles;
131         }
132
133         $roles = [];
134
135         if (auth()->guest()) {
136             $roles[] = $this->role->getSystemRole('public')->id;
137             return $roles;
138         }
139
140
141         foreach ($this->currentUser()->roles as $role) {
142             $roles[] = $role->id;
143         }
144         return $roles;
145     }
146
147     /**
148      * Re-generate all entity permission from scratch.
149      */
150     public function buildJointPermissions()
151     {
152         $this->jointPermission->truncate();
153         $this->readyEntityCache();
154
155         // Get all roles (Should be the most limited dimension)
156         $roles = $this->role->with('permissions')->get()->all();
157
158         // Chunk through all books
159         $this->bookFetchQuery()->chunk(5, function ($books) use ($roles) {
160             $this->buildJointPermissionsForBooks($books, $roles);
161         });
162     }
163
164     /**
165      * Get a query for fetching a book with it's children.
166      * @return QueryBuilder
167      */
168     protected function bookFetchQuery()
169     {
170         return $this->book->newQuery()->select(['id', 'restricted', 'created_by'])->with(['chapters' => function ($query) {
171             $query->select(['id', 'restricted', 'created_by', 'book_id']);
172         }, 'pages'  => function ($query) {
173             $query->select(['id', 'restricted', 'created_by', 'book_id', 'chapter_id']);
174         }]);
175     }
176
177     /**
178      * Build joint permissions for an array of books
179      * @param Collection $books
180      * @param array $roles
181      * @param bool $deleteOld
182      * @throws \Throwable
183      */
184     protected function buildJointPermissionsForBooks($books, $roles, $deleteOld = false)
185     {
186         $entities = clone $books;
187
188         /** @var Book $book */
189         foreach ($books->all() as $book) {
190             foreach ($book->getRelation('chapters') as $chapter) {
191                 $entities->push($chapter);
192             }
193             foreach ($book->getRelation('pages') as $page) {
194                 $entities->push($page);
195             }
196         }
197
198         if ($deleteOld) {
199             $this->deleteManyJointPermissionsForEntities($entities->all());
200         }
201         $this->createManyJointPermissions($entities, $roles);
202     }
203
204     /**
205      * Rebuild the entity jointPermissions for a particular entity.
206      * @param Entity $entity
207      * @throws \Throwable
208      */
209     public function buildJointPermissionsForEntity(Entity $entity)
210     {
211         $entities = [$entity];
212         if ($entity->isA('book')) {
213             $books = $this->bookFetchQuery()->where('id', '=', $entity->id)->get();
214             $this->buildJointPermissionsForBooks($books, $this->role->newQuery()->get(), true);
215             return;
216         }
217
218         if ($entity->book) {
219             $entities[] = $entity->book;
220         }
221
222         if ($entity->isA('page') && $entity->chapter_id) {
223             $entities[] = $entity->chapter;
224         }
225
226         if ($entity->isA('chapter')) {
227             foreach ($entity->pages as $page) {
228                 $entities[] = $page;
229             }
230         }
231
232         $this->buildJointPermissionsForEntities(collect($entities));
233     }
234
235     /**
236      * Rebuild the entity jointPermissions for a collection of entities.
237      * @param Collection $entities
238      * @throws \Throwable
239      */
240     public function buildJointPermissionsForEntities(Collection $entities)
241     {
242         $roles = $this->role->newQuery()->get();
243         $this->deleteManyJointPermissionsForEntities($entities->all());
244         $this->createManyJointPermissions($entities, $roles);
245     }
246
247     /**
248      * Build the entity jointPermissions for a particular role.
249      * @param Role $role
250      */
251     public function buildJointPermissionForRole(Role $role)
252     {
253         $roles = [$role];
254         $this->deleteManyJointPermissionsForRoles($roles);
255
256         // Chunk through all books
257         $this->bookFetchQuery()->chunk(20, function ($books) use ($roles) {
258             $this->buildJointPermissionsForBooks($books, $roles);
259         });
260     }
261
262     /**
263      * Delete the entity jointPermissions attached to a particular role.
264      * @param Role $role
265      */
266     public function deleteJointPermissionsForRole(Role $role)
267     {
268         $this->deleteManyJointPermissionsForRoles([$role]);
269     }
270
271     /**
272      * Delete all of the entity jointPermissions for a list of entities.
273      * @param Role[] $roles
274      */
275     protected function deleteManyJointPermissionsForRoles($roles)
276     {
277         $roleIds = array_map(function ($role) {
278             return $role->id;
279         }, $roles);
280         $this->jointPermission->newQuery()->whereIn('role_id', $roleIds)->delete();
281     }
282
283     /**
284      * Delete the entity jointPermissions for a particular entity.
285      * @param Entity $entity
286      * @throws \Throwable
287      */
288     public function deleteJointPermissionsForEntity(Entity $entity)
289     {
290         $this->deleteManyJointPermissionsForEntities([$entity]);
291     }
292
293     /**
294      * Delete all of the entity jointPermissions for a list of entities.
295      * @param Entity[] $entities
296      * @throws \Throwable
297      */
298     protected function deleteManyJointPermissionsForEntities($entities)
299     {
300         if (count($entities) === 0) {
301             return;
302         }
303
304         $this->db->transaction(function () use ($entities) {
305
306             foreach (array_chunk($entities, 1000) as $entityChunk) {
307                 $query = $this->db->table('joint_permissions');
308                 foreach ($entityChunk as $entity) {
309                     $query->orWhere(function (QueryBuilder $query) use ($entity) {
310                         $query->where('entity_id', '=', $entity->id)
311                             ->where('entity_type', '=', $entity->getMorphClass());
312                     });
313                 }
314                 $query->delete();
315             }
316         });
317     }
318
319     /**
320      * Create & Save entity jointPermissions for many entities and jointPermissions.
321      * @param Collection $entities
322      * @param array $roles
323      * @throws \Throwable
324      */
325     protected function createManyJointPermissions($entities, $roles)
326     {
327         $this->readyEntityCache($entities);
328         $jointPermissions = [];
329
330         // Fetch Entity Permissions and create a mapping of entity restricted statuses
331         $entityRestrictedMap = [];
332         $permissionFetch = $this->entityPermission->newQuery();
333         foreach ($entities as $entity) {
334             $entityRestrictedMap[$entity->getMorphClass() . ':' . $entity->id] = boolval($entity->getRawAttribute('restricted'));
335             $permissionFetch->orWhere(function ($query) use ($entity) {
336                 $query->where('restrictable_id', '=', $entity->id)->where('restrictable_type', '=', $entity->getMorphClass());
337             });
338         }
339         $permissions = $permissionFetch->get();
340
341         // Create a mapping of explicit entity permissions
342         $permissionMap = [];
343         foreach ($permissions as $permission) {
344             $key = $permission->restrictable_type . ':' . $permission->restrictable_id . ':' . $permission->role_id . ':' . $permission->action;
345             $isRestricted = $entityRestrictedMap[$permission->restrictable_type . ':' . $permission->restrictable_id];
346             $permissionMap[$key] = $isRestricted;
347         }
348
349         // Create a mapping of role permissions
350         $rolePermissionMap = [];
351         foreach ($roles as $role) {
352             foreach ($role->permissions as $permission) {
353                 $rolePermissionMap[$role->getRawAttribute('id') . ':' . $permission->getRawAttribute('name')] = true;
354             }
355         }
356
357         // Create Joint Permission Data
358         foreach ($entities as $entity) {
359             foreach ($roles as $role) {
360                 foreach ($this->getActions($entity) as $action) {
361                     $jointPermissions[] = $this->createJointPermissionData($entity, $role, $action, $permissionMap, $rolePermissionMap);
362                 }
363             }
364         }
365
366         $this->db->transaction(function () use ($jointPermissions) {
367             foreach (array_chunk($jointPermissions, 1000) as $jointPermissionChunk) {
368                 $this->db->table('joint_permissions')->insert($jointPermissionChunk);
369             }
370         });
371     }
372
373
374     /**
375      * Get the actions related to an entity.
376      * @param Entity $entity
377      * @return array
378      */
379     protected function getActions(Entity $entity)
380     {
381         $baseActions = ['view', 'update', 'delete'];
382         if ($entity->isA('chapter') || $entity->isA('book')) {
383             $baseActions[] = 'page-create';
384         }
385         if ($entity->isA('book')) {
386             $baseActions[] = 'chapter-create';
387         }
388         return $baseActions;
389     }
390
391     /**
392      * Create entity permission data for an entity and role
393      * for a particular action.
394      * @param Entity $entity
395      * @param Role $role
396      * @param string $action
397      * @param array $permissionMap
398      * @param array $rolePermissionMap
399      * @return array
400      */
401     protected function createJointPermissionData(Entity $entity, Role $role, $action, $permissionMap, $rolePermissionMap)
402     {
403         $permissionPrefix = (strpos($action, '-') === false ? ($entity->getType() . '-') : '') . $action;
404         $roleHasPermission = isset($rolePermissionMap[$role->getRawAttribute('id') . ':' . $permissionPrefix . '-all']);
405         $roleHasPermissionOwn = isset($rolePermissionMap[$role->getRawAttribute('id') . ':' . $permissionPrefix . '-own']);
406         $explodedAction = explode('-', $action);
407         $restrictionAction = end($explodedAction);
408
409         if ($role->system_name === 'admin') {
410             return $this->createJointPermissionDataArray($entity, $role, $action, true, true);
411         }
412
413         if ($entity->restricted) {
414             $hasAccess = $this->mapHasActiveRestriction($permissionMap, $entity, $role, $restrictionAction);
415             return $this->createJointPermissionDataArray($entity, $role, $action, $hasAccess, $hasAccess);
416         }
417
418         if ($entity->isA('book') || $entity->isA('bookshelf')) {
419             return $this->createJointPermissionDataArray($entity, $role, $action, $roleHasPermission, $roleHasPermissionOwn);
420         }
421
422         // For chapters and pages, Check if explicit permissions are set on the Book.
423         $book = $this->getBook($entity->book_id);
424         $hasExplicitAccessToParents = $this->mapHasActiveRestriction($permissionMap, $book, $role, $restrictionAction);
425         $hasPermissiveAccessToParents = !$book->restricted;
426
427         // For pages with a chapter, Check if explicit permissions are set on the Chapter
428         if ($entity->isA('page') && $entity->chapter_id !== 0 && $entity->chapter_id !== '0') {
429             $chapter = $this->getChapter($entity->chapter_id);
430             $hasPermissiveAccessToParents = $hasPermissiveAccessToParents && !$chapter->restricted;
431             if ($chapter->restricted) {
432                 $hasExplicitAccessToParents = $this->mapHasActiveRestriction($permissionMap, $chapter, $role, $restrictionAction);
433             }
434         }
435
436         return $this->createJointPermissionDataArray(
437             $entity,
438             $role,
439             $action,
440             ($hasExplicitAccessToParents || ($roleHasPermission && $hasPermissiveAccessToParents)),
441             ($hasExplicitAccessToParents || ($roleHasPermissionOwn && $hasPermissiveAccessToParents))
442         );
443     }
444
445     /**
446      * Check for an active restriction in an entity map.
447      * @param $entityMap
448      * @param Entity $entity
449      * @param Role $role
450      * @param $action
451      * @return bool
452      */
453     protected function mapHasActiveRestriction($entityMap, Entity $entity, Role $role, $action)
454     {
455         $key = $entity->getMorphClass() . ':' . $entity->getRawAttribute('id') . ':' . $role->getRawAttribute('id') . ':' . $action;
456         return isset($entityMap[$key]) ? $entityMap[$key] : false;
457     }
458
459     /**
460      * Create an array of data with the information of an entity jointPermissions.
461      * Used to build data for bulk insertion.
462      * @param Entity $entity
463      * @param Role $role
464      * @param $action
465      * @param $permissionAll
466      * @param $permissionOwn
467      * @return array
468      */
469     protected function createJointPermissionDataArray(Entity $entity, Role $role, $action, $permissionAll, $permissionOwn)
470     {
471         return [
472             'role_id'            => $role->getRawAttribute('id'),
473             'entity_id'          => $entity->getRawAttribute('id'),
474             'entity_type'        => $entity->getMorphClass(),
475             'action'             => $action,
476             'has_permission'     => $permissionAll,
477             'has_permission_own' => $permissionOwn,
478             'created_by'         => $entity->getRawAttribute('created_by')
479         ];
480     }
481
482     /**
483      * Checks if an entity has a restriction set upon it.
484      * @param Ownable $ownable
485      * @param $permission
486      * @return bool
487      */
488     public function checkOwnableUserAccess(Ownable $ownable, $permission)
489     {
490         if ($this->isAdmin()) {
491             $this->clean();
492             return true;
493         }
494
495         $explodedPermission = explode('-', $permission);
496
497         $baseQuery = $ownable->where('id', '=', $ownable->id);
498         $action = end($explodedPermission);
499         $this->currentAction = $action;
500
501         $nonJointPermissions = ['restrictions', 'image', 'attachment', 'comment'];
502
503         // Handle non entity specific jointPermissions
504         if (in_array($explodedPermission[0], $nonJointPermissions)) {
505             $allPermission = $this->currentUser() && $this->currentUser()->can($permission . '-all');
506             $ownPermission = $this->currentUser() && $this->currentUser()->can($permission . '-own');
507             $this->currentAction = 'view';
508             $isOwner = $this->currentUser() && $this->currentUser()->id === $ownable->created_by;
509             return ($allPermission || ($isOwner && $ownPermission));
510         }
511
512         // Handle abnormal create jointPermissions
513         if ($action === 'create') {
514             $this->currentAction = $permission;
515         }
516
517         $q = $this->entityRestrictionQuery($baseQuery)->count() > 0;
518         $this->clean();
519         return $q;
520     }
521
522     /**
523      * Check if an entity has restrictions set on itself or its
524      * parent tree.
525      * @param Entity $entity
526      * @param $action
527      * @return bool|mixed
528      */
529     public function checkIfRestrictionsSet(Entity $entity, $action)
530     {
531         $this->currentAction = $action;
532         if ($entity->isA('page')) {
533             return $entity->restricted || ($entity->chapter && $entity->chapter->restricted) || $entity->book->restricted;
534         } elseif ($entity->isA('chapter')) {
535             return $entity->restricted || $entity->book->restricted;
536         } elseif ($entity->isA('book')) {
537             return $entity->restricted;
538         }
539     }
540
541     /**
542      * The general query filter to remove all entities
543      * that the current user does not have access to.
544      * @param $query
545      * @return mixed
546      */
547     protected function entityRestrictionQuery($query)
548     {
549         $q = $query->where(function ($parentQuery) {
550             $parentQuery->whereHas('jointPermissions', function ($permissionQuery) {
551                 $permissionQuery->whereIn('role_id', $this->getRoles())
552                     ->where('action', '=', $this->currentAction)
553                     ->where(function ($query) {
554                         $query->where('has_permission', '=', true)
555                             ->orWhere(function ($query) {
556                                 $query->where('has_permission_own', '=', true)
557                                     ->where('created_by', '=', $this->currentUser()->id);
558                             });
559                     });
560             });
561         });
562         $this->clean();
563         return $q;
564     }
565
566     /**
567      * Get the children of a book in an efficient single query, Filtered by the permission system.
568      * @param integer $book_id
569      * @param bool $filterDrafts
570      * @param bool $fetchPageContent
571      * @return QueryBuilder
572      */
573     public function bookChildrenQuery($book_id, $filterDrafts = false, $fetchPageContent = false)
574     {
575         $pageSelect = $this->db->table('pages')->selectRaw($this->page->entityRawQuery($fetchPageContent))->where('book_id', '=', $book_id)->where(function ($query) use ($filterDrafts) {
576             $query->where('draft', '=', 0);
577             if (!$filterDrafts) {
578                 $query->orWhere(function ($query) {
579                     $query->where('draft', '=', 1)->where('created_by', '=', $this->currentUser()->id);
580                 });
581             }
582         });
583         $chapterSelect = $this->db->table('chapters')->selectRaw($this->chapter->entityRawQuery())->where('book_id', '=', $book_id);
584         $query = $this->db->query()->select('*')->from($this->db->raw("({$pageSelect->toSql()} UNION {$chapterSelect->toSql()}) AS U"))
585             ->mergeBindings($pageSelect)->mergeBindings($chapterSelect);
586
587         if (!$this->isAdmin()) {
588             $whereQuery = $this->db->table('joint_permissions as jp')->selectRaw('COUNT(*)')
589                 ->whereRaw('jp.entity_id=U.id')->whereRaw('jp.entity_type=U.entity_type')
590                 ->where('jp.action', '=', 'view')->whereIn('jp.role_id', $this->getRoles())
591                 ->where(function ($query) {
592                     $query->where('jp.has_permission', '=', 1)->orWhere(function ($query) {
593                         $query->where('jp.has_permission_own', '=', 1)->where('jp.created_by', '=', $this->currentUser()->id);
594                     });
595                 });
596             $query->whereRaw("({$whereQuery->toSql()}) > 0")->mergeBindings($whereQuery);
597         }
598
599         $query->orderBy('draft', 'desc')->orderBy('priority', 'asc');
600         $this->clean();
601         return  $query;
602     }
603
604     /**
605      * Add restrictions for a generic entity
606      * @param string $entityType
607      * @param Builder|Entity $query
608      * @param string $action
609      * @return Builder
610      */
611     public function enforceEntityRestrictions($entityType, $query, $action = 'view')
612     {
613         if (strtolower($entityType) === 'page') {
614             // Prevent drafts being visible to others.
615             $query = $query->where(function ($query) {
616                 $query->where('draft', '=', false);
617                 if ($this->currentUser()) {
618                     $query->orWhere(function ($query) {
619                         $query->where('draft', '=', true)->where('created_by', '=', $this->currentUser()->id);
620                     });
621                 }
622             });
623         }
624
625         if ($this->isAdmin()) {
626             $this->clean();
627             return $query;
628         }
629
630         $this->currentAction = $action;
631         return $this->entityRestrictionQuery($query);
632     }
633
634     /**
635      * Filter items that have entities set as a polymorphic relation.
636      * @param $query
637      * @param string $tableName
638      * @param string $entityIdColumn
639      * @param string $entityTypeColumn
640      * @param string $action
641      * @return mixed
642      */
643     public function filterRestrictedEntityRelations($query, $tableName, $entityIdColumn, $entityTypeColumn, $action = 'view')
644     {
645         if ($this->isAdmin()) {
646             $this->clean();
647             return $query;
648         }
649
650         $this->currentAction = $action;
651         $tableDetails = ['tableName' => $tableName, 'entityIdColumn' => $entityIdColumn, 'entityTypeColumn' => $entityTypeColumn];
652
653         $q = $query->where(function ($query) use ($tableDetails) {
654             $query->whereExists(function ($permissionQuery) use (&$tableDetails) {
655                 $permissionQuery->select('id')->from('joint_permissions')
656                     ->whereRaw('joint_permissions.entity_id=' . $tableDetails['tableName'] . '.' . $tableDetails['entityIdColumn'])
657                     ->whereRaw('joint_permissions.entity_type=' . $tableDetails['tableName'] . '.' . $tableDetails['entityTypeColumn'])
658                     ->where('action', '=', $this->currentAction)
659                     ->whereIn('role_id', $this->getRoles())
660                     ->where(function ($query) {
661                         $query->where('has_permission', '=', true)->orWhere(function ($query) {
662                             $query->where('has_permission_own', '=', true)
663                                 ->where('created_by', '=', $this->currentUser()->id);
664                         });
665                     });
666             });
667         });
668         $this->clean();
669         return $q;
670     }
671
672     /**
673      * Filters pages that are a direct relation to another item.
674      * @param $query
675      * @param $tableName
676      * @param $entityIdColumn
677      * @return mixed
678      */
679     public function filterRelatedPages($query, $tableName, $entityIdColumn)
680     {
681         if ($this->isAdmin()) {
682             $this->clean();
683             return $query;
684         }
685
686         $this->currentAction = 'view';
687         $tableDetails = ['tableName' => $tableName, 'entityIdColumn' => $entityIdColumn];
688
689         $q = $query->where(function ($query) use ($tableDetails) {
690             $query->where(function ($query) use (&$tableDetails) {
691                 $query->whereExists(function ($permissionQuery) use (&$tableDetails) {
692                     $permissionQuery->select('id')->from('joint_permissions')
693                         ->whereRaw('joint_permissions.entity_id=' . $tableDetails['tableName'] . '.' . $tableDetails['entityIdColumn'])
694                         ->where('entity_type', '=', 'Bookstack\\Page')
695                         ->where('action', '=', $this->currentAction)
696                         ->whereIn('role_id', $this->getRoles())
697                         ->where(function ($query) {
698                             $query->where('has_permission', '=', true)->orWhere(function ($query) {
699                                 $query->where('has_permission_own', '=', true)
700                                     ->where('created_by', '=', $this->currentUser()->id);
701                             });
702                         });
703                 });
704             })->orWhere($tableDetails['entityIdColumn'], '=', 0);
705         });
706         $this->clean();
707         return $q;
708     }
709
710     /**
711      * Check if the current user is an admin.
712      * @return bool
713      */
714     private function isAdmin()
715     {
716         if ($this->isAdminUser === null) {
717             $this->isAdminUser = ($this->currentUser()->id !== null) ? $this->currentUser()->hasSystemRole('admin') : false;
718         }
719
720         return $this->isAdminUser;
721     }
722
723     /**
724      * Get the current user
725      * @return User
726      */
727     private function currentUser()
728     {
729         if ($this->currentUserModel === false) {
730             $this->currentUserModel = user();
731         }
732
733         return $this->currentUserModel;
734     }
735
736     /**
737      * Clean the cached user elements.
738      */
739     private function clean()
740     {
741         $this->currentUserModel = false;
742         $this->userRoles = false;
743         $this->isAdminUser = null;
744     }
745 }