Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Acme\Demo\Controller;
- use TYPO3\FLOW3\Annotations as FLOW3;
- /**
- * LoginController
- * Handles all stuff that has to do with the login
- */
- class LoginController extends \TYPO3\FLOW3\Mvc\Controller\ActionController {
- /**
- * @var \TYPO3\FLOW3\Security\Authentication\AuthenticationManagerInterface
- * @FLOW3\Inject
- */
- protected $authenticationManager;
- /**
- * @var \TYPO3\FLOW3\Security\AccountRepository
- * @FLOW3\Inject
- */
- protected $accountRepository;
- /**
- * @var \TYPO3\FLOW3\Security\AccountFactory
- * @FLOW3\Inject
- */
- protected $accountFactory;
- /**
- * @var \TYPO3\FLOW3\Security\Context
- */
- protected $securityContext;
- /**
- * Injects the security context
- *
- * @param \TYPO3\FLOW3\Security\Context $securityContext The security context
- * @return void
- */
- public function injectSecurityContext(\TYPO3\FLOW3\Security\Context $securityContext) {
- $this->securityContext = $securityContext;
- }
- /**
- * Index action
- *
- * @return void
- */
- public function indexAction() {
- // example how to access account informations
- $account = $this->securityContext->getAccount()->getRoles();
- if($this->securityContext->hasRole('Visitor')){
- $isset = 'Is set!';
- }else{
- $isset = 'Is not set!';
- }
- $this->view->assign('roles', $account);
- $this->view->assign('isset', $isset);
- }
- /**
- * @throws \TYPO3\FLOW3\Security\Exception\AuthenticationRequiredException
- * @return void
- */
- public function authenticateAction() {
- try {
- $this->authenticationManager->authenticate();
- $this->flashMessageContainer->addMessage(new \TYPO3\FLOW3\Error\Error('Successfully logged in.'));
- $this->redirect('index', 'Login');
- } catch (\TYPO3\FLOW3\Security\Exception\AuthenticationRequiredException $exception) {
- $this->flashMessageContainer->addMessage(new \TYPO3\FLOW3\Error\Error('Wrong username or password.'));
- $this->flashMessageContainer->addMessage(new \TYPO3\FLOW3\Error\Error($exception->getMessage()));
- throw $exception;
- }
- }
- /**
- * @return void
- */
- public function registerAction() {
- // do nothing more than display the register form
- }
- /**
- * save the registration
- *
- * @param string $name
- * @param string $pass
- * @param string $pass2
- */
- public function createAction($name, $pass, $pass2) {
- $defaultRole = 'Visitor';
- if ($name == '' || strlen($name) < 3) {
- $this->flashMessageContainer->addMessage(new \TYPO3\FLOW3\Error\Error('Username too short or empty'));
- $this->redirect('register', 'Login');
- } else {
- if ($pass == '' || $pass != $pass2) {
- $this->flashMessageContainer->addMessage(new \TYPO3\FLOW3\Error\Error('Password too short or does not match'));
- $this->redirect('register', 'Login');
- } else {
- // create a account with password an add it to the accountRepository
- $account = $this->accountFactory->createAccountWithPassword($name, $pass, array($defaultRole));
- $this->accountRepository->add($account);
- // add a message and redirect to the login form
- $this->flashMessageContainer->addMessage(new \TYPO3\FLOW3\Error\Error('Account created. Please login.'));
- $this->redirect('index');
- }
- }
- // redirect to the login form
- $this->redirect('index', 'Login');
- }
- public function logoutAction() {
- $this->authenticationManager->logout();
- $this->flashMessageContainer->addMessage(new \TYPO3\FLOW3\Error\Error('Successfully logged out.'));
- $this->redirect('index', 'Login');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement