luciandex
luciandex

Reputation: 645

OctoberCMS BackendAuth::getUser() return NULL

The problem: Try to get current authenticated user in backend (an administrator with some permissions), OctoberCMS platform. I extended a plugin by a custom plugin ( in boot() method) and I want to remove a sideMenuItem from a menu when a user don't have permission to access it. (I successfully made this - user can't access that item (Access denied) - but I want to remove that item from sideMenu).

Following code is in MyPlugin in that I want to extend OtherAuthor plugin.

public function boot()
    {
        /** Extends plugin */
        $this->otherAuthorPluginExtend();
    }


 public function registerPermissions()
    {
        return [
            /** Permissions for accessing sidemenuItems from OtherAuthor plugin */
            'author.plugin.plugin_access' => [
                'roles' => ['Developer'],
            ],
        ];
    }

public function otherAuthorPluginExtend(){

//>>>THIS NOT WORK
        $user = \Backend\Facades\BackendAuth::getUser();
var_dump($user);

    /*this hide sideMenu item based on permission level*/
          if(array_key_exists('author.plugin.plugin_access', $user->permissions)) {
    //>>>THIS WORKS FINE, but for all backend users
            Event::listen('backend.menu.extendItems', function ($manager) {
                $manager->removeSideMenuItem('OtherAuthor.Plugin', 'Plugin', 'sideMenuItem');
            });
          }
    
      //>>>THIS WORKS FINE
    /*this restrict access to that page from sideMenu*/

        \OtherAuthor\Plugin\Controllers\SideMenuItemController::extend(function ($controller){
            $controller->requiredPermissions = ['author.plugin.plugin_access'];
        });
}

Why the $user get NULL? Is another way to access it?

Thank you in advance!

Upvotes: 0

Views: 454

Answers (2)

Sergey Zakharevich
Sergey Zakharevich

Reputation: 390

Try adding user fetch inside 'backend.menu.extendItems' event

Upvotes: 0

Hardik Satasiya
Hardik Satasiya

Reputation: 9715

The thing is event is called once all plugin registered and user session started so, what you need to do is push your conditional check code inside event callback.

In this way you can get the current backend logged-in user. `if you try to put the current user code outside maybe it's possible that the session is not initialized yet.

Your code should look like this.

public function boot()
{
    /** Extends plugin */
    $this->otherAuthorPluginExtend();
}

public function otherAuthorPluginExtend(){

    // here you can not get user
    // $user = \Backend\Facades\BackendAuth::getUser();

    \Event::listen('backend.menu.extendItems', function ($manager) {
        /*this hide sideMenu item based on permission level*/
        // inside event callback you can retrieve user
        $user = \Backend\Facades\BackendAuth::getUser();
        if(array_key_exists('author.plugin.plugin_access', $user->permissions)) {
            $manager->removeSideMenuItem('OtherAuthor.Plugin', 'Plugin', 'sideMenuItem');
        }
    });

    /*this restrict access to that page from sideMenu*/
    \OtherAuthor\Plugin\Controllers\SideMenuItemController::extend(function ($controller){
        $controller->requiredPermissions = ['author.plugin.plugin_access'];
    });
}

Now everything should work as expected :)

if any doubts please comment.

Upvotes: 0

Related Questions