124
common/Core/Values/GetStaticPermissions.php
Executable file
124
common/Core/Values/GetStaticPermissions.php
Executable file
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Core\Values;
|
||||
|
||||
use Arr;
|
||||
use Common\Settings\Settings;
|
||||
use Illuminate\Filesystem\Filesystem;
|
||||
use Str;
|
||||
|
||||
class GetStaticPermissions
|
||||
{
|
||||
/**
|
||||
* @var Filesystem
|
||||
*/
|
||||
private $fs;
|
||||
|
||||
/**
|
||||
* @param Filesystem $fs
|
||||
*/
|
||||
public function __construct(Filesystem $fs)
|
||||
{
|
||||
$this->fs = $fs;
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
$permissions = array_merge_recursive(
|
||||
$this->fs->getRequire(
|
||||
app('path.common') . '/resources/defaults/permissions.php',
|
||||
),
|
||||
$this->fs->getRequire(resource_path('defaults/permissions.php')),
|
||||
)['all'];
|
||||
|
||||
$compiled = [];
|
||||
foreach ($permissions as $key => $permissionGroup) {
|
||||
// format permissions and add generic description, if needed
|
||||
$compiled[$key] = collect($permissionGroup)->map(function ($item) {
|
||||
if (!is_array($item)) {
|
||||
$item = ['name' => $item];
|
||||
}
|
||||
|
||||
if (!Arr::get($item, 'display_name')) {
|
||||
$item['display_name'] = $this->getDisplayName(
|
||||
$item['name'],
|
||||
);
|
||||
}
|
||||
|
||||
if (!Arr::get($item, 'description')) {
|
||||
$item['description'] = $this->getGenericDescription(
|
||||
$item['name'],
|
||||
);
|
||||
}
|
||||
|
||||
return $item;
|
||||
});
|
||||
}
|
||||
|
||||
// remove billing permissions, if billing functionality is disabled
|
||||
if (
|
||||
isset($compiled['billing_plans']) &&
|
||||
!app(Settings::class)->get('billing.enable')
|
||||
) {
|
||||
unset($compiled['billing_plans']);
|
||||
}
|
||||
|
||||
return $compiled;
|
||||
}
|
||||
|
||||
private function getDisplayName($original)
|
||||
{
|
||||
// files.create => Create Files
|
||||
if (!\Str::contains($original, '.')) {
|
||||
return $original;
|
||||
}
|
||||
[$resource, $action] = explode('.', $original);
|
||||
return ucfirst($action) .
|
||||
' ' .
|
||||
ucwords(str_replace('_', ' ', $resource));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $permission
|
||||
* @return string|null
|
||||
*/
|
||||
private function getGenericDescription($permission)
|
||||
{
|
||||
if (!Str::contains($permission, '.')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
[$resource, $action] = explode('.', $permission);
|
||||
$pluralAction = Str::plural(str_replace('_', ' ', $resource));
|
||||
$verb = $this->getGenericVerb($action, $resource);
|
||||
|
||||
return "Allow $verb $pluralAction, regardless of who is the owner.";
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $action
|
||||
* @param string $resource
|
||||
* @return string|null
|
||||
*/
|
||||
private function getGenericVerb($action, $resource)
|
||||
{
|
||||
if ($resource === 'file' && $action === 'create') {
|
||||
return 'uploading new';
|
||||
}
|
||||
|
||||
switch ($action) {
|
||||
case 'view':
|
||||
return 'viewing ALL';
|
||||
case 'create':
|
||||
return 'creating new';
|
||||
case 'update':
|
||||
return 'updating ALL';
|
||||
case 'delete':
|
||||
return 'deleting ALL';
|
||||
case 'download':
|
||||
return 'downloading ALL';
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
240
common/Core/Values/ValueLists.php
Executable file
240
common/Core/Values/ValueLists.php
Executable file
@@ -0,0 +1,240 @@
|
||||
<?php namespace Common\Core\Values;
|
||||
|
||||
use Common\Admin\Appearance\Themes\CssTheme;
|
||||
use Common\Auth\Permissions\Permission;
|
||||
use Common\Auth\Roles\Role;
|
||||
use Common\Domains\CustomDomain;
|
||||
use Common\Files\FileEntry;
|
||||
use Common\Localizations\Localization;
|
||||
use Common\Pages\CustomPage;
|
||||
use Illuminate\Contracts\Auth\Access\Gate;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Filesystem\Filesystem;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Str;
|
||||
use const App\Providers\WORKSPACED_RESOURCES;
|
||||
|
||||
class ValueLists
|
||||
{
|
||||
public function __construct(
|
||||
protected Filesystem $fs,
|
||||
protected Localization $localization
|
||||
) {
|
||||
}
|
||||
|
||||
public function get(string $names, array $params = []): Collection|array
|
||||
{
|
||||
return collect(explode(',', $names))
|
||||
->mapWithKeys(function ($name) use ($params) {
|
||||
$methodName = Str::studly($name);
|
||||
$value = method_exists($this, $methodName)
|
||||
? $this->$methodName($params)
|
||||
: $this->loadAppValueFile($name, $params);
|
||||
return [$name => $value];
|
||||
})
|
||||
->filter();
|
||||
}
|
||||
|
||||
public function permissions(): Collection
|
||||
{
|
||||
$query = app(Permission::class)->where('type', 'sitewide');
|
||||
|
||||
if (!config('common.site.enable_custom_domains')) {
|
||||
$query->where('group', '!=', 'custom_domains');
|
||||
}
|
||||
|
||||
if (!config('common.site.notifications_integrated')) {
|
||||
$query->where('group', '!=', 'notifications');
|
||||
}
|
||||
|
||||
// TODO: fetch and merge advanced and description from config files here
|
||||
// instead of storing in db. Then can override workspace descriptions and
|
||||
// advanced state easily here from separate config file
|
||||
|
||||
if (!config('common.site.workspaces_integrated')) {
|
||||
$query
|
||||
->where('group', '!=', 'workspaces')
|
||||
->where('group', '!=', 'workspace_members');
|
||||
}
|
||||
|
||||
if (!config('common.site.billing_integrated')) {
|
||||
$query
|
||||
->where('group', '!=', 'plans')
|
||||
->where('group', '!=', 'invoices');
|
||||
}
|
||||
|
||||
if (!Auth::user() || !Auth::user()->hasExactPermission('admin')) {
|
||||
$query->where('name', '!=', 'admin');
|
||||
}
|
||||
|
||||
return $query->get();
|
||||
}
|
||||
|
||||
public function roles(): Collection
|
||||
{
|
||||
return app(Role::class)
|
||||
->where('type', 'sitewide')
|
||||
->get();
|
||||
}
|
||||
|
||||
public function workspaceRoles(): Collection
|
||||
{
|
||||
return app(Role::class)
|
||||
->where('type', 'workspace')
|
||||
->get();
|
||||
}
|
||||
|
||||
public function workspacePermissions($params = []): Collection
|
||||
{
|
||||
$filters = array_map(function ($resource) {
|
||||
if (is_subclass_of($resource, FileEntry::class)) {
|
||||
return 'files';
|
||||
} elseif (is_subclass_of($resource, CustomDomain::class)) {
|
||||
return 'custom_domains';
|
||||
} else {
|
||||
return Str::snake(Str::pluralStudly(class_basename($resource)));
|
||||
}
|
||||
}, WORKSPACED_RESOURCES);
|
||||
|
||||
return app(Permission::class)
|
||||
->where('type', 'workspace')
|
||||
->orWhere(function (Builder $builder) use ($filters) {
|
||||
$builder->where('type', 'sitewide')->whereIn('group', $filters);
|
||||
})
|
||||
// don't return restrictions for workspace permissions so they
|
||||
// are not shown when creating workspace role from admin area
|
||||
->get([
|
||||
'id',
|
||||
'name',
|
||||
'display_name',
|
||||
'description',
|
||||
'group',
|
||||
'type',
|
||||
])
|
||||
->map(function (Permission $permission) {
|
||||
$permission->description = str_replace(
|
||||
'ALL',
|
||||
'all workspace',
|
||||
$permission->description,
|
||||
);
|
||||
$permission->description = str_replace(
|
||||
'creating new',
|
||||
'creating new workspace',
|
||||
$permission->description,
|
||||
);
|
||||
return $permission;
|
||||
});
|
||||
}
|
||||
|
||||
public function currencies()
|
||||
{
|
||||
return json_decode(
|
||||
$this->fs->get(__DIR__ . '/../../resources/lists/currencies.json'),
|
||||
true,
|
||||
);
|
||||
}
|
||||
|
||||
public function timezones()
|
||||
{
|
||||
return json_decode(
|
||||
$this->fs->get(__DIR__ . '/../../resources/lists/timezones.json'),
|
||||
true,
|
||||
);
|
||||
}
|
||||
|
||||
public function countries()
|
||||
{
|
||||
return json_decode(
|
||||
$this->fs->get(__DIR__ . '/../../resources/lists/countries.json'),
|
||||
true,
|
||||
);
|
||||
}
|
||||
|
||||
public function languages()
|
||||
{
|
||||
return json_decode(
|
||||
$this->fs->get(__DIR__ . '/../../resources/lists/languages.json'),
|
||||
true,
|
||||
);
|
||||
}
|
||||
|
||||
public function localizations()
|
||||
{
|
||||
return $this->localization->get(['id', 'name', 'language']);
|
||||
}
|
||||
|
||||
public function googleFonts(): array
|
||||
{
|
||||
$googleFonts = json_decode(
|
||||
$this->fs->get(
|
||||
__DIR__ . '/../../resources/lists/google-fonts.json',
|
||||
),
|
||||
true,
|
||||
);
|
||||
return array_map(function ($font) {
|
||||
return [
|
||||
'family' => $font['family'],
|
||||
'category' => $font['category'],
|
||||
'google' => true,
|
||||
];
|
||||
}, $googleFonts);
|
||||
}
|
||||
|
||||
public function menuItemCategories(): array
|
||||
{
|
||||
return array_map(function ($category) {
|
||||
$category['items'] = app($category['itemsLoader'])->execute();
|
||||
unset($category['itemsLoader']);
|
||||
return $category;
|
||||
}, config('common.menus'));
|
||||
}
|
||||
|
||||
public function pages($params = [])
|
||||
{
|
||||
if (!isset($params['userId'])) {
|
||||
app(Gate::class)->authorize('index', CustomPage::class);
|
||||
}
|
||||
|
||||
$query = app(CustomPage::class)
|
||||
->select(['id', 'title'])
|
||||
->where(
|
||||
'type',
|
||||
Arr::get($params, 'pageType') ?: CustomPage::PAGE_TYPE,
|
||||
);
|
||||
|
||||
if ($userId = Arr::get($params, 'userId')) {
|
||||
$query->where('user_id', $userId);
|
||||
}
|
||||
|
||||
return $query->get();
|
||||
}
|
||||
|
||||
public function domains(Collection|array $params): Collection
|
||||
{
|
||||
return app(CustomDomain::class)
|
||||
->select(['host', 'id'])
|
||||
->where('user_id', Arr::get($params, 'userId'))
|
||||
->orWhere('global', true)
|
||||
->get();
|
||||
}
|
||||
|
||||
public function themes(Collection|array $params): Collection
|
||||
{
|
||||
app(Gate::class)->authorize('index', CssTheme::class);
|
||||
return app(CssTheme::class)
|
||||
->select(['name', 'id'])
|
||||
->get();
|
||||
}
|
||||
|
||||
private function loadAppValueFile(string $name, array $params): ?array
|
||||
{
|
||||
$fileName = Str::kebab($name);
|
||||
$path = resource_path("lists/$fileName.json");
|
||||
if (file_exists($path)) {
|
||||
return json_decode(file_get_contents($path), true);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
12
common/Core/Values/ValueListsController.php
Executable file
12
common/Core/Values/ValueListsController.php
Executable file
@@ -0,0 +1,12 @@
|
||||
<?php namespace Common\Core\Values;
|
||||
|
||||
use Common\Core\BaseController;
|
||||
|
||||
class ValueListsController extends BaseController
|
||||
{
|
||||
public function index(string $names)
|
||||
{
|
||||
$values = app(ValueLists::class)->get($names, request()->all());
|
||||
return $this->success($values);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user