2017 © Pedro Peláez
 

library security-bundle

Effective Solutions (Pvt) Ltd. Security Bundle

image

effective-solutions/security-bundle

Effective Solutions (Pvt) Ltd. Security Bundle

  • Wednesday, August 5, 2015
  • by charithprsg
  • Repository
  • 1 Watchers
  • 0 Stars
  • 17 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Effective Solutions - Security Bundle

Installation

Use following command in command line to install Effective Solutions Security Bundle, (*1)

php composer.phar require effective-solutions/security-bundle, (*2)

After the installation add following in your AppKernel.php file, (*3)

new EffectiveSolutions\SecurityBundle\EffectiveSolutionsSecurityBundle(),, (*4)

Add following in app/config/routing.yml file, (*5)

effective_solutions_security:
    resource: "@EffectiveSolutionsSecurityBundle/Resources/config/routing.yml"
    prefix:   /

Usage

  • Create User.php file in your Entity folder and add following code.
<?php

namespace Your\AppBundle\Entity;
use EffectiveSolutions\SecurityBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table()
 */
class User extends BaseUser
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @ORM\ManyToOne(targetEntity="Role",cascade={"persist"})
     * @ORM\JoinColumn(name="role_id", referencedColumnName="id")
     **/
    private $role;

    /**
     * Set role
     *
     * @param \Base\DataAccessBundle\Entity\Role $role
     * @return User
     */
    public function setRole(\Base\DataAccessBundle\Entity\Role $role = null)
    {
        $this->role = $role;

        return $this;
    }

    /**
     * Get role
     *
     * @return \Base\DataAccessBundle\Entity\Role
     */
    public function getRole()
    {
        return $this->role;
    }

    /**
     * @return array
     */
    public function getRoles()
    {
        $roles =  array();
        if($this->getRole() != null)
            $roles[] = $this->getRole()->getMetacode();
        return $roles;
    }
}
  • Create Role.php file in your Entity folder and add following code.
<?php

namespace Your\AppBundle\Entity;
use EffectiveSolutions\SecurityBundle\Model\Role as BaseRole;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table()
 */
class Role extends BaseRole
{

}

  • Run following code to create your user and role tables in your database

php app/console doctrine:schema:update --force, (*6)

  • Add following code in your security.yml file
security:
    encoders:
        Base\DataAccessBundle\Entity\User:
            algorithm: sha512
            cost: 10

    providers:
        in_db:
            entity:
                class: Base\DataAccessBundle\Entity\User
                property: username
        in_memory:
            memory:
                users:
                    user:  { password: userpass, roles: [ 'ROLE_USER' ] }
                    admin: { password: adminpass, roles: [ 'ROLE_ADMIN' ] }


    firewalls:
        dev:
            pattern:  ^/(_(profiler|wdt)|css|images|js)/
            security: false


        secured_area:
            pattern:    ^/
            form_login:
                login_path: login_route
                check_path: login_check
                default_target_path: /
                failure_path: login_route
                csrf_provider: form.csrf_provider
            logout:
                path:   logout
                target: login_route
            anonymous: ~


    access_control:
        - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY}

  • Run following code in your command line to configure admin user. Admin username is admin and password is admin

php app/console register, (*7)

  • Then go to http://localhost/YourAppName/web/app_dev.php/login

With Sonata Admin Bundle

If you are using Sonata Admin Bundle, create BaseAdmin class and extend your admin classes from BaseAdmin class. Overide isGranted method in the BaseAdmin class as follows., (*8)

public function isGranted($name,$object=null){

        // overridden by effective security bundle
        if(!$this->getService('effective_security.role_handler')->isRouteGranted($this->getBaseRouteName()))
            return false;
        if(is_array($name))
        {
            foreach($name as $element)
            {
                if(!$this->getService('effective_security.role_handler')->isRouteGranted($this->getBaseRouteName().'_'.strtolower($element)))
                    return false;
            }
        }
        else
        {
            if(!$this->getService('effective_security.role_handler')->isRouteGranted($this->getBaseRouteName().'_'.strtolower($name)))
                return false;
        }

        return parent::isGranted($name,$object);
    }

Create roles_access_config.yml in your app/config folder and you can write access control logic in that. For example,, (*9)

admin_base_dataaccess_customer_create:
  - ROLE_SUPER_ADMIN

If you want to secure a controller, you can write route alias as above and call secure() in the Controller or isSecure() to check if the user has permission to access that controller., (*10)

Thank you for using Effective Solutions Security Bundle. Powered By EffectiveSolutions.lk, (*11)

The Versions

05/08 2015

dev-master

9999999-dev http://effectivesolutions.lk/

Effective Solutions (Pvt) Ltd. Security Bundle

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Charith Prasanga

security bundle

05/08 2015

0.0.3

0.0.3.0 http://effectivesolutions.lk/

Effective Solutions (Pvt) Ltd. Security Bundle

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Charith Prasanga

security bundle

24/07 2015

0.0.2

0.0.2.0 http://effectivesolutions.lk/

Effective Solutions (Pvt) Ltd. Security Bundle

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Charith Prasanga

security bundle

22/07 2015

0.0.1

0.0.1.0 http://effectivesolutions.lk/

Effective Solutions (Pvt) Ltd. Security Bundle

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Charith Prasanga

security bundle