mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-08-09 10:22:51 +03:00
Abstracted userCanCreatePage helper to work for any permisison
- Added test to cover scenario where someone with create-own permission would want to copy a viewable item into a container entity that they own.
This commit is contained in:
@@ -557,19 +557,17 @@ class PermissionService
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a user has a book or chapter available to create a page
|
||||
* @param Ownable $ownable
|
||||
* @param $permission
|
||||
* Checks if a user has the given permission for any items in the system.
|
||||
* @param string $permission
|
||||
* @return bool
|
||||
*/
|
||||
public function checkAvailableCreatePageAccess()
|
||||
public function checkUserHasPermissionOnAnything(string $permission)
|
||||
{
|
||||
$userRoleIds = $this->currentUser()->roles()->pluck('id')->toArray();
|
||||
$userRoleIds = $this->currentUser()->roles()->select('id')->pluck('id')->toArray();
|
||||
$userId = $this->currentUser()->id;
|
||||
|
||||
|
||||
$canCreatePage = $this->db->table('joint_permissions')
|
||||
->where('action', '=', 'page-create')
|
||||
->where('action', '=', $permission)
|
||||
->whereIn('role_id', $userRoleIds)
|
||||
->where(function ($query) use ($userId) {
|
||||
$query->where('has_permission', '=', 1)
|
||||
@@ -580,6 +578,7 @@ class PermissionService
|
||||
})
|
||||
->get()->count() > 0;
|
||||
|
||||
$this->clean();
|
||||
return $canCreatePage;
|
||||
}
|
||||
|
||||
|
@@ -1,5 +1,6 @@
|
||||
<?php
|
||||
|
||||
use BookStack\Auth\Permissions\PermissionService;
|
||||
use BookStack\Ownable;
|
||||
|
||||
/**
|
||||
@@ -50,30 +51,31 @@ function signedInUser()
|
||||
* Check if the current user has a permission.
|
||||
* If an ownable element is passed in the jointPermissions are checked against
|
||||
* that particular item.
|
||||
* @param $permission
|
||||
* @param string $permission
|
||||
* @param Ownable $ownable
|
||||
* @return mixed
|
||||
*/
|
||||
function userCan($permission, Ownable $ownable = null)
|
||||
function userCan(string $permission, Ownable $ownable = null)
|
||||
{
|
||||
if ($ownable === null) {
|
||||
return user() && user()->can($permission);
|
||||
}
|
||||
|
||||
// Check permission on ownable item
|
||||
$permissionService = app(\BookStack\Auth\Permissions\PermissionService::class);
|
||||
$permissionService = app(PermissionService::class);
|
||||
return $permissionService->checkOwnableUserAccess($ownable, $permission);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the current user has the ability to create a page for an existing object
|
||||
* Check if the current user has the given permission
|
||||
* on any item in the system.
|
||||
* @param string $permission
|
||||
* @return bool
|
||||
*/
|
||||
function userCanCreatePage()
|
||||
function userCanOnAny(string $permission)
|
||||
{
|
||||
// Check for create page permissions
|
||||
$permissionService = app(\BookStack\Auth\Permissions\PermissionService::class);
|
||||
return $permissionService->checkAvailableCreatePageAccess();
|
||||
$permissionService = app(PermissionService::class);
|
||||
return $permissionService->checkUserHasPermissionOnAnything($permission);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user