CakePHP 2.8 Code has Extra Data in $this->Auth->user. How?












0















I'm working with CakePHP 2.8 code and I don't have access to any of the previous coders, otherwise, I would ask them about this. I'm creating a login point for another app that will bypass the standard form login and just log in behind the scenes. When I log in to the site using the standard login form, $this->Auth->user is populated with extra data as shown here:



array(
'id' => '368',
'email' => 'jay@somewhere.com',
'first_name' => 'Jay',
'last_name' => 'Washere',
'role_id' => '4',
'disabled' => false,
'account_id' => '149',
'Role' => array(
'id' => '4',
'name' => 'Super'
),
'Account' => array(
'id' => '149',
'business_name' => 'Jay's Sun Spot',
<...snip...>
'setup_needed' => false
),
'UserAvailability' => array(
'id' => '1151',
'user_id' => '368',
'sun_start' => '08:00:00',
<...snip...>
'fri_end' => '22:00:00',
'sat_end' => '22:00:00'
),
'UserConfig' => array(
'id' => '38',
'user_id' => '368',
'calendar_view' => '3',
'default_scheduler' => '0'
)
)


However, when I log in from my new controller using the following code:



$user = $this->User->findById($userId);
$this->Auth->login($user['User']);


the login works, but $this->Auth->user only contains:



array(
'id' => '368',
'email' => 'jay@somewhere.com',
'first_name' => 'Jay',
'last_name' => 'Washere',
'role_id' => '4',
'disabled' => false,
'account_id' => '149'
)


The User model (without validation) is below per savedario's request. I removed the validation and some of the model associations that don't include the fields in question.



<?php
App::uses('AppModel', 'Model');
App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');

class User extends AppModel {

public $virtualFields = array('full_name' => 'CONCAT(User.first_name, " ", User.last_name)');
public $displayField = 'full_name';
public $order = 'full_name';

public function beforeSave($options = array()) {
if (isset($this->data[$this->alias]['password'])) {
$passwordHasher = new BlowfishPasswordHasher();
$this->data[$this->alias]['password'] = $passwordHasher->hash(
$this->data[$this->alias]['password']
);
}
return true;
}

// Validation snipped

public function validate_passwords() {
return $this->data[$this->alias]['password'] === $this->data[$this->alias]['confirm_password'];
}

public $belongsTo = array(
'Role' => array(
'className' => 'Role',
'foreignKey' => 'role_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Account' => array(
'className' => 'Account',
'foreignKey' => 'account_id',
'conditions' => '',
'fields' => '',
'order' => ''
)

);

// hasMany associations snipped, there are 16

public $hasOne = array(
'UserAvailability' => array(
'className' => 'UserAvailability',
),
'UserConfig' => array(
'className' => 'UserConfig',
),
'CalendarColor' => array(
'className' => 'CalendarColor',
)
);

// hasAndBelongsToMany snipped, there are 6

protected function setFlashMessage($success, $type = 'sav') {

//if user is a duplicate return custom flash message
if ($success === 'duplicate') {
$this->_flashMessage = 'A user with this email address is already registered to this account.'.
'Please check that you are not creating a duplicate user.';
$this->_flashClass = 'danger';
return $this->_flashMessage;
} else {
return parent::setFlashMessage($success, $type);
}
}

protected function add($save_array) {
//check if new users email already exists in the account
if (!$this->isUniqueInAccount($this->_user['account_id'], $save_array['User']['email'], 'email')) {
$this->setFlashMessage('duplicate');
return false;
}

//set account id and franchise id based on creating user
$save_array['User']['account_id'] = $this->_user['account_id'];

//empty any existing information
$this->create();

//attempt to save user and position data
$result = $this->saveAll($save_array);
if ($result) {

//load Position class and find entry for current position
$Position = ClassRegistry::init('Position');
$position = $Position->find('first', array(
'conditions' => array(
'Position.id' => $save_array['Position']['Position']
)
))['Position'];

//get class type based on position
if($position['group_id'] == 2 || $position['group_id'] == 5){
$workerType = 'Scheduler';
} else if($position['group_id'] == 1){
$workerType = 'Closer';
} else if($position['group_id'] == 3){
$workerType = 'ProposalWriter';
}

//array of data for saving workers and manager as applicable
if(isset($workerType)){
$workerArray = array(
'user_id' => $this->id,
'name' => $save_array['User']['first_name'] . ' ' . $save_array['User']['last_name'],
'account_id' => $this->_user['account_id']
);

//load worker class and save new data
$workerClass = ClassRegistry::init($workerType);
$workerClass->modify('add', null, $this->_user, array(
$workerType => $workerArray
));

//set group and manager based on position
$data['Manager']['group_id'] = $position['group_id'];
$data['Manager']['manager'] = $position['manager'];

//load Manager class and save new data
$Manager = ClassRegistry::init('Manager');
$Manager->modify('add', null, $this->_user, array(
'Manager' => $workerArray
));
}

//get account info for start and end times
$account = $this->_getAccountInfo();

//set start/end time based on configs or default
$startTime = $account['Config']['business_start_time']
? date('H:i:s', strtotime($account['Config']['business_start_time']))
: '8:00:00';
$endTime = $account['Config']['business_end_time']
? date('H:i:s', strtotime($account['Config']['business_end_time']))
: '22:00:00';

//loop through all days and set start/end time
$days = array('sun', 'mon', 'tue','wed','thu','fri','sat');
foreach ($days as $day) {
$data[$day.'_start'] = $startTime;
$data[$day.'_end'] = $endTime;
}
$data['user_id'] = $this->id;

//load UserAvailability class and save new data
$UserAvailability = ClassRegistry::init('UserAvailability');
$UserAvailability->modify('add', null, $this->_user, array(
'UserAvailability' => $data
));
}

$accountModel = ClassRegistry::init('Account');
$account = $accountModel->find('first', array('conditions' => array('Account.id' => $this->_user['account_id'])));
$key = 'images/' . str_replace('s3-', '', $account['Account']['business_logo_name']);
$image = 'https://s3-us-west-2.amazonaws.com/sst-account-logos/'.$key;
$accountData['account'] = $account['Account'];

$siteConfigModel = ClassRegistry::init('SiteConfig');
$siteConfigs = $siteConfigModel->find('first', array('conditions' => array('SiteConfig.id' => 1)));

//set flash and redirect array based on save
$this->setResponseVariables($result, 'add');
return $result;
}
}


I have searched both /app and /lib using "UserAccount", "user_accounts" and similar variations of the other extra data sets and found nothing that explains how this extra data is added to $this->Auth->user.



So, I have two questions: 1) How does the standard form login include the extra data, and 2) Why doesn't logging in with the $user data set the same data?



