1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2024-10-29 23:22:34 +01:00

ZIP Exports: Added new import permission

Also updated new route/view to new non-book-specific flow.
Also fixed down migration of old export permissions migration.
This commit is contained in:
Dan Brown 2024-10-29 12:11:51 +00:00
parent d1f69feb4a
commit 4051d5b803
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
9 changed files with 137 additions and 3 deletions

View File

@ -0,0 +1,24 @@
<?php
namespace BookStack\Exports\Controllers;
use BookStack\Http\Controller;
use Illuminate\Http\Request;
class ImportController extends Controller
{
public function __construct()
{
$this->middleware('can:content-import');
}
public function start(Request $request)
{
return view('exports.import');
}
public function upload(Request $request)
{
// TODO
}
}

View File

@ -11,8 +11,7 @@ return new class extends Migration
*/
public function up(): void
{
// Create new templates-manage permission and assign to admin role
$roles = DB::table('roles')->get('id');
// Create new content-export permission
$permissionId = DB::table('role_permissions')->insertGetId([
'name' => 'content-export',
'display_name' => 'Export Content',
@ -20,6 +19,7 @@ return new class extends Migration
'updated_at' => Carbon::now()->toDateTimeString(),
]);
$roles = DB::table('roles')->get('id');
$permissionRoles = $roles->map(function ($role) use ($permissionId) {
return [
'role_id' => $role->id,
@ -27,6 +27,7 @@ return new class extends Migration
];
})->values()->toArray();
// Assign to all existing roles in the system
DB::table('permission_role')->insert($permissionRoles);
}
@ -40,6 +41,6 @@ return new class extends Migration
->where('name', '=', 'content-export')->first();
DB::table('permission_role')->where('permission_id', '=', $contentExportPermission->id)->delete();
DB::table('role_permissions')->where('id', '=', 'content-export')->delete();
DB::table('role_permissions')->where('id', '=', $contentExportPermission->id)->delete();
}
};

View File

@ -0,0 +1,61 @@
<?php
use Carbon\Carbon;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
// Create new content-import permission
$permissionId = DB::table('role_permissions')->insertGetId([
'name' => 'content-import',
'display_name' => 'Import Content',
'created_at' => Carbon::now()->toDateTimeString(),
'updated_at' => Carbon::now()->toDateTimeString(),
]);
// Get existing admin-level role ids
$settingManagePermission = DB::table('role_permissions')
->where('name', '=', 'settings-manage')->first();
if (!$settingManagePermission) {
return;
}
$adminRoleIds = DB::table('permission_role')
->where('permission_id', '=', $settingManagePermission->id)
->pluck('role_id')->all();
// Assign the new permission to all existing admins
$newPermissionRoles = array_values(array_map(function ($roleId) use ($permissionId) {
return [
'role_id' => $roleId,
'permission_id' => $permissionId,
];
}, $adminRoleIds));
DB::table('permission_role')->insert($newPermissionRoles);
}
/**
* Reverse the migrations.
*/
public function down(): void
{
// Remove content-import permission
$importPermission = DB::table('role_permissions')
->where('name', '=', 'content-import')->first();
if (!$importPermission) {
return;
}
DB::table('permission_role')->where('permission_id', '=', $importPermission->id)->delete();
DB::table('role_permissions')->where('id', '=', $importPermission->id)->delete();
}
};

View File

@ -43,6 +43,7 @@ return [
'default_template' => 'Default Page Template',
'default_template_explain' => 'Assign a page template that will be used as the default content for all pages created within this item. Keep in mind this will only be used if the page creator has view access to the chosen template page.',
'default_template_select' => 'Select a template page',
'import' => 'Import',
// Permissions and restrictions
'permissions' => 'Permissions',

View File

@ -162,6 +162,7 @@ return [
'role_access_api' => 'Access system API',
'role_manage_settings' => 'Manage app settings',
'role_export_content' => 'Export content',
'role_import_content' => 'Import content',
'role_editor_change' => 'Change page editor',
'role_notifications' => 'Receive & manage notifications',
'role_asset' => 'Asset Permissions',

View File

@ -49,6 +49,13 @@
<span>@icon('tag')</span>
<span>{{ trans('entities.tags_view_tags') }}</span>
</a>
@if(userCan('content-import'))
<a href="{{ url('/import') }}" class="icon-list-item">
<span>@icon('upload')</span>
<span>{{ trans('entities.import') }}</span>
</a>
@endif
</div>
</div>

View File

@ -0,0 +1,34 @@
@extends('layouts.simple')
@section('body')
<div class="container small">
<main class="card content-wrap auto-height mt-xxl">
<div class="grid half left-focus v-end gap-m wrap">
<div>
<h1 class="list-heading">{{ trans('entities.import') }}</h1>
<p class="text-muted mb-s">
TODO - Desc
{{-- {{ trans('entities.permissions_desc') }}--}}
</p>
</div>
</div>
<form action="{{ url('/import') }}" method="POST">
{{ csrf_field() }}
<div class="flex-container-row justify-flex-end">
<div class="form-group mb-m">
@include('form.checkbox', ['name' => 'images', 'label' => 'Include Images'])
@include('form.checkbox', ['name' => 'attachments', 'label' => 'Include Attachments'])
</div>
</div>
<div class="text-right">
<a href="{{ url('/books') }}" class="button outline">{{ trans('common.cancel') }}</a>
<button type="submit" class="button">{{ trans('entities.import') }}</button>
</div>
</form>
</main>
</div>
@stop

View File

@ -37,6 +37,7 @@
<div>@include('settings.roles.parts.checkbox', ['permission' => 'templates-manage', 'label' => trans('settings.role_manage_page_templates')])</div>
<div>@include('settings.roles.parts.checkbox', ['permission' => 'access-api', 'label' => trans('settings.role_access_api')])</div>
<div>@include('settings.roles.parts.checkbox', ['permission' => 'content-export', 'label' => trans('settings.role_export_content')])</div>
<div>@include('settings.roles.parts.checkbox', ['permission' => 'content-import', 'label' => trans('settings.role_import_content')])</div>
<div>@include('settings.roles.parts.checkbox', ['permission' => 'editor-change', 'label' => trans('settings.role_editor_change')])</div>
<div>@include('settings.roles.parts.checkbox', ['permission' => 'receive-notifications', 'label' => trans('settings.role_notifications')])</div>
</div>

View File

@ -206,6 +206,10 @@ Route::middleware('auth')->group(function () {
// Watching
Route::put('/watching/update', [ActivityControllers\WatchController::class, 'update']);
// Importing
Route::get('/import', [ExportControllers\ImportController::class, 'start']);
Route::post('/import', [ExportControllers\ImportController::class, 'upload']);
// Other Pages
Route::get('/', [HomeController::class, 'index']);
Route::get('/home', [HomeController::class, 'index']);