Jump to content
celfred

Restrict access to a field, except for admin, keeping front-end edition possible

Recommended Posts

Hello,

I'm facing an issue trying to restrict access to a field so front-end edition is possible in an easy manner for the user. Let me explain :

  • I have a field 'group' which is a Page list of group names.
  • I want the user to be able to front-end edit a 'player' page in which this group field appears.
  • I'd like the user to have a list of groups limited to his or her own groups (so he or she doesn't see all groups created by other users).
  • In the back-end, I use this selector string :
    template=group, created_users_id=[user], sort=title

This works great ! BUT here's my issue :

  • If I back-end edit a 'player' page as 'Admin', the 'group' is empty (since admin is not the created-users-id and the group field is then empty...

In other words, I must choose between all groups shown to all users (which may be scary for a user having only 2 or 3 groups whena list of 60 shows up) OR having a correct list for the front-end user, but empty for the back-end 'admin'.

Can anyone think of another way I could get the expected result ? I have a feeling the simplest way would be to have a "Ignore for superusers" checkbox next to the 'selector string'. 

Thanks !

Share this post


Link to post
Share on other sites

You can try to get that using hook below.
But please note, because you want to get that works only inside back-end (admin) place this in site/templates/admin.php before require(...) statement:

// site/templates/admin.php
// set different SelectablePages for page field "group" in case if user is superuser

wire()->addHookAfter('InputfieldPage::getSelectablePages', function(HookEvent $e){ 	
	// here we check desired page field only
	if($e->object->name == 'group'){
		if( wire('user')->isSuperuser() ){
			$e->return = wire('pages')->find('template=group, sort=title');
		}
	}
});

Thanks to PW there are and other options to get the same (eg. hook in custom module, or ready.php).
Regards.

  • Thanks 1

Share this post


Link to post
Share on other sites

Thanks @OLSA for this reply. This put me back on some tests I had done previously but didn't work, but I've managed thanks to you ! 🙂

In fact, I had tested the hook you mentionned (for the 1st time, by reading about it in the 'Custom PHP cide' section in the template page) but I had put it in a _init.php file I include in all my templates, but I guess this was the wrong place for back-end templates.

From what you've indicated, I put my code in admin.php and it works as expected 🙂 So a big thanks to you !

For information in case someone reads this, here's my code :

 

	  $wire->addHookAfter('InputfieldPage::getSelectablePages', function(HookEvent $e) {
    $user = wire('user');
    $pages = wire('pages');
    if($e->object->name == 'group') {
      if ($user->isSuperuser()) {
        $selector = "template=group, sort=title";
      } else {
        $selector = "template=group, created_users_id=$user->id, sort=title";
      }
      $e->return = $e->pages->find($selector);
    }
  });

Of course, I had to remove my selector string in the template page because it triggered an error otherwise by indicating that the saved page didn't match.

  • Like 1

Share this post


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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By rushy
      Hi
      I have a number of pages with images and I'm having a problem selecting on the image descriptions. In the code snippet below, the I added images.description field to my selector and it does work in that I get the expected no. of matches. So findTag gets me a list of images matching the tags in the code, but if I swap that line for the commented line  $match->images->find      it does not work giving 0 results. 
      There is findTag for tags but for description there is not a dedicated function, so I should use find. But what selector will give me a list of images matched by image description?
      Many thanks.
      if($q !== '') { $keywords = $sanitizer->selectorValue($q); $matches = $pages->find("template=album, title|summary|images.tags|images.description~=$keywords"); echo "<p>match count: $matches->count</p>"; foreach($matches as $match){ echo "<p>{$match->title}</p>"; $i = $match->images->findTag($keywords); //$i = $match->images->find("template=album, images.description~=$keywords"); GIVES ZERO MATCHES $j = count($i); echo "<p>No. of image matching: $j</p>"; foreach($i as $img){ echo "<img src='{$img->url}' alt='{$img->description}'>"; } } }  
    • By opalepatrick
      Hi there, just looking for ideas for an IP restricted login page for employees on site? Where to start? Is the IP restricted idea best? Just basically want to make sure employees are in the building when they sign in. Thanks for any help.
×
×
  • Create New...