php-censor/PHPCI/Controller/SessionController.php

82 lines
2.1 KiB
PHP

<?php
/**
* 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/
*/
namespace PHPCI\Controller;
use b8;
/**
* Session Controller - Handles user login / logout.
* @author Dan Cryer <dan@block8.co.uk>
* @package PHPCI
* @subpackage Web
*/
class SessionController extends \PHPCI\Controller
{
public function init()
{
$this->response->disableLayout();
$this->_userStore = b8\Store\Factory::getStore('User');
}
/**
* Handles user login (form and processing)
*/
public function login()
{
if ($this->request->getMethod() == 'POST') {
$user = $this->_userStore->getByEmail($this->getParam('email'));
if ($user && password_verify($this->getParam('password', ''), $user->getHash())) {
$_SESSION['user_id'] = $user->getId();
header('Location: ' . PHPCI_URL);
die;
}
}
$form = new b8\Form();
$form->setMethod('POST');
$form->setAction(PHPCI_URL.'session/login');
$email = new b8\Form\Element\Email('email');
$email->setLabel('Email Address');
$email->setRequired(true);
$email->setContainerClass('form-group');
$email->setClass('form-control');
$form->addField($email);
$pwd = new b8\Form\Element\Password('password');
$pwd->setLabel('Password');
$pwd->setRequired(true);
$pwd->setContainerClass('form-group');
$pwd->setClass('form-control');
$form->addField($pwd);
$pwd = new b8\Form\Element\Submit();
$pwd->setValue('Login &raquo;');
$pwd->setClass('btn-success');
$form->addField($pwd);
$this->view->form = $form->render();
return $this->view->render();
}
/**
* Handles user logout.
*/
public function logout()
{
$_SESSION = array();
session_destroy();
header('Location: ' . PHPCI_URL);
die;
}
}