PHP code example of crocos / security-bundle

1. Go to this page and download the library: Download crocos/security-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

crocos / security-bundle example snippets


public function registerBundles()
{
    $bundles = array(
        // ...
        new Crocos\SecurityBundle\CrocosSecurityBundle(),

    );
}



use Crocos\SecurityBundle\Annotation\Secure;
use Crocos\SecurityBundle\Annotation\SecureConfig;

/**
 * @SecureConfig(forward="CrocosAppBundle:Security:login")
 */
abstract class AppController
{
}

class SampleController extends AppController
{
    /**
     * @Secure
     */
    public function securedAction()
    {
        $user = $this->get('crocos_security.context')->getUser();
    }
}

/**
 * @Secure
 */
class SecurityController extends AppController
{
    public function login(Request $request)
    {
        $user = $this->findUser($request->request->get('username'), $request->request->get('password'));

        $this->get('crocos_security.context')->login($user);

        return $this->redirect('/');
    }

    public function logout()
    {
        $this->get('crocos_security.context')->logout();

        return $this->redirect('/login');
    }
}



namespace Crocos\AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Crocos\SecurityBundle\Annotation\Secure;
use Crocos\SecurityBundle\Annotation\SecureConfig;

/**
 * @SecureConfig(forward="CrocosAppBundle:Accont:login")
 */
abstract class AppController extends Controller
{
    protected function getUser()
    {
        return $this->get('crocos_security.context')->getUser();
    }
}

/**
 * @Route("/product")
 */
class ProductController extends AppController
{
    /**
     * @Route("/{id}", ('doctrine')->getRepository('CrocosAppBundle:User')
                ->findUser($username, $password);

            $this->get('crocos_security.context')->login($user);

            return $this->redirect('/');
        }

        return array();
    }
}



namespace Crocos\AppBundle\Controller\Admin;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Crocos\SecurityBundle\Annotation\Secure;
use Crocos\SecurityBundle\Annotation\SecureConfig;

/**
 * @Secure
 * @SecureConfig(domain="admin", forward="CrocosAppBundle:Admin\Accont:login")
 */
abstract class AppController extends Controller
{
    protected function getUser()
    {
        return $this->get('crocos_security.context')->getUser();
    }
}

/**
 * @Route("/admin")
 */
class AccountController extends AppController
{
    /**
     * @Route("/login")
     */
    public function loginAction(Request $request)
    {
        // ...
    }
}



namespace Crocos\AppBundle\Controller\Admin;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Crocos\SecurityBundle\Annotation\Secure;
use Crocos\SecurityBundle\Annotation\SecureConfig;

/**
 * @SecureConfig(domain="admin", basic="admin:password")
 */
abstract class AppController extends Controller
{
}

$user = $userRepository->findUser('Katsuhiro Ogawa');
$this->get('crocos_security.context')->login($user);

if ($this->get('crocos_security.context')->isAuthenticated()) {
    echo 'ログインしています';
}

$user = $this->get('crocos_security.context')->getUser();

$this->get('crocos_security.context')->logout();

$this->get('crocos_security.context')->getUser();           // => null
$this->get('crocos_security.context')->isAuthenticated();   // => false

/**
 * @SecureConfig(auth="my_auth")
 */
class AppController
{
}



namespace Crocos\AppBundle\Controller;

use Crocos\SecurityBundle\Annotation\Secure;
use Crocos\SecurityBundle\Annotation\SecureConfig;
use Crocos\SecurityBundle\Exception\AuthException;

/**
 * @SecureConfig(forward="CrocosAppBundle:Demo:login")
 */
class AppController
{
}

class DemoController extends AppController
{
    /**
     * @Secure
     */
    public function someAction($id)
    {
        if ($this->hasSomeError()) {
            throw new AuthException('Login