Jump to content

Hide page tree in the admin


renobird
 Share

Recommended Posts

I need to prevent access to the page tree for a particular role (intern).

The goal is the jail them to a set of custom admin pages.

I have a clumsy solution that I've been using for a while that involves checking for the intern role as part of the rendering the navigation in my admin theme. It works, but now with admin themes being modules, I need something that isn't tied to a theme.

ProcessPageList needs page-edit permission. I can't take that away because they need to be able to edit some pages.

Any suggestions would be appreciated.

Link to comment
Share on other sites

Hi reno,

I think it is doable with two hooks. One hook would return false for Page::viewable() for the ProcessPageList page.

Another Hook can be used to redirect your users to a custom admin page after login.

I've only used the second one in a project, but I think something like this should work:

// This needs to be an autoload module

public function init() {
  $this->addHookBefore('ProcessHome::execute', $this, 'rootPage');
  $this->addHookAfter('Page::viewable', $this, 'viewable');
}

/**
 * Redirect users with custom-role to another page after login
 */
public function rootPage(HookEvent $event) {
  if ($this->user->hasRole('custom-role')) {
    $this->session->redirect('custom-admin-page/');		
  }
}

/**
 * Don't give users with custom-role access to Pages page
 */
public function viewable(HookEvent $event) {
  $page = $event->object; 
  $user = $this->user;  
  if ($page->id == 3 && $user->hasRole('custom-role') {
    $event->return = false;
  }
}

  • Like 12
Link to comment
Share on other sites

Wanze. Thanks for sharing this (and to Reno for asking the question!)!  :)

Everyday I continue to be awestruck by the simplicity, cleverness and power of ProcessWire. With about 11 easy-to-follow lines of code, without touching the core, without bashing the system into submission by throwing all sorts of plugins at it and without compromising security and future updates, this opens up a whole new world of possibilities all at the drop of a hat.....Fantastic! ^-^

  • Like 4
Link to comment
Share on other sites

  • 6 months later...

This looks like just what I need but what I'm puzzled about is where do I put this code? Do I create a new module with this code or?

Hi reno,

I think it is doable with two hooks. One hook would return false for Page::viewable() for the ProcessPageList page.

Another Hook can be used to redirect your users to a custom admin page after login.

I've only used the second one in a project, but I think something like this should work:

// This needs to be an autoload module

public function init() {
  $this->addHookBefore('ProcessHome::execute', $this, 'rootPage');
  $this->addHookAfter('Page::viewable', $this, 'viewable');
}

/**
 * Redirect users with custom-role to another page after login
 */
public function rootPage(HookEvent $event) {
  if ($this->user->hasRole('custom-role')) {
    $this->session->redirect('custom-admin-page/');		
  }
}

/**
 * Don't give users with custom-role access to Pages page
 */
public function viewable(HookEvent $event) {
  $page = $event->object; 
  $user = $this->user;  
  if ($page->id == 3 && $user->hasRole('custom-role') {
    $event->return = false;
  }
}

Link to comment
Share on other sites

@Mary,

Yes, you need to create an autoload module...You would need the other bits required in a module, e.g. the install and uninstall methods...Shout if you need any help..

  ??? I've never created or tried to create a PW module... :undecided:   Time to leap into the docs again... I'm sure I'll be shouting soon...

thanks for your help kongondo

Link to comment
Share on other sites

It's about time this was pulled together as a configurable module.

So here it is on github and in the module DB.

You can configure a set of roles and a target redirect page under the module config. I've done very limited testing here - hence it being marked as alpha in the module DB - so please let me know if it works for you.

A word of thanks to both Wanze and kongondo are in order as they pulled most of the material together that I used for this!

  • Like 10
Link to comment
Share on other sites

  • 2 months later...

@kongondo @netcarver @einsteinsboi

 
Is it possible, that something was changed for this module in last PW dev?
 
I use PW 2.5.15 and have strange issue. When I try to edit page I get page witch pointed in "redirect restrict user to" on top of normal edit page.
 
Like:
<code of "redirect restrict user to page">
<code of normal page edit page>
Link to comment
Share on other sites

Steve - thanks for the module - it has proven very useful, however I wonder if you'd consider having multiple rules so it would be possible to redirect some roles to one page and other roles to a different page, etc?

I might end up making the changes myself and submitting a PR if you don't have the time to tackle it, but hoping you might :)

  • Like 3
Link to comment
Share on other sites

  • 3 weeks later...
  • 4 months later...

I've installed http://modules.processwire.com/modules/process-dashboard/

and redirected some custom role to this page after login.

... after that browser freezes in endless "waiting" mode when user (with that custom role) attempts to login.

Any ideas why this happens?

P.S. PW 2.6.4

Edit: two bugs discovered

(1) infinite redirects caused by process-dashboard. Had no time yet to understand why...

(2) hidePageTree redirects only to /admin/smth while smth could be in /admin/somefolder/smth

Link to comment
Share on other sites

  • 1 year later...

This module redirects to the custom page when page/  , But it does not redirect when page/list/
In the latter situation the pagetree is still visible.

What can I do to prevent that?

Is this a good practice to prevent it?
 

public function viewable($event) {
		$page = $event->object; 
		$user = $this->user;  
		if ( ($page->id == 3 || $page->id == 8) && $user->hasRole('custom-role')) {
			$event->return = false;
		}
	}

ps: thus with the extra added $page->id = 8

Link to comment
Share on other sites

16 hours ago, KarlvonKarton said:

This module redirects to the custom page when page/  , But it does not redirect when page/list/
In the latter situation the pagetree is still visible.

What can I do to prevent that?

Is this a good practice to prevent it?
 


public function viewable($event) {
		$page = $event->object; 
		$user = $this->user;  
		if ( ($page->id == 3 || $page->id == 8) && $user->hasRole('custom-role')) {
			$event->return = false;
		}
	}

ps: thus with the extra added $page->id = 8

you can check its path:

if ($page->path == '/admin-page-name/page/list/') $event->return = false ;

Link to comment
Share on other sites

  • 2 years later...
  • 1 month later...

It seems all solutions, including my own one from here 

 don't work with AdminThemeUikit.

So I proposed a change to the getPrimaryNavArray function which you can read on github and enables us to hide the "pages" menu item from the top navigation.

Vote for it, if you like.

EDIT: Actually, there is a working module from @netcarver - http://modules.processwire.com/modules/admin-restrict-page-tree/
My own function had an error and now I replaced it with the mentioned module.

@bernhard Maybe this interesting for you also?!

Edited by jmartsch
Found a working solution
  • Like 1
Link to comment
Share on other sites

@adrian Yes, this module  works flawless, and I had an error in my own hook, so there IS a way to hide the "page" navigation item already. Don't know, why it worked before, and now it did not. Also I don't know why I did not tried this module, because I was aware of it.

However, I still think my pull-request is a valid option, for changing the navigation.

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...