Another Edit:



I found the following in BaseAuthenticate's _findUser function:



$result = ClassRegistry::init($userModel)->find('first', array(
'conditions' => $conditions,
'recursive' => $this->settings['recursive'],
'fields' => $userFields,
'contain' => $this->settings['contain'],
));


So now the question is how do I set $this->settings['recursive'] and $this->settings['contain'] from my controller?










share|improve this question

























  • My first guess would be that the standard one is reading the user record with "recursive" set at the default 1, so it pulls in one level of associated records.

    – Greg Schmidt
    Nov 16 '18 at 23:45











  • Can you post the content of Model/User.php. It is likely there are some model associations defined there.

    – savedario
    Nov 18 '18 at 0:52











  • @GregSchmidt: That could be but where does that happen? I've searched everything trying to find where $this->Auth->user gets set. I've also tried to add the extra data right after $this->Auth->login($user) but it won't let me write to write to $this->Auth->user.

    – Jay Rennemeyer
    Nov 19 '18 at 15:35











  • @savedario Done.

    – Jay Rennemeyer
    Nov 19 '18 at 15:36
















0















I'm working with CakePHP 2.8 code and I don't have access to any of the previous coders, otherwise, I would ask them about this. I'm creating a login point for another app that will bypass the standard form login and just log in behind the scenes. When I log in to the site using the standard login form, $this->Auth->user is populated with extra data as shown here:



array(
'id' => '368',
'email' => 'jay@somewhere.com',
'first_name' => 'Jay',
'last_name' => 'Washere',
'role_id' => '4',
'disabled' => false,
'account_id' => '149',
'Role' => array(
'id' => '4',
'name' => 'Super'
),
'Account' => array(
'id' => '149',
'business_name' => 'Jay's Sun Spot',
<...snip...>
'setup_needed' => false
),
'UserAvailability' => array(
'id' => '1151',
'user_id' => '368',
'sun_start' => '08:00:00',
<...snip...>
'fri_end' => '22:00:00',
'sat_end' => '22:00:00'
),
'UserConfig' => array(
'id' => '38',
'user_id' => '368',
'calendar_view' => '3',
'default_scheduler' => '0'
)
)


However, when I log in from my new controller using the following code:



$user = $this->User->findById($userId);
$this->Auth->login($user['User']);


the login works, but $this->Auth->user only contains:



array(
'id' => '368',
'email' => 'jay@somewhere.com',
'first_name' => 'Jay',
'last_name' => 'Washere',
'role_id' => '4',
'disabled' => false,
'account_id' => '149'
)


The User model (without validation) is below per savedario's request. I removed the validation and some of the model associations that don't include the fields in question.



