Finished authentication

Signed-off-by: Gergely POLONKAI (W00d5t0ck) <polesz@w00d5t0ck.info>
This commit is contained in:
Polonkai Gergely 2012-07-02 22:41:06 +02:00
parent f2370faa68
commit e1fde17057
16 changed files with 214 additions and 20 deletions

View File

@ -20,6 +20,7 @@ class AppKernel extends Kernel
new JMS\DiExtraBundle\JMSDiExtraBundle($this), new JMS\DiExtraBundle\JMSDiExtraBundle($this),
new JMS\SecurityExtraBundle\JMSSecurityExtraBundle(), new JMS\SecurityExtraBundle\JMSSecurityExtraBundle(),
new KekRozsak\FrontBundle\KekRozsakFrontBundle(), new KekRozsak\FrontBundle\KekRozsakFrontBundle(),
new KekRozsak\SecurityBundle\KekRozsakSecurityBundle(),
); );
if (in_array($this->getEnvironment(), array('dev', 'test'))) { if (in_array($this->getEnvironment(), array('dev', 'test'))) {

View File

@ -1,3 +1,7 @@
KekRozsakSecurityBundle:
resource: "@KekRozsakSecurityBundle/Resources/config/routing.yml"
prefix: /
KekRozsakFrontBundle: KekRozsakFrontBundle:
resource: "@KekRozsakFrontBundle/Resources/config/routing.yml" resource: "@KekRozsakFrontBundle/Resources/config/routing.yml"
prefix: / prefix: /

View File

@ -5,17 +5,18 @@ jms_security_extra:
security: security:
encoders: encoders:
Symfony\Component\Security\Core\User\User: plaintext Symfony\Component\Security\Core\User\User: plaintext
KekRozsak\FrontBundle\Entity\User:
id: kek_rozsak_security.encoder.crypt
role_hierarchy: role_hierarchy:
ROLE_ADMIN: ROLE_USER ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH] ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
providers: providers:
in_memory: user_db:
memory: entity:
users: class: KekRozsak\FrontBundle\Entity\User
user: { password: userpass, roles: [ 'ROLE_USER' ] } property: username
admin: { password: adminpass, roles: [ 'ROLE_ADMIN' ] }
firewalls: firewalls:
dev: dev:
@ -23,21 +24,23 @@ security:
security: false security: false
login: login:
pattern: ^/demo/secured/login$ pattern: ^/login$
security: false security: false
secured_area: members:
pattern: ^/demo/secured/ pattern: ^/
form_login: form_login:
check_path: /demo/secured/login_check check_path: /login_check
login_path: /demo/secured/login login_path: /login
logout: logout:
path: /demo/secured/logout path: /logout
target: /demo/ target: /
#anonymous: ~ anonymous: ~
#http_basic:
# realm: "Secured Demo Area"
access_control: access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/$, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/cikk/, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/, roles: [ IS_AUTHENTICATED_FULLY, IS_AUTHENTICATED_REMEMBERED ] }
#- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https } #- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }
#- { path: ^/_internal/secure, roles: IS_AUTHENTICATED_ANONYMOUSLY, ip: 127.0.0.1 } #- { path: ^/_internal/secure, roles: IS_AUTHENTICATED_ANONYMOUSLY, ip: 127.0.0.1 }

View File

@ -26,4 +26,9 @@ class DefaultController extends Controller
'article' => $article 'article' => $article
)); ));
} }
public function forumMainAction()
{
return $this->forward('KekRozsakFrontBundle:Default:homepage');
}
} }

View File

