Skip to content

Commit 097d9c9

Browse files
committed
Updated entity restrictions to allow permissions, Not just restrict
Also changed wording from 'Restrictions' to 'Permissions' to keep things more familiar and to better reflect what they do. Referenced in issue #89.
1 parent 491f73e commit 097d9c9

File tree

15 files changed

+201
-56
lines changed

15 files changed

+201
-56
lines changed

app/Http/routes.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
Route::delete('/{id}', 'BookController@destroy');
2020
Route::get('/{slug}/sort-item', 'BookController@getSortItem');
2121
Route::get('/{slug}', 'BookController@show');
22-
Route::get('/{bookSlug}/restrict', 'BookController@showRestrict');
23-
Route::put('/{bookSlug}/restrict', 'BookController@restrict');
22+
Route::get('/{bookSlug}/permissions', 'BookController@showRestrict');
23+
Route::put('/{bookSlug}/permissions', 'BookController@restrict');
2424
Route::get('/{slug}/delete', 'BookController@showDelete');
2525
Route::get('/{bookSlug}/sort', 'BookController@sort');
2626
Route::put('/{bookSlug}/sort', 'BookController@saveSort');
@@ -36,8 +36,8 @@
3636
Route::get('/{bookSlug}/page/{pageSlug}/edit', 'PageController@edit');
3737
Route::get('/{bookSlug}/page/{pageSlug}/delete', 'PageController@showDelete');
3838
Route::get('/{bookSlug}/draft/{pageId}/delete', 'PageController@showDeleteDraft');
39-
Route::get('/{bookSlug}/page/{pageSlug}/restrict', 'PageController@showRestrict');
40-
Route::put('/{bookSlug}/page/{pageSlug}/restrict', 'PageController@restrict');
39+
Route::get('/{bookSlug}/page/{pageSlug}/permissions', 'PageController@showRestrict');
40+
Route::put('/{bookSlug}/page/{pageSlug}/permissions', 'PageController@restrict');
4141
Route::put('/{bookSlug}/page/{pageSlug}', 'PageController@update');
4242
Route::delete('/{bookSlug}/page/{pageSlug}', 'PageController@destroy');
4343
Route::delete('/{bookSlug}/draft/{pageId}', 'PageController@destroyDraft');
@@ -54,8 +54,8 @@
5454
Route::get('/{bookSlug}/chapter/{chapterSlug}', 'ChapterController@show');
5555
Route::put('/{bookSlug}/chapter/{chapterSlug}', 'ChapterController@update');
5656
Route::get('/{bookSlug}/chapter/{chapterSlug}/edit', 'ChapterController@edit');
57-
Route::get('/{bookSlug}/chapter/{chapterSlug}/restrict', 'ChapterController@showRestrict');
58-
Route::put('/{bookSlug}/chapter/{chapterSlug}/restrict', 'ChapterController@restrict');
57+
Route::get('/{bookSlug}/chapter/{chapterSlug}/permissions', 'ChapterController@showRestrict');
58+
Route::put('/{bookSlug}/chapter/{chapterSlug}/permissions', 'ChapterController@restrict');
5959
Route::get('/{bookSlug}/chapter/{chapterSlug}/delete', 'ChapterController@showDelete');
6060
Route::delete('/{bookSlug}/chapter/{chapterSlug}', 'ChapterController@destroy');
6161

app/Services/RestrictionService.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,25 @@ public function checkIfEntityRestricted(Entity $entity, $action)
4141
return false;
4242
}
4343

