How to create cakephp database config settings (login, password) dynamically - database

Every cakephp user will have his database(Postgres) user replica. Therefore when he logs in, the database default config must take the "login" and "password" sent in the login.ctp form and with those values create the DATABASE_CONFIG. So far I have this, but I can't figure it out how to pass variables to the constructor. Is it possible? Is there an alternative?. Please help.
enter code here
class DATABASE_CONFIG{
var $default = array(
'datasource' => 'Database/Postgres',
'persistent' => false,
'host' => 'localhost',
'login' => 'XXX',
'password' => 'YYY',
'database' => 'db1',
'prefix' => '',
'schema'=>'public'
//'encoding' => 'utf8',
);
function __construct(){
$this->default['login'] = $userSentFromForm;
$this->default['password'] = $passwordSentFromForm;
}
}

I think that you should have some global database with users which stores login information (username and hashsed password) so you can validate & login user.
When user logs-in you save data from that form into the session.
Then you redirect the user to some action, like /dashboard or any else. Following request will have your desired data in session object, so you can read it in DATBASE_CONFIG::__construct().

Related

Cakephp v3.0.5 use different model in Component Auth

I read all the cakephp component Auth documentation in http://book.cakephp.org/3.0/en/controllers/components/authentication.html but I cant find a solution :-(
I'm trying to use different model called "Usuarios" in component Auth and change the field "username" by "cedula". This is my configuration in AppController.php:
public function initialize() {
parent::initialize();
$this->loadComponent('Flash');
$this->loadComponent('Auth');
$this->Auth->config('authenticate', [
'Basic' => [
'userModel' => 'Usuarios',
'fields' => [
'username' => 'cedula',
'password' => 'password']],
'Form' => ['userModel' => 'Usuarios',
'fields' => [
'username' => 'cedula',
'password' => 'password']]
]);
But nothing happend. No appear login form and session is open.
What i'm doing wrong?
Match the controller name to the template directory, and the method to the template.
If your controller is src/Controllers/Usuarios, method login(), then in src/Templates/Usuarios/ you need to have login.ctp as your view file.
If you have all that, it should work. By the way, if you are using 'password' as your password field, you don't need to specify that when you configure Auth; You only need to set 'username' => 'cedula' because you are changing the default.
Also, have you set anything in $this->Auth->allow() in the beforeFilter() method?

cakephp error. Connection to database ...not ...: Access denied for user 'my_app'#'localhost' (using password: YES)

I am trying to start CakePHP. I made bookmarker and tested by command bin\cake server. It showed one error as connection to database could not be established:
SQLSTATE[HY000] [1045] Access denied for user 'my_app'#'localhost' (using password: YES).
I read the config/app.default.php file. It says there is a database my_app and another database test_myapp with some users. I can not find these databases in phymyadmin in xampp. Am I supposed to create the named databases and users manually? I thought CakePHP should create these automatically. Or should I give names of databases etc. which I like and create the same.I'm using xampp with windows 7 and am very new to CakePHP.
Cake will not create the database or database user for you. You need to create them yourself and then match these database credentials into the db config file. Your datasource in app/Config/app.php file should look something similar to:
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
'username' => 'my_db_user',
'password' => 'my_db_user_password',
'database' => 'cake_database_name',
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,
]
],
In this example you would have to create a database named: cake_database_name, and then add a database user named my_db_user with the password of my_db_user_password.
Edit Config/app_local.php file instead of Config/app.php with your database connection parameters, i.e. host, username, password, database name. This will solve the problem.
Be sure to create same database first and providing enough privileges to same user you're writing in app_local.php file.
See in Mysql.php:
class Mysql extends Driver
{
use MysqlDialectTrait;
use PDODriverTrait;
/**
* Base configuration settings for MySQL driver
*
* #var array
*/
protected $_baseConfig = [
'persistent' => true,
'host' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'cake',
'port' => '3306',
'flags' => [],
'encoding' => 'utf8',
'timezone' => null,
'init' => [],
];
Then in app.php write
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
/**
* CakePHP will use the default DB port based on the driver selected
* MySQL on MAMP uses port 8889, MAMP users will want to uncomment
* the following line and set the port accordingly
*/
//'port' => 'non_standard_port_number',
'username' => 'root',
'password' => '',
'database' => 'cake',
'encoding' => 'utf8',
'timezone' => 'UTC',
'flags' => [],
'cacheMetadata' => true,
'log' => false,
...],
In phpMyAdmin
create cake database.
It's all.
Edit Config/app_local.php file with your database connection parameters. You will find host, username, password, database name their.
don't edit config/app.php with database connection parameters.
Your problem will be now solved
CakePHP 4.x
Error:
SQLSTATE[HY000] [1045] Access denied for user 'my_app'#'localhost'
(using password: YES).
Reason:
No user id: "my_app" with password: "secret" [or any other] set in config/app.php
file.
Solution:
In case of WAMP, go to phpmyadmin page, then we can select cake_cms
database -> privilage-> create user id and password, same as in config
file config/app.php, refresh the http://localhost:8765/ page.
it should now show
CakePHP is able to connect to the database.
If you are using wamp, make sure port is uncomment in Datasources and 'port' => '3308'
I solved this problem by checking port at phpmyadmin top
and uncommit below line in app_local.php
'port' => '3308',
CakePHP3.9.3,app_local.php,'username`=>'root','password'=>'','database' => 'db name'

