Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: voidet/remember_me
base: 1.3
...
head fork: voidet/remember_me
compare: master
Checking mergeability… Don’t worry, you can still create the pull request.
  • 8 commits
  • 3 files changed
  • 0 commit comments
  • 3 contributors
View
1  .gitignore
@@ -0,0 +1 @@
+.DS_Store
View
90 controllers/components/remember_me.php → Controller/Component/RememberMeComponent.php
@@ -7,7 +7,9 @@
*
**/
-class RememberMeComponent extends Object {
+App::uses('BaseAuthenticate', 'Controller/Component/Auth');
+App::uses('Component', 'Controller');
+class RememberMeComponent extends Component {
/**
* Include the neccessary components for RememberMe to function with
@@ -18,14 +20,15 @@ class RememberMeComponent extends Object {
* @param array $settings overrides default settings for token fieldnames and data fields
* @return false
*/
- function initialize(&$Controller, $settings = array()) {
+ function initialize($Controller, $settings = array()) {
$defaults = array(
'timeout' => '+1 month',
'field_name' => 'remember_me',
'token_field' => 'token',
'token_salt' => 'token_salt'
);
- $this->Controller = &$Controller;
+
+ $this->Controller = $Controller;
$this->settings = array_merge($defaults, $settings);
}
@@ -35,13 +38,17 @@ function initialize(&$Controller, $settings = array()) {
*/
private function initializeModel() {
if (!isset($this->userModel)) {
- App::import('Model', $this->Auth->userModel);
- $this->userModel = new $this->Auth->userModel();
+ $userModel = $this->AuthSettings['userModel'];
+ if (empty($userModel)) {
+ throw new BadMethodCallException('Please specify what user model to authenticate against');
+ }
+ App::import('Model', $userModel);
+ $this->userModel = new $userModel;
}
}
/**
- * tokenSupports checks to see whether or not the current setup supports tokenizing or tokenizing with series
+ * tokenSupports checks to see whether or not the current setup supports tokenizing or tokeniz\ing with series
* @param type specifies which field & setting is functional
* @return bool
*/
@@ -79,10 +86,10 @@ public function setRememberMe($userData) {
* @return false
*/
private function writeTokenCookie($tokens = array(), $userData = array()) {
- $cookieData[$this->Auth->fields['username']] = $userData[$this->Auth->userModel][$this->Auth->fields['username']];
- $cookieData[$this->settings['token_field']] = $tokens[$this->Auth->userModel][$this->settings['token_field']];
+ $cookieData[$this->AuthSettings['fields']['username']] = $userData[$this->AuthSettings['fields']['username']];
+ $cookieData[$this->settings['token_field']] = $tokens[$this->settings['token_field']];
if ($this->tokenSupports('token_salt')) {
- $cookieData[$this->settings['token_salt']] = $tokens[$this->Auth->userModel][$this->settings['token_salt']];
+ $cookieData[$this->settings['token_salt']] = $tokens[$this->settings['token_salt']];
}
$this->Cookie->write($this->Cookie->name, $cookieData, true, $this->settings['timeout']);
}
@@ -92,6 +99,8 @@ private function writeTokenCookie($tokens = array(), $userData = array()) {
* @return false
*/
public function setupUser() {
+ $auth = $this->Auth->constructAuthenticate();
+ $this->AuthSettings = reset($auth)->settings;
$this->setUserScope();
$this->checkUser();
}
@@ -99,7 +108,6 @@ public function setupUser() {
/**
* setUserScope public method must be called manually in beforeFilter
* It will then add in extra userscope conditions to authorise a user against
- * param
* @return false
*/
protected function setUserScope() {
@@ -107,6 +115,9 @@ protected function setUserScope() {
empty($this->Controller->data[$this->Auth->userModel][$this->settings['field_name']]) && $this->tokenSupports('token_field')) {
$tokenField = $this->Auth->userModel.'.'.$this->settings['token_field'];
$cookieData = $this->Cookie->read($this->Cookie->name);
+ if (empty($this->Auth->userScope)) {
+ $this->Auth->userScope = array();
+ }
unset($this->Auth->userScope[$tokenField]);
$this->Auth->userScope += array($tokenField => $cookieData[$this->settings['token_field']]);
}
@@ -118,20 +129,23 @@ protected function setUserScope() {
* @return false
*/
public function checkUser() {
- if ($this->Cookie->read($this->Cookie->name) && !$this->Session->check($this->Auth->sessionKey)) {
+ if ($this->Cookie->read($this->Cookie->name) && !$this->Session->check('Auth.'.$this->Auth->userModel)) {
+
+ $cookieData = $this->Cookie->read($this->Cookie->name);
+
if ($this->tokenSupports('token_field')) {
$userData = $this->checkTokens();
if ($userData) {
$this->setUserScope();
- $this->Auth->login($userData[$this->Auth->userModel][$this->userModel->primaryKey]);
}
+ $user = $this->getUserByTokens($cookieData, true);
+ $this->Auth->login($user);
} else {
- $cookieData = unserialize($this->Cookie->read($this->Cookie->name));
$this->Auth->login($cookieData);
}
}
- if ($this->Cookie->read($this->Cookie->name) && $this->Session->check($this->Auth->sessionKey)) {
+ if ($this->Cookie->read($this->Cookie->name) && $this->Session->check('Auth.'.$this->Auth->userModel)) {
$this->rewriteCookie();
}
}
@@ -167,7 +181,7 @@ public function checkTokens() {
private function writeCookie($userData = array()) {
if ($this->tokenSupports('token_field')) {
$tokens = $this->makeToken($userData);
- $this->userModel->id = $userData[$this->Auth->userModel][$this->userModel->primaryKey];
+ $this->userModel->id = $userData[$this->userModel->primaryKey];
if ($this->userModel->id && $this->userModel->save($tokens)) {
$this->writeTokenCookie($tokens, $userData);
}
@@ -191,7 +205,7 @@ public function logout($flushTokens = false, $user = array(), $redirect = true)
if (empty($user) && $this->Auth->user()) {
$user = $this->Auth->user();
}
- $this->clearTokens($user[$this->Auth->userModel][$this->userModel->primaryKey]);
+ $this->clearTokens($user[$this->userModel->primaryKey]);
}
$this->Cookie->destroy();
$this->Session->destroy();
@@ -216,7 +230,7 @@ public function rewriteCookie() {
* @return array
*/
private function setBasicCookieFields() {
- $fields = array($this->Auth->fields['username'], $this->Auth->fields['password']);
+ $fields = array($this->AuthSettings['fields']['username'], $this->AuthSettings['fields']['password']);
return $fields;
}
@@ -225,7 +239,7 @@ private function setBasicCookieFields() {
* @return array
*/
private function setTokenFields() {
- $fields = array($this->Auth->fields['username'], $this->settings['token_field']);
+ $fields = array($this->AuthSettings['fields']['username'], $this->settings['token_field']);
if ($this->tokenSupports('token_salt')) {
$fields[] = $this->settings['token_salt'];
}
@@ -237,7 +251,7 @@ private function setTokenFields() {
* @return array
*/
private function prepForOr($data) {
- $query['username'] = $data[$this->Auth->fields['username']];
+ $query['username'] = $data[$this->AuthSettings['fields']['username']];
$query['OR'][$this->settings['token_field']] = $data[$this->settings['token_field']];
if ($this->tokenSupports('token_salt')) {
$query['OR'][$this->settings['token_salt']] = $data[$this->settings['token_salt']];
@@ -250,11 +264,15 @@ private function prepForOr($data) {
* getUserByTokens returns user information based on authScope and cookie information
* @return array
*/
- public function getUserByTokens($cookieData) {
+ public function getUserByTokens($cookieData, $allFields = false) {
$this->initializeModel();
- $fields = array($this->userModel->primaryKey);
- $fields = array_merge($fields, $this->setTokenFields());
- return $this->userModel->find('first', array('fields' => array_values($fields), 'conditions' => $this->prepForOr($cookieData), 'recursive' => -1));
+ $fields = array();
+ if ($allFields === false) {
+ $fields = array($this->userModel->primaryKey);
+ $fields = array_merge($fields, $this->setTokenFields());
+ }
+ $user = $this->userModel->find('first', array('fields' => array_values($fields), 'conditions' => $this->prepForOr($cookieData), 'recursive' => -1));
+ return $user[$this->userModel->alias];
}
/**
@@ -263,9 +281,9 @@ public function getUserByTokens($cookieData) {
* @return bool
*/
private function handleHijack($cookieData, $user) {
- if (($cookieData[$this->settings['token_salt']] == $user[$this->Auth->userModel][$this->settings['token_salt']] &&
- $cookieData[$this->settings['token_field']] != $user[$this->Auth->userModel][$this->settings['token_field']]) ||
- ($cookieData[$this->settings['token_salt']] != $user[$this->Auth->userModel][$this->settings['token_salt']])) {
+ if (($cookieData[$this->settings['token_salt']] == $user[$this->settings['token_salt']] &&
+ $cookieData[$this->settings['token_field']] != $user[$this->settings['token_field']]) ||
+ ($cookieData[$this->settings['token_salt']] != $user[$this->settings['token_salt']])) {
$this->logout(false, $user);
return true;
}
@@ -278,9 +296,9 @@ private function handleHijack($cookieData, $user) {
public function clearTokens($id = '') {
$this->initializeModel();
$this->userModel->id = $id;
- $userOverride[$this->Auth->userModel][$this->settings['token_field']] = null;
+ $userOverride[$this->settings['token_field']] = null;
if ($this->tokenSupports('token_salt')) {
- $userOverride[$this->Auth->userModel][$this->settings['token_salt']] = null;
+ $userOverride[$this->settings['token_salt']] = null;
}
if ($id) {
$this->userModel->save($userOverride);
@@ -296,22 +314,20 @@ private function makeToken($user = array()) {
$this->initializeModel();
if ($this->tokenSupports('token_field')) {
if ($this->tokenSupports('token_salt')) {
- if (!empty($user[$this->Auth->userModel]['token_salt'])) {
- $tokens[$this->Auth->userModel][$this->settings['token_salt']] = $user[$this->Auth->userModel]['token_salt'];
+ if (!empty($user['token_salt'])) {
+ $tokens[$this->settings['token_salt']] = $user['token_salt'];
} else {
- $tokens[$this->Auth->userModel][$this->settings['token_salt']] = $this->generateHash();
+ $tokens[$this->settings['token_salt']] = $this->generateHash();
}
}
- if (empty($this->Controller->data[$this->Auth->userModel][$this->settings['field_name']]) && $this->Auth->user($this->settings['token_field'])) {
- $tokens[$this->Auth->userModel][$this->settings['token_field']] = $this->Auth->user($this->settings['token_field']);
+ if (empty($this->Controller->data[$this->settings['field_name']]) && $this->Auth->user($this->settings['token_field'])) {
+ $tokens[$this->settings['token_field']] = $this->Auth->user($this->settings['token_field']);
} else {
- $tokens[$this->Auth->userModel][$this->settings['token_field']] = $this->generateHash();
+ $tokens[$this->settings['token_field']] = $this->generateHash();
}
return $tokens;
}
}
}
-}
-
-?>
+}
View
4 readme.markdown
@@ -9,11 +9,11 @@ This component was written very quickly and requires a lot of code cleaning up,
Install the plugin:
cd myapp
- git clone git://github.com/voidet/remember_me.git remember_me
+ git clone git://github.com/voidet/remember_me.git app/Plugin/RememberMe
Depending on which user controller you would like the RememberMe functions to work on, open up the controller and type in.
- var $components = array('RememberMe.RememberMe');
+ public $components = array('RememberMe.RememberMe');
In order to log a user in and set the cookie information you can use something like this in your login action in your controller:

No commit comments for this range

Something went wrong with that request. Please try again.