Created
August 3, 2011 07:50
-
-
Save winzou/1122123 to your computer and use it in GitHub Desktop.
Abstract Controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Asso\AbstractBundle\Controller; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
use Symfony\Component\Security\Core\Exception\AccessDeniedException; | |
use Symfony\Component\Security\Core\User\UserInterface; | |
/** | |
* AbstractController | |
* @author winzou | |
*/ | |
abstract class AbstractController extends Controller | |
{ | |
/** | |
* Get a user from the security context | |
* | |
* @throws AccessDeniedException if no user is authenticated | |
* @return User | |
*/ | |
protected function getUser() | |
{ | |
$user = $this->container->get('security.context')->getToken()->getUser(); | |
if( ! is_object($user) || ! $user instanceof UserInterface ) | |
{ | |
throw new AccessDeniedException('User does not have access to this page.'); | |
} | |
return $user; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Asso\BookBundle\Controller; | |
use Asso\AbstractBundle\Controller\AbstractController; | |
/** | |
* BookController | |
* @author winzou | |
*/ | |
class BookController extends AbstractController | |
{ | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment