FosUserBundle Confirm Email after registration - symfony

I want to do email confirmation after each registration of new user in my website, so when the registration is done the system must redirect him to anther page that tells that he should confirm his email address, and only give access only to users they had an activated mail address.
My config file is:
fos_user:
db_driver: orm # other valid values are 'mongodb' and 'couchdb'
firewall_name: main
user_class: AppBundle\User
registration:
confirmation:
from_email: # Use this node only if you don't want the global email address for the confirmation email
address: test#example.com
sender_name: Example sender
enabled: true # change to true for required email confirmation
template: '#FOSUser/Registration/email.txt.twig'
This is the error I am getting:
Type error: Argument 1 passed to FOS\UserBundle\EventListener\EmailConfirmationListener::onRegistrationSuccess() must be an instance of FOS\UserBundle\Event\FormEvent, instance of FOS\UserBundle\Event\GetResponseUserEvent given
EmailConfirmationListener onRegistrationSuccess must be an instance of FormEvent

What you are trying to achieve is something that happens by default. It seems like you changed an eventlistener or made a new one that does not work. Also, in your config file 'template:' does not seem to be in the right place because it does not line up with 'enabled' and 'from_email'.

Related

How to authorize custom user from db in Symfony4

I just need to authorize custom user. Without any froms, tokens and etc. I have user entity in my DB.
User class is already configuren it fos_user.yaml:
fos_user:
db_driver: orm
firewall_name: main
from_email:
address: '***#*******.**'
sender_name: '**.****.**'
user_class: App\Entity\User
Is it possible? Somethong like
Authurizer::authorize($userEntity)
Thanks.
Authorization in Symfony done Tokens. To be logged in Symfony's World you'll need to set a Token.
One of the common use cases is "auto login" right after registration.
Take a look at this article -> https://ourcodeworld.com/articles/read/459/how-to-authenticate-login-manually-an-user-in-a-controller-with-or-without-fosuserbundle-on-symfony-3
especially for that part
$token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
$this->get('security.token_storage')->setToken($token);
BUT also take a look at symfony's impersonalizaiton => https://symfony.com/doc/current/security/impersonating_user.html
It basically allows you to switch users without filling out forms and knowing users' credentials
Custom Commands for FOSUserBundle are
fos:user:activate
fos:user:change-password
fos:user:create
fos:user:deactivate
fos:user:demote
fos:user:promote
There is no such way to authorize directly as you want it.
You can create a custom command in symfony which accepts username and authenticates a user. Here is a way to create such a command : https://symfony.com/doc/current/console.html
Thanks you, guys! I found the solution here!
How to programmatically login/authenticate a user?
Sorry for wrong or stupid question.

Sonata Admin Bundle ACL don't grant edit

I integrated the Sonata Admin Bundle with ACL, and have the following configs:
config.yml
sonata_admin:
security:
handler: sonata.admin.security.handler.acl
# acl security information
information:
GUEST: [VIEW, LIST]
MAINTAINER: [EDIT, LIST]
STAFF: [EDIT, LIST, CREATE]
EDITOR: [OPERATOR, EXPORT]
ADMIN: [MASTER]
# permissions not related to an object instance and also to be available when objects do not exist
# the DELETE admin permission means the user is allowed to batch delete objects
admin_permissions: [CREATE, LIST, DELETE, UNDELETE, EXPORT, OPERATOR, MASTER]
# permission related to the objects
object_permissions: [VIEW, EDIT, DELETE, UNDELETE, OPERATOR, MASTER, OWNER]
security.yml
security:
role_hierarchy:
ROLE_OPERATOR:
- ROLE_ADMIN_BOOKING_ADMIN
- ROLE_ADMIN_PAYMENT_ADMIN
The flow is we create a booking object via BookingAdmin class and in postPersist doctrine event listener I create the payment Object.
$payment = new Payment();
//... set here
$this->entityManager->persist($payment);
$this->entityManager->flush();
The problem is in list I'm not able to see the edit button, but I can delete.
And when run manual the command:
php bin/console sonata:admin:generate-object-acl
after that I'm able to see the edit button.
What I do wrong here ? Because I'm logged with the same user.
EDIT
After few research I found the next problem https://sonata-project.org/bundles/admin/2-3/doc/reference/security.html#acl-and-friendsofsymfony-userbundle
A listener must be implemented that creates the object Access Control List with the required rules if objects are created outside the Admin
What this mean, and how I should do in listener to take the correct ACL role?
I think you don't pointed out your allowed action detailed enough ... you only point to the admin with e.g. "ROLE_ADMIN_BOOKING_ADMIN" ... following the documentation, the config should be "ROLE_ADMIN_BOOKING_ADMIN_EDIT" for example to allow this role to edit your admin ... write "ROLE_ADMIN_BOOKING_ADMIN_ALL" to allow to edit everything ...

