Skip to content
MSDLT Blog

MSDLT Blog

Learning and teaching with technology in the medical sciences

  • Technical
    • Angular
    • AngularJS
    • CakePHP
    • CSS
    • JQuery
    • Mobile
    • Moodle
    • WordPress
  • Assessment
    • QTI
    • QuestionMark Perception
    • Rogo
  • General
    • Canvas
    • CSlide
    • Learning technology
    • WebLearn (Sakai)

Tag: authentication

Posted on February 10, 2014

CakePHP 2.x: Refreshing the Auth session user data

Something you will inevitably need to do when using the CakePHP Auth Component is to refresh the Auth user data that is stored in the session, for example after a user has updated their email address or other personal/login details. The docs don’t make it obvious how to do this, but a quick search led me to various Stackoverflow questions and this blog post by MilesJ, which is quite old, so presumably refers to CakePHP 1.3. MilesJ’s solution looked like a good one, so I thought I would use this and adapt it as necessary for Cake 2.x.

The things that needed to be changed were as follows:

  1. To get the Auth SessionKey, you must now use AuthComponent::$sessionKey
  2. Instead of ClassRegistry::init('User') to load the User model , you need to use $this->loadModel('User');
  3. Rather than passing the result from read() (containing the user data array as subarray of the ‘User’ index in the array that is returned), straight to the login method, it is necessary for the user data to be at the top level of the array that passed. Therefore, it is necessary to get the user data and then pass the $user[‘User’] array to the login method. It took me a while to work this out, during which time I was getting very frustrated with being logged out whenever I updated my details, and this was why.

This gives us the following function, which seems to do the trick for me:

/**
* Refreshes the Auth session
* Modified from MilesJ: http://milesj.me/blog/read/refreshing-auth
* @param string $field
* @param string $value
* @return void
*/
protected function _refreshAuth($field = '', $value = '') {
   if (!empty($field) && !empty($value)) { //Update just a single field in the Auth session data
      $this->Session->write(AuthComponent::$sessionKey .'.'. $field, $value);
   } else {
      if (!isset($this->User)) {
         $this->loadModel('User'); //Load the User model, if it is not already loaded
      }
      $user = $this->User->read(false, $this->Auth->user('id')); //Get the user's data
      $this->Auth->login($user[User']); //Must have user data at top level of array that is passed to login method
   }
}

About MSDLT

Technology for teaching, learning and assessment: assistance and software development.

We offer:

  • Help technology for teaching, learning and assessment for courses taught at Oxford & at a distance
  • A huge variety of educational technologies, from delivering an online exam and help with Canvas, to creating bespoke, interactive simulations, online experiments and websites

Find out more on the MSD website.

Recent Posts

  • Online exam in kiosk mode on a Chromebook for Medical Schools Applied Knowledge Test
  • Embedding a GitHub gist in Instructure Canvas
  • A dashboard view of Modules in Canvas v2
  • Angular Reactive Form Custom Validator for ORCID (plus Laravel rule)
  • Styling & Layout in Instructure Canvas

Recent Comments

  • Nikodem on Angular Reactive Form Custom Validator for ORCID (plus Laravel rule)
  • Sergio de EOM on CakePHP/MySQL: Want a Boolean? Use TINYINT(1), not BIT(1)
  • Sergio de EOM on CakePHP/MySQL: Want a Boolean? Use TINYINT(1), not BIT(1)
  • Mr. Lance E Sloan on Working with the Canvas API in plain JS – Pt 1
  • Kiley Ohl on Styling & Layout in Instructure Canvas

Categories

  • Angular
  • AngularJS
  • Assessment
  • CakePHP
  • Canvas
  • Chromebook
  • CSlide
  • CSS
  • General
  • JQuery
  • Learning technology
  • Mobile
  • Moodle
  • MSCAA Assessment Suite
  • Other
  • Plain JS
  • QTI
  • QuestionMark Perception
  • ReactJS
  • Rogo
  • Technical
  • Vagrant
  • WebLearn (Sakai)
  • WordPress

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Recent Posts

  • Online exam in kiosk mode on a Chromebook for Medical Schools Applied Knowledge Test
  • Embedding a GitHub gist in Instructure Canvas
  • A dashboard view of Modules in Canvas v2
  • Angular Reactive Form Custom Validator for ORCID (plus Laravel rule)
  • Styling & Layout in Instructure Canvas

Recent Comments

  • Nikodem on Angular Reactive Form Custom Validator for ORCID (plus Laravel rule)
  • Sergio de EOM on CakePHP/MySQL: Want a Boolean? Use TINYINT(1), not BIT(1)
  • Sergio de EOM on CakePHP/MySQL: Want a Boolean? Use TINYINT(1), not BIT(1)
  • Mr. Lance E Sloan on Working with the Canvas API in plain JS – Pt 1
  • Kiley Ohl on Styling & Layout in Instructure Canvas

Archives

  • November 2022
  • October 2018
  • August 2018
  • July 2018
  • June 2018
  • April 2018
  • March 2018
  • February 2018
  • December 2017
  • April 2017
  • December 2016
  • October 2016
  • July 2016
  • June 2016
  • May 2016
  • April 2016
  • February 2016
  • January 2016
  • November 2015
  • October 2015
  • July 2015
  • January 2015
  • December 2014
  • October 2014
  • July 2014
  • June 2014
  • May 2014
  • April 2014
  • March 2014
  • February 2014
  • January 2014
  • December 2013
  • November 2013
  • October 2013
  • September 2013
  • August 2013
  • June 2013
  • May 2013
  • April 2013
  • March 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • October 2012
  • September 2012
  • August 2012
  • July 2012
  • June 2012
  • May 2012
  • April 2012

Categories

  • Angular
  • AngularJS
  • Assessment
  • CakePHP
  • Canvas
  • Chromebook
  • CSlide
  • CSS
  • General
  • JQuery
  • Learning technology
  • Mobile
  • Moodle
  • MSCAA Assessment Suite
  • Other
  • Plain JS
  • QTI
  • QuestionMark Perception
  • ReactJS
  • Rogo
  • Technical
  • Vagrant
  • WebLearn (Sakai)
  • WordPress

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
  • YouTube
Proudly powered by WordPress
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.Accept Read More
Cookie Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT