Error: An Internal Error Has Occurred - cakephp

I am using cakephp 3 installed on xampp. It was running fine. but now I got the error after login.
database table relation:
users hasMany roles
roles hasMany users
relational table:roles_users
AppController.php
$this->loadComponent('TinyAuth.Auth', [
'loginAction' => [
'controller' => 'Users',
'action' => 'login'
],
'loginRedirect' => [
'controller' => 'Users',
'action' => 'dashboard'
],
'authError' => 'Did you really think you are allowed to see that?',
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'username',
'password' => 'password'
],
// 'scope' => ['Users.active' => true],
'contain' => ['Roles']
]
],
]
);
I got the following error in error.log file
2017-02-02 01:04:08 Error: [Cake\Core\Exception\Exception] Missing
TinyAuth role id field (Auth.User.role_id) in user session

It looks you are using muli-role process, but you may be forgot to add multi-role enable
// in your app.php
'TinyAuth' => [
'multiRole' => true,
...
],

Related

Belongs to Many: save custom field

I need help with saving/updating belongsToMany data with custom field.
I have a tables acl, managers, groups, sections.
I need to save/update data in acl table from sections/edit or sections/add
acl table
id|target_id|target_type|section_id
I need set target_id data 'user' or 'group' depending entities.
edit.ctp
echo $this->Form->control('managers._ids', ['class' => 'multiple', 'multiple' => true]);
echo $this->Form->control('groups._ids', ['class' => 'multiple', 'multiple' => true]);
SectionsTable.php
$this->belongsToMany('Managers', [
'joinTable' => 'acl',
'through' => 'Acl',
'foreignKey' => 'section_id',
'targetForeignKey' => 'target_id',
'conditions' => [
'target_type' => 'user'
]
]);
$this->belongsToMany('Groups', [
'joinTable' => 'acl',
'through' => 'Acl',
'foreignKey' => 'section_id',
'targetForeignKey' => 'target_id',
'conditions' => [
'target_type' => 'group'
]
]);
AclTable.php
$this->belongsTo('Managers', [
'foreignKey' => 'target_id',
'conditions' => [
'target_type' => 'user'
]
]);
$this->belongsTo('Groups', [
'foreignKey' => 'target_id',
'conditions' => [
'target_type' => 'group'
]
]);
I tried to use _joinData, but nothing work
before and after patchEntity()
foreach ($section->groups as &$group) {
$group->_joinData = ['target_type' => 'group'];
}
whithout save()
foreach ($this->request->data['managers']['_ids'] as $id) {
$manager = $this->Sections->Managers->get($id);
$manager->_joinData = new Entity(['target_type' => 'user'], ['markNew' => true]);
$this->Sections->Managers->link($section, [$manager]);
}
$this->request->data has a follow structure:
[
'controller' => '*',
'action' => '*',
'title' => '*',
'managers' => [
'_ids' => [
'0' => '*',
'1' => '*',
'2' => '*',
]
],
'groups' => [
'_ids' => [
'0' => '*',
'1' => '*',
'2' => '*',
]
]
]
Alltimes error: Cannot insert row, some of the primary key values are missing. Got (3, 114, ), expecting (target_id, section_id, target_type)
Now working with following code
$section = $this->Sections->patchEntity($section, $this->request->data, ['associated' => ['Managers', 'Groups']]);
foreach ($section->managers as &$manager) {
$manager->_joinData = new Entity([
'target_id' => $manager->id,
'target_type' => 'user',
'section_id' => $section->id
], ['markNew' => true]);
}
foreach ($section->groups as &$group) {
$group->_joinData = new Entity([
'target_id' => $group->id,
'target_type' => 'group',
'section_id' => $section->id
], ['markNew' => true]);
}
if ($this->Sections->save($section, ['associated' => ['Managers', 'Groups']])) {
$this->Flash->success(__('The section has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The section could not be saved. Please, try again.'));
But, maybe can do it don't listing all fields?
Also I need to create/add new records to acl table when adding new section
But section_id unavailable until save(). Can I save/add acl records by associations
Updated
AclTAble.php
$this->setPrimaryKey(['target_type', 'target_id', 'section_id']);
.
.
.
$this->belongsTo('Managers', [
'foreignKey' => 'target_id',
'conditions' => [
'target_type' => 'user'
]
]);
$this->belongsTo('Groups', [
'foreignKey' => 'target_id',
'conditions' => [
'target_type' => 'group'
]
]);
With primaryKey id, adding and editing entities working, but editing makes dublicates, it's are not perfect for me. And in documentations use composite primaryKey, so i use ['target_id', 'target_type', 'section_id'], with it cake don't make dublicated, but aggain show me error: Cannot insert row, some of the primary key values are missing. Got (8, 197, ), expecting (target_id, section_id, target_type)

CakePHP Auth->identify() returning false

I'm playing around with CakePHP and can't seem to get the login working. It seems that $this->Auth->identify() is constantly returning false and not allowing me to login.
I've read all previous posts regarding my issue, however, none have provided me with a solution. The users I am trying to log in as have all been created using the cake password hasher, which I have checked have been stored in the database. I have checked the password field length in the database, which is set to varchar (255), I've checked the Auth => authenticate => Form => fields are set to the correct values (the login.ctp fields are also correct). I also tried changing the $this->Form->control() to $this->Form->input() as someone suggested, with no luck.
AppController:
$this->loadComponent('Auth', [
'loginRedirect' => [
'controller' => 'Users',
'action' => 'classes'
],
'logoutRedirect' => [
'controller' => 'Users',
'action' => 'index'
]
]);
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'email',
'password' => 'password'
]
]
],
'loginAction' => [
'controller' => 'Users',
'action' => 'login'
]
]);
login() function in UsersController:
public function login()
{
if ($this->request->is('post')) {
$user = $this->Auth->identify();
pj($user);
if ($user) {
$this->Auth->setUser($user);
return $this->redirect(['controller' => 'users']);
}
$this->Flash->error(__('Invalid username or password, try again'));
}
}
login.ctp:
<div class="users form">
<?= $this->Form->create() ?>
<fieldset>
<legend><?= __('Please enter your username and password') ?></legend>
<?= $this->Form->input('email') ?>
<?= $this->Form->input('password') ?>
</fieldset>
<?= $this->Form->button(__('Login')); ?>
<?= $this->Form->end() ?>
</div>
EDIT: I forgot to add that I can successfully add users, I just can't log them in.
You are loading the AuthComponent twice in AppController. The second load with the config for form fields is not loaded.
Load the component one time with the wanted config.
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'email',
'password' => 'password'
]
]
],
'loginRedirect' => [
'controller' => 'Users',
'action' => 'classes'
],
'logoutRedirect' => [
'controller' => 'Users',
'action' => 'index'
],
'loginAction' => [
'controller' => 'Users',
'action' => 'login'
]
]);

