dweidner / kirby-firewall

Protect your pages and files from unauthorized access
MIT License
23 stars 2 forks source link

Extend pages-methods.php #8

Closed oliverschneider closed 7 years ago

oliverschneider commented 7 years ago

To get only the user accessible pages for the menu (in template files) with easy chaining like so

$page->children()->accessibleByCurrentUser()->visible()

I created another function for the pages object, that works directly with the current user role. As there is no role for a not logged in user and Kirby returns false if you ask for the user, I had to temporarily create one. In my code, I even created an extension for Kirby $site object with a function $site->userRole() that does lines 48-55 and returns role wether there is a logged in user or not.

dweidner commented 7 years ago

Shouldn't be the following extension be enough?

/**
 * Check whether the page is accessible by the current user.
 *
 * @param \Page $page Page to test.
 * @param \User|\Role $obj User or role object.
 * @return bool
 */
$kirby->set('page::method', 'isAccessibleByCurrentUser', function($page) {
  return $page->isAccessibleBy($page->site()->user());
});

I will test the suggested method and will add the corresponding extensions for File, Files and Pages as well.

dweidner commented 7 years ago

The new release provides the desired page methods.

oliverschneider commented 7 years ago

Thank you, works as expected 👍 .