php-censor/PHPCI/Controller/SessionController.php

79 lines
2 KiB
PHP
Raw Normal View History

2013-05-10 17:25:51 +02:00
<?php
2013-05-16 03:16:56 +02:00
/**
* PHPCI - Continuous Integration for PHP
*
* @copyright Copyright 2013, Block 8 Limited.
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
* @link http://www.phptesting.org/
*/
2013-05-10 17:25:51 +02:00
namespace PHPCI\Controller;
2013-05-10 17:25:51 +02:00
use b8;
/**
* Session Controller - Handles user login / logout.
* @author Dan Cryer <dan@block8.co.uk>
* @package PHPCI
* @subpackage Web
*/
2013-05-10 17:25:51 +02:00
class SessionController extends b8\Controller
{
public function init()
{
$this->_userStore = b8\Store\Factory::getStore('User');
}
2013-05-10 17:25:51 +02:00
/**
* Handles user login (form and processing)
*/
public function login()
{
if (b8\Registry::getInstance()->get('requestMethod') == 'POST') {
$user = $this->_userStore->getByEmail($this->getParam('email'));
2013-05-10 17:25:51 +02:00
if ($user && password_verify($this->getParam('password', ''), $user->getHash())) {
$_SESSION['user_id'] = $user->getId();
header('Location: /');
die;
}
}
2013-05-10 17:25:51 +02:00
$form = new b8\Form();
$form->setMethod('POST');
$form->setAction('/session/login');
2013-05-10 17:25:51 +02:00
$email = new b8\Form\Element\Email('email');
$email->setLabel('Email Address');
$email->setRequired(true);
$email->setClass('span3');
$form->addField($email);
2013-05-10 17:25:51 +02:00
$pwd = new b8\Form\Element\Password('password');
$pwd->setLabel('Password');
$pwd->setRequired(true);
$pwd->setClass('span3');
$form->addField($pwd);
2013-05-10 17:25:51 +02:00
$pwd = new b8\Form\Element\Submit();
$pwd->setValue('Login &raquo;');
$pwd->setClass('btn-success');
$form->addField($pwd);
2013-05-10 17:25:51 +02:00
$view = new b8\View('Login');
$view->form = $form->render();
die($view->render());
}
2013-05-10 17:25:51 +02:00
/**
* Handles user logout.
*/
public function logout()
{
unset($_SESSION['user_id']);
unset($_SESSION['github_token']);
header('Location: /');
die;
}
}