Sending email from gmail using CakePHP3, Connection refused

I am having a problem with configuration with CakePHP3 for sending email from my Gmail account. I always get an error Connection refused. I don't know where the problem is. So I hope anyone here has any knowledge about sending emails from my Gmail account.
EDIT: I am using GoDaddy hosting server
Here are my configurations.
'gmail' => [
'host' => '(ssl://)smtp.gmail.com',
'port' => 465,
'username' => 'MyGmail#gmail.com',
'password' => 'pass',
'className' => 'Smtp',
'tls' => true
],
'Email' => [
'gmail' => [
'transport' => 'gmail',
'from' => 'myGmail#gmail.com',
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
],
],
First update email config in CakePHP
Your Updated config/app.php file should be as :
'EmailTransport' => [
'default' => [
'className' => 'Smtp',
// The following keys are used in SMTP transports
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'timeout' => 30,
'username' => 'username#domain.com',
'password' => 'your_password',
],
],
and now use the mail function of CakePHP Email Function.

create a separate database config file in CakePHP 3

I'm using CakePHP 3.4
default database settings exists in config/app.php
I want to separate out or override database configuration outside app.php say in config/my_db.php and load it in bootstrap.php file.
This setting will now override default database setting that exists in app.php file.
Is there some way to do this ?
Edit 2
config/my.db.php file
<?php
return [
'my_db' => [
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
'username' => 'root',
'password' => 'my_pass',
'database' => 'testdb',
'encoding' => 'utf8',
'timezone' => 'UTC',
'flags' => [],
'cacheMetadata' => true,
'log' => false,
]
]
]
];
loading in bootstrap.php
Configure::load('my_db', 'default', false);
Create new file into folder config/
Name it whatever you like: my_db.php
Add your code configuration Code:
return [
'my_db' => [
'setting_1' => 'value_1',
'setting_2' => 'value_2',
'setting_3' => 'value_3',
],
];
Now you have to load it. Open file config/bootstrap.php,
locate line:
Configure::load('app', 'default', false);
and append this line underneath:
Configure::load('my_db', 'default');
Try THis ::
config/bootstrap.php
Configure::load('my_app', 'default','false');
config/my_app.php
<?php
return [
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
'username' => 'root',
'password' => 'my_pass',
'database' => 'my_db',
'encoding' => 'utf8',
'timezone' => 'UTC',
'flags' => [],
'cacheMetadata' => true,
'log' => false,
]
]
];
make a copy of your app.php, name it app_override.php and change your db-settings.
Then adapt your bootstrap.php like this
try {
Configure::config('default', new PhpConfig());
Configure::load('app', 'default', false);
} catch (\Exception $e) {
exit($e->getMessage() . "\n");
}
Configure::load('app_override', 'default');

Different cakephp datasource for local and live

My MySQL connection details are different for both my local connection and my deployed live hosted. I am using CakePHP 3
At the moment I have to keep changing the default datasource which is not really the best way to do it.
I have not added two datasources but I am not sure how to switch between them?
'Datasources' => [
'development' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => '127.0.0.1',
'port' => '8889',
'username' => 'root',
'password' => 'root',
'database' => 'local',
],
'deployment' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
'username' => 'username',
'password' => 'password',
'database' => 'live_database',
],
In boostrap or in App Controller, paste this
if(Configure::read('debug')){
ConnectionManager::config('deployment');
}
this change the default config of database when the debug is true.

Resources