Jump to content

Allow Editors to only add or edit their pages


John W.
 Share

Recommended Posts

Hi, have a site where I set up a news category, then under that I have different regions where editors in those regions can add news stories.  I'm trying to figure out how to allow each editor to only add/edit stories in their region, and not allow editors from other regions to edit them.

example:

News  (template:news-category)

     Washington County (template:news-region)

               My First News Story for Washington County by Editor 1 (template:news-article)

     Jefferson County (template:news-region)

               A News Story by the jefferson county editor (template:news-article)

      Taylor County (template:news-region)

              A news story by a Taylor county editor (template:news-article)

 

I really don't want to create 3 different news-region templates along with 3 different news-article variations, and 3 different types of Editor roles.

Is there a way / module that will let me say, "jane which is an editor can only add and edit to Washington County" and "bill which is an editor can only add or edit to Taylor County", etc?  Also, it would be nice to allow the "home office" editor to add/delete/modify contents created by the other editors, a "Super Editor" if you will.

 

Thanks mucho!

  • Like 1
Link to comment
Share on other sites

You can achieve this with hooks to Page::addable and Page::editable.

1. Define access controls for the news-region and news-article templates so that users with the news-editor role can add children under news-region pages and they can edit and create news-article pages.

2021-07-08_101832.thumb.png.4588eef07a0f813974f38da3d5018d47.png

2021-07-08_102057.thumb.png.ae6c8f313980787a317569d1d6a4e4ad.png

2. Create a news_regions Page Reference field which allows pages with template news-region. Best to make it a "multiple" AsmSelect field so it covers the potential case that a user needs to add/edit within multiple regions. Add this field to the user template.

3. Edit the news-editor users and set the regions they are allowed to add/edit within.

2021-07-08_102008.thumb.png.3e04d14731be733f184304edbe989d3b.png

4. In /site/ready.php:

$wire->addHookAfter('Page::addable', function(HookEvent $event) {
	/** @var Page $page */
	$page = $event->object;
	$user = $event->wire()->user;
	// Return early if PW has already determined that the user is not allowed to add
	if(!$event->return) return;
	if($user->hasRole('news-editor') && $page->template == 'news-region') {
		// Addability depends on if the news_regions Page Reference field has the page
		$event->return = $user->news_regions->has($page);
	}
});

$wire->addHookAfter('Page::editable', function(HookEvent $event) {
	/** @var Page $page */
	$page = $event->object;
	$user = $event->wire()->user;
	// Return early if PW has already determined that the user is not allowed to edit
	if(!$event->return) return;
	if($user->hasRole('news-editor') && $page->template == 'news-article') {
		// Editability depends on if the news_regions Page Reference field has the parent page
		$event->return = $user->news_regions->has($page->parent);
	}
});

The result for user "ringo":

ringo.gif.27c8515ad55df18878c806f827fecd0f.gif

 

7 hours ago, John W. said:

Also, it would be nice to allow the "home office" editor to add/delete/modify contents created by the other editors, a "Super Editor" if you will.

You could add all the regions to that user's page, but personally I would create a different news-supereditor role and just not make them subject to the restrictions in the hooks.

  • Like 12
  • Thanks 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

×
×
  • Create New...