0

I have a question and I can't find a solution yet, I apologize if I can't be clear on my question, but follow below.

In October, when I am creating a navigation menu, the parent menu forces me to take a route, however, many times the user cannot have permission for a certain page, example below in the image.

enter image description here

Possible solution: Create a route and put several "ifs" in it to redirect the user, the problem with this is that the work would be extremely repetitive for each plugin ...

My question would be, how could I create a route that would automatically identify what permission the user would have at the moment and redirect to the correct page in a dynamic way for each plugin?

Or was the solution I indicated the only way?

Crazy
  • 346
  • 1
  • 4
  • 17

1 Answers1

0

I have one solution in mind.

Just make One extra fake controller and make index action of that controller.

In this index action we can have our own permission check and based on that we can redirect user to our predefined location.

Now for main menu just point URL to => myplugin/fakecontroller/index

this controller has no permission means its public so every one can use it.

Now in index action check permission of current user and based on that you can select redirect url.

// in fakecontroller's index action
$redirectUrl = 'default-url';

if($this->user->hasAccess('bla-bla')) {
    $redirectUrl = 'some-other-url';
}

return \Redirect::to($redirectUrl);

To make it more dynamic with multiple plugins

To make it more dynamic you can just pass id of plugin to index action. make sure plugin url will be same for all other plugins. we just change id

Any how you have to use if statements because you want custom behaviour and i don't think we can add those info in to backend and if we try to implement that it will take too long. So, for now I have other solution which can make it little bit easy with multiple plugins.

for ex.

  1. for plugin-A main menu url could be myplugin/fakecontroller/index/plugin-A
  2. for plugin-B main menu url could be myplugin/fakecontroller/index/plugin-B
// fakecontroller
$pluginWiseRedirect = [
    'pluginA' => [
        'permission-name' => 'redirect-url-A',
        'bla-bla' => 'new-url-A' 
    ],
    'pluginB' => [
        'permission-name' => 'redirect-url-B',
        'bla-bla' => 'new-url-B' 
    ],
];

// in fakecontroller's index action
public function index($pluginId = 'pluginA') {
    $redirectUrl = 'default-url';

    $permissionName = 'permission-name';
    if($this->user->hasAccess($permissionName)) {
        // please make check here for key exist
        $redirectUrl = $this->pluginWiseRedirect[$pluginId][$permissionName];
    }

    $permissionName = 'bla-bla';
    if($this->user->hasAccess($permissionName)) {
        // please make check here for key exist 
        $redirectUrl = $this->pluginWiseRedirect[$pluginId][$permissionName];
    }

    return \Redirect::to($redirectUrl);
}

if any doubts please comment.

Hardik Satasiya
  • 9,547
  • 3
  • 22
  • 40