Jump to content
renobird

Hide page tree in the admin

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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Wanze,

Thanks — very much appreciated. I just couldn't seem to get my head around this on my own.

Testing now. Will report back shortly.

Cheers!

Share this post


Link to post
Share on other sites

This is great as it's Something I've wanted to do on a couple of projects the last few weeks, thanks!

  • Like 1

Share this post


Link to post
Share on other sites

This is something i was thinking about a couple of days ago. I was wondering if it would be possible to serve somekind of dashboard page instead of the page tree. Looks like this is the base for that to build it upon.

  • Like 1

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Raymond - did you see my ProcessDashboard module? :)

  • Like 1

Share this post


Link to post
Share on other sites

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;
  }
}

Share this post


Link to post
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..

  • Like 1

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

I had some trouble getting this to work, but it magically started to work after a nth reinstall. Thanks for this, it's doing exactly what I need!

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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 ;

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...