Advertisement
Jafix

LoginController.php

Nov 21st, 2012
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.53 KB | None | 0 0
  1. <?php
  2. namespace Acme\Demo\Controller;
  3.  
  4. use TYPO3\FLOW3\Annotations as FLOW3;
  5.  
  6. /**
  7.  * LoginController
  8.  * Handles all stuff that has to do with the login
  9.  */
  10. class LoginController extends \TYPO3\FLOW3\Mvc\Controller\ActionController {
  11.  
  12.     /**
  13.      * @var \TYPO3\FLOW3\Security\Authentication\AuthenticationManagerInterface
  14.      * @FLOW3\Inject
  15.      */
  16.     protected $authenticationManager;
  17.  
  18.     /**
  19.      * @var \TYPO3\FLOW3\Security\AccountRepository
  20.      * @FLOW3\Inject
  21.      */
  22.     protected $accountRepository;
  23.  
  24.     /**
  25.      * @var \TYPO3\FLOW3\Security\AccountFactory
  26.      * @FLOW3\Inject
  27.      */
  28.     protected $accountFactory;
  29.  
  30.     /**
  31.      * @var \TYPO3\FLOW3\Security\Context
  32.      */
  33.     protected $securityContext;
  34.  
  35.     /**
  36.      * Injects the security context
  37.      *
  38.      * @param \TYPO3\FLOW3\Security\Context $securityContext The security context
  39.      * @return void
  40.      */
  41.     public function injectSecurityContext(\TYPO3\FLOW3\Security\Context $securityContext) {
  42.         $this->securityContext = $securityContext;
  43.     }
  44.  
  45.     /**
  46.      * Index action
  47.      *
  48.      * @return void
  49.      */
  50.     public function indexAction() {
  51.         // example how to access account informations
  52.         $account = $this->securityContext->getAccount()->getRoles();
  53.         if($this->securityContext->hasRole('Visitor')){
  54.             $isset = 'Is set!';
  55.         }else{
  56.             $isset = 'Is not set!';
  57.         }
  58.         $this->view->assign('roles', $account);
  59.         $this->view->assign('isset', $isset);
  60.     }
  61.  
  62.     /**
  63.      * @throws \TYPO3\FLOW3\Security\Exception\AuthenticationRequiredException
  64.      * @return void
  65.      */
  66.     public function authenticateAction() {
  67.         try {
  68.             $this->authenticationManager->authenticate();
  69.             $this->flashMessageContainer->addMessage(new \TYPO3\FLOW3\Error\Error('Successfully logged in.'));
  70.             $this->redirect('index', 'Login');
  71.         } catch (\TYPO3\FLOW3\Security\Exception\AuthenticationRequiredException $exception) {
  72.             $this->flashMessageContainer->addMessage(new \TYPO3\FLOW3\Error\Error('Wrong username or password.'));
  73.             $this->flashMessageContainer->addMessage(new \TYPO3\FLOW3\Error\Error($exception->getMessage()));
  74.             throw $exception;
  75.         }
  76.     }
  77.  
  78.     /**
  79.      * @return void
  80.      */
  81.     public function registerAction() {
  82.         // do nothing more than display the register form
  83.     }
  84.  
  85.     /**
  86.      * save the registration
  87.      *
  88.      * @param string $name
  89.      * @param string $pass
  90.      * @param string $pass2
  91.      */
  92.     public function createAction($name, $pass, $pass2) {
  93.  
  94.         $defaultRole = 'Visitor';
  95.  
  96.         if ($name == '' || strlen($name) < 3) {
  97.             $this->flashMessageContainer->addMessage(new \TYPO3\FLOW3\Error\Error('Username too short or empty'));
  98.             $this->redirect('register', 'Login');
  99.         } else {
  100.             if ($pass == '' || $pass != $pass2) {
  101.                 $this->flashMessageContainer->addMessage(new \TYPO3\FLOW3\Error\Error('Password too short or does not match'));
  102.                 $this->redirect('register', 'Login');
  103.             } else {
  104.  
  105.                 // create a account with password an add it to the accountRepository
  106.                 $account = $this->accountFactory->createAccountWithPassword($name, $pass, array($defaultRole));
  107.                 $this->accountRepository->add($account);
  108.  
  109.                 // add a message and redirect to the login form
  110.                 $this->flashMessageContainer->addMessage(new \TYPO3\FLOW3\Error\Error('Account created. Please login.'));
  111.                 $this->redirect('index');
  112.             }
  113.         }
  114.  
  115.         // redirect to the login form
  116.         $this->redirect('index', 'Login');
  117.     }
  118.  
  119.     public function logoutAction() {
  120.         $this->authenticationManager->logout();
  121.         $this->flashMessageContainer->addMessage(new \TYPO3\FLOW3\Error\Error('Successfully logged out.'));
  122.         $this->redirect('index', 'Login');
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement