my entity file is not being included in the cakephp. Can someone help me? - cakephp

I was trying to hash the password but it turns out that my entity file is not being included in the cakephp. Can someone help me why it is happening ???
When i run debug(get_included_file()); it does not show my User.php entity file.
and my User.php file content is :
<?php
declare(strict_types=1);
namespace App\Model\Entity;
use Cake\ORM\Entity;
use Cake\Auth\DefaultPasswordHasher;
class User extends Entity
{
protected $_accessible = [
'email' => true,
'password' => true,
'created' => true,
'modified' => true,
];
protected $_hidden = [
'password',
];
public function _setPassword($password) : ?string {
if(strlen($password) > 0 ){
return (new DefaultPasswordHasher())->hash($password);
}
}
}

Related

Cakephp authentication plugin how can I implement login in admin prefix?

I'm trying to implement cakephp authentication plugin in admin prefix
Route I have written for admin prefix
$routes->prefix('admin', function (RouteBuilder $routes) {
$routes->connect('/',['controller'=>'AdminUsers','action'=>'login']);
$routes->fallbacks(DashedRoute::class);
});
In application.php , I have followed everything That mentioned in authentication documentation
public function getAuthenticationService(ServerRequestInterface $request): AuthenticationServiceInterface
{
$authenticationService = new AuthenticationService([
'unauthenticatedRedirect' => Router::url('/admin'),
'queryParam' => 'redirect',
]);
// Load identifiers, ensure we check email and password fields
$authenticationService->loadIdentifier('Authentication.Password', [
'fields' => [
'username' => 'email',
'password' => 'password',
]
]);
// Load the authenticators, you want session first
$authenticationService->loadAuthenticator('Authentication.Session');
// Configure form data check to pick email and password
$authenticationService->loadAuthenticator('Authentication.Form', [
'fields' => [
'username' => 'email',
'password' => 'password',
],
'userModel' => 'AdminUsers',
'loginUrl' => Router::url('/admin'),
]);
return $authenticationService;
}
I have changed users model to AdminUsers for database table admin_users
Now in admin/appController.php
I have loadComponent in initialize method
$this->loadComponent('Authentication.Authentication');
In before filter method I have added
$this->Authentication->allowUnauthenticated(['login']);
Now after submit login form I am getting error
Table class for alias Users could not be found.
In getAuthenticationService method I have changed model Users to AdminUsers. Why it's going for Users model rather then AdminUsers model ?
My Table/AdminUsersTable.php table class look likes
<?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;
class AdminUsersTable extends Table
{
public function initialize(array $config): void
{
parent::initialize($config);
$this->setTable('admin_users');
--------
After ndm comment I am able to change default model in application.php , But how can I change it in Admin/AppController.php ? Code that I have tried.
public function beforeFilter(EventInterface $event)
{
$service = new AuthenticationService();
$service->loadIdentifier('Authentication.Password', [
'resolver' => [
'className' => 'Authentication.Orm',
'userModel' => 'AdminUsers',
],
]);
$this->Authentication->allowUnauthenticated(['login','signup']);
$this->viewBuilder()->setLayout('admin');
}

Cakephp 3.7.4 Auth hash password Not working

I am trying to implement auth login and register cakephp 3.7.4
I have used following code for UsersController add method
public function add()
{
$this->viewBuilder()->setLayout('login');
$user = $this->Users->newEntity();
if ($this->request->is('post')) {
$post = $this->request->getData();
$post['created'] = date('Y-m-d H:i:s');
$post['modified'] = date('Y-m-d H:i:s');
$user = $this->Users->patchEntity($user, $post);
if ($this->Users->save($user)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['action' => 'login']);
}
$this->Flash->error(__('Unable to add the user.'));
}
$this->set('user', $user);
}
But it can't save password in Hash Format
I have also create entity and used this function but it also not helped me
class User extends Entity
{
protected $_accessible = [
'email' => true,
'password' => true
];
protected $_hidden = [
'password'
];
protected function _setPassword($password){
return(new DefaultPasswordHasher)->hash($password);
}
}
I installed a fresh Cake 3.7.4 installation, baked a users table, users controller and users table/entity. The following code for the User Class successfully hashed the password before it is written to the database. Please compare it with your code.
namespace App\Model\Entity;
use Cake\Auth\DefaultPasswordHasher;
use Cake\ORM\Entity;
class User extends Entity
{
protected $_accessible = [
'email' => true,
'password' => true,
'modified' => true,
'created' => true
];
protected $_hidden = [
'password'
];
protected function _setPassword($password)
{
return (new DefaultPasswordHasher())->hash($password);
}
}
Just use parenthesis around DefaultPasswordHaser():
return(new DefaultPasswordHasher())->hash($password);

Why a virtual property getter should not be called in cakePHP 3?

in a ctp, I display several virtual properties from 2 entities, computed by getters.
It's ok for one of the both entities but for the other, none of the properties getters are never called.
Here is an excerpt of the ctp:
<table>
....
<td><?= $agTheme->agthemelanguages_wc_string ?></td>
<td><?= $agPoi->paragraph_length_string ?></td>
<td><?= $agPoi->images_string ?></td>
<td><?= $agPoi->audios_string ?></td>
....
</table>
It's ok for AgTheme's property but no getter of the Agpoi's properties is called.
Agtheme model is:
<?php
namespace App\Model\Entity;
use Cake\ORM\Entity;
use Cake\Collection\Collection;
use Cake\Core\Configure;
use Cake\Log\Log;
use Cake\ORM\TableRegistry;
class Agtheme extends Entity {
protected $_accessible = [
'site_id' => true,
'site' => true,
'name' => true,
'directory_name' => true,
'comment' => true,
'image_0' => true,
'imgpathname0' => true,
'imgfile0' => true,
'mobile_theme' => true,
'agthemelanguages' => true,
'poi_by_tag' => true,
'poi_to_map' => true,
'unlock_mode' => true,
'unlock_code' => true,
'published' => true,
'approved' => true,
];
protected function _getAgthemelanguagesWcString() {
if (isset($this->_properties['agthemelanguages_wc_string'])) {
return $this->_properties['agthemelanguages_wc_string'];
}
if (empty($this->agthemelanguages)) {
return '';
}
$agthemelanguages = new Collection($this->agthemelanguages);
$str = $agthemelanguages->reduce(function ($string, $agthemelanguage) {
return $string . $agthemelanguage->language->name_fr . ' :<br/>';
}, '');
return trim($str, '<br/>');
}
function _getImgpathname0() {
if (isset($this->_properties['imgpathname0'])) {
return $this->_properties['imgpathname0'];
}
if (!isset($this->_properties['id']) || empty($this->image_0) || !isset($this->_properties['directory_name'])) {
return '';
}
$img_path = SITES_CONTENT_URL . $this->_properties['directory_name'] .'/imgTheme_0.' . $this->_properties['image_0'];
return $img_path;
}
}
And Agpoi model is (Agpoi.php):
<?php
namespace App\Model\Entity;
use Cake\ORM\Entity;
use Cake\Collection\Collection;
class Agpoi extends Entity
{
protected $_accessible = [
'name' => true,
'latitude' => true,
'longitude' => true,
'bearing' => true,
'preload' => true,
'agtheme' => true,
'agpoiaudios' => true,
'agpoiimages' => true,
'agpoitextes' => true,
'agpoiwaypoints' => true,
];
protected function _getImagesString()
{
if (isset($this->_properties['images_string'])) {
return $this->_properties['images_string'];
}
if (empty($this->agpoiimages)) {
return '';
}
$agpoiimages = new Collection($this->agpoiimages);
$str = $agpoiimages->reduce(function ($string, $agpoiimage)
{
return $string . $agpoiimage->image . ', ';
}, '');
return trim($str, ', ');
}
protected function _getAudiosString()
{
if (isset($this->_properties['audios_string'])) {
return $this->_properties['audios_string'];
}
if (empty($this->agpoiaudios)) {
return '';
}
$agpoiaudios = new Collection($this->agpoiaudios);
$str = $agpoiaudios->reduce(function ($string, $agpoiaudio)
{
return $string . $agpoiaudio->filename . ', ';
}, '');
return trim($str, ', ');
}
protected function _getParagraphLengthString() {
if (isset($this->_properties['paragraph_length_string'])) {
return $this->_properties['paragraph_length_string'];
}
if (empty($this->agpoitextes)) {
return '';
}
$agpoitextes = new Collection($this->agpoitextes);
$str = $agpoitextes->reduce(function ($string, $agpoitexte) {
return $string . str_word_cound($agpoitexte->paragraph) . __(" mots<br/>");
}, '');
return trim($str, '<br/>');
}
}
Unfortunately, I doubt that the problem could come from the code above, I better think about of a stupid mistake somewhere else but I really wonder what could cause such problem and here is my question:
Could anyone tell me what allow cake to look for a getter?
What could prevent cake to call a getter??
I guess the answer is not obvious for the last question but any hint would be appreciated...
EDIT
#ndm
debug(get_class($agPoi));
'Cake\ORM\Entity'
whereas
debug(get_class($agTheme));
'App\Model\Entity\Agtheme'
so obviously Agpoi is not well defined but I don't see why...
EDIT 2
Table is (AgpoisTable.php):
<?php
namespace App\Model\Table;
use Cake\ORM\Query;
use Cake\ORM\Table;
use Cake\Validation\Validator;
/**
* Agpois Model
*/
class AgpoisTable extends Table {
/**
* Initialize method
*
* #param array $config The configuration for the Table.
* #return void
*/
public function initialize(array $config) {
$this->table('agpois');
$this->displayField('id');
$this->primaryKey('id');
$this->belongsToMany('Agthemes', [
'foreignKey' => 'agpoi_id',
'targetForeignKey' => 'agtheme_id',
'joinTable' => 'agthemes_agpois',
'dependent' => true,
]);
$this->hasMany('Agpoiaudios', [
'dependent' => true,
'foreignKey' => 'agpoi_id',
]);
$this->hasMany('Agpoiimages', [
'dependent' => true,
'foreignKey' => 'agpoi_id',
]);
$this->hasMany('Agpoitextes', [
'dependent' => true,
'foreignKey' => 'agpoi_id',
]);
$this->hasMany('Agpoiwaypoints', [
'dependent' => true,
'foreignKey' => 'agpoi_id',
]);
}
public function validationDefault(Validator $validator) {
$validator
....
return $validator;
}
}
I also made a search for 'agpoi' everywhere in case.... no unexpected other agpoi classname or something like that.
debug($this->Agpois);
object(App\Model\Table\AgpoisTable) {
'registryAlias' => 'Agpois',
'table' => 'agpois',
'alias' => 'Agpois',
'entityClass' => '\Cake\ORM\Entity',
'associations' => [
(int) 0 => 'agthemes',
(int) 1 => 'agpoiaudios',
(int) 2 => 'agpoiimages',
(int) 3 => 'agpoitextes',
(int) 4 => 'agpoiwaypoints',
(int) 5 => 'agthemesagpois'
],
'behaviors' => [],
'defaultConnection' => 'default',
'connectionName' => 'default'
}
so always the problem of entityClass of course.
The problem here seems to be name of the table class, respetively the name of the entity class, probably depends on your perspective.
By default, the name of the corresponding entity class is guessed by using the singular table class name without the trailing Table, however Agpois doesn't inflect to Agpoi as you might have assumed, but just Agpois, and so it's looking for the class App\Model\Entity\Agpois, which doesn't exist, hence the fallback to the default entity base class.
I'm not sure what Agpois means, and which language it stems from, but it's certainly not english, and thus such problems aren't unexpected, as the Inflector is ment to handle english words only.
tl;dr
If you must use the name Apgois, then you'll have to either define the name of the entity class manually in your tables initialize() method:
public function initialize(array $config)
{
// ...
$this->entityClass('Apgoi');
// ...
}
or configure the inflector so that it can handle that word:
Inflector::rules('irregular', ['apgoi' => 'apgois']);
or even rename the entity class to Apgois if that is applicable and doesn't cause any naming conflicts.
See also
Cookbook > ORM > Table Objects > Customizing the Entity Class a Table Uses
Cookbook > Inflector > Loading Custom Inflections

Can't submit form using create();

So i have this method inside of my:
JobsController.ctp:
<?php
namespace App\Controller;
use App\Controller\AppController;
use Cake\ORM\TableRegistry;
/**
* Jobs Controller
*
* #property \App\Model\Table\JobsTable $Jobs
*/
class JobsController extends AppController
{
public $name = 'Jobs';
public function add()
{
//Some Vars assigning skipped, var job is empty
$this->set('job','Job');
$this->Job->create();
}
}
And I have this view with the form itself:
add.ctp:
<?= $this->Form->create($job); ?>
<fieldset>
<legend><?= __('Add Job Listing'); ?></legend>
<?php
echo $this->Form->input('title');
echo $this->Form->input('company_name');
echo $this->Form->input('category_id',array(
'type' => 'select',
'options' => $categories,
'empty' => 'Select Category'
));
echo $this->Form->input('type_id',array(
'type' => 'select',
'options' => $types,
'empty' => 'Select Type'
));
echo $this->Form->input('description', array('type' => 'textarea'));
echo $this->Form->input('city');
echo $this->Form->input('contact_email');
?>
</fieldset>
<?php
echo $this->Form->button('Add');
$this->Form->end();
?>
Also this table class:
JobsTable.php
<?php
namespace App\Model\Table;
use Cake\ORM\Table;
class JobsTable extends Table
{
public function initialize(array $config)
{
$this->belongsTo('Types', [
'foreignKey' => 'type_id',
'joinType' => 'INNER',
]);
$this->belongsTo('Categories', [
'foreignKey' => 'category_id',
'joinType' => 'INNER',
]);
}
}
And when I submit it, it gives me next error:
Error: Call to a member function create() on boolean
No idea how to fix.
I also have an entity
Job.php:
<?php
namespace App\Model\Entity;
use Cake\ORM\Entity;
/**
* Job Entity.
*/
class Job extends Entity
{
/**
* Fields that can be mass assigned using newEntity() or patchEntity().
*
* #var array
*/
protected $_accessible = array(
'category_id' => true,
'user_id' => true,
'type_id' => true,
'company_name' => true,
'title' => true,
'description' => true,
'city' => true,
'contact_email' => true,
'category' => true,
'user' => true,
'type' => true,
);
}
So how do I fix this error, that appears on form submit?
Error: Call to a member function create() on boolean
I guess I need to do something with $this->set('job'); ? but I'm not sure what exactly
By convention the default, auto-loadable table for a controller is based on the controller name without the trailing Controller, so for JobsController a table class named Jobs(Table) can be autoloaded.
In case the table class cannot be loaded (for example because it doesn't exist, or because the name doesn't match the one derived from the controller name), the magic getter that handles this will return false, a boolean, and this is where you are trying to call a method on, hence the error.
create() btw doesn't exist anymore, you should have a look at the ORM migration guide, and the docs in general to get a grasp on how things now work.
So either use $this->Jobs and make sure that you have a table class named JobsTable, or override the default model to use (Controller::_setModelClass()), or load the desired table manually (TableRegistry::get() or Controller::loadModel()).
See also
Cookbook > Database Access & ORM
Cookbook > Controllers > Loading Additional Models

Include fixture from Tags plugin in app test

I'm using the excellent CakeDC Tags plugin on my Solutions model:
class Solution extends AppModel {
public $actsAs = array(
'Tags.Taggable',
'Search.Searchable',
);
}
I have a SolutionsController::search() method:
App::uses('AppController', 'Controller');
class SolutionsController extends AppController {
public $components = array(
'Paginator',
'Search.Prg',
);
public $presetVars = true; // using the model configuration ('Search' plugin)
public function search() {
$this->Prg->commonProcess();
$this->Paginator->settings['conditions'] = $this->Solution->parseCriteria($this->Prg->parsedParams());
$solutions = $this->Paginator->paginate();
if (!empty($solutions)) {
$this->Session->setFlash('Solutions found');
} else {
$this->Session->setFlash('No solutions found');
}
$this->set('solutions', $solutions);
$this->render('index');
}
I'm trying to write a test for this method:
App::uses('SolutionsController', 'Controller');
class SolutionsControllerTest extends ControllerTestCase {
public $fixtures = array(
'app.solution',
'plugin.tags.tag'
);
public function testSearchForOneResultShouldOutputText() {
$data = array('search' => 'fiery');
$result = $this->Solution->search($data);
debug($result);
$expected = array(
'id' => 3,
'name' => 'fiery-colored horse',
'shortdesc' => 'war',
'body' => 'it was granted to the one seated on it..',
'category_id' => 3,
'created_by' => 1,
'modified_by' => 1,
'created' => '2014-02-14 21:28:46',
'modified' => '2014-02-14 21:28:46'
);
$this->assertContains($expected);
}
}
I'm getting this error when running the test:
Missing Database Table
Error: Table tags for model Tag was not found in datasource test.
I've tried copying the plugin Tag fixture to my app Test/fixtures folder and including it as an app fixture. I can't get my test to run. How do I get my test to see the tags fixture from app\Plugin\tags\Test\Fixture\TagFixture.php and run?
Problem turned out to be my SolutionsFixture, which imported the table schema and the records, then also included a $records array (oops). Re-baking this fixture to import neither schema nor records resolved the error.

Resources