All Projects → EasyCorp → Easy Security Bundle

EasyCorp / Easy Security Bundle

Licence: mit
EasySecurityBundle

Projects that are alternatives of or similar to Easy Security Bundle

Neo4j Symfony
Symfony Bundle for the Neo4j Graph Database
Stars: ✭ 69 (-27.37%)
Mutual labels:  bundle, symfony, symfony-bundle
Swarrotbundle
A symfony bundle for swarrot integration
Stars: ✭ 89 (-6.32%)
Mutual labels:  bundle, symfony, symfony-bundle
Beelabrecaptcha2bundle
💻 Symfony bundle for Google Recaptcha2
Stars: ✭ 47 (-50.53%)
Mutual labels:  bundle, symfony, symfony-bundle
Sonatacachebundle
This bundle provides caching services
Stars: ✭ 66 (-30.53%)
Mutual labels:  bundle, symfony, symfony-bundle
Pugxgeneratorbundle
An enhancement of SensioGeneratorBundle
Stars: ✭ 58 (-38.95%)
Mutual labels:  bundle, symfony, symfony-bundle
Featureflagsbundle
Symfony2 Bundle to implement Feature Flags to your Application
Stars: ✭ 63 (-33.68%)
Mutual labels:  bundle, symfony, symfony-bundle
Twigextensionsbundle
Useful Twig extensions for your Symfony project.
Stars: ✭ 75 (-21.05%)
Mutual labels:  bundle, symfony, symfony-bundle
Ismaambrosigeneratorbundle
Generates Symfony2 documents, forms and CRUD for MongoDB documents
Stars: ✭ 27 (-71.58%)
Mutual labels:  bundle, symfony, symfony-bundle
Knpmenubundle
Object Oriented menus for your Symfony project.
Stars: ✭ 1,242 (+1207.37%)
Mutual labels:  bundle, symfony, symfony-bundle
Fmbbcodebundle
🔠 BBCode bundle for Symfony projects
Stars: ✭ 56 (-41.05%)
Mutual labels:  bundle, symfony, symfony-bundle
Foselasticabundle
Elasticsearch PHP integration for your Symfony project using Elastica.
Stars: ✭ 1,142 (+1102.11%)
Mutual labels:  bundle, symfony, symfony-bundle
Web Server Bundle
WebServerBundle provides commands for running applications using the PHP built-in web server. It simplifies your local development setup because you don't have to configure a proper web server such as Apache or Nginx to run your application.
Stars: ✭ 1,281 (+1248.42%)
Mutual labels:  bundle, symfony, symfony-bundle
Lexikpayboxbundle
LexikPayboxBundle eases the implementation of the Paybox payment system
Stars: ✭ 42 (-55.79%)
Mutual labels:  bundle, symfony, symfony-bundle
Cronos Bundle
Easy update your crontab by using @cron annotations in Symfony commands.
Stars: ✭ 73 (-23.16%)
Mutual labels:  bundle, symfony, symfony-bundle
Sncredisbundle
A Redis bundle for Symfony supporting Predis and PhpRedis
Stars: ✭ 980 (+931.58%)
Mutual labels:  bundle, symfony, symfony-bundle
Lexikcurrencybundle
This Symfony2 bundle provide a service and a Twig extension to convert and display currencies.
Stars: ✭ 59 (-37.89%)
Mutual labels:  bundle, symfony, symfony-bundle
Symfony
The Symfony PHP framework
Stars: ✭ 26,220 (+27500%)
Mutual labels:  bundle, symfony, symfony-bundle
Webfactoryicutranslationbundle
Enables ICU message formatting for translations in Symfony applications.
Stars: ✭ 27 (-71.58%)
Mutual labels:  bundle, symfony, symfony-bundle
Liipurlautoconverterbundle
[DEPRECATED] This bundle will add a Twig Extension for templates with a new filter for automatically converting urls and emails in a string to html links
Stars: ✭ 53 (-44.21%)
Mutual labels:  bundle, symfony, symfony-bundle
Sonataclassificationbundle
Symfony SonataClassificationBundle
Stars: ✭ 76 (-20%)
Mutual labels:  bundle, symfony, symfony-bundle

THIS BUNDLE IS NO LONGER MAINTAINED. SYMFONY 3.4 ADDED A SIMILAR FEATURE SO THIS IS NO LONGER NEEDED.


EasySecurityBundle

This bundle provides useful shortcuts to hide the Symfony Security component complexity.

Installation

Step 1: Download the Bundle

$ composer require easycorp/easy-security-bundle

