When I login to my site it gives me this exception and I don’t know why that’s happening.
How can I solve that issue?
This is the code for User.php:
<?php
declare(strict_types=1);
namespace App\Model\Entity;
use Cake\ORM\Entity;
use Cake\ORM\TableRegistry;
/**
* User Entity
*
* @property int $id
* @property string $username
* @property string $password
* @property string|null $token
* @property string $email
* @property int $group_id
* @property int|null $enterprise_id
* @property \Cake\I18n\FrozenTime $created
* @property \Cake\I18n\FrozenTime $modified
*
* @property \Acl\Model\Entity\Aro[] $aro
* @property \App\Model\Entity\Group $group
* @property \App\Model\Entity\Enterprise $enterprise
* @property \App\Model\Entity\Event[] $events
* @property \App\Model\Entity\Observation[] $observations
*/
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 = [
'username' => true,
'password' => true,
'token' => true,
'email' => true,
'group_id' => true,
'enterprise_id' => true,
'created' => true,
'modified' => true,
'aro' => true,
'group' => true,
'enterprise' => true,
'events' => true,
'observations' => true,
];
/**
* Fields that are excluded from JSON versions of the entity.
*
* @var array
*/
protected $_hidden = [
'password',
'token',
];
public function parentNode()
{
if (!$this->id) {
return null;
}
if (isset($this->group_id)) {
$groupId = $this->group_id;
} else {
$Users = TableRegistry::get('Users');
$user = $Users->find('all', ['fields' => ['group_id']])->where(['id' => $this->id])->first();
$groupId = $user->group_id;
}
if (!$groupId) {
return null;
}
return ['Groups' => ['id' => $groupId]];
}
}
And this is the code for UsersTable.php:
<?php
declare(strict_types=1);
namespace App\Model\Table;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
use Cake\Auth\DefaultPasswordHasher;
/**
* Users Model
*
* @property \App\Model\Table\GroupsTable&\Cake\ORM\Association\BelongsTo $Groups
* @property \App\Model\Table\EnterprisesTable&\Cake\ORM\Association\BelongsTo $Enterprises
* @property \App\Model\Table\EventsTable&\Cake\ORM\Association\HasMany $Events
* @property \App\Model\Table\ObservationsTable&\Cake\ORM\Association\HasMany $Observations
*
* @method \App\Model\Entity\User newEmptyEntity()
* @method \App\Model\Entity\User newEntity(array $data, array $options = [])
* @method \App\Model\Entity\User[] newEntities(array $data, array $options = [])
* @method \App\Model\Entity\User get($primaryKey, $options = [])
* @method \App\Model\Entity\User findOrCreate($search, ?callable $callback = null, $options = [])
* @method \App\Model\Entity\User patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
* @method \App\Model\Entity\User[] patchEntities(iterable $entities, array $data, array $options = [])
* @method \App\Model\Entity\User|false save(\Cake\Datasource\EntityInterface $entity, $options = [])
* @method \App\Model\Entity\User saveOrFail(\Cake\Datasource\EntityInterface $entity, $options = [])
* @method \App\Model\Entity\User[]|\Cake\Datasource\ResultSetInterface|false saveMany(iterable $entities, $options = [])
* @method \App\Model\Entity\User[]|\Cake\Datasource\ResultSetInterface saveManyOrFail(iterable $entities, $options = [])
* @method \App\Model\Entity\User[]|\Cake\Datasource\ResultSetInterface|false deleteMany(iterable $entities, $options = [])
* @method \App\Model\Entity\User[]|\Cake\Datasource\ResultSetInterface deleteManyOrFail(iterable $entities, $options = [])
*
* @mixin \Cake\ORM\Behavior\TimestampBehavior
*/
class UsersTable extends Table
{
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config): void
{
parent::initialize($config);
$this->setTable('users');
$this->setDisplayField('username');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp');
$this->addBehavior('Acl.Acl', ['type' => 'requester', 'enabled' => false]);
$this->belongsTo('Groups', [
'foreignKey' => 'group_id',
'joinType' => 'INNER',
]);
$this->belongsTo('Enterprises', [
'foreignKey' => 'enterprise_id',
]);
$this->hasMany('Events', [
'foreignKey' => 'user_id',
]);
$this->hasMany('Observations', [
'foreignKey' => 'user_id',
]);
}
/**
* Default validation rules.
*
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator): Validator
{
$validator
->integer('id')
->allowEmptyString('id', null, 'create');
$validator
->scalar('username')
->maxLength('username', 255)
->requirePresence('username', 'create')
->notEmptyString('username')
->add('username', 'unique', ['rule' => 'validateUnique', 'provider' => 'table']);
$validator
->scalar('password')
->maxLength('password', 60)
->requirePresence('password', 'create')
->notEmptyString('password');
$validator
->scalar('token')
->maxLength('token', 200)
->allowEmptyString('token');
$validator
->email('email')
->requirePresence('email', 'create')
->notEmptyString('email');
return $validator;
}
/**
* 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): RulesChecker
{
$rules->add($rules->isUnique(['username']), ['errorField' => 'username']);
$rules->add($rules->isUnique(['email']), ['errorField' => 'email']);
$rules->add($rules->existsIn(['group_id'], 'Groups'), ['errorField' => 'group_id']);
$rules->add($rules->existsIn(['enterprise_id'], 'Enterprises'), ['errorField' => 'enterprise_id']);
return $rules;
}
public function beforeSave(\Cake\Event\Event $event, \Cake\ORM\Entity $entity, \ArrayObject $options)
{
$hasher = new DefaultPasswordHasher;
$entity->password = $hasher->hash($entity->password);
return true;
}
public function bindNode($user) {
return ['model' => 'Group', 'foreign_key' => $user->group_id];
}
}