<?php
App::uses('AppModel', 'Model');
App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');

class User extends AppModel {

public $virtualFields = array('full_name' => 'CONCAT(User.first_name, " ", User.last_name)');
public $displayField = 'full_name';
public $order = 'full_name';

public function beforeSave($options = array()) {
if (isset($this->data[$this->alias]['password'])) {
$passwordHasher = new BlowfishPasswordHasher();
$this->data[$this->alias]['password'] = $passwordHasher->hash(
$this->data[$this->alias]['password']
);
}
return true;
}

// Validation snipped

public function validate_passwords() {
return $this->data[$this->alias]['password'] === $this->data[$this->alias]['confirm_password'];
}

public $belongsTo = array(
'Role' => array(
'className' => 'Role',
'foreignKey' => 'role_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Account' => array(
'className' => 'Account',
'foreignKey' => 'account_id',
'conditions' => '',
'fields' => '',
'order' => ''
)

);

// hasMany associations snipped, there are 16

public $hasOne = array(
'UserAvailability' => array(
'className' => 'UserAvailability',
),
'UserConfig' => array(
'className' => 'UserConfig',
),
'CalendarColor' => array(
'className' => 'CalendarColor',
)
);

// hasAndBelongsToMany snipped, there are 6

protected function setFlashMessage($success, $type = 'sav') {

//if user is a duplicate return custom flash message
if ($success === 'duplicate') {
$this->_flashMessage = 'A user with this email address is already registered to this account.'.
'Please check that you are not creating a duplicate user.';
$this->_flashClass = 'danger';
return $this->_flashMessage;
} else {
return parent::setFlashMessage($success, $type);
}
}

protected function add($save_array) {
//check if new users email already exists in the account
if (!$this->isUniqueInAccount($this->_user['account_id'], $save_array['User']['email'], 'email')) {
$this->setFlashMessage('duplicate');
return false;
}

//set account id and franchise id based on creating user
$save_array['User']['account_id'] = $this->_user['account_id'];

//empty any existing information
$this->create();

//attempt to save user and position data
$result = $this->saveAll($save_array);
if ($result) {

//load Position class and find entry for current position
$Position = ClassRegistry::init('Position');
$position = $Position->find('first', array(
'conditions' => array(
'Position.id' => $save_array['Position']['Position']
)
))['Position'];

//get class type based on position
if($position['group_id'] == 2 || $position['group_id'] == 5){
$workerType = 'Scheduler';
} else if($position['group_id'] == 1){
$workerType = 'Closer';
} else if($position['group_id'] == 3){
$workerType = 'ProposalWriter';
}

//array of data for saving workers and manager as applicable
if(isset($workerType)){
$workerArray = array(
'user_id' => $this->id,
'name' => $save_array['User']['first_name'] . ' ' . $save_array['User']['last_name'],
'account_id' => $this->_user['account_id']
);

//load worker class and save new data
$workerClass = ClassRegistry::init($workerType);
$workerClass->modify('add', null, $this->_user, array(
$workerType => $workerArray
));

//set group and manager based on position
$data['Manager']['group_id'] = $position['group_id'];
$data['Manager']['manager'] = $position['manager'];

//load Manager class and save new data
$Manager = ClassRegistry::init('Manager');
$Manager->modify('add', null, $this->_user, array(
'Manager' => $workerArray
));
}

//get account info for start and end times
$account = $this->_getAccountInfo();

//set start/end time based on configs or default
$startTime = $account['Config']['business_start_time']
? date('H:i:s', strtotime($account['Config']['business_start_time']))
: '8:00:00';
$endTime = $account['Config']['business_end_time']
? date('H:i:s', strtotime($account['Config']['business_end_time']))
: '22:00:00';

//loop through all days and set start/end time
$days = array('sun', 'mon', 'tue','wed','thu','fri','sat');
foreach ($days as $day) {
$data[$day.'_start'] = $startTime;
$data[$day.'_end'] = $endTime;
}
$data['user_id'] = $this->id;

//load UserAvailability class and save new data
$UserAvailability = ClassRegistry::init('UserAvailability');
$UserAvailability->modify('add', null, $this->_user, array(
'UserAvailability' => $data
));
}

$accountModel = ClassRegistry::init('Account');
$account = $accountModel->find('first', array('conditions' => array('Account.id' => $this->_user['account_id'])));
$key = 'images/' . str_replace('s3-', '', $account['Account']['business_logo_name']);
$image = 'https://s3-us-west-2.amazonaws.com/sst-account-logos/'.$key;
$accountData['account'] = $account['Account'];

$siteConfigModel = ClassRegistry::init('SiteConfig');
$siteConfigs = $siteConfigModel->find('first', array('conditions' => array('SiteConfig.id' => 1)));

//set flash and redirect array based on save
$this->setResponseVariables($result, 'add');
return $result;
}
}


I have searched both /app and /lib using "UserAccount", "user_accounts" and similar variations of the other extra data sets and found nothing that explains how this extra data is added to $this->Auth->user.



So, I have two questions: 1) How does the standard form login include the extra data, and 2) Why doesn't logging in with the $user data set the same data?



