check database connect in symfony controller - symfony

I build some kind CMS, where I can change parameters (parameters.yml) from my admin panel. User (admin) will be able to change database connections (name, host etc.). But I need to handle errors after that and let him rollback or something like that.
What I need:
- make connection to database after parameters change
- check if exists, etc..
What I can do:
- make new instance of \PDO and try / catch etc.
What I prefer:
- some kind symfony/doctrine help for that.
Is there something I can to instead new PDO? OR meyby my thinkinh way is wrong? :)

You can use Doctrine DBAL Connection factory for that. In your controller (or service) you can get the "doctrine.dbal.connection_factory" service from container and create a new connection with the new parameters:
https://github.com/doctrine/DoctrineBundle/blob/master/ConnectionFactory.php#L51
and then try to connect with it:
https://github.com/doctrine/dbal/blob/master/lib/Doctrine/DBAL/Connection.php#L350
Here you can catch the errors that may occur.
Hope it helps

Related

How to create database in Redis

I am new to Redis and trying to create a new database. Also, How to write a connecting string to Redis database with C#.net? Please somebody help me how to implement this.
In general, all use StackExchange.Redis, you will find all you need in documentation

Symfony - Log runnables natives queries when database is out

I'am working on a Symfony app that provides a rest web service (simple HTTP Request with JSON).
That service check some rules and inserts few lines in two MySQL table (write only).
For optimize reason, even if Doctrine bundle is available, i use native MySQL Query (with bind params) to insert this lines.
My need is : If for any reason, the database is not available, write "runnables" queries into a log file.
The final purpose is that when database is back, i want to be able to execute directly the file's content on the database.
Note that there is no unique constraint (pk is a generated uuid) and no lock or transaction to handle (simple insert statements).
I write a custom SQLLogger, but when $connection->insert(...) is called, the connect fail before logger is called.
So, my question is : There is a way to get the final query (with binded parameters) without database connection ?
Or should i rewrite the mecanism that bind params into query and log it myself when database is not available ?
Best regards,
Julien
As the final query with parameters is build by the database, there is just no way to build the query with PHP and to be garanteed that the query will be the same as the database.
The only way si to build query without binded parameters, but this is clearly not a good practice.
So, i finally decided to store all the JSON (API request body) in a file if the database is not available.
So when the database is back, instead of replay SQL queries, i can replay the original HTTP query.
Hope this late self-anwser will help someone.
Best regards.

Dynamics SqlDataDictionaryPermission failure

I am trying to truncate one of our tables in a class to run as a batch job, but keep getting a "Request for permission of type SqlDataDictionaryPermission failed. This is on an AX 4.0 system. I followed MSDN example on acquiring permissions and I am an admin. Here's the code:
//Truncate table
new SqlDataDictionaryPermission(
methodstr(SqlDataDictionary, tableTruncate)).assert();
sqlDict = new SqlDataDictionary();
sqlDict.tableTruncate(tableNum(PMF_INVENTTABLEMODULELOG),false);
CodeAccessPermission::revertAssert();
As I stated, I have admin access to this, so should have the required security key. Though this is displaying the (usr) environment in the class list.
Are you sure it is running server side?
You did not expose your method definition.
I stumbled upon this issue in Ax 2009, while trying to set up a batch that reindexes tables from parameters.
The method doesn't need to be static or have server predicate. The only thing that works is setting the class "RunOn" parameter as "Server".
I know it's and old question with low views, but perhaps some poor soul can be saved.

WebSecurity SimpleMembershipProvider not working together?

Getting a bit disappointed with all the weird exception I keep running into while working with WebSecurity. Also the poor integration with OAuth doesn't make it look prettier. Considering to drop the concept and write the whole user management manually...
Anyway, I am using WebSecurity to administer users and passwords. Now I try to implement the part where accounts can be deleted. Weirdly enough this method is not on the static class WebSecurity. Apparently I need to delete accounts via the SimpleMembershipProvider.
var provider = new SimpleMembershipProvider();
provider.DeleteAccount(username);
The deleteAccount method throws an invalidoperation exception with the following message:
You must call the "WebSecurity.InitializeDatabaseConnection" method before you call
any other method of the "WebSecurity" class. This call should be placed in an
_AppStart.cshtml file in the root of your site.
Well that's weird since I already have this in my _ViewStart (otherwise I wouldn't have been able to create the accounts in the first place).
if (!WebSecurity.Initialized)
{
WebSecurity.InitializeDatabaseConnection(
"DefaultConnection",
"Users",
"UserId",
"UserName",
true);
}
What am I doing wrong this time?
Have you tried:
Membership.DeleteUser(username);

How can I create a database on my server from the web?

I have an admin account for my website where I add new clients. When a new client is added, they get an account and their own database.
The problem is I can't create new databases on my server from my admin account. When running it locally, I can add a database locally. But, when I try adding to the server running my website off the server, I get
CREATE DATABASE permission denied in database 'master'.
I've been able to add the database (locally) a few ways. This is one of the simpler working versions:
tmpConn.ConnectionString = "Data Source=.\\SQLEXPRESS; DATABASE = master;Integrated Security=True;";
sqlCreateDBQuery = " CREATE DATABASE " + dbname;
SqlCommand myCommand = new SqlCommand(sqlCreateDBQuery, tmpConn);
try
{
tmpConn.Open();
myCommand.ExecuteNonQuery();
}
catch (System.Exception ex)
{}
I suspect that whatever account you're using to connect to Sql Server doesn't have permissions to CREATE DATABASE. You're probably using Integrated Security, which would use Network Service/ASP.NET to connect to MSSQL. You need to create a new connection string that uses Sql Authentication with sa (or another sysadmin) credentials.
Oh - and this would work locally because you're running it under Visual Studio's WebDev.exe which is run with your local user account - which is probably set up as a sysadmin in MSSQL.
You should contact your service provider. (Or the maintainer of the server).
You need create database and create database user permissions. Your service provider should be able to facilitate this.
Something else no one has suggested is what kind of validation you are doing on the dbname value. Are you sure there are no spaces in it? That it's not a reserved word? That it doesn't contain malicious code? At very least you should encase it in brackets:
sqlCreateDBQuery = String.Format(" CREATE DATABASE [{0}]", dbname);
I really hope you aren't allowing the user to type this name directly into a textbox somewhere. Even if you use property security on the initial input and this is pulled back from a common "clients" db of some kind, you might be setting yourself up for a 2nd order Sql Injection vulnerability.
After you've addressed that, we can look at the error message here. In this case, the problem is that your web user does not have appropriate CREATE permissions. You need to correct that and it should allow you to proceed. You probably want to reserve a special account for this that you switch to just at this time, so your application doesn't normally run in a context that would allow this kind of action.
Check the permissions for MySQL: Does your admin account have different settings for a local connection versus any host?

Resources