2015-12-16 18:09:44 +01:00
|
|
|
<?php
|
|
|
|
|
2016-03-05 19:09:21 +01:00
|
|
|
if (!function_exists('versioned_asset')) {
|
2015-12-16 18:09:44 +01:00
|
|
|
/**
|
|
|
|
* Get the path to a versioned file.
|
|
|
|
*
|
2016-03-05 19:09:21 +01:00
|
|
|
* @param string $file
|
2015-12-16 18:09:44 +01:00
|
|
|
* @return string
|
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
function versioned_asset($file)
|
|
|
|
{
|
|
|
|
static $manifest = null;
|
|
|
|
|
|
|
|
if (is_null($manifest)) {
|
|
|
|
$manifest = json_decode(file_get_contents(public_path('build/manifest.json')), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($manifest[$file])) {
|
|
|
|
return '/' . $manifest[$file];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file_exists(public_path($file))) {
|
|
|
|
return '/' . $file;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new InvalidArgumentException("File {$file} not defined in asset manifest.");
|
|
|
|
}
|
2016-02-27 20:24:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the current user has a permission.
|
|
|
|
* If an ownable element is passed in the permissions are checked against
|
|
|
|
* that particular item.
|
|
|
|
* @param $permission
|
|
|
|
* @param \BookStack\Ownable $ownable
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
function userCan($permission, \BookStack\Ownable $ownable = null)
|
|
|
|
{
|
2016-03-05 19:09:21 +01:00
|
|
|
if (!auth()->check()) return false;
|
2016-02-27 20:24:42 +01:00
|
|
|
if ($ownable === null) {
|
|
|
|
return auth()->user() && auth()->user()->can($permission);
|
|
|
|
}
|
|
|
|
|
2016-02-29 21:31:21 +01:00
|
|
|
// Check permission on ownable item
|
2016-02-27 20:24:42 +01:00
|
|
|
$permissionBaseName = strtolower($permission) . '-';
|
2016-02-29 21:31:21 +01:00
|
|
|
$hasPermission = false;
|
|
|
|
if (auth()->user()->can($permissionBaseName . 'all')) $hasPermission = true;
|
2016-03-05 19:09:21 +01:00
|
|
|
if (auth()->user()->can($permissionBaseName . 'own') && $ownable->createdBy && $ownable->createdBy->id === auth()->user()->id) $hasPermission = true;
|
2016-02-29 21:31:21 +01:00
|
|
|
|
2016-03-05 19:09:21 +01:00
|
|
|
if (!$ownable instanceof \BookStack\Entity) return $hasPermission;
|
2016-02-29 21:31:21 +01:00
|
|
|
|
|
|
|
// Check restrictions on the entitiy
|
|
|
|
$restrictionService = app('BookStack\Services\RestrictionService');
|
|
|
|
$explodedPermission = explode('-', $permission);
|
|
|
|
$action = end($explodedPermission);
|
|
|
|
$hasAccess = $restrictionService->checkIfEntityRestricted($ownable, $action);
|
|
|
|
return $hasAccess && $hasPermission;
|
2015-12-16 18:09:44 +01:00
|
|
|
}
|