8bitOrange

  • Archive
  • RSS
  • Ask me anything

Nerd Post: Extending the Auth Component in Cakephp

You may have guessed by now, I do a lot of my php (if not all) in CakePHP and I really enjoy it. There are times where the core libs are good, but not quite to my liking of what I need, in this case it was the built in ACL and Auth Components. The ACL has a list of needs that I won’t get into right here and now, I will probably write a post about the custom one I am building when it is finished, but for now just understand that I needed people to be able to be in multiple groups at a time and I wanted to be able to access this information through Cake’s built in Auth component.

Up until recently I would have simply done something in the app_controller to go through and attach that data to the user data once they were logged in, but NO MORE my friends. I decided to jump in to the Core component and start tinkering. After looking at it I realized it was just one function, identify, which was attaching that user data to my Auth’s session and that was where I needed to go to access it. So I extended my Auth component and overwrote that function, really only changed a couple of lines and only added one variable to the Auth and thanks to some help from the excellent cakebaker blog and a little google groups search, this is my newly extended component with all of the trimmings to make it work appropriately without throwing a wrench into your app. Here we go:

First, go to the model you are using for your auth data (mine is User) and add the following line:

var $actsAs = array('Containable')

This attaches the containable behaviour to your model and allows us to easily associate data as needed to that model.

Second copy this code (now on github) and add it as your new component and call it in your app_controller instead of the regular Auth component, so add:

var $components = array('MyAuth');
 

Then in your app_controller add the following:

public function constructClasses() {
    parent::constructClasses();
    $this->Auth = $this->MyAuth;
}

Thanks to cakebaker for this tidbit. This allows you to still use $this->Auth uninterrupted and only use MyAuth when setting the one variable below.

After that add this in your beforeFilter():

$this->MyAuth->included = array( 'Group', 'Profile' );

You don’t have to put Group or Profile there, but those are the models I wanted to associate.

Now when you look at $this->Auth->user you will see an array containing all your necessary data. Unfortunately this completely destroys the conveinance of saying $this->Auth->user(‘email’) instead you would say $this->Auth->user(‘User’) for the User array or ‘Group’ for the group array. I will work on fixing this up and adding it as an edit to the github repo when its ready. Hope this helps anyone looking to get a bit more out of the great Auth component in cake.

    • #cakephp
    • #auth
    • #php
    • #code
  • 2 years ago
  • Permalink
  • Share
    Tweet
← Previous • Next →

About

I'm a nerd lost on the internet. I build things with code and write things with words. Fork me on GitHub

Pages

  • I am Matt Caron and I am addicted to the internet
  • Code Blog
  • Resume
  • Stack Overflow
  • My Photography
  • Nerd Appropriate
  • I'm Google-able

Twitter

loading tweets…

  • RSS
  • Random
  • Archive
  • Ask me anything
  • Mobile

Effector Theme by Carlo Franco.

Powered by Tumblr