Another Edit:



I found the following in BaseAuthenticate's _findUser function:



$result = ClassRegistry::init($userModel)->find('first', array(
'conditions' => $conditions,
'recursive' => $this->settings['recursive'],
'fields' => $userFields,
'contain' => $this->settings['contain'],
));


So now the question is how do I set $this->settings['recursive'] and $this->settings['contain'] from my controller?










share|improve this question

























  • My first guess would be that the standard one is reading the user record with "recursive" set at the default 1, so it pulls in one level of associated records.

    – Greg Schmidt
    Nov 16 '18 at 23:45











  • Can you post the content of Model/User.php. It is likely there are some model associations defined there.

    – savedario
    Nov 18 '18 at 0:52











  • @GregSchmidt: That could be but where does that happen? I've searched everything trying to find where $this->Auth->user gets set. I've also tried to add the extra data right after $this->Auth->login($user) but it won't let me write to write to $this->Auth->user.

    – Jay Rennemeyer
    Nov 19 '18 at 15:35











  • @savedario Done.

    – Jay Rennemeyer
    Nov 19 '18 at 15:36














0












0








0








I'm working with CakePHP 2.8 code and I don't have access to any of the previous coders, otherwise, I would ask them about this. I'm creating a login point for another app that will bypass the standard form login and just log in behind the scenes. When I log in to the site using the standard login form, $this->Auth->user is populated with extra data as shown here:



array(
'id' => '368',
'email' => 'jay@somewhere.com',
'first_name' => 'Jay',
'last_name' => 'Washere',
'role_id' => '4',
'disabled' => false,
'account_id' => '149',
'Role' => array(
'id' => '4',
'name' => 'Super'
),
'Account' => array(
'id' => '149',
'business_name' => 'Jay's Sun Spot',
<...snip...>
'setup_needed' => false
),
'UserAvailability' => array(
'id' => '1151',
'user_id' => '368',
'sun_start' => '08:00:00',
<...snip...>
'fri_end' => '22:00:00',
'sat_end' => '22:00:00'
),
'UserConfig' => array(
'id' => '38',
'user_id' => '368',
'calendar_view' => '3',
'default_scheduler' => '0'
)
)


However, when I log in from my new controller using the following code:



$user = $this->User->findById($userId);
$this->Auth->login($user['User']);


the login works, but $this->Auth->user only contains:



array(
'id' => '368',
'email' => 'jay@somewhere.com',
'first_name' => 'Jay',
'last_name' => 'Washere',
'role_id' => '4',
'disabled' => false,
'account_id' => '149'
)


The User model (without validation) is below per savedario's request. I removed the validation and some of the model associations that don't include the fields in question.



<?php
App::uses('AppModel', 'Model');
App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');

