Authentication CakePHP2.8代码在$this->Auth->user中有额外的数据。怎样

Authentication CakePHP2.8代码在$this->Auth->user中有额外的数据。怎样,authentication,cakephp-2.0,cakephp-2.3,Authentication,Cakephp 2.0,Cakephp 2.3,我使用的是CakePHP2.8代码,我没有访问任何以前的代码编写者的权限,否则,我会问他们这方面的问题。我正在为另一个应用程序创建一个登录点,它将绕过标准表单登录,只是在后台登录。当我使用标准登录表单登录站点时,$this->Auth->user将填充额外的数据,如下所示: array( 'id' => '368', 'email' => 'jay@somewhere.com', 'first_name' => 'Jay', 'last_name

我使用的是CakePHP2.8代码,我没有访问任何以前的代码编写者的权限,否则,我会问他们这方面的问题。我正在为另一个应用程序创建一个登录点,它将绕过标准表单登录,只是在后台登录。当我使用标准登录表单登录站点时,$this->Auth->user将填充额外的数据,如下所示:

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'
    )
)
登录正常,但$this->Auth->user仅包含:

array(
    'id' => '368',
    'email' => 'jay@somewhere.com',
    'first_name' => 'Jay',
    'last_name' => 'Washere',
    'role_id' => '4',
    'disabled' => false,
    'account_id' => '149'
)
未经验证的用户模型低于savedario的要求。我删除了验证和一些不包括相关字段的模型关联

<?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;
    }
}

所以现在的问题是如何从我的控制器设置$this->settings['recursive']和$this->settings['contain']?

您的用户模型通过关联与许多其他模型链接

除非另有要求,否则CakePHP将在使用用户->查找时自动从其他模型读取关联行。。。。它在您发布的数据User.account\u id=account.id中可见

在控制器中:

$user = $this->User->find('first',array('recursive'=>-1,...)); 
将仅从用户模型中读取行

$user = $this->User->find('first',array('recursive'=>1,...));
将从用户和与其直接关联的所有模型中读取该行

AuthComponent主要位于AppController中,您可以在其中找到如下内容:

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()),
        ...
    ),
)

我的第一个猜测是,标准版读取用户记录时,递归设置为默认值1,因此它会拉入一级相关记录。很可能在那里定义了一些模型关联。@GregSchmidt:可能是这样,但在哪里发生的呢?我搜索了所有东西,试图找到$this->Auth->user的设置位置。我还试图在$this->Auth->login$user之后添加额外的数据,但它不允许我写入$this->Auth->user。@savedario完成。
$user = $this->User->find('first',array('recursive'=>1,...));
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()),
        ...
    ),
)