Skip to content

Commit

Permalink
added the Security Component and its integration into the MVC framework
Browse files Browse the repository at this point in the history
Happy birthday symfony!
  • Loading branch information
fabpot committed Oct 19, 2010
1 parent 0fc6b15 commit f216f31
Show file tree
Hide file tree
Showing 110 changed files with 6,308 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Symfony/Bundle/DoctrineBundle/Resources/config/orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
<parameter key="doctrine.orm.xml_mapping_dirs">%doctrine.orm.metadata_driver.mapping_dirs%</parameter>
<parameter key="doctrine.orm.yml_mapping_dirs">%doctrine.orm.metadata_driver.mapping_dirs%</parameter>
<parameter key="doctrine.orm.metadata_driver.entity_dirs" type="collection"></parameter>

<!-- security/user -->
<parameter key="security.user.provider.entity.class">Symfony\Bundle\DoctrineBundle\Security\EntityUserProvider</parameter>
</parameters>

<services>
Expand All @@ -63,5 +66,7 @@
<service id="doctrine.orm.metadata_driver.yml" class="%doctrine.orm.metadata.yml_class%">
<argument>%doctrine.orm.metadata_driver.mapping_dirs%</argument>
</service>

<service id="security.user.entity_manager" alias="doctrine.orm.default_entity_manager" />
</services>
</container>
</container>
41 changes: 41 additions & 0 deletions src/Symfony/Bundle/DoctrineBundle/Security/EntityUserProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Symfony\Bundle\DoctrineBundle\Security;

use Doctrine\ORM\EntityRepository;
use Symfony\Component\Security\User\UserProviderInterface;
use Symfony\Component\Security\Exception\UsernameNotFoundException;

class EntityUserProvider implements UserProviderInterface
{
protected $repository;
protected $property;

public function __construct($em, $class, $property = null)
{
$this->repository = $em->getRepository($class);
$this->property = $property;
}

/**
* {@inheritdoc}
*/
public function loadUserByUsername($username)
{
if (null !== $this->property) {
$user = $this->repository->findOneBy(array($this->property => $username));
} else {
if (!$this->repository instanceof UserProviderInterface) {
throw new \InvalidArgumentException('The Doctrine user manager must implement UserManagerInterface.');
}

$user = $this->repository->loadUserByUsername($username);
}

if (null === $user) {
throw new UsernameNotFoundException(sprintf('User "%s" not found.', $username));
}

return $user;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public function exceptionAction(FlattenException $exception, DebugLoggerInterfac
$currentContent .= $content;
}

if ('Symfony\Component\Security\Exception\AccessDeniedException' === $exception->getClass()) {
$exception->setStatusCode($exception->getCode());
}

$response = $this->container->get('templating')->renderResponse(
'FrameworkBundle:Exception:'.($this->container->get('kernel')->isDebug() ? 'exception.php' : 'error.php'),
array(
Expand All @@ -51,6 +55,7 @@ public function exceptionAction(FlattenException $exception, DebugLoggerInterfac
'embedded' => $embedded,
)
);

$response->setStatusCode($exception->getStatusCode());

return $response;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Symfony\Bundle\FrameworkBundle\Controller;

use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\Security\SecurityContext;

/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

/**
* SecurityController.
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class SecurityController extends ContainerAware
{
/**
* Displays the login form.
*
* @return Response A Response instance
*/
public function loginAction()
{
$request = $this->container->get('request');
if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
$error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
} else {
$error = $request->getSession()->get(SecurityContext::AUTHENTICATION_ERROR);
}

return $this->container->get('templating')->renderResponse('FrameworkBundle:Security:login.php', array(
'last_username' => $request->getSession()->get(SecurityContext::LAST_USERNAME),
'error' => $error,
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpFoundation\RequestMatcher;

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

if (isset($config['security'])) {
$security = new SecurityLoader();

This comment has been minimized.

Copy link
@blue-eyes

blue-eyes Oct 22, 2010

Hi, it seems the SecurityLoader class has been forgotten

This comment has been minimized.

Copy link
@fabpot

fabpot Oct 22, 2010

Author Owner

This is obsolete code. I will remove it. thanks.

$security->registerSecurityConfiguration($config, $container);
}

if (array_key_exists('test', $config)) {
$this->registerTestConfiguration($config, $container);
}
Expand Down
Loading

16 comments on commit f216f31

@henrikbjorn
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ROCK ON!

@immutef
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

THIS IS SPARTA! ... ERR SYMFONY!

@svenwin
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy B-Day!

@docteurklein
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yay! Happy 5th b-day Symfony ! Thanks for the present :)

@jseverson
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I smell an application refactor coming...nice work Fabien. Looks amazing.

@WhiteOwlUk
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonderful!

@avalanche123
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well done, Fabien, your hard work is much appreciated!

@datiecher
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent stuff, Fabien!

Happy 5th birthday!

@DavertMik
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Fabien! Symfony is great!
Happy 5ymfony day!

@tecbot
Copy link

@tecbot tecbot commented on f216f31 Nov 8, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why you set in the AnonymousToken class the authenticated status to true if the user is a anonymous user? The documentation says a anonymous user is not authenticated and check if a user is fully-authenticated with the isAuthenticated() of the security context. But the isAuthenticated() of the security context return always true if the user is a anonymous user.

@beberlei
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 on that question, i have the same problem with this.

@schmittjoh
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isAuthenticated() is supposed to be used internally only, not by the end user. Roles are used for authorization. The following is directly from Spring Security:
/**
* Used to indicate to {@code AbstractSecurityInterceptor} whether it should present the
* authentication token to the AuthenticationManager. Typically an AuthenticationManager
* (or, more often, one of its AuthenticationProviders) will return an immutable authentication token
* after successful authentication, in which case that token can safely return true to this method.
* Returning true will improve performance, as calling the AuthenticationManager for
* every request will no longer be necessary.
*


* For security reasons, implementations of this interface should be very careful about returning
* true from this method unless they are either immutable, or have some way of ensuring the properties
* have not been changed since original creation.
*
* @return true if the token has been authenticated and the AbstractSecurityInterceptor does not need
* to present the token to the AuthenticationManager again for re-authentication.
*/

@tecbot
Copy link

@tecbot tecbot commented on f216f31 Nov 8, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok but how can I now test whether the user is not an anonymous user? Currently, I call for two functions. First isAuthenticated () and then getUser() and check if it is not == 'anon.' . I think that it can not be useful right?

@schmittjoh
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use the IS_AUTHENTICATED_FULLY role.

@tecbot
Copy link

@tecbot tecbot commented on f216f31 Nov 8, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you it works :)

@fabpot
Copy link
Owner Author

@fabpot fabpot commented on f216f31 Nov 9, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the confusion. I have just fixed the documentation: symfony/symfony-docs@434a1de

Please sign in to comment.