CakeDC Users appears to be unable to load custom Controller

373 views Asked by At

I have a CakePHP application running on Cake PHP 3.8.13, CakeDC Users 8.5.1 and PHP 7.4. I am attempting to use a custom controller so that I can adjust the behaviour of the LoginTrait and PasswordChangeTrait.

I have been following the documentation in https://github.com/CakeDC/users/blob/master/Docs/Documentation/Configuration.md#using-the-users-email-to-login

However my custom UsersController is not being used.

I have added the following lines to /config/bootstrap.php

Configure::write('Users.config', ['users']);
Plugin::load('CakeDC/Users', ['routes' => true, 'bootstrap' => true]);

I have created a new file /config/users.php

<?php

$config = [
    'Users' => [
        // Table used to manage users
        'table' => 'SomethingThatDoesNotExist',
        // Controller used to manage users plugin features & actions
        'controller' => 'MyUsers',
        // configure Auth component
        'auth' => true,
        // Password Hasher
        'passwordHasher' => '\Cake\Auth\DefaultPasswordHasher',
        // token expiration, 1 hour
        'Token' => ['expiration' => 3600],
        'Email' => [
            // determines if the user should include email
            'required' => true,
            // determines if registration workflow includes email validation
            'validate' => true,
        ],
        'Registration' => [
            // determines if the register is enabled
            'active' => false,
            // determines if the reCaptcha is enabled for registration
            'reCaptcha' => true,
            // allow a logged in user to access the registration form
            'allowLoggedIn' => false,
            //ensure user is active (confirmed email) to reset his password
            'ensureActive' => false,
            // default role name used in registration
            'defaultRole' => 'user',
        ],
        'reCaptcha' => [
            // reCaptcha key goes here
            'key' => null,
            // reCaptcha secret
            'secret' => null,
            // use reCaptcha in registration
            'registration' => false,
            // use reCaptcha in login, valid values are false, true
            'login' => false,
        ],
        'Tos' => [
            // determines if the user should include tos accepted
            'required' => true,
        ],
        'Social' => [
            // enable social login
            'login' => false,
            // enable social login
            'authenticator' => 'CakeDC/Users.Social',
        ],
        'GoogleAuthenticator' => [
            // enable Google Authenticator
            'login' => false,
            'issuer' => null,
            // The number of digits the resulting codes will be
            'digits' => 6,
            // The number of seconds a code will be valid
            'period' => 30,
            // The algorithm used
            'algorithm' => 'sha1',
            // QR-code provider (more on this later)
            'qrcodeprovider' => null,
            // Random Number Generator provider (more on this later)
            'rngprovider' => null
        ],
        'Profile' => [
            // Allow view other users profiles
            'viewOthers' => true,
            'route' => ['prefix' => false, 'plugin' => 'CakeDC/Users', 'controller' => 'Users', 'action' => 'profile'],
        ],
        'Key' => [
            'Session' => [
                // session key to store the social auth data
                'social' => 'Users.social',
                // userId key used in reset password workflow
                'resetPasswordUserId' => 'Users.resetPasswordUserId',
            ],
            // form key to store the social auth data
            'Form' => [
                'social' => 'social'
            ],
            'Data' => [
                // data key to store the users email
                'email' => 'email',
                // data key to store email coming from social networks
                'socialEmail' => 'info.email',
                // data key to check if the remember me option is enabled
                'rememberMe' => 'remember_me',
            ],
        ],
        // Avatar placeholder
        'Avatar' => ['placeholder' => 'CakeDC/Users.avatar_placeholder.png'],
        'RememberMe' => [
            // configure Remember Me component
            'active' => false,
            'checked' => true,
            'Cookie' => [
                'name' => 'remember_me',
                'Config' => [
                    'expires' => '1 month',
                    'httpOnly' => true,
                ]
            ]
        ],
        'Superuser' => ['allowedToChangePasswords' => true], 
    ],
    'GoogleAuthenticator' => [
        'checker' => \CakeDC\Users\Auth\DefaultTwoFactorAuthenticationChecker::class,
        'verifyAction' => [
            'plugin' => 'CakeDC/Users',
            'controller' => 'Users',
            'action' => 'verify',
            'prefix' => false,
        ],
    ],
    'U2f' => [
        'enabled' => false,
        'checker' => \CakeDC\Users\Auth\DefaultU2fAuthenticationChecker::class,
        'startAction' => [
            'plugin' => 'CakeDC/Users',
            'controller' => 'Users',
            'action' => 'u2f',
            'prefix' => false,
        ]
    ],
    
];

return $config;

Note that the table name specified is not a real class name and does not exist. The application continues to run without issue. If I change the config to:

$config = [
    'Users' => [
        // Table used to manage users
        'table' => '\SomethingThatDoesNotExist',
...

i.e. just add the backslash to the table name I receive an error

Class '\SomethingThatDoesNotExist' not found. Error in: ROOT\vendor\cakephp\cakephp\src\ORM\Locator\TableLocator.php, line 239

I do not actually need to customise the table I want to customise the controller, but no matter what I enter in the controller configuration I receive no error! I have a valid controller in the following path /src/Controller/MyUsersController.php, however none of the below configurations trigger its use:

'controller' => 'MyUsers',
'controller' => '\MyUsers',
'controller' => 'App\Controller\MyUsers',
'controller' => '\App\Controller\MyUsers',

How can I get my custom controller to load?

1

There are 1 answers

0
Andrew Gill On BEST ANSWER

It appears that customising the controller does not overwrite the current route it only overwrites based on the custom controller name, i.e. instead of /login it is /myusers/login