Skip to content

Commit 4051d5b

Browse files
committed
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.
1 parent d1f69fe commit 4051d5b

File tree

9 files changed

+137
-3
lines changed

9 files changed

+137
-3
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace BookStack\Exports\Controllers;
4+
5+
use BookStack\Http\Controller;
6+
use Illuminate\Http\Request;
7+
8+
class ImportController extends Controller
9+
{
10+
public function __construct()
11+
{
12+
$this->middleware('can:content-import');
13+
}
14+
15+
public function start(Request $request)
16+
{
17+
return view('exports.import');
18+
}
19+
20+
public function upload(Request $request)
21+
{
22+
// TODO
23+
}
24+
}

database/migrations/2021_08_28_161743_add_export_role_permission.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,23 @@
1111
*/
1212
public function up(): void
1313
{
14-
// Create new templates-manage permission and assign to admin role
15-
$roles = DB::table('roles')->get('id');
14+
// Create new content-export permission
1615
$permissionId = DB::table('role_permissions')->insertGetId([
1716
'name' => 'content-export',
1817
'display_name' => 'Export Content',
1918
'created_at' => Carbon::now()->toDateTimeString(),
2019
'updated_at' => Carbon::now()->toDateTimeString(),
2120
]);
2221

22+
$roles = DB::table('roles')->get('id');
2323
$permissionRoles = $roles->map(function ($role) use ($permissionId) {
2424
return [
2525
'role_id' => $role->id,
2626
'permission_id' => $permissionId,
2727
];
2828
})->values()->toArray();
2929

30+
// Assign to all existing roles in the system
3031
DB::table('permission_role')->insert($permissionRoles);
3132
}
3233

@@ -40,6 +41,6 @@ public function down(): void
4041
->where('name', '=', 'content-export')->first();
4142

4243
DB::table('permission_role')->where('permission_id', '=', $contentExportPermission->id)->delete();
43-
DB::table('role_permissions')->where('id', '=', 'content-export')->delete();
44+
DB::table('role_permissions')->where('id', '=', $contentExportPermission->id)->delete();
4445
}
4546
};
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
use Carbon\Carbon;
4+
use Illuminate\Database\Migrations\Migration;
5+
use Illuminate\Support\Facades\DB;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
// Create new content-import permission
15+
$permissionId = DB::table('role_permissions')->insertGetId([
16+
'name' => 'content-import',
17+
'display_name' => 'Import Content',
18+
'created_at' => Carbon::now()->toDateTimeString(),
19+
'updated_at' => Carbon::now()->toDateTimeString(),
20+
]);
21+
22+
// Get existing admin-level role ids
23+
$settingManagePermission = DB::table('role_permissions')
24+
->where('name', '=', 'settings-manage')->first();
25+
26+
if (!$settingManagePermission) {
27+
return;
28+
}
29+
30+
$adminRoleIds = DB::table('permission_role')
31+
->where('permission_id', '=', $settingManagePermission->id)
32+
->pluck('role_id')->all();
33+
34+
// Assign the new permission to all existing admins
35+
$newPermissionRoles = array_values(array_map(function ($roleId) use ($permissionId) {
36+
return [
37+
'role_id' => $roleId,
38+
'permission_id' => $permissionId,
39+
];
40+
}, $adminRoleIds));
41+
42+
DB::table('permission_role')->insert($newPermissionRoles);
43+
}
44+
45+
/**
46+
* Reverse the migrations.
47+
*/
48+
public function down(): void
49+
{
50+
// Remove content-import permission
51+
$importPermission = DB::table('role_permissions')
52+
->where('name', '=', 'content-import')->first();
53+
54+
if (!$importPermission) {
55+
return;
56+
}
57+
58+
DB::table('permission_role')->where('permission_id', '=', $importPermission->id)->delete();
59+
DB::table('role_permissions')->where('id', '=', $importPermission->id)->delete();
60+
}
61+
};

