8000 added the Security Component and its integration into the MVC framework · symfony/symfony@f216f31 · GitHub
[go: up one dir, main page]

Skip to content

Commit f216f31

Browse files
committed
added the Security Component and its integration into the MVC framework
Happy birthday symfony!
1 parent 0fc6b15 commit f216f31

File tree

110 files changed

+6308
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

110 files changed

+6308
-1
lines changed

src/Symfony/Bundle/DoctrineBundle/Resources/config/orm.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
<parameter key="doctrine.orm.xml_mapping_dirs">%doctrine.orm.metadata_driver.mapping_dirs%</parameter>
3939
<parameter key="doctrine.orm.yml_mapping_dirs">%doctrine.orm.metadata_driver.mapping_dirs%</parameter>
4040
<parameter key="doctrine.orm.metadata_driver.entity_dirs" type="collection"></parameter>
41+
42+
<!-- security/user -->
43+
<parameter key="security.user.provider.entity.class">Symfony\Bundle\DoctrineBundle\Security\EntityUserProvider</parameter>
4144
</parameters>
4245

4346
<services>
@@ -63,5 +66,7 @@
6366
<service id="doctrine.orm.metadata_driver.yml" class="%doctrine.orm.metadata.yml_class%">
6467
<argument>%doctrine.orm.metadata_driver.mapping_dirs%</argument>
6568
</service>
69+
70+
<service id="security.user.entity_manager" alias="doctrine.orm.default_entity_manager" />
6671
</services>
67-
</container>
72+
</container>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\DoctrineBundle\Security;
4+
5+
use Doctrine\ORM\EntityRepository;
6+
use Symfony\Component\Security\User\UserProviderInterface;
7+
use Symfony\Component\Security\Exception\UsernameNotFoundException;
8+
9+
class EntityUserProvider implements UserProviderInterface
10+
{
11+
protected $repository;
12+
protected $property;
13+
14+
public function __construct($em, $class, $property = null)
15+
{
16+
$this->repository = $em->getRepository($class);
17+
$this->property = $property;
18+
}
19+
20+
/**
21+
* {@inheritdoc}
22+
*/
23+
public function loadUserByUsername($username)
24+
{
25+
if (null !== $this->property) {
26+
$user = $this->repository->findOneBy(array($this->property => $username));
27+
} else {
28+
if (!$this->repository instanceof UserProviderInterface) {
29+
throw new \InvalidArgumentException('The Doctrine user manager must implement UserManagerInterface.');
30+
}
31+
32+
$user = $this->repository->loadUserByUsername($username);
33+
}
34+
35+
if (null === $user) {
36+
throw new UsernameNotFoundException(sprintf('User "%s" not found.', $username));
37+
}
38+
39+
return $user;
40+
}
41+
}

src/Symfony/Bundle/FrameworkBundle/Controller/ExceptionController.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ public function exceptionAction(FlattenException $exception, DebugLoggerInterfac
4242
$currentContent .= $content;
4343
}
4444

45+
if ('Symfony\Component\Security\Exception\AccessDeniedException' === $exception->getClass()) {
46+
$exception->setStatusCode($exception->getCode());
47+
}
48+
4549
$response = $this->container->get('templating')->renderResponse(
4650
'FrameworkBundle:Exception:'.($this->container->get('kernel')->isDebug() ? 'exception.php' : 'error.php'),
4751
array(
@@ -51,6 +55,7 @@ public function exceptionAction(FlattenException $exception, DebugLoggerInterfac
5155
'embedded' => $embedded,
5256
)
5357
);
58+
5459
$response->setStatusCode($exception->getStatusCode());
5560

5661
return $response;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\FrameworkBundle\Controller;
4+
5+
use Symfony\Component\DependencyInjection\ContainerAware;
6+
use Symfony\Component\Security\SecurityContext;
7+
8+
/*
9+
* This file is part of the Symfony framework.
10+
*
11+
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
12+
*
13+
* This source file is subject to the MIT license that is bundled
14+
* with this source code in the file LICENSE.
15+
*/
16+
17+
/**
18+
* SecurityController.
19+
*
20+
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
21+
*/
22+
class SecurityController extends ContainerAware
23+
{
24+
/**
25+
* Displays the login form.
26+
*
27+
* @return Response A Response instance
28+
*/
29+
public function loginAction()
30+
{
31+
$request = $this->container->get('request');
32+
if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
33+
$error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
34+
} else {
35+
$error = $request->getSession()->get(SecurityContext::AUTHENTICATION_ERROR);
36+
}
37+
38+
return $this->container->get('templating')->renderResponse('FrameworkBundle:Security:login.php', array(
39+
'last_username' => $request->getSession()->get(SecurityContext::LAST_USERNAME),
40+
'error' => $error,
41+
));
42+
}
43+
}

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Symfony\Component\DependencyInjection\Reference;
1010
use Symfony\Component\DependencyInjection\Definition;
1111
use Symfony\Component\Finder\Finder;
12+
use Symfony\Component\HttpFoundation\RequestMatcher;
1213

1314
/*
1415
* This file is part of the Symfony framework.
@@ -92,6 +93,11 @@ public function configLoad($config, ContainerBuilder $container)
9293
$this->registerTemplatingConfiguration($config, $container);
9394
}
9495

96+
if (isset($config['security'])) {
97+
$security = new SecurityLoader();
98+
$security->registerSecurityConfiguration($config, $container);
99+
}
100+
95101
if (array_key_exists('test', $config)) {
96102
$this->registerTestConfiguration($config, $container);
97103
}

0 commit comments

Comments
 (0)
0