Authorization
Authorization where is determined whether a user (clients are treated as users too) is allowed access to a certain resource/functionality.
Most commonly this happens based on a User Role or Authentication Status (Authenticated/Not Authenticated). Also it's quite common to validate whether a User or a Client is requesting a resource.
#
VotersTo determine whether access is granted system of Voters is used. Voters should implement the VoterInterface. All voters are automatically registered in the AccessDecisionManager. The AccessDecisionManager will ask all voters to vote on the provided subject. A voter can return three possible answers:
- ACCESS_GRANTED
- ACCESS_DENIED
- ACCESS_ABSTAIN
Abstain is relevant when the voter has no possible answer. E.g. the Authenticated Voter has no clue when there's asked for a vote on a User Role. In this case the voter would abstain from voting.
The component comes with default Voters on User Roles and User Authentication. No need to write custom Voters on this subject if you're not adding major extended functionality in that area.
#
User Role VoterUser Role Voter confirms whether a user has a certain role. Do not use Swift\Security\User\UserInterface::getRoles() to determine whether the user has a certain role as this only return the directly assigned roles. Roles can be related with each other or have a certain hierarchy. There are some default options:
#
Authenticated VoterThe Authenticated voter determines whether the current user or client has authenticated, but also how it authenticated. These are the options:
#
Custom VoterTo create a custom voter simply implement the Swift\Security\Authorization\Voter\VoterInterface
. This Interface is pre-tagged and will automatically register in the AccessDecisionManager.
Note: Make sure to return ACCESS_ABSTAIN if no vote could be made!
#
StrategiesBy default there's four possible strategies on voting.
Swift\Security\Authorization\Strategy\AffirmativeDecisionStrategy
Grants access if any voter returns an affirmative responseSwift\Security\Authorization\Strategy\ConsensusDecisionStrategy
Grants access if there is consensus of granted against denied responses.
Consensus means majority-rule (ignoring abstains) rather than unanimous agreement (ignoring abstains).Swift\Security\Authorization\Strategy\PriorityDecisionStrategy
Grant or deny access depending on the first voter that does not abstain.
The priority of voters can be used to overrule a decision.Swift\Security\Authorization\Strategy\UnanimousDecisionStrategy
Grants access if only grant (ignoring abstain) votes were received.
What if all voters abstain from voting? By default access is denied when all voters abstain from voting. The can be overruled in the configuration.
The default strategy is Swift\Security\Authorization\Strategy\AffirmativeDecisionStrategy
. This can be overruled on the configuration.
#
Custom strategyEasily create your own strategy by implementing the Swift\Security\Authorization\Strategy\DecisionStrategyInterface
. The Interface is pre-tagged and will automatically register the Strategy. To use it as default set it as default in the Security Configuration.
#
RolesRoles are used to represent the users authenticity and what the user is allowed to do. Roles can be defined in the configuration as below. The roles in de example below are already present by default. Custom Roles can be added. A role will automatically also have all it's child roles. So ROLE_CLIENT will also have ROLE_USERS_LIST. To take it a step further, ROLE_SUPER_ADMIN will also have ROLE_ADMIN and therefore also ROLE_USERS_LIST. See how this works now?
#
Assign role to authenticated userNow it's highly possible you'd want to assign a user more rights or validate the given rights of a user once it authenticates.
The proper way to do this is to listen to the authentication events and add/remove the appropriate roles.
Most appropriate events:
Swift\Security\Authentication\Events\AuthenticationTokenCreatedEvent
Token has been created, user authentication is not validated yetSwift\Security\Authentication\Events\AuthenticationSuccessEvent
Token has been created, authentication is successful. This usually makes the most sense.
Example
#
ConfigurationSecurity configuration happens through separate configuration file /etc/security.yaml.
#
UsageAuthorization validation is presented through a simple interface called the AuthorizationChecker. It comes with two methods.
Inject it with Swift\Security\Authorization\AuthorizationCheckerInterface $authorizationChecker
#
How to use#
Controller shortcutsControllers are equipped with some handy shortcuts. In a Controller you can directly call $this->denyAccessUnlessGranted()
.
However, even more useful for REST endpoints is the isGranted parameter on the Route Attribute. Below an example of the '/users/me/' endpoint.