Skip to content

Instantly share code, notes, and snippets.

@guilhermeblanco
Created January 21, 2014 04:02
Show Gist options
  • Save guilhermeblanco/8534291 to your computer and use it in GitHub Desktop.
Save guilhermeblanco/8534291 to your computer and use it in GitHub Desktop.
<?php
namespace My\AdminBundle\Controller;
use My\AdminBundle\Form\Model\AuthenticationFormModel;
use My\AdminBundle\Form\Type\AuthenticationFormType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\SecurityContext;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
/**
* Authentication Controller
*
* @Route("/authentication")
*/
class AuthenticationController extends Controller
{
/**
* @Route("/login", name="admin_authentication_login")
*/
public function loginAction(Request $request)
{
$response = $this->buildLoginResponse();
if ($response->isNotModified($request)) {
return $response;
}
$loginForm = $this->buildLoginForm($request);
$template = 'MyAdminBundle:Authentication:login.html.twig';
$parameters = array(
'login_form' => $loginForm->createView(),
'authentication_error' => $this->getAuthenticationError($request),
);
return $this->render($template, $parameters, $response);
}
/**
* @Route("/check", name="admin_authentication_check")
*/
public function checkAction()
{
// The security layer will intercept this request
}
/**
* @Route("/logout", name="admin_authentication_logout")
*/
public function logoutAction()
{
// The security layer will intercept this request
}
/**
* Build initial login response.
*
* @return \Symfony\Component\HttpFoundation\Response
*/
private function buildLoginResponse()
{
$response = new Response();
$lastModified = new \DateTime('@' . filemtime(__FILE__));
$expires = new \DateTime('@' . strtotime('+2 hours', $lastModified->getTimestamp()));
$response->setLastModified($lastModified);
$response->setExpires($expires);
$response->setMaxAge($expires->format('D, d M Y H:i:s').' GMT');
$response->setSharedMaxAge($expires->format('D, d M Y H:i:s').' GMT');
$response->setPublic();
return $response;
}
private function buildLoginForm(Request $request)
{
$session = $request->getSession();
$lastUsername = $session->get(SecurityContext::LAST_USERNAME);
$loginFormType = new AuthenticationFormType();
$loginFormModel = new AuthenticationFormModel();
$loginFormConfig = array(
'action' => $this->generateUrl('admin_authentication_check'),
'last_username' => $lastUsername,
);
return $this->createForm($loginFormType, $loginFormModel, $loginFormConfig);
}
/**
* Retrieve last authentication error message
*
* @param \Symfony\Component\HttpFoundation\Request $request
*
* @return string
*/
private function getAuthenticationError(Request $request)
{
if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
return $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
}
$session = $request->getSession();
return $session->get(SecurityContext::AUTHENTICATION_ERROR);
}
}
<?php
namespace My\AdminBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
/**
* Dashboard Controller
*
* @Route("/dashboard")
*/
class DashboardController extends Controller
{
/**
* @Route("/home", name="admin_dashboard_home")
* @Security("has_role('ROLE_ADMIN_USER')")
* @Template()
*/
public function homeAction(Request $request)
{
$kernel = $this->get('kernel');
return array('bundle_list' => $kernel->getBundles());
}
}
my_admin:
resource: "@MyAdminBundle/Controller/"
type: 'annotation'
prefix: '/admin'
security:
session_fixation_strategy: 'migrate'
hide_user_not_found: true
always_authenticate_before_granting: false
erase_credentials: true
access_decision_manager:
strategy: 'affirmative'
allow_if_all_abstain: false
allow_if_equal_granted_denied: true
acl:
connection: 'default'
cache:
id: 'apc'
tables:
class: 'acl_classes'
entry: 'acl_entries'
object_identity: 'acl_object_identities'
object_identity_ancestors: 'acl_object_identity_ancestors'
security_identity: 'acl_security_identities'
voter:
allow_if_object_identity_unavailable: false
encoders:
My\AdminBundle\Entity\User:
algorithm: 'pbkdf2'
hash_algorithm: 'sha512'
iterations: 1000
encode_as_base64: true
providers:
admin_user_db:
id: 'my_admin.security.user_provider'
firewalls:
dev:
pattern: '^/(_(profiler|wdt)|css|images|js|vendor)/'
security: false
login_area:
pattern: '^/admin/authentication/login'
anonymous: ~
security: false
admin_area:
pattern: '^/admin'
provider: 'admin_user_db'
access_denied_url: "/admin/authentication/login"
stateless: false
form_login:
use_forward: false
always_use_default_target_path: false
default_target_path: 'admin_dashboard_home'
check_path: 'admin_authentication_check'
login_path: 'admin_authentication_login'
username_parameter: 'authentication[username]'
password_parameter: 'authentication[password]'
post_only: true
remember_me: false
logout:
invalidate_session: false
path: 'admin_authentication_logout'
target: 'admin_authentication_login'
role_hierarchy:
ROLE_ADMIN_USER: 'IS_AUTHENTICATED_FULLY'
access_control:
- { path: '^/admin/authentication/login', role: 'IS_AUTHENTICATED_ANONYMOUSLY' }
- { path: '^/', role: 'IS_AUTHENTICATED_REMEMBERED' }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment