]> BookStack Code Mirror - bookstack/commitdiff
Use joint_permissions to determine is a user has an available page or chapter to... 1202/head
authorMark James <redacted>
Wed, 2 Jan 2019 05:55:28 +0000 (16:55 +1100)
committerMark James <redacted>
Wed, 2 Jan 2019 05:55:28 +0000 (16:55 +1100)
app/Auth/Permissions/PermissionService.php
app/helpers.php
resources/views/pages/show.blade.php

index d0e6ccceab56640ddbee6b48a5b99b4cf14f815f..fa375b4ddf18893735dbeb6723b26042d2719bf2 100644 (file)
@@ -556,6 +556,33 @@ class PermissionService
         return $q;
     }
 
+    /**
+     * Checks if a user has a book or chapter available to create a page
+     * @param Ownable $ownable
+     * @param $permission
+     * @return bool
+     */
+    public function checkAvailableCreatePageAccess()
+    {
+        $userRoleIds = $this->currentUser()->roles()->pluck('id')->toArray();
+        $userId = $this->currentUser()->id;
+
+
+        $canCreatePage = $this->db->table('joint_permissions')
+            ->where('action', '=', 'page-create')
+            ->whereIn('role_id', $userRoleIds)
+            ->where(function ($query) use ($userId) {
+                $query->where('has_permission', '=', 1)
+                ->orWhere(function ($query2) use ($userId) {
+                    $query2->where('has_permission_own', '=', 1)
+                    ->where('created_by', '=', $userId);
+                });       
+            })
+            ->get()->count() > 0;
+
+        return $canCreatePage;
+    }
+
     /**
      * Check if an entity has restrictions set on itself or its
      * parent tree.
index b0886d02b9762c27383e37f5b7989014607ed03c..383e25c1594e8eac7e5b1b3f6cb2f8d3c3fa8c6e 100644 (file)
@@ -65,6 +65,17 @@ function userCan($permission, Ownable $ownable = null)
     return $permissionService->checkOwnableUserAccess($ownable, $permission);
 }
 
+/**
+ * Check if the current user has the ability to create a page for an existing object
+ * @return bool
+ */
+function userCanCreatePage()
+{
+    // Check for create page permissions
+    $permissionService = app(\BookStack\Auth\Permissions\PermissionService::class);
+    return $permissionService->checkAvailableCreatePageAccess();
+}
+
 /**
  * Helper to access system settings.
  * @param $key
index 19df337fcbbb0d5be368bab9890d27ecaa7783c5..011158dfbfd84440e92cd73dc4129b69809a0d97 100644 (file)
             @if(userCan('page-update', $page))
                 <a href="{{ $page->getUrl('/edit') }}" class="text-primary text-button" >@icon('edit'){{ trans('common.edit') }}</a>
             @endif
-            @if(userCan('page-create-own') || userCan('page-create-all') || userCan('page-update', $page) || userCan('restrictions-manage', $page) || userCan('page-delete', $page))
+            @if((userCan('page-view', $page) && userCanCreatePage()) || userCan('page-update', $page) || userCan('restrictions-manage', $page) || userCan('page-delete', $page))
                 <div dropdown class="dropdown-container">
                     <a dropdown-toggle class="text-primary text-button">@icon('more') {{ trans('common.more') }}</a>
                     <ul>
-                        @if(userCan('page-create-own') || userCan('page-create-all'))
+                        @if(userCanCreatePage())
                             <li><a href="{{ $page->getUrl('/copy') }}" class="text-primary" >@icon('copy'){{ trans('common.copy') }}</a></li>
                         @endif
                         @if(userCan('page-update', $page))
Morty Proxy This is a proxified and sanitized view of the page, visit original site.