athome-solution / user-bundle
Bundle for manage user
Requires
- php: >=7.1
- symfony/form: *
- symfony/framework-bundle: *
- symfony/orm-pack: *
- symfony/security-bundle: *
- symfony/validator: *
Requires (Dev)
- dev-master
- v2.2.0
- v2.1.1
- v2.1.0
- v2.0.3
- v2.0.2
- 2.0.1
- v2.0.0
- v1.2.3.x-dev
- v1.2.3
- v1.2.2
- v1.2.1
- v1.2.0
- v1.1.0
- v1.0.1
- v1.0.0
- dev-password-request
- dev-symfony5-compatibility
- dev-hotfix/email-as-text
- dev-password-constraints
- dev-sf4
- dev-twig-compatibility
- dev-doctrine-compatibility
- dev-AHUBS
- dev-mailer-fix
- dev-develop
- dev-release/v1.0.0
- dev-feature/orm
This package is not auto-updated.
Last update: 2024-12-20 12:01:55 UTC
README
AthomeUserBundle allows you to add simple user / authentication process to your Symfony 4 application. The bundle provides :
- an authentication system (form login) + logout
- a registration process (with a confirmation process by email or not)
- a forgot password process (by sending an email)
- a "edit account" feature
Getting Started With AthomeUserBundle
Configuration
in config/packages/athome_user.yaml :
athome_user:
user_class: App\Entity\User
from_email: 'no-reply@email.com'
from_name: 'Athome-Solution' #not mandatory : default to null
enable_on_registration: false #not mandatory : default to true
in config/routes.yaml :
app_security:
resource: '@AthomeUserBundle/Resources/config/routing/security.yaml'
in config/bundles.php :
Athome\UserBundle\AthomeUserBundle::class => ['all' => true],
in config/packages/security.yaml :
security:
encoders:
App\Entity\User:
algorithm: bcrypt
security:
providers:
users:
entity:
# the class of the entity that represents users
class: 'App\Entity\User'
# the property to query by - e.g. username, email, etc
property: 'email'
security:
access_control:
- { path: ^/$, roles: IS_AUTHENTICATED_ANONYMOUSLY } # racine
# user bundle
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/password, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/account, roles: IS_AUTHENTICATED_FULLY }
# user bundle end
You can use the bundle raw authenticator or inherit it.
firewalls:
main:
guard:
authenticators: [ Athome\UserBundle\Security\Authenticator ]
logout:
path: user_bundle_security_logout
target: user_bundle_security_login
in config/packages/doctrine.yaml
doctrine:
# ...
orm:
# ...
auto_mapping: false
mappings:
# ...
App:
type: xml
dir: '%kernel.project_dir%/src/Resources/config/doctrine'
Extend the user model
<?php
namespace App\Entity;
use Athome\UserBundle\Model\User as BaseUser;
class User extends BaseUser
{
public $property;
public $anotherProperty;
// ...
Overriding the bundle
In order to override any part of the bundle, you can extend the base class or use service decoration
ex: (src/Security/Authenticator.php)
<?php
namespace App\Security;
use Athome\UserBundle\Security\Authenticator as BaseAuthenticator;
class Authenticator extends BaseAuthenticator
{
// Inherit functions as needed
}
ex: (config/services.yaml)
App\Form\RegisterType:
decorates: Athome\UserBundle\Form\Type\RegisterType
Overriding templates :
Just follow the same directory structure
ex: to override register, create template in templates/bundles/AthomeUserBundle/security/register.html.twig
Events
With every action come an event which carries an object, the request and the response.
For example, if you want to keep the bundle default behavior for register, but you want to add business logic :
- Create a subscriber (src/EventSubscriber/RegisterSubscriber.php) Redirect after register use RegisterEvent->setResponse()
public static function getSubscribedEvents()
{
return [
UserEvents::REGISTRATION_SUCCESSFUL => 'onRegistrationSuccessful'
];
}
/**
* @param UserEvent $event
*/
public function onRegistrationSuccessful(UserEvent $event)
{
$user = $event->getUser();
// Authenticate user
$token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
$this->tokenStorage->setToken($token);
$this->session->set('_security_main', serialize($token));
// Redirect user
$event->setResponse(new RedirectResponse($this->router->generate('homepage')));
}
List of available events:
REGISTRATION_SUCCESSFUL : Fired when registration is completed
REGISTRATION_CONFIRMED : Fired when account is activated
PASSWORD_REQUEST_SUCCESSFUL : Fired when reset password email has been sent
PASSWORD_RESET_SUCCESSFUL : Fired when password has been reset
ACCOUNT_UPDATE_SUCCESSFUL : Fired when account has been updated