How can I load one plugin (AdminLTE) after I did the user auth with another plugin (Ektanjali Usermgmt)?

Hi, I have an User management (by Ektanjali) plugin, and it is working, but, after I do the User Auth, I need to load an another plugin interface, and I don’t know how to do that, because the layout of the Users plugin is inside the layout of the AdminLTE plugin, but I dont want that. How can I solve that? If you don’t understand, reply asking me, please, I really need to solve that.

Plugins:
https://www.ektanjali.com/Products (Version 3)
https://github.com/maiconpinto/cakephp-adminlte-theme (Release 1.1.0)

The other plugin should be loaded only for people who are logged in? When you say “plugin”, do you mean that in the CakePHP sense (i.e. these are both CakePHP plugins), or a more general sense?

It feels like you are just going to do something like:

if ($this->isAuthorized()) {        //or whatever your plugin check is
   $this->loadPlugin('AdminLTE');   //or whatever your correct load command is
}

Those two plugins seem to be for different Cake versions. What version are you in and where are you currently loading the plugins? Show your current code?

Yes, the other plugin should be loaded only for people who are logged in!
And yes, these are both CakePHP plugins

I tried to do that in Application.php or in bootstrap.php:

  • Application.php
    if($this->UserAuth->isLogged()) {
    $this->addPlugin(‘AdminLTE’);
    }

  • bootstrap.php
    if($this->UserAuth->isLogged()) {
    Configure::load(‘adminlte’, ‘default’);
    }

But, in those two cases I get the same two errors:

  • Undefined property: App\Application::$UserAuth
  • [Cake\Core\Exception\MissingPluginException] Plugin AdminLTE could not be found.

That error message is simply telling you that the propery UserAuth does not exist on Application.php. You’re trying to use UserAuth as though you expect it to be an object…

What are you expecting it to be? Where do you believe it is being set? Possibly you have a typo? Or maybe you are expecting a tool to magically appear for your use :slight_smile: ?

I’ve recently had a bit of a struggle like this, though not auth related, so my solution won’t be useful to you. The problem is that plugins get loaded early in the process, but authentication isn’t done (especially if you’re expecting “remember me” cookies to kick in and affect this) until the middleware stack is executing. I’d love to hear about any robust and Cake-standard way to handle this!

I understand the error, but I can’t understand how to call this propery $UserAuth to application.php, because this function exists in the UserAuthComponent.php of the users plugin and I’d already used this propery in another file to give layout permissions and it worked, so I don’t understand why it isn’t working there.
I’m going to look if I see where this propery is being set, if I find, I’ll problably come back here with more doubts hhahaah

At the time that bootstrap.php and Application.php are running, authentication doesn’t exist yet. If you’re using the authentication plugin, it’s added as part of the middleware stack; if you’re using the authentication component, it’s initialized when the controller is. Both of these happen after where you’re trying to reference it.

It looks like $UserAuth is a component. This will be available in Controllers. But Application.php is outside that scope.

AdminLTE looks like it has both Model and Helper tools.

So you could possibly run your conditional plugin loading in AppController initialize or somewhere early like that. Your Component should be available and ready to go in that context.

So, you’re suggesting me to run this conditional function “islogged()” in AppController.php to load the AdminLTE plugin?

  • I tried to do run this code at AppController.php:

if($this->UserAuth->isLogged()) {
$this->addPlugin(‘AdminLTE’);
}

  • but I get this error: An Internal Server Error Occurred

Sorry about all those questions, I’m new at programming at CakePHP, but I’m trying to get better.

hmm… yes. looks like plugin installs is only supported in Application.php with addPlugin(). I was distracted by the fact that the AdminLTE plugin appears to only using a Behavior and Helper, two classes that can be loaded anytime before needed.

So the question is, in Application.php context, what tools does your auth provided to make the decision.

I see you tool is based on the Cake Authentication component. Probably the Session indicates whether the user is logged in. I guess in the absence of UserAuth you could use the Session to look and see if there is a logged in user.

But what the heck are they making available for you?

You could debug Application.php and/or the Request to see what is in them (and what the property is named). Looking in mine on cake 4.x I see the event manager. It might be possible to register an event in Application.php that will load the plugin, then in AppController trigger the event.

This is an interesting idea. I may give this a try in my project and see whether it works.

For the OP, can you not just always enable the plugin, but then later on only set the theme to it and load its form helper if it’s an admin that’s logged in?

I tried that and it worked, thank you so much! I appreciate your attention