@ -3,11 +3,12 @@
namespace KekRozsak\FrontBundle\Entity; namespace KekRozsak\FrontBundle\Entity;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\Role\RoleInterface;
/** /**
* KekRozsak\FrontBundle\Entity\Role * KekRozsak\FrontBundle\Entity\Role
*/ */
class Role class Role implements RoleInterface
{ {
/** /**
* @var integer $id * @var integer $id
@ -136,4 +137,9 @@ class Role
{ {
return $this->included_roles; return $this->included_roles;
} }
public function getRole()
{
return $this->name;
}
} }

View File

@ -3,11 +3,12 @@
namespace KekRozsak\FrontBundle\Entity; namespace KekRozsak\FrontBundle\Entity;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
/** /**
* KekRozsak\FrontBundle\Entity\User * KekRozsak\FrontBundle\Entity\User
*/ */
class User class User implements UserInterface
{ {
/** /**
* @var integer $id * @var integer $id
@ -188,6 +189,15 @@ class User
*/ */
public function getRoles() public function getRoles()
{ {
return $this->roles; return $this->roles->toArray();
}
public function eraseCredentials()
{
}
public function getSalt()
{
return $this->password;
} }
} }

View File

@ -7,3 +7,8 @@ KekRozsakFrontBundle_article:
pattern: /cikk/{articleSlug} pattern: /cikk/{articleSlug}
defaults: defaults:
_controller: KekRozsakFrontBundle:Default:article _controller: KekRozsakFrontBundle:Default:article
KekRozsakFrontBundle_forum_main:
pattern: /forum
defaults:
_controller: KekRozsakFrontBundle:Default:forumMain

View File

@ -0,0 +1,30 @@
<?php
namespace KekRozsak\SecurityBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\SecurityContext;
class DefaultController extends Controller
{
public function loginAction()
{
$request = $this->getRequest();
$session = $request->getSession();
if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR))
{
$error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
}
else
{
$error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
$session->remove(SecurityContext::AUTHENTICATION_ERROR);
}
return $this->render('KekRozsakSecurityBundle:Default:login.html.twig', array(
'last_username' => $session->get(SecurityContext::LAST_USERNAME),
'error' => $error,
));
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace KekRozsak\SecurityBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
/**
* This is the class that validates and merges configuration from your app/config files
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
*/
class Configuration implements ConfigurationInterface
{
/**
* {@inheritDoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('kek_rozsak_security');
// Here you should define the parameters that are allowed to
// configure your bundle. See the documentation linked above for
// more information on that topic.
return $treeBuilder;
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace KekRozsak\SecurityBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
/**
* This is the class that loads and manages your bundle configuration
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
*/
class KekRozsakSecurityExtension extends Extension
{
/**
* {@inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
}
}

View File

@ -0,0 +1,9 @@
<?php
namespace KekRozsak\SecurityBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class KekRozsakSecurityBundle extends Bundle
{
}

View File

@ -0,0 +1,10 @@
KekRozsakSecurityBundle_login:
pattern: /login
defaults:
_controller: KekRozsakSecurityBundle:Default:login
KekRozsakSecurityBundle_login_check:
pattern: /login_check
KekRozsakSecurityBundle_logout:
pattern: /logout

View File

@ -0,0 +1,6 @@
parameters:
# kek_rozsak_security.example.class: KekRozsak\SecurityBundle\Example
services:
kek_rozsak_security.encoder.crypt:
class: KekRozsak\SecurityBundle\Service\CryptEncoder

View File

@ -0,0 +1,13 @@
{% extends '::main_template.html.twig' %}
{% block title %}- Bejelentkezés{% endblock %}
{% block content %}
{% if error %}
<div id="error-message">{{ error.message }}</div>
{% endif %}
<form action="{{ path('KekRozsakSecurityBundle_login_check') }}" method="post">
<input type="text" id="username" name="_username" value="{{ last_username }}" />
<input type="password" id="password" name="_password" />
<button type="submit">Bejelentkezés</button>
</form>
{% endblock content %}

View File

@ -0,0 +1,18 @@
<?php
namespace KekRozsak\SecurityBundle\Service;
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
class CryptEncoder implements PasswordEncoderInterface
{
function encodePassword($raw, $salt)
{
return crypt($raw);
}
function isPasswordValid($encoded, $raw, $salt)
{
return (crypt($raw, $salt) == $encoded);
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace KekRozsak\SecurityBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class DefaultControllerTest extends WebTestCase
{
public function testIndex()
{
$client = static::createClient();
$crawler = $client->request('GET', '/hello/Fabien');
$this->assertTrue($crawler->filter('html:contains("Hello Fabien")')->count() > 0);
}
}