mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-08-25 19:56:05 +00:00
Updated shelf menu item to show on custom permission
- Extended new 'userCanOnAny' helper to take a entity class for filtering. Closes #1201
This commit is contained in:
parent
04287745e4
commit
042a6f9760
@ -558,15 +558,17 @@ class PermissionService
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if a user has the given permission for any items in the system.
|
* Checks if a user has the given permission for any items in the system.
|
||||||
|
* Can be passed an entity instance to filter on a specific type.
|
||||||
* @param string $permission
|
* @param string $permission
|
||||||
|
* @param string $entityClass
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function checkUserHasPermissionOnAnything(string $permission)
|
public function checkUserHasPermissionOnAnything(string $permission, string $entityClass = null)
|
||||||
{
|
{
|
||||||
$userRoleIds = $this->currentUser()->roles()->select('id')->pluck('id')->toArray();
|
$userRoleIds = $this->currentUser()->roles()->select('id')->pluck('id')->toArray();
|
||||||
$userId = $this->currentUser()->id;
|
$userId = $this->currentUser()->id;
|
||||||
|
|
||||||
$canCreatePage = $this->db->table('joint_permissions')
|
$permissionQuery = $this->db->table('joint_permissions')
|
||||||
->where('action', '=', $permission)
|
->where('action', '=', $permission)
|
||||||
->whereIn('role_id', $userRoleIds)
|
->whereIn('role_id', $userRoleIds)
|
||||||
->where(function ($query) use ($userId) {
|
->where(function ($query) use ($userId) {
|
||||||
@ -575,11 +577,16 @@ class PermissionService
|
|||||||
$query2->where('has_permission_own', '=', 1)
|
$query2->where('has_permission_own', '=', 1)
|
||||||
->where('created_by', '=', $userId);
|
->where('created_by', '=', $userId);
|
||||||
});
|
});
|
||||||
})
|
}) ;
|
||||||
->get()->count() > 0;
|
|
||||||
|
|
||||||
|
if (!is_null($entityClass)) {
|
||||||
|
$entityInstance = app()->make($entityClass);
|
||||||
|
$permissionQuery = $permissionQuery->where('entity_type', '=', $entityInstance->getMorphClass());
|
||||||
|
}
|
||||||
|
|
||||||
|
$hasPermission = $permissionQuery->count() > 0;
|
||||||
$this->clean();
|
$this->clean();
|
||||||
return $canCreatePage;
|
return $hasPermission;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use BookStack\Auth\Permissions\PermissionService;
|
use BookStack\Auth\Permissions\PermissionService;
|
||||||
|
use BookStack\Entities\Entity;
|
||||||
use BookStack\Ownable;
|
use BookStack\Ownable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -70,12 +71,13 @@ function userCan(string $permission, Ownable $ownable = null)
|
|||||||
* Check if the current user has the given permission
|
* Check if the current user has the given permission
|
||||||
* on any item in the system.
|
* on any item in the system.
|
||||||
* @param string $permission
|
* @param string $permission
|
||||||
|
* @param string|null $entityClass
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
function userCanOnAny(string $permission)
|
function userCanOnAny(string $permission, string $entityClass = null)
|
||||||
{
|
{
|
||||||
$permissionService = app(PermissionService::class);
|
$permissionService = app(PermissionService::class);
|
||||||
return $permissionService->checkUserHasPermissionOnAnything($permission);
|
return $permissionService->checkUserHasPermissionOnAnything($permission, $entityClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -48,7 +48,7 @@
|
|||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
<div class="links text-center">
|
<div class="links text-center">
|
||||||
@if(userCan('bookshelf-view-all') || userCan('bookshelf-view-own'))
|
@if(userCanOnAny('view', \BookStack\Entities\Bookshelf::class) || userCan('bookshelf-view-own'))
|
||||||
<a href="{{ baseUrl('/shelves') }}">@icon('bookshelf'){{ trans('entities.shelves') }}</a>
|
<a href="{{ baseUrl('/shelves') }}">@icon('bookshelf'){{ trans('entities.shelves') }}</a>
|
||||||
@endif
|
@endif
|
||||||
<a href="{{ baseUrl('/books') }}">@icon('book'){{ trans('entities.books') }}</a>
|
<a href="{{ baseUrl('/books') }}">@icon('book'){{ trans('entities.books') }}</a>
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
<?php namespace Tests;
|
<?php namespace Tests;
|
||||||
|
|
||||||
|
use BookStack\Auth\Role;
|
||||||
|
use BookStack\Auth\User;
|
||||||
use BookStack\Entities\Book;
|
use BookStack\Entities\Book;
|
||||||
use BookStack\Entities\Bookshelf;
|
use BookStack\Entities\Bookshelf;
|
||||||
|
|
||||||
@ -27,6 +29,22 @@ class BookShelfTest extends TestCase
|
|||||||
$resp->assertElementContains('header', 'Shelves');
|
$resp->assertElementContains('header', 'Shelves');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function test_shelves_shows_in_header_if_have_any_shelve_view_permission()
|
||||||
|
{
|
||||||
|
$user = factory(User::class)->create();
|
||||||
|
$this->giveUserPermissions($user, ['image-create-all']);
|
||||||
|
$shelf = Bookshelf::first();
|
||||||
|
$userRole = $user->roles()->first();
|
||||||
|
|
||||||
|
$resp = $this->actingAs($user)->get('/');
|
||||||
|
$resp->assertElementNotContains('header', 'Shelves');
|
||||||
|
|
||||||
|
$this->setEntityRestrictions($shelf, ['view'], [$userRole]);
|
||||||
|
|
||||||
|
$resp = $this->get('/');
|
||||||
|
$resp->assertElementContains('header', 'Shelves');
|
||||||
|
}
|
||||||
|
|
||||||
public function test_shelves_page_contains_create_link()
|
public function test_shelves_page_contains_create_link()
|
||||||
{
|
{
|
||||||
$resp = $this->asEditor()->get('/shelves');
|
$resp = $this->asEditor()->get('/shelves');
|
||||||
|
Loading…
Reference in New Issue
Block a user