Jump to content

Whitelist of editable fields by role?


GKM490
 Share

Recommended Posts

Hi there,

I am currently setting up a role for a user that will be in charge of editing a list of topics for our pages. I don't want them to be able to edit *anything* else about the page, including the title. Right now I'm accomplishing this by going through the fields on every page template that they have access to, and changing the access so that the "view" permission is set for all, but "edit" is only checked for admin roles and unchecked for the topic editor, and then the "topic" field is the only one with edit access checked for this topic editor role.

This is very tedious, and doesn't account for future changes to the templates if we add or remove fields. I'll have to be sure to go into the permissions for new fields and make sure they all follow this format.

Is there some sort of "Role Field Edit Whitelist" where all fields are disabled for editing for a role, except for ones that appear in the whitelist?

Thanks!

Link to comment
Share on other sites

The quickest way would be to hook $page->editable($field). An raw example for site/ready.php, you'd have to extend it to match your role/templates/fields:

wire()->addHookAfter("Page::editable", function(HookEvent $event) {
	if(!$event->return) return;
	$fieldName = $event->arguments(0);
	if(!$fieldName) return;
	$page = $event->object;

	if(! $event->user->hasRole('your-limited-editor-role'))
		return;

	if($page->template->name !== 'your-editable-template')
		$event->return = false;

	if($fieldName !== 'your-editable-field')
		$event->return = false;
});

 

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