This command requires you to have Composer installed globally, as explained in the Composer documentation.

Step 2: Enable the Bundle

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new EasyCorp\Bundle\EasySecurityBundle\EasySecurityBundle(),
        );
    }

    // ...
}

Basic Usage

Once installed, this bundle creates a new service called security that provides lots of shortcuts for the most common security operations. The main advantages over the Symfony Security component/bundle are:

1) It hides internal complexity

The Security component and bundle are some of the most complex Symfony pieces. They require you to learn lots of internal details that you probably don't care about:

// get the current user
$user = $this->get('security.token_storage')->getToken()->getUser();
// check their permissions
$user = $this->get('security.authorization_checker')->isGranted('ROLE_ADMIN');
// get the last login attempt error, if any
$error = $this->get('security.authentication_utils')->getLastAuthenticationError();

This bundle hides this complexity centralizing all the operations under the security service:

// get the current user
$user = $this->get('security')->getUser();
// check their permissions
$user = $this->get('security')->isGranted('ROLE_ADMIN');
// get the last login attempt error, if any
$error = $this->get('security')->getLoginError();

2) It makes code less verbose

Sometimes, the code needed to do common tasks is ridiculously verbose. For example, to login a user programmatically, Symfony requires you to do the following:

$user = ...
$token = new UsernamePasswordToken($user, $user->getPassword(), 'main', $user->getRoles());
$token->setAuthenticated(true);
$this->get('security.token_storage')->setToken($token);
$this->get('session')->set('_security_main', serialize($token));
$this->get('session')->save();

This bundle makes login a user as simple as it can be:

$user = ...
$this->get('security')->login($user);

3) It fixes some unintuitive behaviors

In Symfony applications, the way to check if a user is anonymous, remembered or fully authenticated doesn't work as most people expect. For example, if a user logs in with their username + password using a form login, this will happen:

// returns true
$this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_ANONYMOUSLY');
// returns true
$this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_REMEMBERED');
// returns true
$this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY');

Symfony grants the anonymous and remembered attributes to fully authenticated users, so it's complicated to differentiate between them. This bundle changes this unintuitive behavior and helps you know if a user is truly anonymous, remembered or authenticated. In the same example as before:

// returns false
$this->get('security')->isAnonymous();
// returns false
$this->get('security')->isRemembered();
// returns true
$this->get('security')->isFullyAuthenticated();

Injecting the security service

These shortcuts can be used across your application if you inject the security service. For example, if you define your services in YAML format:

# app/config/services.yml
services:
    app.my_service:
        # ...
        arguments: ['@security']

Then, update the constructor of your service to get the security service:

// src/AppBundle/MyService.php
// ...
use EasyCorp\Bundle\EasySecurityBundle\Security\Security;

class MyService
{
    private $security;

    public function __construct(Security $security)
    {
        $this->security = $security;
    }

    public function myMethod()
    {
        // ...
        $user = $this->security->getUser();
    }
}

List of Shortcuts

Getting users

  • getUser(): returns the current application user.
  • getImpersonatingUser(): when impersonating a user, it returns the original user who started the impersonation.

Checking permissions

  • isGranted($attributes, $object = null): checks if the attributes (usually security roles) are granted for the current application user and the optionally given object.
  • hasRole($role, $user = null): returns true if the current application user (or the optionally given user) has the given role. It takes into account the full role hierarchy.

Types of users

  • isAnonymous($user = null): returns true if the current application user (or the optionally given user) is anonymous. This behaves differently than Symfony built-in methods and it returns true only when the user is really anonymous.
  • isRemembered($user = null): returns true if the current application user (or the optionally given user) is remembered. This behaves differently than Symfony built-in methods and it returns true only when the user is really remembered and they haven't introduced their credentials (username and password).
  • isFullyAuthenticated($user = null): returns true if the current application user (or the optionally given user) is authenticated because they have introduced their credentials (username and password).
  • isAuthenticated($user = null): returns true if the current application user (or the optionally given user) is authenticated in any way (because they have introduced their credentials (username and password) or they have been remembered).

Login

  • login(UserInterface $user, $firewallName = 'main'): it logs in the given user in the main application firewall (or the optionally given firewall name).
  • getLoginError(): returns the error of the last failed login attempt, if any.
  • getLoginUsername(): returns the username of the last failed login attempt, if any.

Passwords

  • encodePassword($plainPassword, $user = null): returns the given plain password encoded/hashed using the encoder of the current application user or the optionally given user.
  • isPasswordValid($plainPassword, $user = null): returns true if the given plain password is valid for the current application user or the optionally given user.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].