Skip to content

Commit be31b1d

Browse files
authored
Merge pull request #9 from sylvainjule/next
Compatibility with 3.6.1
2 parents 2421d2a + 05dcbde commit be31b1d

File tree

9 files changed

+2448
-1333
lines changed

9 files changed

+2448
-1333
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ permissions:
4040
panel: true
4141
site: true
4242
settings: false
43+
languages: false
4344
users: false
4445
# ...
4546
user:
@@ -86,7 +87,7 @@ fields:
8687
query: site.pages # or any query that suits your needs
8788
```
8889

89-
Add a `bouncernav` section in every page you'd like to display the language switcher on:
90+
Add a `bouncernav` section in every page you'd like to display the page switcher on:
9091

9192
```php
9293
// Anywhere in any blueprint
@@ -114,8 +115,7 @@ return [
114115

115116
## 3. Disclaimer
116117

117-
- I needed this functionnality for a website and turned it into a plugin. I hope it can prove helpful, but do not intend to extend it or support more refined restriction scenarios with this plugin.
118-
- The plugin uses Vue router's `beforeResolve` guard, currently not used by the panel (Kirby 3.3.5). This may change in the future, please look for any change in that direction before updating your websites, and please let me know if you spot an interfering update.
118+
I needed this functionnality for a website and turned it into a plugin. I hope it can prove helpful, but do not intend to extend it or support more refined restriction scenarios with this plugin.
119119

120120
<br/>
121121

index.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.php

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,58 @@
11
<?php
22

3+
require_once __DIR__ . '/lib/bouncer.php';
4+
35
Kirby::plugin('sylvainjule/bouncer', [
4-
'options' => [
5-
'list' => []
6+
'options' => [
7+
'list' => []
68
],
79
'sections' => [
810
'bouncernav' => []
911
],
10-
'api' => [
12+
'hooks' => [
13+
'panel.route:before' => function($route, $path, $method) {
14+
$user = kirby()->user();
15+
if(!$user) return;
16+
17+
$currentRole = $user->role()->name();
18+
19+
foreach(option('sylvainjule.bouncer.list') as $role => $options) {
20+
if($currentRole == $role) {
21+
$fieldname = $options['fieldname'];
22+
$allowed = Bouncer::getAllowedPages($user, $fieldname, true);
23+
$allowedPaths = A::pluck($allowed, 'path');
24+
$currentPath = '/'. $path;
25+
26+
if(!in_array($currentPath, $allowedPaths)) {
27+
Panel::go($allowedPaths[0]);
28+
}
29+
}
30+
}
31+
}
32+
],
33+
'api' => [
1134
'routes' => function ($kirby) {
1235
return [
1336
[
1437
'pattern' => 'current-user',
1538
'action' => function() use ($kirby) {
1639
$currentUser = $kirby->user();
1740
$currentRole = $currentUser->role()->name();
18-
$restriction = [];
41+
$allowed = [];
1942
$nav = false;
2043

2144
foreach(option('sylvainjule.bouncer.list') as $role => $options) {
2245
if($currentRole == $role) {
2346
$fieldname = $options['fieldname'];
24-
// can't use ->toPages() here because it won't include drafts
25-
$pages = $currentUser->$fieldname()->yaml();
26-
$pages = array_map(function($p) use($kirby) { return $kirby->page($p); }, $pages);
27-
$pages = new Pages($pages);
28-
$nav = array_key_exists('nav', $options) && $options['nav'] ? $options['nav'] : false;
2947

30-
if($pages->count()) {
31-
foreach($pages as $page) {
32-
$restriction[] = [
33-
'title' => $page->title()->value(),
34-
'path' => $page->panelUrl(true)
35-
];
36-
}
37-
}
38-
else {
39-
$restriction[] = [
40-
'title' => 'Account',
41-
'path' => '/account'
42-
];
43-
}
48+
$allowed = Bouncer::getAllowedPages($currentUser, $fieldname);
49+
$nav = array_key_exists('nav', $options) && $options['nav'] ? $options['nav'] : false;
4450
}
4551
}
4652

4753
return array(
48-
'nav' => $nav,
49-
'restriction' => $restriction,
54+
'nav' => $nav,
55+
'allowed' => $allowed,
5056
);
5157
}
5258
]

lib/bouncer.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
class Bouncer {
4+
5+
public static function getAllowedPages($user, $fieldname, $extra = false) {
6+
$kirby = kirby();
7+
$allowed = [];
8+
$pages = $user->$fieldname()->yaml();
9+
$pages = array_map(function($p) use($kirby) { return $kirby->page($p); }, $pages);
10+
$pages = new Pages($pages);
11+
12+
if($pages->count()) {
13+
foreach($pages as $page) {
14+
$allowed[] = [
15+
'title' => $page->title()->value(),
16+
'path' => $page->panelUrl(true)
17+
];
18+
}
19+
}
20+
21+
if($extra) {
22+
$allowed[] = [
23+
'title' => 'Account',
24+
'path' => '/account'
25+
];
26+
$allowed[] = [
27+
'title' => 'Logout',
28+
'path' => '/logout'
29+
];
30+
}
31+
32+
return $allowed;
33+
}
34+
35+
}

0 commit comments

Comments
 (0)