Security
Security
Security - User provider, firewall, access control, voters
Authentication:
Handled by firewalls and user providers.
Firewalls manage how authentication is performed and define security contexts.
User providers load user data based on credentials.
Authorization:
Managed by access control rules, roles, and voters.
Access control rules define which roles can access specific routes.
Roles represent user permissions and are assigned to users.
Voters implement custom access control logic for fine-grained permissions.
Users
create user entity
loading the user: the user provider.
Used
when login
At the beginning of each request, the user is loaded from the session
# config/packages/security.yaml security: # ... providers: app_user_provider: entity: class: App\Entity\User property: emailbuilt in providers: entity user provider, ldap user provider, memory user provider, chain user provider (merge of providers), custom provider
password hashing
# config/packages/security.yaml security: # ... password_hashers: # Use native password hasher, which auto-selects and migrates the best # possible hashing algorithm (which currently is "bcrypt") Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto'FosUser bundle??
Firewall
Firewall: Authentication system
The firewall defines which parts of your application are secured and how your users will be able to authenticate
The dev firewall is really a fake firewall: it makes sure that you don't accidentally block Symfony's dev tools - which live under URLs like /_profiler and /_wdt.
All real URLs are handled by the main firewall (no pattern key means it matches all URLs).
A firewall can have many modes of authentication
Authenticating Users
From login, json login, http basic, access tokens, login link
Authorization
Roles
There are 3 ways to deny access: access control, php code, attributes
access_control in security.yaml
php code
You can deny access to
Access control
match access control entry
enforce access restrictions
roles: If the user does not have the given role, then access is denied
allow_if: If the expression returns false, then access is denied;
requires_channel: If the incoming request's channel (e.g. http) does not match this value (e.g. https), the user will be redirected
Checking to see if a User is Logged In
If you only want to check if a user is logged in (you don't care about roles), you have the following two options.
Firstly, if you've given every user ROLE_USER, you can check for that role.
Secondly, you can use the special "attribute" IS_AUTHENTICATED_FULLY in place of a role:
Voters
Voters in Symfony are components used to implement complex authorization logic beyond simple role checks.
Affirmative: This strategy grants access as soon as any voter returns a positive response. If all voters abstain, access is denied.
Consensus: Access is granted if there are more grants than denies. Abstentions are not counted. If the number of grant and deny votes is equal, the decision is based on the
allowIfEqualGrantedDeniedconfiguration (grants access if true, denies otherwise).Unanimous: This requires all voters to grant access. If any voter denies access, or if all abstain, access is denied.