Cakephp auth component with two models session

后端 未结 3 852
盖世英雄少女心
盖世英雄少女心 2020-12-31 09:11

I have two cakephp2 applications running on same database, but having different Auth tables and different $this->Auth->userModel values accordingly. Authentication works wel

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-31 09:15

    Extend the Model/Datasource/Session/DatabaseSession.php session handler with something like MyDatabaseSession and overwrite the write and read methods. Maybe simply copy the existing code of both methods and add something like

    'app_id' => Configure::read('App.appId')

    to the read() conditions and do the same in the write method. And do not forget to add the field to your session database schema and to configure the session to use your handler.

    _model->find('first', array(
                    'conditions' => array(
                        'app_id' => Configure::read('App.appId'),
                        $this->_model->primaryKey => $id)));
    
            if (empty($row[$this->_model->alias]['data'])) {
                return false;
            }
    
            return $row[$this->_model->alias]['data'];
        }
    
        public function write($id, $data) {
            if (!$id) {
                return false;
            }
            $expires = time() + $this->_timeout;
            $record = compact('id', 'data', 'expires');
            $record[$this->_model->primaryKey] = $id;
            $record['app_id'] = Configure::read('App.appId');
            return $this->_model->save($record);
        }
    }
    

    I do not know your app, so were you write the app id to the config data is up to you, bootstrap or beforeFilter() maybe. You should add it before the session gets initialized I think or you'll need to re-init the session or something. I leave it up to you to look the right point up. :)

提交回复
热议问题