Skip to content

Commit

Permalink
Merge pull request #9 from sylvainjule/next
Browse files Browse the repository at this point in the history
Compatibility with 3.6.1
  • Loading branch information
sylvainjule authored Dec 7, 2021
2 parents 2421d2a + 05dcbde commit be31b1d
Show file tree
Hide file tree
Showing 9 changed files with 2,448 additions and 1,333 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ permissions:
panel: true
site: true
settings: false
languages: false
users: false
# ...
user:
Expand Down Expand Up @@ -86,7 +87,7 @@ fields:
query: site.pages # or any query that suits your needs
```

Add a `bouncernav` section in every page you'd like to display the language switcher on:
Add a `bouncernav` section in every page you'd like to display the page switcher on:

```php
// Anywhere in any blueprint
Expand Down Expand Up @@ -114,8 +115,7 @@ return [

## 3. Disclaimer

- 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.
- 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.
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.

<br/>

Expand Down
2 changes: 1 addition & 1 deletion index.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 31 additions & 25 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,52 +1,58 @@
<?php

require_once __DIR__ . '/lib/bouncer.php';

Kirby::plugin('sylvainjule/bouncer', [
'options' => [
'list' => []
'options' => [
'list' => []
],
'sections' => [
'bouncernav' => []
],
'api' => [
'hooks' => [
'panel.route:before' => function($route, $path, $method) {
$user = kirby()->user();
if(!$user) return;

$currentRole = $user->role()->name();

foreach(option('sylvainjule.bouncer.list') as $role => $options) {
if($currentRole == $role) {
$fieldname = $options['fieldname'];
$allowed = Bouncer::getAllowedPages($user, $fieldname, true);
$allowedPaths = A::pluck($allowed, 'path');
$currentPath = '/'. $path;

if(!in_array($currentPath, $allowedPaths)) {
Panel::go($allowedPaths[0]);
}
}
}
}
],
'api' => [
'routes' => function ($kirby) {
return [
[
'pattern' => 'current-user',
'action' => function() use ($kirby) {
$currentUser = $kirby->user();
$currentRole = $currentUser->role()->name();
$restriction = [];
$allowed = [];
$nav = false;

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

if($pages->count()) {
foreach($pages as $page) {
$restriction[] = [
'title' => $page->title()->value(),
'path' => $page->panelUrl(true)
];
}
}
else {
$restriction[] = [
'title' => 'Account',
'path' => '/account'
];
}
$allowed = Bouncer::getAllowedPages($currentUser, $fieldname);
$nav = array_key_exists('nav', $options) && $options['nav'] ? $options['nav'] : false;
}
}

return array(
'nav' => $nav,
'restriction' => $restriction,
'nav' => $nav,
'allowed' => $allowed,
);
}
]
Expand Down
35 changes: 35 additions & 0 deletions lib/bouncer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

class Bouncer {

public static function getAllowedPages($user, $fieldname, $extra = false) {
$kirby = kirby();
$allowed = [];
$pages = $user->$fieldname()->yaml();
$pages = array_map(function($p) use($kirby) { return $kirby->page($p); }, $pages);
$pages = new Pages($pages);

if($pages->count()) {
foreach($pages as $page) {
$allowed[] = [
'title' => $page->title()->value(),
'path' => $page->panelUrl(true)
];
}
}

if($extra) {
$allowed[] = [
'title' => 'Account',
'path' => '/account'
];
$allowed[] = [
'title' => 'Logout',
'path' => '/logout'
];
}

return $allowed;
}

}
Loading

0 comments on commit be31b1d

Please sign in to comment.