0

I am using CakeDC Users & ACL plugins in my CakePhp app. I have different roles for my users in my app and I would like to have different dashboards based on roles after login.

I extend the plugin with my own table and controller based on the documentation here, so I have MyUsersController and MyUsersTable which override the initial files of the plugin, UsersController and UsersTable. Everything works fine. I create an event in my events.php file which contains:

use CakeDC\Users\Controller\Component\UsersAuthComponent;
use Cake\Event\Event;
use Cake\Event\EventManager;

EventManager::instance()->on(
UsersAuthComponent::EVENT_AFTER_LOGIN,
['priority' => 99], 
function (Event $event) {
    if ($event->data['user']['role_id'] === 'bbcb3031-ebed-445e-8507-f9effb2de026') //the id of my client role{
        return ['plugin' => 'CakeDC/Users', 'controller' => 'MyUsers', 'action' => 'index', '_full' => true, 'prefix' => false];
    }
}
);

But it seems like the override is not working because I have an error:

Error: CakeDC/Users.MyUsersController could not be found.

In my URL I have /users/my-users instead of /my-users and I don't know why. I have test with a template file which is include in the plugin and the Users controller like this:

function (Event $event) {
 if ($event->data['user']['role_id'] === 'bbcb3031-ebed-445e-8507- 
 f9effb2de026') //the id of role{
    return ['plugin' => 'CakeDC/Users', 'controller' => 'Users', 'action' => 'profile';
 }

And it works. My URL redirect after login as a client is /profile. Could someone help me to understand? Please tell me if it's not clear enough and if it's missing parts of codes that might be important to understand my problem.

I specify that I am beginner with Cake.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Amélie
  • 17
  • 7

1 Answers1

2

Your custom controller doesn't live in the CakeDC/Users plugin, hence you must disable the plugin key accordingly, so that the correct URL is being generated (assuming your routes are set up correctly) that connects to your controller, like this:

[
    'plugin' => null,
    'controller' => 'MyUsers',
    'action' => 'index',
    '_full' => true,
    'prefix' => false
]

That would for example match the default fallback routes, generating a URL like /my-users.

See also:

ndm
  • 59,784
  • 9
  • 71
  • 110