That's interesting, do you use PostgreSQL? Probably the problem is that "user" might be a reserved word, so try to use a different name for your table, e.g. "app_user". Does it help?
when running the command still got same result php app/console doctrine:query:sql 'SELECT * FROM user' array(1) { [0]=> array(1) { ["current_user"]=> string(8) "postgres" } }
Wait, but if you use the different from "user" database name now, you should use the new name in your SQL query :) So, base on your mapping, the query should be:
> php app/console doctrine:query:sql 'SELECT * FROM fos_user'
It depends on where you need to do it. In twig templates we can use is_granted() Twig function to check if users have some required role and if they have - show them a link to the admin dashboard for example. But of course, just hiding links from non-admin users is not enough, because some tricky users can guess the admin URL. That's why you can use "secrity.access_control" list in your app/config/security.yml to configure who have access to the URLs with "/admin" prefix for example, etc. Also, in case you love PHP annotations, you can use "@Security()" annotation: https://symfony.com/doc/cur... - for controllers and actions you want to secure with for a special user role. And, of course, if you extend Symfony controller, you can call denyAccessUnlessGranted() method in PHP code to be extra sure things are safe. Also, check this page about Security from Symfony Best Practices: http://symfony.com/doc/curr...
For reasons, try to figure out why findOneByUsername() returns null. Probably $imie is empty or holds invalid data, i.e. username that you do not have in DB
You are close to achieve it, but instead of defining multiple labels, you can make use of the 'choice_label' option, by setting up a custom callback function as shown here: https://symfony.com/doc/cur...
Actually, I think you are VERY close: you probably just need to add tranlsations for the "form.choice.Admin" and "form.choice.Jakosc" keys. Find/create your FOSUserBundle.en.yml file (or change en for your language), and add these translation keys there. For example:
I have added those keys on the begining to fosuserbundle.pl.yml file but problem was that i did it with lower case because i was sure it is not case sensitivity. :)
Hi Ryan - I have FOSUserBundle install and it's working great. When a user is logged in there is a route '/profile/edit'. I don't yet see how I can view all users? I want to do is setup routes that 1) Displays all registered users (sort of like I saw in another screencast '/admin/genus' 2) An edit user route for each user (ie. /profile/edit/{id} separate from groups which I currently do not have any groups setup. Is there an event I can override or service I need to create? Any tip/ideas on how I might accomplish this would be greatly appreciated.
The route '/profile/edit' is only for *that* logged in user, what you need is an *admin* section so you can manage all your users, something similar as you said about 'admin/genus', but 'admin/users/{id}' instead. Maybe EasyAdminBundle can help you on this, is easy to use and we have a fresh tutorial about it https://knpuniversity.com/s...
Where I needed to add user management was already an admin site so EasyAdminBundle wasn't an option this time. I did get this working by extending the ProfileFormType as BaseRegistrationFormType, removed 'current_password' from the form, added methods to extend the fos user manager, little bit of wrangling and got it to suite my needs for user management. Though, I will look to use EasyAdminBundle in the future.
21 Comments
so... i got the different result when running this command
php app/console doctrine:query:sql 'SELECT * FROM user'
array(1) {
[0]=>
array(1) {
["current_user"]=>
string(8) "postgres"
}
}
Hey Lavin,
That's interesting, do you use PostgreSQL? Probably the problem is that "user" might be a reserved word, so try to use a different name for your table, e.g. "app_user". Does it help?
Cheers!
yes im using PostgreSQL i will update the asnwer ASAP if it help and clear the issue
/**
* @ORM\Entity
* @ORM\Table(name="fos_user")
*/
when running the command still got same result
php app/console doctrine:query:sql 'SELECT * FROM user'
array(1) {
[0]=>
array(1) {
["current_user"]=>
string(8) "postgres"
}
}
Hey Lavin,
Wait, but if you use the different from "user" database name now, you should use the new name in your SQL query :) So, base on your mapping, the query should be:
> php app/console doctrine:query:sql 'SELECT * FROM fos_user'
Cheers!
ah sorry silly me hahaha its worked now thank you hahaha
Haha, great :) Glad it works now!
Cheers!
How do you deny access to a User that hasn't have for example the ROLE "Admin"?
Hey Jelle,
It depends on where you need to do it. In twig templates we can use is_granted() Twig function to check if users have some required role and if they have - show them a link to the admin dashboard for example. But of course, just hiding links from non-admin users is not enough, because some tricky users can guess the admin URL. That's why you can use "secrity.access_control" list in your app/config/security.yml to configure who have access to the URLs with "/admin" prefix for example, etc. Also, in case you love PHP annotations, you can use "@Security()" annotation: https://symfony.com/doc/cur... - for controllers and actions you want to secure with for a special user role. And, of course, if you extend Symfony controller, you can call denyAccessUnlessGranted() method in PHP code to be extra sure things are safe. Also, check this page about Security from Symfony Best Practices: http://symfony.com/doc/curr...
Cheers!
Hi.
I created my own form to change users roles.
This is my function in controller:
Unfortunatelly after submit a have this error: "Call to a member function setRoles() on null" which link to this line of code: $user->setRoles($rola);
Does somebody know what is wrong?
Hey Szymon,
Looks like your user is equal to null. To debug it, add dump() after:
For reasons, try to figure out why findOneByUsername() returns null. Probably $imie is empty or holds invalid data, i.e. username that you do not have in DB
Cheers!
Thank you!!!
Problem was with $imie variable. It's object.
I called it in wrong way :)
Of course it should by like that:
All the best !!!
I have another question:)
How to translate labels in form with choice type ?
I dont mean main label but labels for choices.
This is what i have in form builder:
Ofcourse i added 'Admin' and 'Jakosc' to proper file which is:FOSUserBundle.pl.yml but it doesn't work.
Hey Szymon Chomej
You are close to achieve it, but instead of defining multiple labels, you can make use of the 'choice_label' option, by setting up a custom callback function as shown here: https://symfony.com/doc/cur...
I hope it helps you :)
Cheers!
Now i ended up with this:
It stil doesnt work:(
I t shows:
'form.choice.Admin'
'form.choice.Jakosc'
Any idea?
Hey Szymon Chomej!
Actually, I think you are VERY close: you probably just need to add tranlsations for the "form.choice.Admin" and "form.choice.Jakosc" keys. Find/create your FOSUserBundle.en.yml file (or change en for your language), and add these translation keys there. For example:
Cheers!
Hey weaverryan !
Problem solved !!! :)
I have added those keys on the begining to fosuserbundle.pl.yml file
but problem was that i did it with lower case because i was sure it is not case sensitivity. :)
Now i have this in fosuserbundle.pl.yml file:
and it works :)
Thank you and all the best.
Awesome! I'm happy to hear that you could fix your problem :)
Cheers!
Hi Ryan -
I have FOSUserBundle install and it's working great. When a user is logged in there is a route '/profile/edit'. I don't yet see how I can view all users? I want to do is setup routes that 1) Displays all registered users (sort of like I saw in another screencast '/admin/genus' 2) An edit user route for each user (ie. /profile/edit/{id} separate from groups which I currently do not have any groups setup. Is there an event I can override or service I need to create? Any tip/ideas on how I might accomplish this would be greatly appreciated.
Hey Brent
The route '/profile/edit' is only for *that* logged in user, what you need is an *admin* section so you can manage all your users, something similar as you said about 'admin/genus', but 'admin/users/{id}' instead. Maybe EasyAdminBundle can help you on this, is easy to use and we have a fresh tutorial about it https://knpuniversity.com/s...
Cheers!
Where I needed to add user management was already an admin site so EasyAdminBundle wasn't an option this time. I did get this working by extending the ProfileFormType as BaseRegistrationFormType, removed 'current_password' from the form, added methods to extend the fos user manager, little bit of wrangling and got it to suite my needs for user management. Though, I will look to use EasyAdminBundle in the future.
Thank you!
Ohh, so you are a pro now extending a bundle's behaviour
When you get a chance, give it a try to EasyAdminBundle, it is great!
Have a nice day :)
"Houston: no signs of life"
Start the conversation!