FosUserBundle don't send email to resset password

I have a simple form to login and reminder password, I overwrite the FOSUser template and everything works fine, but when I want to email the password reminder I do not get it to the mail.
This looks like a swiftmailer and fos configuration in config.yml:
swiftmailer:
transport: '%mailer_transport%'
host: '%mailer_host%'
username: '%mailer_user%'
password: '%mailer_password%'
spool: { type: memory }
fos_user:
db_driver: orm
firewall_name: main
user_class: AirblogBundle\Entity\User
from_email:
address: "admin#admin.com"
sender_name: "Admin"
resetting:
email:
from_email:
address: noreply#yoursite.com
sender_name: No Reply
token_ttl: 0
After sending the email from the resetting/request path, I get the following information:
An email has been sent to ...#wp.pl. It contains a link you must click to reset your password.
Which seems correct because the address entered in the form has the same ending, but my mailbox does not receive any message.
ps: One more question, how to simply overwrite the Twig template, so the login and reminder of the hymn was on one page? Do you need to overwrite the controller?
Did you configure the needed parameters of SwiftMailerBundle well ?
First of all I suggest you to use Symfony's command to check your swift mailer configuration.
Specify the good arguments to send you a debug mail with reading the help command
php app/console swiftmailer:email:send --help
You should receive a mail on the email address you wrote in command.
After this step you can assume that your mail configuration is good
Reset your forget password request
One important (in the version installed on my project, composer requirement ~2.0) thing to know that is FOSUserBundle don't resend mail if you didn't click on the previous reset link sended BUT he display to you the message that he sended the mail.
If you want to "reset" your password request for a specific user
UPDATE user SET confirmation_token = NULL, password_requested_at = NULL WHERE username = "yourdebugusername"
Extending FOSUserBundle Twig templates
You should read this documentation
Basically you can override all the FOSUserTemplate under vendor/friendsofsymfony/user-bundle/Resources/views
I hope it's help !
For overriding FOSUser twig template looks the Symfony Doc

multiuser in symfony using fosuserBundle

I have three tables in my database: Simpleuser and Admin and StudentUser, I would like use this 3 table to connect in my symphony app , but I know that with fosuserBundle I should configurate just one table to be able to connect to the application.
I already change the first one using config.yml
here :
fos_user:
db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel'
firewall_name: main
user_class: MyApp\UserBundle\Entity\StudentUser
i'm searching solution that let me log in my symfony application with many users , any one have an idea ?
thank you :)
You can use PUGXMultiuserBundle to handle multiple user classes.

FOSUserBundle sending bad confirmation links

1 or 2% of users registering seem to get no confirmation email or they get a bad token appended to the confirmation link in the email. I can't determine why or where this wrong token comes from. It doesn't look like a truncated version of the correct one. It's totally different. Clicking it yields a 404 response.
I am not overriding the RegistrationController.
Could it be the mailer settings?
swiftmailer:
~
spool: { type: memory }
The only other changes I've made were in validation of a few custom fields.
registration:
form:
type: acme_user_registration
validation_groups: [MyRegistration]
confirmation:
enabled: true
Any ideas what's going on?
Thanks.
I found a problem while viewing a confirmation email in outlook.com. I turns out that outlook.com is 'previewing' the link to show some expanded metadata in the email message. This has the side effect of enabling the user and deleting the token. Once the user actually clicks the link, they get a token not found error, but their account is enabled.

Resources