I'm currently writing an app in PHP that connects to a MongoDB database.
I've managed to successfully authenticate a user with the dbAdmin role in the MongoDB Database that I've setup, and I've created a user "TestUser" with no roles that I want to get information about while authenticated as the dbAdmin User. however I can't figure out how to run the "db.getUser()" command from PHP.
My understanding is that I should be able to use the "MongoDB::command()" method to run any actual mongoDB commands directly, however, the following code...
$mongo = new MongoClient("localhost:27017");
$mongoDb = $mongo->{'test'}
$mongoDb->authenticate($username, $password); //This returns a successful response of array('ok' => 1)
print_r($mongoDb->command(array("getUser" => "TestUser")));
Generates this response: -
(
[ok] => 0
[errmsg] => no such command: getUser
[code] => 59
[bad cmd] => stdClass Object
(
[getUser] => TestUser
)
)
The "getUser" is a command of a Database in MongoDB though, right? If so, why am I getting this response, and how do I construct a correct statement to be able to get information about a User from MongoDB in PHP?
I looked into this more, by fetching a list of all possible methods available to the database by using...
$mongoDb->command(array("listCommands" => 1));
This informed me that "getUser" apparently is not an existing command. I'm not sure why not, I guess it's because it's a User Management command and not a typical Database Command.
At any rate, "usersInfo" is a command that is available. By doing the following...
$mongoDb->command(array("usersInfo" => array("user" => $username, "db" => $database)));
I was able to get the data about the user that I wanted, so I guess that answers my question. Hopefully this helps anyone else whose encountering the same problem trying to setup their PHP app with MongoDb.
Related
I've created a rather simple multi-tenant application using separate schemas in a Postgresql database. I keep a public schema, which only a few model use and then the rest of my models use the tenant. I determine the Client from the subdomain through a middleware then, call the following function on my Client model to set the credentials for the connection.
public function logInAsClient()
{
$settings = $this->settings;
// set tenant db
config([
'client' => $settings,
'client.id' => $this->id,
'database.connections.tenant.schema' => $this->schema,
'schema' => $this->schema,
'domain' => $this->domain,
]);
DB::disconnect('tenant');
DB::reconnect('tenant');
return true;
}
It's working great in normal functions, but I have several tasks which require me to queue up hundreds of jobs at a time. Since they are all running on the same app, I pass the client_id to each job and then at the start of the handle run:
Client::find($this->client_id)->logInAsClient();
However, when running this code I get the following error:
PDOException: SQLSTATE[08006] [7] FATAL: sorry, too many clients already
FATAL: sorry, too many clients already
I don't there there is any other part of my app where I am connecting or reconnecting to databases so I'm not sure where else this issue might be coming from. If there are any hints on how to debug an issue like this it would be greatly appreciated.
I have an Android app in which I've implemented AWS Cognito. I'm hoping to use this as a means for controlling access to PHP scripts on my web root which connect to an RDS instance with a MySQL db. So far, I've set the registration process in my app to use a developer authenticated id to register the user in a cognito identity pool. Now, what I would like to do is have a method for checking whether the user trying to access the various scripts I've exposed in my web root is indeed a verified user. What I was thinking of doing is implementing a script like this:
use Aws\CognitoIdentity\CognitoIdentityClient;
$identityId = $_POST['identityId'];//sending cached identity id from client
$client = CognitoIdentityClient::factory ( array (
'profile' => 'profile',
'region' => 'region'
) );
$result = $client->lookupDeveloperIdentity(array(
'IdentityPoolId' => 'IdentityPoolId',
'IdentityId' => $identityId,
'MaxResults' => 1,
));
if ($result != null) {
//connect to db and do whatever operation/query needs to be done
}
However, checking this every time I need to make some kind of transaction on my db seems to be pretty inefficient and slow.
a) Am I using Cognito in the intended fashion?
b) If not, what is a better way of going about this?
Please let me know if I'm way off base here. Thanks!
Is there a way to get the output of "rs.status();" from the php driver?
Passing the following setting to the Command function of the MongoDB Driver
array('replSetGetStatus'=>1)
Results in an error message:
Array
(
[0] => access denied; use admin db
[1] => 0
)
You can reuse the same connection; you just have to get the admin db from the object:
$connection->admin->command(array("replSetGetStatus"=>1)
If you haven't already, you'll need to create a database connection object to the 'admin' db so that you can then call the output of rs.status() through the driver as you specified.
mongodb://[username:password#]host1[:port1][,host2[:port2:],...]/db
See the docs here
Using the example from the manual:
$mongo = new Mongo("mongodb://sf2.example.com,ny1.example.com", array("replicaSet" => "myReplSet"));
When I check $mongo, it says it is indeed connected. I thought I could then call $mongo->isMaster() to get replica set details, but that doesn't work. Is that not a proper way of doing this?
isMaster isn't a PHP function (see http://www.php.net/manual/en/class.mongo.php for a list of functions available in the Mongo class). You can do:
$result = $mongo->myDb->command(array("isMaster" => 1));
This runs the isMaster command on the myDb database (it doesn't matter what db you run it on).
I'm getting a weird error when running through the tutorial in the e-book, "Practical CakePHP Projects" in chapter 8, "A Cake Control Panel". Everything is built out, the DB is set-up and completely blank, and I'm running through the test at the end of the chapter to verify everything is working... I'm not sure what happened, but I keep running into the following error:
Warning (512): DbAcl::check() - Failed ARO/ACO node lookup in permissions check. Node references:
Aro: Array
(
[User] => Array
(
[id] => 2
[username] => admin
[group_id] => 1
[created] => 2010-07-05 12:07:45
[modified] => 2010-07-05 12:08:00
)
)
It looks like it's telling me that there is no node in my Aro that is identified by the model 'User' with the id of '2'. But looking at my Aros table, I can clearly see that the last record is the following:
id:4, parent_id:2, model:User, foreign_key:2, alias:User:2, lft:2, rght:3
And the following relative record is my users table:
id:2, username:admin, password:hashed, group_id:1, created:date, modified:date
Where am I going wrong? Any direction someone could point me in would be greatly appreciated.
Thanks!
If you've setup the ACLExtras Automated tool to sync ACOs, Execute the following command in the CakePHP console:
./Console/cake AclExtras.AclExtras aco_sync
It seems for me that you either do not have corresponding record in acos table or permission in acos_aros. If that is the case you will need to add $this->Auth->allow('*'); in beforeFilter() in you app_controller.php and after that add ACO(s) and allow it to admin user.
I would advice you to follow official tutorial Simple Acl controlled Application because the information in the book becomes outdated. Something may not work with CakePHP 1.3.