mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-08-29 21:52:35 +00:00
55 lines
1.6 KiB
PHP
55 lines
1.6 KiB
PHP
![]() |
<?php
|
||
|
|
||
|
use Illuminate\Database\Schema\Blueprint;
|
||
|
use Illuminate\Database\Migrations\Migration;
|
||
|
|
||
|
class AddViewPermissionsToRoles extends Migration
|
||
|
{
|
||
|
/**
|
||
|
* Run the migrations.
|
||
|
*
|
||
|
* @return void
|
||
|
*/
|
||
|
public function up()
|
||
|
{
|
||
|
$currentRoles = \BookStack\Role::all();
|
||
|
|
||
|
// Create new view permissions
|
||
|
$entities = ['Book', 'Page', 'Chapter'];
|
||
|
$ops = ['View All', 'View Own'];
|
||
|
foreach ($entities as $entity) {
|
||
|
foreach ($ops as $op) {
|
||
|
$newPermission = new \BookStack\Permission();
|
||
|
$newPermission->name = strtolower($entity) . '-' . strtolower(str_replace(' ', '-', $op));
|
||
|
$newPermission->display_name = $op . ' ' . $entity . 's';
|
||
|
$newPermission->save();
|
||
|
foreach ($currentRoles as $role) {
|
||
|
$role->attachPermission($newPermission);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Reverse the migrations.
|
||
|
*
|
||
|
* @return void
|
||
|
*/
|
||
|
public function down()
|
||
|
{
|
||
|
// Delete the new view permissions
|
||
|
$entities = ['Book', 'Page', 'Chapter'];
|
||
|
$ops = ['View All', 'View Own'];
|
||
|
foreach ($entities as $entity) {
|
||
|
foreach ($ops as $op) {
|
||
|
$permissionName = strtolower($entity) . '-' . strtolower(str_replace(' ', '-', $op));
|
||
|
$newPermission = \BookStack\Permission::where('name', '=', $permissionName)->first();
|
||
|
foreach ($newPermission->roles as $role) {
|
||
|
$role->detachPermission($newPermission);
|
||
|
}
|
||
|
$newPermission->delete();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|