class User extends AppModel {

public $virtualFields = array('full_name' => 'CONCAT(User.first_name, " ", User.last_name)');
public $displayField = 'full_name';
public $order = 'full_name';

public function beforeSave($options = array()) {
if (isset($this->data[$this->alias]['password'])) {
$passwordHasher = new BlowfishPasswordHasher();
$this->data[$this->alias]['password'] = $passwordHasher->hash(
$this->data[$this->alias]['password']
);
}
return true;
}

// Validation snipped

public function validate_passwords() {
return $this->data[$this->alias]['password'] === $this->data[$this->alias]['confirm_password'];
}

public $belongsTo = array(
'Role' => array(
'className' => 'Role',
'foreignKey' => 'role_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Account' => array(
'className' => 'Account',
'foreignKey' => 'account_id',
'conditions' => '',
'fields' => '',
'order' => ''
)

);

// hasMany associations snipped, there are 16

public $hasOne = array(
'UserAvailability' => array(
'className' => 'UserAvailability',
),
'UserConfig' => array(
'className' => 'UserConfig',
),
'CalendarColor' => array(
'className' => 'CalendarColor',
)
);

// hasAndBelongsToMany snipped, there are 6

protected function setFlashMessage($success, $type = 'sav') {

//if user is a duplicate return custom flash message
if ($success === 'duplicate') {
$this->_flashMessage = 'A user with this email address is already registered to this account.'.
'Please check that you are not creating a duplicate user.';
$this->_flashClass = 'danger';
return $this->_flashMessage;
} else {
return parent::setFlashMessage($success, $type);
}
}

protected function add($save_array) {
//check if new users email already exists in the account
if (!$this->isUniqueInAccount($this->_user['account_id'], $save_array['User']['email'], 'email')) {
$this->setFlashMessage('duplicate');
return false;
}

//set account id and franchise id based on creating user
$save_array['User']['account_id'] = $this->_user['account_id'];

//empty any existing information
$this->create();

//attempt to save user and position data
$result = $this->saveAll($save_array);
if ($result) {

//load Position class and find entry for current position
$Position = ClassRegistry::init('Position');
$position = $Position->find('first', array(
'conditions' => array(
'Position.id' => $save_array['Position']['Position']
)
))['Position'];

//get class type based on position
if($position['group_id'] == 2 || $position['group_id'] == 5){
$workerType = 'Scheduler';
} else if($position['group_id'] == 1){
$workerType = 'Closer';
} else if($position['group_id'] == 3){
$workerType = 'ProposalWriter';
}

//array of data for saving workers and manager as applicable
if(isset($workerType)){
$workerArray = array(
'user_id' => $this->id,
'name' => $save_array['User']['first_name'] . ' ' . $save_array['User']['last_name'],
'account_id' => $this->_user['account_id']
);

//load worker class and save new data
$workerClass = ClassRegistry::init($workerType);
$workerClass->modify('add', null, $this->_user, array(
$workerType => $workerArray
));

//set group and manager based on position
$data['Manager']['group_id'] = $position['group_id'];
$data['Manager']['manager'] = $position['manager'];

//load Manager class and save new data
$Manager = ClassRegistry::init('Manager');
$Manager->modify('add', null, $this->_user, array(
'Manager' => $workerArray
));
}

//get account info for start and end times
$account = $this->_getAccountInfo();

//set start/end time based on configs or default
$startTime = $account['Config']['business_start_time']
? date('H:i:s', strtotime($account['Config']['business_start_time']))
: '8:00:00';
$endTime = $account['Config']['business_end_time']
? date('H:i:s', strtotime($account['Config']['business_end_time']))
: '22:00:00';

//loop through all days and set start/end time
$days = array('sun', 'mon', 'tue','wed','thu','fri','sat');
foreach ($days as $day) {
$data[$day.'_start'] = $startTime;
$data[$day.'_end'] = $endTime;
}
$data['user_id'] = $this->id;

//load UserAvailability class and save new data
$UserAvailability = ClassRegistry::init('UserAvailability');
$UserAvailability->modify('add', null, $this->_user, array(
'UserAvailability' => $data
));
}

$accountModel = ClassRegistry::init('Account');
$account = $accountModel->find('first', array('conditions' => array('Account.id' => $this->_user['account_id'])));
$key = 'images/' . str_replace('s3-', '', $account['Account']['business_logo_name']);
$image = 'https://s3-us-west-2.amazonaws.com/sst-account-logos/'.$key;
$accountData['account'] = $account['Account'];

$siteConfigModel = ClassRegistry::init('SiteConfig');
$siteConfigs = $siteConfigModel->find('first', array('conditions' => array('SiteConfig.id' => 1)));

//set flash and redirect array based on save
$this->setResponseVariables($result, 'add');
return $result;
}
}


I have searched both /app and /lib using "UserAccount", "user_accounts" and similar variations of the other extra data sets and found nothing that explains how this extra data is added to $this->Auth->user.



So, I have two questions: 1) How does the standard form login include the extra data, and 2) Why doesn't logging in with the $user data set the same data?



Another Edit:



I found the following in BaseAuthenticate's _findUser function:



$result = ClassRegistry::init($userModel)->find('first', array(
'conditions' => $conditions,
'recursive' => $this->settings['recursive'],
'fields' => $userFields,
'contain' => $this->settings['contain'],
));


So now the question is how do I set $this->settings['recursive'] and $this->settings['contain'] from my controller?










share|improve this question
















I'm working with CakePHP 2.8 code and I don't have access to any of the previous coders, otherwise, I would ask them about this. I'm creating a login point for another app that will bypass the standard form login and just log in behind the scenes. When I log in to the site using the standard login form, $this->Auth->user is populated with extra data as shown here:



array(
'id' => '368',
'email' => 'jay@somewhere.com',
'first_name' => 'Jay',
'last_name' => 'Washere',
'role_id' => '4',
'disabled' => false,
'account_id' => '149',
'Role' => array(
'id' => '4',
'name' => 'Super'
),
'Account' => array(
'id' => '149',
'business_name' => 'Jay's Sun Spot',
<...snip...>
'setup_needed' => false
),
'UserAvailability' => array(
'id' => '1151',
'user_id' => '368',
'sun_start' => '08:00:00',
<...snip...>
'fri_end' => '22:00:00',
'sat_end' => '22:00:00'
),
'UserConfig' => array(
'id' => '38',
'user_id' => '368',
'calendar_view' => '3',
'default_scheduler' => '0'
)
)


