3 namespace BookStack\Auth\Permissions;
5 use BookStack\Auth\Role;
6 use BookStack\Auth\User;
7 use BookStack\Entities\Models\Entity;
8 use BookStack\Entities\Models\Page;
10 use BookStack\Traits\HasCreatorAndUpdater;
11 use BookStack\Traits\HasOwner;
12 use Illuminate\Database\Eloquent\Builder;
13 use Illuminate\Database\Query\Builder as QueryBuilder;
15 class PermissionApplicator
20 protected $userRoles = null;
25 protected $currentUserModel = null;
28 * Get the roles for the current logged in user.
30 protected function getCurrentUserRoles(): array
32 if (!is_null($this->userRoles)) {
33 return $this->userRoles;
36 if (auth()->guest()) {
37 $this->userRoles = [Role::getSystemRole('public')->id];
39 $this->userRoles = $this->currentUser()->roles->pluck('id')->values()->all();
42 return $this->userRoles;
46 * Checks if an entity has a restriction set upon it.
48 * @param HasCreatorAndUpdater|HasOwner $ownable
50 public function checkOwnableUserAccess(Model $ownable, string $permission): bool
52 $explodedPermission = explode('-', $permission);
54 $baseQuery = $ownable->newQuery()->where('id', '=', $ownable->id);
55 $action = end($explodedPermission);
56 $user = $this->currentUser();
58 $nonJointPermissions = ['restrictions', 'image', 'attachment', 'comment'];
60 // Handle non entity specific jointPermissions
61 if (in_array($explodedPermission[0], $nonJointPermissions)) {
62 $allPermission = $user && $user->can($permission . '-all');
63 $ownPermission = $user && $user->can($permission . '-own');
64 $ownerField = ($ownable instanceof Entity) ? 'owned_by' : 'created_by';
65 $isOwner = $user && $user->id === $ownable->$ownerField;
67 return $allPermission || ($isOwner && $ownPermission);
70 // Handle abnormal create jointPermissions
71 if ($action === 'create') {
72 $action = $permission;
75 $hasAccess = $this->entityRestrictionQuery($baseQuery, $action)->count() > 0;
82 * Checks if a user has the given permission for any items in the system.
83 * Can be passed an entity instance to filter on a specific type.
85 public function checkUserHasPermissionOnAnything(string $permission, ?string $entityClass = null): bool
87 $userRoleIds = $this->currentUser()->roles()->select('id')->pluck('id')->toArray();
88 $userId = $this->currentUser()->id;
90 $permissionQuery = JointPermission::query()
91 ->where('action', '=', $permission)
92 ->whereIn('role_id', $userRoleIds)
93 ->where(function (Builder $query) use ($userId) {
94 $this->addJointHasPermissionCheck($query, $userId);
97 if (!is_null($entityClass)) {
98 $entityInstance = app($entityClass);
99 $permissionQuery = $permissionQuery->where('entity_type', '=', $entityInstance->getMorphClass());
102 $hasPermission = $permissionQuery->count() > 0;
105 return $hasPermission;
109 * The general query filter to remove all entities
110 * that the current user does not have access to.
112 protected function entityRestrictionQuery(Builder $query, string $action): Builder
114 $q = $query->where(function ($parentQuery) use ($action) {
115 $parentQuery->whereHas('jointPermissions', function ($permissionQuery) use ($action) {
116 $permissionQuery->whereIn('role_id', $this->getCurrentUserRoles())
117 ->where('action', '=', $action)
118 ->where(function (Builder $query) {
119 $this->addJointHasPermissionCheck($query, $this->currentUser()->id);
130 * Limited the given entity query so that the query will only
131 * return items that the user has permission for the given ability.
133 public function restrictEntityQuery(Builder $query, string $ability = 'view'): Builder
137 return $query->where(function (Builder $parentQuery) use ($ability) {
138 $parentQuery->whereHas('jointPermissions', function (Builder $permissionQuery) use ($ability) {
139 $permissionQuery->whereIn('role_id', $this->getCurrentUserRoles())
140 ->where('action', '=', $ability)
141 ->where(function (Builder $query) {
142 $this->addJointHasPermissionCheck($query, $this->currentUser()->id);
149 * Extend the given page query to ensure draft items are not visible
150 * unless created by the given user.
152 public function enforceDraftVisibilityOnQuery(Builder $query): Builder
154 return $query->where(function (Builder $query) {
155 $query->where('draft', '=', false)
156 ->orWhere(function (Builder $query) {
157 $query->where('draft', '=', true)
158 ->where('owned_by', '=', $this->currentUser()->id);
164 * Add restrictions for a generic entity.
166 public function enforceEntityRestrictions(Entity $entity, Builder $query, string $action = 'view'): Builder
168 if ($entity instanceof Page) {
169 // Prevent drafts being visible to others.
170 $this->enforceDraftVisibilityOnQuery($query);
173 return $this->entityRestrictionQuery($query, $action);
177 * Filter items that have entities set as a polymorphic relation.
178 * For simplicity, this will not return results attached to draft pages.
179 * Draft pages should never really have related items though.
181 * @param Builder|QueryBuilder $query
183 public function filterRestrictedEntityRelations($query, string $tableName, string $entityIdColumn, string $entityTypeColumn, string $action = 'view')
185 $tableDetails = ['tableName' => $tableName, 'entityIdColumn' => $entityIdColumn, 'entityTypeColumn' => $entityTypeColumn];
186 $pageMorphClass = (new Page())->getMorphClass();
188 $q = $query->whereExists(function ($permissionQuery) use (&$tableDetails, $action) {
189 /** @var Builder $permissionQuery */
190 $permissionQuery->select(['role_id'])->from('joint_permissions')
191 ->whereColumn('joint_permissions.entity_id', '=', $tableDetails['tableName'] . '.' . $tableDetails['entityIdColumn'])
192 ->whereColumn('joint_permissions.entity_type', '=', $tableDetails['tableName'] . '.' . $tableDetails['entityTypeColumn'])
193 ->where('joint_permissions.action', '=', $action)
194 ->whereIn('joint_permissions.role_id', $this->getCurrentUserRoles())
195 ->where(function (QueryBuilder $query) {
196 $this->addJointHasPermissionCheck($query, $this->currentUser()->id);
198 })->where(function ($query) use ($tableDetails, $pageMorphClass) {
199 /** @var Builder $query */
200 $query->where($tableDetails['entityTypeColumn'], '!=', $pageMorphClass)
201 ->orWhereExists(function (QueryBuilder $query) use ($tableDetails, $pageMorphClass) {
202 $query->select('id')->from('pages')
203 ->whereColumn('pages.id', '=', $tableDetails['tableName'] . '.' . $tableDetails['entityIdColumn'])
204 ->where($tableDetails['tableName'] . '.' . $tableDetails['entityTypeColumn'], '=', $pageMorphClass)
205 ->where('pages.draft', '=', false);
215 * Add conditions to a query to filter the selection to related entities
216 * where view permissions are granted.
218 public function filterRelatedEntity(string $entityClass, Builder $query, string $tableName, string $entityIdColumn): Builder
220 $fullEntityIdColumn = $tableName . '.' . $entityIdColumn;
221 $instance = new $entityClass();
222 $morphClass = $instance->getMorphClass();
224 $existsQuery = function ($permissionQuery) use ($fullEntityIdColumn, $morphClass) {
225 /** @var Builder $permissionQuery */
226 $permissionQuery->select('joint_permissions.role_id')->from('joint_permissions')
227 ->whereColumn('joint_permissions.entity_id', '=', $fullEntityIdColumn)
228 ->where('joint_permissions.entity_type', '=', $morphClass)
229 ->where('joint_permissions.action', '=', 'view')
230 ->whereIn('joint_permissions.role_id', $this->getCurrentUserRoles())
231 ->where(function (QueryBuilder $query) {
232 $this->addJointHasPermissionCheck($query, $this->currentUser()->id);
236 $q = $query->where(function ($query) use ($existsQuery, $fullEntityIdColumn) {
237 $query->whereExists($existsQuery)
238 ->orWhere($fullEntityIdColumn, '=', 0);
241 if ($instance instanceof Page) {
242 // Prevent visibility of non-owned draft pages
243 $q->whereExists(function (QueryBuilder $query) use ($fullEntityIdColumn) {
244 $query->select('id')->from('pages')
245 ->whereColumn('pages.id', '=', $fullEntityIdColumn)
246 ->where(function (QueryBuilder $query) {
247 $query->where('pages.draft', '=', false)
248 ->orWhere('pages.owned_by', '=', $this->currentUser()->id);
259 * Add the query for checking the given user id has permission
260 * within the join_permissions table.
262 * @param QueryBuilder|Builder $query
264 protected function addJointHasPermissionCheck($query, int $userIdToCheck)
266 $query->where('joint_permissions.has_permission', '=', true)->orWhere(function ($query) use ($userIdToCheck) {
267 $query->where('joint_permissions.has_permission_own', '=', true)
268 ->where('joint_permissions.owned_by', '=', $userIdToCheck);
273 * Get the current user.
275 private function currentUser(): User
277 if (is_null($this->currentUserModel)) {
278 $this->currentUserModel = user();
281 return $this->currentUserModel;
285 * Clean the cached user elements.
287 private function clean(): void
289 $this->currentUserModel = null;
290 $this->userRoles = null;