Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakeDC Users Plugin How Do I Extend It To Be Able To Customise Login Control?

I'm using cakePHP 2.0, and have installed the cakeDC Users plugin. The Users plugin seems to be working, in so far as I can login and be redirected to "/" as defined in the plugin's UsersController _setupAuth function.

But, as I want to modify this, I added some auth setup code from another stackoverflow question to my AppController beforeFilter function.

The problem is, this seems to be ignored, and the _setupAuth code in UsersController takes priority.

So, my question is, how do I add custom redirect control, in my AppController, without having to alter the code in the plugin?

I'm even more confused by the fact that the question I've linked to tells us to put an isAuthorized function in AppController, which seems to mean I have to remove the one in UsersAppController.

like image 408
Jimbo Avatar asked Dec 04 '25 18:12

Jimbo


2 Answers

The idea is that you extend it by overriding it's classes in your app.

You are correct to configure your app specific Auth settings in your AppController. You'll also want to include the Auth component in your project.

Then in your AppController::beforeFilter() you can configure your specific settings such as

$this->Auth->loginRedirect = '/';

So the other question you've linked to is correct, but you don't need all the settings, just the ones you want to overwrite.

I would also strongly recommend adding an isAuthorized() to your AppController if you are using Controller authentication, as you'll want to control who can see what. DO NOT as the other question says just return true, as you're not checking any roles or logins which is bad.

If you want to change the login action, you'll need to create your own controller in your 'app/Controller' and call it something other than Users, I tend to go with MyUsersController.php and in this controller, you'll want to extend the plugin controller. You will need to include the file with App::uses('Users.UsersController','Controller') or similar, and then your controller can

class MyUsersController extends UsersController{
  public function login(){

  }
}

I haven't had the need to do this though with the Users plugin, so perhaps just try configuring your AppController::beforeFilter() first, and checking that you are calling parent::beforeFilter() at the top.

like image 136
David Yell Avatar answered Dec 07 '25 14:12

David Yell


Did you change the routes.php redirections? Here is mine, I disabled the plugin route to refer to my own controller AppUsersController.php

Note that I use an /admin/ prefix. If you don't need it remove the '/admin/' at the beginning of the route path, and 'prefix' and 'admin' keys table.

Router::connect('/admin/users', array('plugin' => null, 'controller' => 'app_users', 'prefix' => 'admin', 'admin' => true));
Router::connect('/admin/users/index/*', array('plugin' => null, 'controller' => 'app_users', 'prefix' => 'admin', 'admin' => true));
Router::connect('/admin/users/users/:action/*', array('plugin' => null, 'controller' => 'app_users', 'prefix' => 'admin', 'admin' => true));
Router::connect('/admin/users', array('plugin' => null, 'controller' => 'app_users', 'prefix' => 'admin', 'admin' => true));
Router::connect('/users/:action/*', array('admin' => false, 'plugin' => null, 'controller' => 'app_users'));
Router::connect('/login/*', array('plugin' => null, 'controller' => 'app_users', 'action' => 'login'));
Router::connect('/logout/*', array('plugin' => null, 'controller' => 'app_users', 'action' => 'logout'));
Router::connect('/register/*', array('plugin' => null, 'controller' => 'app_users', 'action' => 'add'));
like image 39
vinzcelavi Avatar answered Dec 07 '25 13:12

vinzcelavi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!