However, when I log in from my new controller using the following code:



$user = $this->User->findById($userId);
$this->Auth->login($user['User']);


the login works, but $this->Auth->user only contains:



array(
'id' => '368',
'email' => 'jay@somewhere.com',
'first_name' => 'Jay',
'last_name' => 'Washere',
'role_id' => '4',
'disabled' => false,
'account_id' => '149'
)


The User model (without validation) is below per savedario's request. I removed the validation and some of the model associations that don't include the fields in question.



<?php
App::uses('AppModel', 'Model');
App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');

class User extends AppModel {

public $virtualFields = array('full_name' => 'CONCAT(User.first_name, " ", User.last_name)');
public $displayField = 'full_name';
public $order = 'full_name';

public function beforeSave($options = array()) {
if (isset($this->data[$this->alias]['password'])) {
$passwordHasher = new BlowfishPasswordHasher();
$this->data[$this->alias]['password'] = $passwordHasher->hash(
$this->data[$this->alias]['password']
);
}
return true;
}

// Validation snipped

public function validate_passwords() {
return $this->data[$this->alias]['password'] === $this->data[$this->alias]['confirm_password'];
}

public $belongsTo = array(
'Role' => array(
'className' => 'Role',
'foreignKey' => 'role_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Account' => array(
'className' => 'Account',
'foreignKey' => 'account_id',
'conditions' => '',
'fields' => '',
'order' => ''
)

);

// hasMany associations snipped, there are 16

public $hasOne = array(
'UserAvailability' => array(
'className' => 'UserAvailability',
),
'UserConfig' => array(
'className' => 'UserConfig',
),
'CalendarColor' => array(
'className' => 'CalendarColor',
)
);

// hasAndBelongsToMany snipped, there are 6

protected function setFlashMessage($success, $type = 'sav') {

//if user is a duplicate return custom flash message
if ($success === 'duplicate') {
$this->_flashMessage = 'A user with this email address is already registered to this account.'.
'Please check that you are not creating a duplicate user.';
$this->_flashClass = 'danger';
return $this->_flashMessage;
} else {
return parent::setFlashMessage($success, $type);
}
}

protected function add($save_array) {
//check if new users email already exists in the account
if (!$this->isUniqueInAccount($this->_user['account_id'], $save_array['User']['email'], 'email')) {
$this->setFlashMessage('duplicate');
return false;
}

//set account id and franchise id based on creating user
$save_array['User']['account_id'] = $this->_user['account_id'];

//empty any existing information
$this->create();

//attempt to save user and position data
$result = $this->saveAll($save_array);
if ($result) {

//load Position class and find entry for current position
$Position = ClassRegistry::init('Position');
$position = $Position->find('first', array(
'conditions' => array(
'Position.id' => $save_array['Position']['Position']
)
))['Position'];

//get class type based on position
if($position['group_id'] == 2 || $position['group_id'] == 5){
$workerType = 'Scheduler';
} else if($position['group_id'] == 1){
$workerType = 'Closer';
} else if($position['group_id'] == 3){
$workerType = 'ProposalWriter';
}

//array of data for saving workers and manager as applicable
if(isset($workerType)){
$workerArray = array(
'user_id' => $this->id,
'name' => $save_array['User']['first_name'] . ' ' . $save_array['User']['last_name'],
'account_id' => $this->_user['account_id']
);

//load worker class and save new data
$workerClass = ClassRegistry::init($workerType);
$workerClass->modify('add', null, $this->_user, array(
$workerType => $workerArray
));

//set group and manager based on position
$data['Manager']['group_id'] = $position['group_id'];
$data['Manager']['manager'] = $position['manager'];

//load Manager class and save new data
$Manager = ClassRegistry::init('Manager');
$Manager->modify('add', null, $this->_user, array(
'Manager' => $workerArray
));
}

//get account info for start and end times
$account = $this->_getAccountInfo();

//set start/end time based on configs or default
$startTime = $account['Config']['business_start_time']
? date('H:i:s', strtotime($account['Config']['business_start_time']))
: '8:00:00';
$endTime = $account['Config']['business_end_time']
? date('H:i:s', strtotime($account['Config']['business_end_time']))
: '22:00:00';

//loop through all days and set start/end time
$days = array('sun', 'mon', 'tue','wed','thu','fri','sat');
foreach ($days as $day) {
$data[$day.'_start'] = $startTime;
$data[$day.'_end'] = $endTime;
}
$data['user_id'] = $this->id;

//load UserAvailability class and save new data
$UserAvailability = ClassRegistry::init('UserAvailability');
$UserAvailability->modify('add', null, $this->_user, array(
'UserAvailability' => $data
));
}

$accountModel = ClassRegistry::init('Account');
$account = $accountModel->find('first', array('conditions' => array('Account.id' => $this->_user['account_id'])));
$key = 'images/' . str_replace('s3-', '', $account['Account']['business_logo_name']);
$image = 'https://s3-us-west-2.amazonaws.com/sst-account-logos/'.$key;
$accountData['account'] = $account['Account'];

$siteConfigModel = ClassRegistry::init('SiteConfig');
$siteConfigs = $siteConfigModel->find('first', array('conditions' => array('SiteConfig.id' => 1)));

//set flash and redirect array based on save
$this->setResponseVariables($result, 'add');
return $result;
}
}


I have searched both /app and /lib using "UserAccount", "user_accounts" and similar variations of the other extra data sets and found nothing that explains how this extra data is added to $this->Auth->user.



So, I have two questions: 1) How does the standard form login include the extra data, and 2) Why doesn't logging in with the $user data set the same data?