Request object exposing password check variable

So in the sake of security I force all password change requests to validate through a password check. The problem I have is that using a debug($this->request) call I'm seeing a plaintext password:
object(CakeRequest) {
...snip...
data => array(
'User' => array(
'password' => '*****',
'password_check' => 'asdfa',
'id' => '38'
)
)
...snip...
}
Should this be a concern for me? Is there a way I can have password_check treated like a password?
as long as you don't actually print it anywhere on the page you will be fine!

Multiple applications can share the same ACL tables?

Currently I have to share my database with many CakePHP applications and the tables are prefixed to identify each application respectively.
So, can many applications share the same ACL tables?
Or, I could change the default names of the tables ACL and add the prefix of each application, eg. app_aros, app_acos, app_aros_acos?
/* beforeFilter() # AppController */
$this->Acl->Aro->useTable = 'app_aros';
$this->Acl->Aco->useTable = 'app_acos';
This code worked but I haven't found a way to change the tablename of the model Permission...
Suggestions? What could I do?
You have change this line in app/Config/core.php in all of your apps
Configure::write('Acl.database', 'default');
to:
Configure::write('Acl.database', 'your_acl_connection');
And also add connection in app/Config/database.php
example:
public $your_acl_connection = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'user',
'password' => 'password',
'database' => 'database_name',
'prefix' => '',
//'encoding' => 'utf8',
);
Simply you will have one DB for managing all yours app ACLs.
In this DB you create all ACL tables.

How can I connect to second database with my cakephp application

my application's requirement is that to display data of magento database table forexample admin_user that is reside at locally connected pc.
so I need to keep as it is my cakephp database values in displaying-modifying-etc, and in only one page magento's database value needs to be printed and updated.
I have kept 2 variables in databse.php
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'myappdatabase',
'prefix' => '',
//'encoding' => 'utf8',
);
var $vsdatabase = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => '192.168.1.36',
'login' => 'root',
'password' => '',
'database' => 'magento',
'prefix' => '',
);
and in controller,
App::import('Model','ConnectionManager');
$db = ConnectionManager::getDataSource('vsdatabase');
$database = $db->config['database'];
$data = $this->User->query("select * from $database.admin_user as t1");
the host I like to keep as written above means default is from my local database and other is remote PC's magento database
If I both host keep same then its working but if write different then It's not working
So I solve this problem?
Plz help me in finding out solution
What you do is plain wrong and even if you would use that code it should belong into a model not a controller.
Simply create a new model named after the magento table, but I would prefix it with magento or something. The model has to be configured to use this db connection.
class MagentoUser extends AppModel {
public $useDbConfig = 'vsdatabase';
}
You can also init models with other aliases and data sources on the fly using ClassRegistry. See http://api20.cakephp.org/class/class-registry#method-ClassRegistryinit
And stay away from using plain SQL queries, you will very likely ending up writing insecure queries and lose other features of the CakePHP ORM.

Resources