cakephp 3编辑用户用户名和密码-找不到当前实体的表类

cakephp 3编辑用户用户名和密码-找不到当前实体的表类,php,cakephp,Php,Cakephp,我正在使用CakePHP3.5(今天运行了更新)制作我的第一个非教程web应用程序。我正在尝试设置我的应用程序,以便编辑我的用户帐户详细信息(用户名和密码)。 我不确定我做了什么,但我无法在登录时实际访问我的edit.ctp(视图)。我一直收到一个“找不到当前实体的表类”错误 我的最终目标是让用户能够编辑他们的用户名(这是一个电子邮件地址)并在他们愿意的时候更改密码 有人能帮我找出哪里出了问题,为什么我总是遇到“找不到当前实体的表类”错误,以及我能做些什么来修复它。 我已经阅读了尽可能多的相关文

我正在使用CakePHP3.5(今天运行了更新)制作我的第一个非教程web应用程序。我正在尝试设置我的应用程序,以便编辑我的用户帐户详细信息(用户名和密码)。 我不确定我做了什么,但我无法在登录时实际访问我的edit.ctp(视图)。我一直收到一个“找不到当前实体的表类”错误

我的最终目标是让用户能够编辑他们的用户名(这是一个电子邮件地址)并在他们愿意的时候更改密码

有人能帮我找出哪里出了问题,为什么我总是遇到“找不到当前实体的表类”错误,以及我能做些什么来修复它。 我已经阅读了尽可能多的相关文章。我尝试过各种“Isauthorized”函数版本,我不断地得到相同的错误,所以我确信这是我不知道要寻找的东西

下面是我的代码:

The User.php file:

<?php
namespace App\Model\Entity;

use Cake\Auth\DefaultPasswordHasher;
use Cake\ORM\Entity;

/**
 * User Entity.
 *
 * @property int $id
 * @property string $username
 * @property string $password
 * @property string $role
 * @property \Cake\I18n\Time $created
 * @property \Cake\I18n\Time $activity_date
 * @property bool $terms_of_service
 * @property bool $paid
 * @property \Cake\I18n\Time $paid_date
 * @property \Cake\I18n\Time $paid_until
 * @property bool $verified
 * @property \App\Model\Entity\Agent[] $agents
 */
class User extends Entity
{

    /**
     * Fields that can be mass assigned using newEntity() or patchEntity().
     *
     * Note that when '*' is set to true, this allows all unspecified fields to
     * be mass assigned. For security purposes, it is advised to set '*' to false
     * (or remove it), and explicitly make individual fields accessible as needed.
     *
     * @var array
     */
    protected $_accessible = [
        '*' => true,
        'id' => false,
    ];
    protected function _setPassword($password)
    {
        return (new DefaultPasswordHasher)->hash($password);
    }
}

The UsersTable.php file:
<?php 
namespace App\Model\Table;

use App\Model\Entity\User;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;

/**
 * Users Model
 *
 * @property \Cake\ORM\Association\HasMany $Agents
 */
class UsersTable extends Table
{
    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->table('users');
        $this->displayField('username');
        $this->primaryKey('id');

        $this->addBehavior('Timestamp');

        $this->hasMany('Agents', [
            'foreignKey' => 'user_id'
        ]);
    }

    /**
     * Default validation rules.
     *
     * @param \Cake\Validation\Validator $validator Validator instance.
     * @return \Cake\Validation\Validator
     */
    public function validationDefault(Validator $validator)
    {
         $validator
            ->notEmpty('username','Please supply your email address')
            ->add('username', 'valid', ['rule'=>'email']);

        $validator
            ->notEmpty('password','Please supply your password');

        $validator
            ->notEmpty('role','Please select your account type')
            ->add('role','inList',[
                'rule' => ['inList',['agent','buyer']],
                'message' => 'Please select a valid role'
            ]);

        $validator
            ->add('terms_of_service', 'valid', ['rule' => 'boolean'])
            ->notEmpty('terms_of_service','You must agree to the terms of service to register');

        return $validator;
    }
    public function isOwnedBy($userId)
    {
        return $this->exists(['id' => $userId]);
    }
    /**
     * Returns a rules checker object that will be used for validating
     * application integrity.
     *
     * @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
     * @return \Cake\ORM\RulesChecker
     */
    public function buildRules(RulesChecker $rules)
    {
        $rules->add($rules->isUnique(['username']));
        return $rules;
    }
}

The relevant pieces UsersController.php file:

<?php
namespace App\Controller;

use App\Controller\AppController;
use Cake\ORM\TableRegistry;
use Cake\Event\Event;
//use Cake\ORM\Entity;

/**
 * Users Controller
 *
 * @property \App\Model\Table\UsersTable $Users
 */
class UsersController extends AppController
{
    public function beforeFilter(Event $event)
    {
        parent::beforeFilter($event);
        $this->Auth->allow(['signup','logout']);
        $this->Auth->deny(['index']);

    }
    // The owner of an user can edit and delete it
    public function isAuthorized($user)
    {
        if (in_array($this->request->action, ['edit', 'delete','view'])) {
            if ($this->Users->isOwnedBy($user['id'])) {
                return true;
            }
        }
        return parent::isAuthorized($user);
    }

