Fixing merge conflict

This commit is contained in:
Dan Cryer 2014-05-09 15:56:34 +01:00
commit af0d5147ea
8 changed files with 120 additions and 44 deletions

View file

@ -10,6 +10,7 @@
namespace PHPCI; namespace PHPCI;
use b8; use b8;
use b8\Exception\HttpException;
use b8\Http\Response; use b8\Http\Response;
use b8\Http\Response\RedirectResponse; use b8\Http\Response\RedirectResponse;
use b8\View; use b8\View;
@ -70,7 +71,25 @@ class Application extends b8\Application
*/ */
public function handleRequest() public function handleRequest()
{ {
$this->response = parent::handleRequest(); try {
$this->response = parent::handleRequest();
} catch (HttpException $ex) {
$this->config->set('page_title', 'Error');
$view = new View('exception');
$view->exception = $ex;
$this->response->setResponseCode($ex->getErrorCode());
$this->response->setContent($view->render());
} catch (\Exception $ex) {
$this->config->set('page_title', 'Error');
$view = new View('exception');
$view->exception = $ex;
$this->response->setResponseCode(500);
$this->response->setContent($view->render());
}
if (View::exists('layout') && $this->response->hasLayout()) { if (View::exists('layout') && $this->response->hasLayout()) {
$view = new View('layout'); $view = new View('layout');

View file

@ -21,11 +21,16 @@ class BuildFactory
/** /**
* @param $buildId * @param $buildId
* @return Build * @return Build
* @throws \Exception
*/ */
public static function getBuildById($buildId) public static function getBuildById($buildId)
{ {
$build = Factory::getStore('Build')->getById($buildId); $build = Factory::getStore('Build')->getById($buildId);
if (empty($build)) {
throw new \Exception('Build ID ' . $buildId . ' does not exist.');
}
return self::getBuild($build); return self::getBuild($build);
} }

View file

@ -10,6 +10,7 @@
namespace PHPCI\Controller; namespace PHPCI\Controller;
use b8; use b8;
use b8\Exception\HttpException\NotFoundException;
use PHPCI\BuildFactory; use PHPCI\BuildFactory;
use PHPCI\Model\Build; use PHPCI\Model\Build;
@ -36,7 +37,16 @@ class BuildController extends \PHPCI\Controller
*/ */
public function view($buildId) public function view($buildId)
{ {
$build = BuildFactory::getBuildById($buildId); try {
$build = BuildFactory::getBuildById($buildId);
} catch (\Exception $ex) {
$build = null;
}
if (empty($build)) {
throw new NotFoundException('Build with ID: ' . $buildId . ' does not exist.');
}
$this->view->plugins = $this->getUiPlugins(); $this->view->plugins = $this->getUiPlugins();
$this->view->build = $build; $this->view->build = $build;
$this->view->data = $this->getBuildData($build); $this->view->data = $this->getBuildData($build);
@ -110,6 +120,10 @@ class BuildController extends \PHPCI\Controller
{ {
$copy = BuildFactory::getBuildById($buildId); $copy = BuildFactory::getBuildById($buildId);
if (empty($copy)) {
throw new NotFoundException('Build with ID: ' . $buildId . ' does not exist.');
}
$build = new Build(); $build = new Build();
$build->setProjectId($copy->getProjectId()); $build->setProjectId($copy->getProjectId());
$build->setCommitId($copy->getCommitId()); $build->setCommitId($copy->getCommitId());
@ -134,11 +148,10 @@ class BuildController extends \PHPCI\Controller
throw new \Exception('You do not have permission to do that.'); throw new \Exception('You do not have permission to do that.');
} }
$build = BuildFactory::getBuildById($buildId); $build = BuildFactory::getBuildById($buildId);
if (!$build) { if (empty($build)) {
$this->response->setResponseCode(404); throw new NotFoundException('Build with ID: ' . $buildId . ' does not exist.');
return '404 - Not Found';
} }
$this->buildStore->delete($build); $this->buildStore->delete($build);

View file

@ -65,7 +65,8 @@ class BuildStatusController extends \PHPCI\Controller
public function view($projectId) public function view($projectId)
{ {
$project = $this->projectStore->getById($projectId); $project = $this->projectStore->getById($projectId);
if (!$project) {
if (empty($project)) {
throw new NotFoundException('Project with id: ' . $projectId . ' not found'); throw new NotFoundException('Project with id: ' . $projectId . ' not found');
} }

View file

@ -9,17 +9,17 @@
namespace PHPCI\Controller; namespace PHPCI\Controller;
use b8;
use b8\Controller;
use b8\Form;
use b8\Exception\HttpException\ForbiddenException;
use b8\Exception\HttpException\NotFoundException;
use b8\Store;
use PHPCI\BuildFactory; use PHPCI\BuildFactory;
use PHPCI\Helper\Github; use PHPCI\Helper\Github;
use PHPCI\Helper\SshKey; use PHPCI\Helper\SshKey;
use PHPCI\Model\Build; use PHPCI\Model\Build;
use PHPCI\Model\Project; use PHPCI\Model\Project;
use b8;
use b8\Config;
use b8\Controller;
use b8\Store;
use b8\Form;
use b8\Exception\HttpException\NotFoundException;
/** /**
* Project Controller - Allows users to create, edit and view projects. * Project Controller - Allows users to create, edit and view projects.
@ -41,8 +41,8 @@ class ProjectController extends \PHPCI\Controller
public function init() public function init()
{ {
$this->buildStore = Store\Factory::getStore('Build'); $this->buildStore = Store\Factory::getStore('Build');
$this->projectStore = Store\Factory::getStore('Project'); $this->projectStore = Store\Factory::getStore('Project');
} }
/** /**
@ -51,17 +51,18 @@ class ProjectController extends \PHPCI\Controller
public function view($projectId) public function view($projectId)
{ {
$project = $this->projectStore->getById($projectId); $project = $this->projectStore->getById($projectId);
if (!$project) {
if (empty($project)) {
throw new NotFoundException('Project with id: ' . $projectId . ' not found'); throw new NotFoundException('Project with id: ' . $projectId . ' not found');
} }
$page = $this->getParam('p', 1); $page = $this->getParam('p', 1);
$builds = $this->getLatestBuildsHtml($projectId, (($page - 1) * 10)); $builds = $this->getLatestBuildsHtml($projectId, (($page - 1) * 10));
$this->view->builds = $builds[0]; $this->view->builds = $builds[0];
$this->view->total = $builds[1]; $this->view->total = $builds[1];
$this->view->project = $project; $this->view->project = $project;
$this->view->page = $page; $this->view->page = $page;
$this->config->set('page_title', $project->getTitle()); $this->config->set('page_title', $project->getTitle());
@ -76,6 +77,10 @@ class ProjectController extends \PHPCI\Controller
/* @var \PHPCI\Model\Project $project */ /* @var \PHPCI\Model\Project $project */
$project = $this->projectStore->getById($projectId); $project = $this->projectStore->getById($projectId);
if (empty($project)) {
throw new NotFoundException('Project with id: ' . $projectId . ' not found');
}
$build = new Build(); $build = new Build();
$build->setProjectId($projectId); $build->setProjectId($projectId);
$build->setCommitId('Manual'); $build->setCommitId('Manual');
@ -96,7 +101,7 @@ class ProjectController extends \PHPCI\Controller
public function delete($projectId) public function delete($projectId)
{ {
if (!$_SESSION['user']->getIsAdmin()) { if (!$_SESSION['user']->getIsAdmin()) {
throw new \Exception('You do not have permission to do that.'); throw new ForbiddenException('You do not have permission to do that.');
} }
$project = $this->projectStore->getById($projectId); $project = $this->projectStore->getById($projectId);
@ -120,10 +125,10 @@ class ProjectController extends \PHPCI\Controller
*/ */
protected function getLatestBuildsHtml($projectId, $start = 0) protected function getLatestBuildsHtml($projectId, $start = 0)
{ {
$criteria = array('project_id' => $projectId); $criteria = array('project_id' => $projectId);
$order = array('id' => 'DESC'); $order = array('id' => 'DESC');
$builds = $this->buildStore->getWhere($criteria, 10, $start, array(), $order); $builds = $this->buildStore->getWhere($criteria, 10, $start, array(), $order);
$view = new b8\View('BuildsTable'); $view = new b8\View('BuildsTable');
foreach ($builds['items'] as &$build) { foreach ($builds['items'] as &$build) {
$build = BuildFactory::getBuild($build); $build = BuildFactory::getBuild($build);
@ -142,7 +147,7 @@ class ProjectController extends \PHPCI\Controller
$this->config->set('page_title', 'Add Project'); $this->config->set('page_title', 'Add Project');
if (!$_SESSION['user']->getIsAdmin()) { if (!$_SESSION['user']->getIsAdmin()) {
throw new \Exception('You do not have permission to do that.'); throw new ForbiddenException('You do not have permission to do that.');
} }
$method = $this->request->getMethod(); $method = $this->request->getMethod();
@ -210,11 +215,15 @@ class ProjectController extends \PHPCI\Controller
public function edit($projectId) public function edit($projectId)
{ {
if (!$_SESSION['user']->getIsAdmin()) { if (!$_SESSION['user']->getIsAdmin()) {
throw new \Exception('You do not have permission to do that.'); throw new ForbiddenException('You do not have permission to do that.');
} }
$method = $this->request->getMethod(); $method = $this->request->getMethod();
$project = $this->projectStore->getById($projectId); $project = $this->projectStore->getById($projectId);
if (empty($project)) {
throw new NotFoundException('Project with id: ' . $projectId . ' not found');
}
$this->config->set('page_title', 'Edit: ' . $project->getTitle()); $this->config->set('page_title', 'Edit: ' . $project->getTitle());
@ -234,7 +243,7 @@ class ProjectController extends \PHPCI\Controller
} }
$form = $this->projectForm($values, 'edit/' . $projectId); $form = $this->projectForm($values, 'edit/' . $projectId);
if ($method != 'POST' || ($method == 'POST' && !$form->validate())) { if ($method != 'POST' || ($method == 'POST' && !$form->validate())) {
$view = new b8\View('ProjectForm'); $view = new b8\View('ProjectForm');

View file

@ -10,6 +10,8 @@
namespace PHPCI\Controller; namespace PHPCI\Controller;
use b8; use b8;
use b8\Exception\HttpException\ForbiddenException;
use b8\Exception\HttpException\NotFoundException;
use b8\Form; use b8\Form;
use PHPCI\Controller; use PHPCI\Controller;
use PHPCI\Model\User; use PHPCI\Model\User;
@ -106,12 +108,11 @@ class UserController extends Controller
public function add() public function add()
{ {
if (!$_SESSION['user']->getIsAdmin()) { if (!$_SESSION['user']->getIsAdmin()) {
throw new \Exception('You do not have permission to do that.'); throw new ForbiddenException('You do not have permission to do that.');
} }
$this->config->set('page_title', 'Add User'); $this->config->set('page_title', 'Add User');
$method = $this->request->getMethod(); $method = $this->request->getMethod();
if ($method == 'POST') { if ($method == 'POST') {
@ -132,7 +133,6 @@ class UserController extends Controller
} }
$values = $form->getValues(); $values = $form->getValues();
$values['is_admin'] = $values['admin'] ? 1 : 0;
$values['hash'] = password_hash($values['password'], PASSWORD_DEFAULT); $values['hash'] = password_hash($values['password'], PASSWORD_DEFAULT);
$user = new User(); $user = new User();
@ -150,13 +150,15 @@ class UserController extends Controller
public function edit($userId) public function edit($userId)
{ {
if (!$_SESSION['user']->getIsAdmin()) { if (!$_SESSION['user']->getIsAdmin()) {
throw new \Exception('You do not have permission to do that.'); throw new ForbiddenException('You do not have permission to do that.');
} }
$method = $this->request->getMethod(); $method = $this->request->getMethod();
$user = $this->userStore->getById($userId); $user = $this->userStore->getById($userId);
$this->config->set('page_title', 'Edit: ' . $user->getName()); if (empty($user)) {
throw new NotFoundException('User with ID: ' . $userId . ' does not exist.');
}
$values = array_merge($user->getDataArray(), $this->getParams()); $values = array_merge($user->getDataArray(), $this->getParams());
$form = $this->userForm($values, 'edit/' . $userId); $form = $this->userForm($values, 'edit/' . $userId);
@ -170,13 +172,17 @@ class UserController extends Controller
return $view->render(); return $view->render();
} }
$values['is_admin'] = $values['admin'] ? 1 : 0;
if (!empty($values['password'])) { if (!empty($values['password'])) {
$values['hash'] = password_hash($values['password'], PASSWORD_DEFAULT); $values['hash'] = password_hash($values['password'], PASSWORD_DEFAULT);
} }
$user->setValues($values); $user->setValues($values);
$isAdmin = $this->getParam('is_admin');
if (empty($isAdmin)) {
$user->setIsAdmin(0);
}
$this->userStore->save($user); $this->userStore->save($user);
header('Location: '.PHPCI_URL.'user'); header('Location: '.PHPCI_URL.'user');
@ -208,8 +214,15 @@ class UserController extends Controller
$form->addField($field); $form->addField($field);
$field = new Form\Element\Password('password'); $field = new Form\Element\Password('password');
$field->setRequired(true);
$field->setLabel('Password' . ($type == 'edit' ? ' (leave blank to keep current password)' : '')); if ($type == 'add') {
$field->setRequired(true);
$field->setLabel('Password');
} else {
$field->setRequired(false);
$field->setLabel('Password (leave blank to keep current password)');
}
$field->setClass('form-control'); $field->setClass('form-control');
$field->setContainerClass('form-group'); $field->setContainerClass('form-group');
$form->addField($field); $form->addField($field);
@ -236,10 +249,15 @@ class UserController extends Controller
public function delete($userId) public function delete($userId)
{ {
if (!$_SESSION['user']->getIsAdmin()) { if (!$_SESSION['user']->getIsAdmin()) {
throw new \Exception('You do not have permission to do that.'); throw new ForbiddenException('You do not have permission to do that.');
} }
$user = $this->userStore->getById($userId); $user = $this->userStore->getById($userId);
if (empty($user)) {
throw new NotFoundException('User with ID: ' . $userId . ' does not exist.');
}
$this->userStore->delete($user); $this->userStore->delete($user);
header('Location: '.PHPCI_URL.'user'); header('Location: '.PHPCI_URL.'user');

View file

@ -0,0 +1,9 @@
<div class="panel panel-danger">
<div class="panel-heading">
<h2 class="panel-title">Sorry, there was a problem</h2>
</div>
<div class="panel-body">
<?php print $exception->getMessage(); ?>
</div>
</div>

View file

@ -59,8 +59,10 @@ if (!file_exists(dirname(__FILE__) . '/vendor/autoload.php') && defined('PHPCI_I
// Load Composer autoloader: // Load Composer autoloader:
require_once(dirname(__FILE__) . '/vendor/autoload.php'); require_once(dirname(__FILE__) . '/vendor/autoload.php');
$loggerConfig = LoggerConfig::newFromFile(__DIR__ . "/loggerconfig.php"); if (defined('PHPCI_IS_CONSOLE') && PHPCI_IS_CONSOLE) {
Handler::register($loggerConfig->getFor('_')); $loggerConfig = LoggerConfig::newFromFile(__DIR__ . "/loggerconfig.php");
Handler::register($loggerConfig->getFor('_'));
}
// Load configuration if present: // Load configuration if present:
$conf = array(); $conf = array();