lang/en/entities.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
'default_template' => 'Default Page Template',
4444
'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.',
4545
'default_template_select' => 'Select a template page',
46+
'import' => 'Import',
4647

4748
// Permissions and restrictions
4849
'permissions' => 'Permissions',

lang/en/settings.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@
162162
'role_access_api' => 'Access system API',
163163
'role_manage_settings' => 'Manage app settings',
164164
'role_export_content' => 'Export content',
165+
'role_import_content' => 'Import content',
165166
'role_editor_change' => 'Change page editor',
166167
'role_notifications' => 'Receive & manage notifications',
167168
'role_asset' => 'Asset Permissions',

resources/views/books/index.blade.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@
4949
<span>@icon('tag')</span>
5050
<span>{{ trans('entities.tags_view_tags') }}</span>
5151
</a>
52+
53+
@if(userCan('content-import'))
54+
<a href="{{ url('/import') }}" class="icon-list-item">
55+
<span>@icon('upload')</span>
56+
<span>{{ trans('entities.import') }}</span>
57+
</a>
58+
@endif
5259
</div>
5360
</div>
5461

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
@extends('layouts.simple')
2+
3+
@section('body')
4+
5+
<div class="container small">
6+
7+
<main class="card content-wrap auto-height mt-xxl">
8+
<div class="grid half left-focus v-end gap-m wrap">
9+
<div>
10+
<h1 class="list-heading">{{ trans('entities.import') }}</h1>
11+
<p class="text-muted mb-s">
12+
TODO - Desc
13+
{{-- {{ trans('entities.permissions_desc') }}--}}
14+
</p>
15+
</div>
16+
</div>
17+
<form action="{{ url('/import') }}" method="POST">
18+
{{ csrf_field() }}
19+
<div class="flex-container-row justify-flex-end">
20+
<div class="form-group mb-m">
21+
@include('form.checkbox', ['name' => 'images', 'label' => 'Include Images'])
22+
@include('form.checkbox', ['name' => 'attachments', 'label' => 'Include Attachments'])
23+
</div>
24+
</div>
25+
26+
<div class="text-right">
27+
<a href="{{ url('/books') }}" class="button outline">{{ trans('common.cancel') }}</a>
28+
<button type="submit" class="button">{{ trans('entities.import') }}</button>
29+
</div>
30+
</form>
31+
</main>
32+
</div>
33+
34+
@stop

resources/views/settings/roles/parts/form.blade.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
<div>@include('settings.roles.parts.checkbox', ['permission' => 'templates-manage', 'label' => trans('settings.role_manage_page_templates')])</div>
3838
<div>@include('settings.roles.parts.checkbox', ['permission' => 'access-api', 'label' => trans('settings.role_access_api')])</div>
3939
<div>@include('settings.roles.parts.checkbox', ['permission' => 'content-export', 'label' => trans('settings.role_export_content')])</div>
40+
<div>@include('settings.roles.parts.checkbox', ['permission' => 'content-import', 'label' => trans('settings.role_import_content')])</div>
4041
<div>@include('settings.roles.parts.checkbox', ['permission' => 'editor-change', 'label' => trans('settings.role_editor_change')])</div>
4142
<div>@include('settings.roles.parts.checkbox', ['permission' => 'receive-notifications', 'label' => trans('settings.role_notifications')])</div>
4243
</div>

routes/web.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@
206206
// Watching
207207
Route::put('/watching/update', [ActivityControllers\WatchController::class, 'update']);
208208

209+
// Importing
210+
Route::get('/import', [ExportControllers\ImportController::class, 'start']);
211+
Route::post('/import', [ExportControllers\ImportController::class, 'upload']);
212+
209213
// Other Pages
210214
Route::get('/', [HomeController::class, 'index']);
211215
Route::get('/home', [HomeController::class, 'index']);

0 commit comments

Comments
 (0)