44+
/**
45+
* Check if an entity has restrictions set on itself or its
46+
* parent tree.
47+
* @param Entity $entity
48+
* @param $action
49+
* @return bool|mixed
50+
*/
51+
public function checkIfRestrictionsSet(Entity $entity, $action)
52+
{
53+
$this->currentAction = $action;
54+
if ($entity->isA('page')) {
55+
return $entity->restricted || ($entity->chapter && $entity->chapter->restricted) || $entity->book->restricted;
56+
} elseif ($entity->isA('chapter')) {
57+
return $entity->restricted || $entity->book->restricted;
58+
} elseif ($entity->isA('book')) {
59+
return $entity->restricted;
60+
}
61+
}
62+
4463
/**
4564
* Add restrictions for a page query
4665
* @param $query

app/helpers.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,13 @@ function userCan($permission, \BookStack\Ownable $ownable = null)
5252

5353
if (!$ownable instanceof \BookStack\Entity) return $hasPermission;
5454

55-
// Check restrictions on the entitiy
55+
// Check restrictions on the entity
5656
$restrictionService = app('BookStack\Services\RestrictionService');
5757
$explodedPermission = explode('-', $permission);
5858
$action = end($explodedPermission);
5959
$hasAccess = $restrictionService->checkIfEntityRestricted($ownable, $action);
60-
return $hasAccess && $hasPermission;
60+
$restrictionsSet = $restrictionService->checkIfRestrictionsSet($ownable, $action);
61+
return ($hasAccess && $restrictionsSet) || (!$restrictionsSet && $hasPermission);
6162
}
6263

6364
/**

resources/views/books/restrictions.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717

1818
<div class="container" ng-non-bindable>
19-
<h1>Book Restrictions</h1>
19+
<h1>Book Permissions</h1>
2020
@include('form/restriction-form', ['model' => $book])
2121
</div>
2222

resources/views/books/show.blade.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<li><a href="{{ $book->getUrl() }}/sort" class="text-primary"><i class="zmdi zmdi-sort"></i>Sort</a></li>
2525
@endif
2626
@if(userCan('restrictions-manage', $book))
27-
<li><a href="{{$book->getUrl()}}/restrict" class="text-primary"><i class="zmdi zmdi-lock-outline"></i>Restrict</a></li>
27+
<li><a href="{{$book->getUrl()}}/permissions" class="text-primary"><i class="zmdi zmdi-lock-outline"></i>Permissions</a></li>
2828
@endif
2929
@if(userCan('book-delete', $book))
3030
<li><a href="{{ $book->getUrl() }}/delete" class="text-neg"><i class="zmdi zmdi-delete"></i>Delete</a></li>
@@ -90,9 +90,9 @@
9090
@if($book->restricted)
9191
<p class="text-muted">
9292
@if(userCan('restrictions-manage', $book))
93-
<a href="{{ $book->getUrl() }}/restrict"><i class="zmdi zmdi-lock-outline"></i>Book Restricted</a>
93+
<a href="{{ $book->getUrl() }}/permissions"><i class="zmdi zmdi-lock-outline"></i>Book Permissions Active</a>
9494
@else
95-
<i class="zmdi zmdi-lock-outline"></i>Book Restricted
95+
<i class="zmdi zmdi-lock-outline"></i>Book Permissions Active
9696
@endif
9797
</p>
9898
@endif

resources/views/chapters/restrictions.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
</div>
1818

1919
<div class="container" ng-non-bindable>
20-
<h1>Chapter Restrictions</h1>
20+
<h1>Chapter Permissions</h1>
2121
@include('form/restriction-form', ['model' => $chapter])
2222
</div>
2323

resources/views/chapters/show.blade.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<a href="{{$chapter->getUrl() . '/edit'}}" class="text-primary text-button"><i class="zmdi zmdi-edit"></i>Edit</a>
2020
@endif
2121
@if(userCan('restrictions-manage', $chapter))
22-
<a href="{{$chapter->getUrl()}}/restrict" class="text-primary text-button"><i class="zmdi zmdi-lock-outline"></i>Restrict</a>
22+
<a href="{{$chapter->getUrl()}}/permissions" class="text-primary text-button"><i class="zmdi zmdi-lock-outline"></i>Permissions</a>
2323
@endif
2424
@if(userCan('chapter-delete', $chapter))
2525
<a href="{{$chapter->getUrl() . '/delete'}}" class="text-neg text-button"><i class="zmdi zmdi-delete"></i>Delete</a>
@@ -69,18 +69,18 @@
6969

7070
@if($book->restricted)
7171
@if(userCan('restrictions-manage', $book))
72-
<a href="{{ $book->getUrl() }}/restrict"><i class="zmdi zmdi-lock-outline"></i>Book Restricted</a>
72+
<a href="{{ $book->getUrl() }}/permissions"><i class="zmdi zmdi-lock-outline"></i>Book Permissions Active</a>
7373
@else
74-
<i class="zmdi zmdi-lock-outline"></i>Book Restricted
74+
<i class="zmdi zmdi-lock-outline"></i>Book Permissions Active
7575
@endif
7676
<br>
7777
@endif
7878

7979
@if($chapter->restricted)
8080
@if(userCan('restrictions-manage', $chapter))
81-
<a href="{{ $chapter->getUrl() }}/restrict"><i class="zmdi zmdi-lock-outline"></i>Chapter Restricted</a>
81+
<a href="{{ $chapter->getUrl() }}/permissions"><i class="zmdi zmdi-lock-outline"></i>Chapter Permissions Active</a>
8282
@else
83-
<i class="zmdi zmdi-lock-outline"></i>Chapter Restricted
83+
<i class="zmdi zmdi-lock-outline"></i>Chapter Permissions Active
8484
@endif
8585
@endif
8686
</div>

resources/views/form/restriction-form.blade.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
<form action="{{ $model->getUrl() }}/restrict" method="POST">
1+
<form action="{{ $model->getUrl() }}/permissions" method="POST">
22
{!! csrf_field() !!}
33
<input type="hidden" name="_method" value="PUT">
44

5+
<p>Once enabled, These permissions will take priority over any set role permissions.</p>
6+
57
<div class="form-group">
6-
@include('form/checkbox', ['name' => 'restricted', 'label' => 'Restrict this ' . $model->getClassName()])
8+
@include('form/checkbox', ['name' => 'restricted', 'label' => 'Enable custom permissions'])
79
</div>
810

11+
912
<table class="table">
1013
<tr>
1114
<th>Role</th>
@@ -25,5 +28,5 @@
2528
</table>
2629

2730
<a href="{{ $model->getUrl() }}" class="button muted">Cancel</a>
28-
<button type="submit" class="button pos">Save Restrictions</button>
31+
<button type="submit" class="button pos">Save Permissions</button>
2932
</form>

resources/views/pages/restrictions.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
</div>
2525

2626
<div class="container" ng-non-bindable>
27-
<h1>Page Restrictions</h1>
27+
<h1>Page Permissions</h1>
2828
@include('form/restriction-form', ['model' => $page])
2929
</div>
3030

resources/views/pages/show.blade.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<a href="{{$page->getUrl()}}/edit" class="text-primary text-button" ><i class="zmdi zmdi-edit"></i>Edit</a>
3333
@endif
3434
@if(userCan('restrictions-manage', $page))
35-
<a href="{{$page->getUrl()}}/restrict" class="text-primary text-button"><i class="zmdi zmdi-lock-outline"></i>Restrict</a>
35+
<a href="{{$page->getUrl()}}/permissions" class="text-primary text-button"><i class="zmdi zmdi-lock-outline"></i>Permissions</a>
3636
@endif
3737
@if(userCan('page-delete', $page))
3838
<a href="{{$page->getUrl()}}/delete" class="text-neg text-button"><i class="zmdi zmdi-delete"></i>Delete</a>
@@ -76,27 +76,27 @@
7676

7777
@if($book->restricted)
7878
@if(userCan('restrictions-manage', $book))
79-
<a href="{{ $book->getUrl() }}/restrict"><i class="zmdi zmdi-lock-outline"></i>Book restricted</a>
79+
<a href="{{ $book->getUrl() }}/permissions"><i class="zmdi zmdi-lock-outline"></i>Book Permissions Active</a>
8080
@else
81-
<i class="zmdi zmdi-lock-outline"></i>Book restricted
81+
<i class="zmdi zmdi-lock-outline"></i>Book Permissions Active
8282
@endif
8383
<br>
8484
@endif
8585

8686
@if($page->chapter && $page->chapter->restricted)
8787
@if(userCan('restrictions-manage', $page->chapter))
88-
<a href="{{ $page->chapter->getUrl() }}/restrict"><i class="zmdi zmdi-lock-outline"></i>Chapter restricted</a>
88+
<a href="{{ $page->chapter->getUrl() }}/permissions"><i class="zmdi zmdi-lock-outline"></i>Chapter Permissions Active</a>
8989
@else
90-
<i class="zmdi zmdi-lock-outline"></i>Chapter restricted
90+
<i class="zmdi zmdi-lock-outline"></i>Chapter Permissions Active
9191
@endif
9292
<br>
9393
@endif
9494

9595
@if($page->restricted)
9696
@if(userCan('restrictions-manage', $page))
97-
<a href="{{ $page->getUrl() }}/restrict"><i class="zmdi zmdi-lock-outline"></i>Page restricted</a>
97+
<a href="{{ $page->getUrl() }}/permissions"><i class="zmdi zmdi-lock-outline"></i>Page Permissions Active</a>
9898
@else
99-
<i class="zmdi zmdi-lock-outline"></i>Page restricted
99+
<i class="zmdi zmdi-lock-outline"></i>Page Permissions Active
100100
@endif
101101
<br>
102102
@endif

0 commit comments

Comments
 (0)