    /**
     * Edit method
     *
     * @param string|null $id User id.
     * @return void Redirects on successful edit, renders view otherwise.
     * @throws \Cake\Network\Exception\NotFoundException When record not found.
     */
    public function edit($id = null)
    {
        $id = $this->Auth->user('id');
        $user = $this->Users->get($id, [
            'contain' => []
        ]); 
        if ($this->request->is(['patch', 'post', 'put'])) {
            $user = $this->Users->patchEntity($user, $this->request->data);
            if ($this->Users->save($user)) {
                $this->Flash->success(__('Your account has been edited.'));
                return $this->redirect(['controller','Users','action' => 'edit']);
            } else {
                $this->Flash->error(__('The user could not be saved. Please, try again.'));
            }
        }
        $this->set(compact('user'));
        $this->set('_serialize', ['user']);
    }

The AppController.php file

<?php
/**
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link      http://cakephp.org CakePHP(tm) Project
 * @since     0.2.9
 * @license   http://www.opensource.org/licenses/mit-license.php MIT License
 */
namespace App\Controller;

use Cake\Controller\Controller;
use Cake\Event\Event;

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @link http://book.cakephp.org/3.0/en/controllers.html#the-app-controller
 */
class AppController extends Controller
{
    /**
     * Initialization hook method.
     *
     * Use this method to add common initialization code like loading components.
     *
     * e.g. `$this->loadComponent('Security');`
     *
     * @return void
     */
    public function initialize()
    {
        parent::initialize();

        $this->loadComponent('RequestHandler');
        $this->loadComponent('Flash');
        $this->loadComponent('Auth', [
            /*'authenticate' => [
                'Form' => [
                    'fields' => ['username' => 'email', 'password' => 'password']
                ]
            ],*/
            'authorize' => 'Controller',
            'loginRedirect' => [
                'controller' => 'Properties',
                'action' => 'myproperties'
            ],
            'logoutRedirect' => [
                'controller' => '',
                'action' => 'index'
            ],
            'unauthorizedRedirect' => $this->referer(),
            //'authError' => 'You must be logged in to view that page.',
        ]);
        // Allow the display action so our pages controller continues to work
        $this->Auth->allow(['display']);
    }
    public function beforeFilter(Event $event)
    {
        $this->Auth->allow(['index', 'view','logout','search']);
    }
    /**
     * Before render callback.
     *
     * @param \Cake\Event\Event $event The beforeRender event.
     * @return void
     */
    public function beforeRender(Event $event)
    {
        if (!array_key_exists('_serialize', $this->viewVars) &&
            in_array($this->response->type(), ['application/json', 'application/xml'])
        ) {
            $this->set('_serialize', true);
        }
        $this -> set('user', $this -> Auth -> user());
    }

    public function isAuthorized($user) {
    $childClass = get_called_class();

    if(method_exists($childClass, '_isAuthorized'))
        return $childClass::_isAuthorized($user, $this -> request);

    return static::_isAuthorized($user, $request);
    }
    static public function _isAuthorized($user, $request)
    {

        // Admin can access every action
        if (isset($user['role']) && $user['role'] === 'admin') {
            return true;
        }       
        // Default deny
        return false;
    }
}
User.php文件:
设置(“U序列化”,真);
}
$this->set('user',$this->Auth->user());
}
公共功能已授权($user){
$childClass=get_调用_class();
如果(方法_存在($childClass,_已授权)))
返回$childClass::_已授权($user,$this->request);
返回静态::_已授权($user,$request);
}
静态公共函数\u已授权($user,$request)
{
//管理员可以访问每个操作
如果(isset($user['role'])&&$user['role']=='admin'){
返回true;
}       
//默认拒绝
返回false;
}
}

好的,经过反复试验,我已经解决了这个问题。这是一个简单的修复,虽然我不知道为什么它工作。。。因此,如果有人能解释一下,那就太好了:我将控制器文件编辑功能中的
$user
变量更改为
$users
,将edit.ctp文件中的
$user
变量更改为
$users
,嘿,它成功了。

问题是,在
beforeRender
方法您可以使用此方法

    $this -> set('user', $this -> Auth -> user());
这意味着您将与所有控制器和操作共享记录的当前用户

还请注意,在用户控制器中的编辑操作中,您还调用了变量
$user
,并通过
集将其发送到视图中


这就是问题的根源,这就是为什么如果您通过
用户更改名称
它会起作用!!在用户控制器中的编辑操作中发送的
$user
方法与
$user
方法之间似乎存在冲突,David,您使用这种方式授权的是什么原因?如果您的孩子有一个未授权的,它应该称之为。你不必让你的应用程序控制器调用你的子类IsAuthorized函数。@chrishick No。。。在我试图解决我的问题时,我看到的一个例子就是以这种方式使用它。我以前的isAuthorized看起来是这样的:
公共函数isAuthorized($user){if(isset($user['role'])&&$user['role']=='admin'){return true;}return false;}
你也可以尝试一些简单的方法,比如管理基于角色的访问。那么就不需要在控制器中进行黑客攻击了。谢谢@mark,我将尝试一下。但是在您的示例中,看起来您还需要一些基于行的访问。那就行不通了。哦,我也有类似的问题,这就是原因。非常感谢。