Another Edit:



I found the following in BaseAuthenticate's _findUser function:



$result = ClassRegistry::init($userModel)->find('first', array(
'conditions' => $conditions,
'recursive' => $this->settings['recursive'],
'fields' => $userFields,
'contain' => $this->settings['contain'],
));


So now the question is how do I set $this->settings['recursive'] and $this->settings['contain'] from my controller?







authentication cakephp-2.0 cakephp-2.3






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 15:48







Jay Rennemeyer

















asked Nov 16 '18 at 23:28









Jay RennemeyerJay Rennemeyer

33




33













  • My first guess would be that the standard one is reading the user record with "recursive" set at the default 1, so it pulls in one level of associated records.

    – Greg Schmidt
    Nov 16 '18 at 23:45











  • Can you post the content of Model/User.php. It is likely there are some model associations defined there.

    – savedario
    Nov 18 '18 at 0:52











  • @GregSchmidt: That could be but where does that happen? I've searched everything trying to find where $this->Auth->user gets set. I've also tried to add the extra data right after $this->Auth->login($user) but it won't let me write to write to $this->Auth->user.

    – Jay Rennemeyer
    Nov 19 '18 at 15:35











  • @savedario Done.

    – Jay Rennemeyer
    Nov 19 '18 at 15:36



















  • My first guess would be that the standard one is reading the user record with "recursive" set at the default 1, so it pulls in one level of associated records.

    – Greg Schmidt
    Nov 16 '18 at 23:45











  • Can you post the content of Model/User.php. It is likely there are some model associations defined there.

    – savedario
    Nov 18 '18 at 0:52











  • @GregSchmidt: That could be but where does that happen? I've searched everything trying to find where $this->Auth->user gets set. I've also tried to add the extra data right after $this->Auth->login($user) but it won't let me write to write to $this->Auth->user.

    – Jay Rennemeyer
    Nov 19 '18 at 15:35











  • @savedario Done.

    – Jay Rennemeyer
    Nov 19 '18 at 15:36

















My first guess would be that the standard one is reading the user record with "recursive" set at the default 1, so it pulls in one level of associated records.

– Greg Schmidt
Nov 16 '18 at 23:45





My first guess would be that the standard one is reading the user record with "recursive" set at the default 1, so it pulls in one level of associated records.

– Greg Schmidt
Nov 16 '18 at 23:45













Can you post the content of Model/User.php. It is likely there are some model associations defined there.

– savedario
Nov 18 '18 at 0:52





Can you post the content of Model/User.php. It is likely there are some model associations defined there.

– savedario
Nov 18 '18 at 0:52













@GregSchmidt: That could be but where does that happen? I've searched everything trying to find where $this->Auth->user gets set. I've also tried to add the extra data right after $this->Auth->login($user) but it won't let me write to write to $this->Auth->user.

– Jay Rennemeyer
Nov 19 '18 at 15:35





@GregSchmidt: That could be but where does that happen? I've searched everything trying to find where $this->Auth->user gets set. I've also tried to add the extra data right after $this->Auth->login($user) but it won't let me write to write to $this->Auth->user.

– Jay Rennemeyer
Nov 19 '18 at 15:35













@savedario Done.

– Jay Rennemeyer
Nov 19 '18 at 15:36





@savedario Done.

– Jay Rennemeyer
Nov 19 '18 at 15:36












1 Answer
1






active

oldest

votes


















1














You User Model is linked with a number of other Models via Associations CakePHP Book.



Unless it is requested otherwise, CakePHP will automatically read associated rows from the other models when using Users->find(...). It is visible in the data you posted (User.account_id = Account.id).



In your Controller:



$user = $this->User->find('first',array('recursive'=>-1,...)); 


will only read the row from User model.



$user = $this->User->find('first',array('recursive'=>1,...));


will read the row from User AND all the Models directly associated with it.



The AuthComponent is configured (mainly) in AppController where you should find something like:



public $components = array(
'Auth' => array(
'authenticate' => array(
'Basic' => array('userModel' => 'User'),
'Form' => array('userModel' => 'User'),
...
),
'authorize' => array('Controller'),
'flash' => array('element' => 'popup_error', 'key' => 'flash', 'params' => array()),
...
),
)





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53346685%2fcakephp-2-8-code-has-extra-data-in-this-auth-user-how%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You User Model is linked with a number of other Models via Associations CakePHP Book.



    Unless it is requested otherwise, CakePHP will automatically read associated rows from the other models when using Users->find(...). It is visible in the data you posted (User.account_id = Account.id).



    In your Controller:



    $user = $this->User->find('first',array('recursive'=>-1,...)); 


    will only read the row from User model.



    $user = $this->User->find('first',array('recursive'=>1,...));


    will read the row from User AND all the Models directly associated with it.



    The AuthComponent is configured (mainly) in AppController where you should find something like:



    public $components = array(
    'Auth' => array(
    'authenticate' => array(
    'Basic' => array('userModel' => 'User'),
    'Form' => array('userModel' => 'User'),
    ...
    ),
    'authorize' => array('Controller'),
    'flash' => array('element' => 'popup_error', 'key' => 'flash', 'params' => array()),
    ...
    ),
    )





    share|improve this answer




























      1














      You User Model is linked with a number of other Models via Associations CakePHP Book.



      Unless it is requested otherwise, CakePHP will automatically read associated rows from the other models when using Users->find(...). It is visible in the data you posted (User.account_id = Account.id).



      In your Controller:



      $user = $this->User->find('first',array('recursive'=>-1,...)); 


      will only read the row from User model.



      $user = $this->User->find('first',array('recursive'=>1,...));


      will read the row from User AND all the Models directly associated with it.



      The AuthComponent is configured (mainly) in AppController where you should find something like:



      public $components = array(
      'Auth' => array(
      'authenticate' => array(
      'Basic' => array('userModel' => 'User'),
      'Form' => array('userModel' => 'User'),
      ...
      ),
      'authorize' => array('Controller'),
      'flash' => array('element' => 'popup_error', 'key' => 'flash', 'params' => array()),
      ...
      ),
      )





      share|improve this answer


























        1












        1








        1







        You User Model is linked with a number of other Models via Associations CakePHP Book.



        Unless it is requested otherwise, CakePHP will automatically read associated rows from the other models when using Users->find(...). It is visible in the data you posted (User.account_id = Account.id).



        In your Controller:



        $user = $this->User->find('first',array('recursive'=>-1,...)); 


        will only read the row from User model.



        $user = $this->User->find('first',array('recursive'=>1,...));


        will read the row from User AND all the Models directly associated with it.



        The AuthComponent is configured (mainly) in AppController where you should find something like:



        public $components = array(
        'Auth' => array(
        'authenticate' => array(
        'Basic' => array('userModel' => 'User'),
        'Form' => array('userModel' => 'User'),
        ...
        ),
        'authorize' => array('Controller'),
        'flash' => array('element' => 'popup_error', 'key' => 'flash', 'params' => array()),
        ...
        ),
        )





        share|improve this answer













        You User Model is linked with a number of other Models via Associations CakePHP Book.



        Unless it is requested otherwise, CakePHP will automatically read associated rows from the other models when using Users->find(...). It is visible in the data you posted (User.account_id = Account.id).



        In your Controller:



        $user = $this->User->find('first',array('recursive'=>-1,...)); 


        will only read the row from User model.



        $user = $this->User->find('first',array('recursive'=>1,...));


        will read the row from User AND all the Models directly associated with it.



        The AuthComponent is configured (mainly) in AppController where you should find something like:



        public $components = array(
        'Auth' => array(
        'authenticate' => array(
        'Basic' => array('userModel' => 'User'),
        'Form' => array('userModel' => 'User'),
        ...
        ),
        'authorize' => array('Controller'),
        'flash' => array('element' => 'popup_error', 'key' => 'flash', 'params' => array()),
        ...
        ),
        )






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 8:13









        savedariosavedario

        7371222




        7371222






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53346685%2fcakephp-2-8-code-has-extra-data-in-this-auth-user-how%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)