Merge fixes

This commit is contained in:
Dan Cryer 2014-12-18 10:16:31 +00:00
commit d52e64d7f3
3 changed files with 128 additions and 26 deletions

View file

@ -47,11 +47,30 @@ class Application extends b8\Application
return false; return false;
}; };
// Check settings for disable_authentication enabled and user_id
$skipAuth = function () {
$config = b8\Config::getInstance();
$state = (bool)$config->get('phpci.authentication_settings.state', false);
$id = $config->get('phpci.authentication_settings.user_id', 0);
if (false !== $state && 0 != (int)$id) {
$user = b8\Store\Factory::getStore('User')
->getByPrimaryKey($id);
if ($user) {
$_SESSION['phpci_user'] = $user;
return true;
}
}
return false;
};
// Handler for the route we're about to register, checks for a valid session where necessary: // Handler for the route we're about to register, checks for a valid session where necessary:
$routeHandler = function (&$route, Response &$response) use (&$request, $validateSession) { $routeHandler = function (&$route, Response &$response) use (&$request, $validateSession, $skipAuth) {
$skipValidation = in_array($route['controller'], array('session', 'webhook', 'build-status')); $skipValidation = in_array($route['controller'], array('session', 'webhook', 'build-status'));
if (!$skipValidation && !$validateSession()) { if (!$skipValidation && !$validateSession() && !$skipAuth()) {
if ($request->isAjax()) { if ($request->isAjax()) {
$response->setResponseCode(401); $response->setResponseCode(401);
$response->setContent(''); $response->setContent('');
@ -72,8 +91,10 @@ class Application extends b8\Application
} }
/** /**
* Handle an incoming web request. * Handle an incoming web request.
*/ *
* @return b8\b8\Http\Response|Response
*/
public function handleRequest() public function handleRequest()
{ {
try { try {

View file

@ -19,12 +19,17 @@ use Symfony\Component\Yaml\Parser;
/** /**
* Settings Controller * Settings Controller
*
* @author Dan Cryer <dan@block8.co.uk> * @author Dan Cryer <dan@block8.co.uk>
* @package PHPCI * @package PHPCI
* @subpackage Web * @subpackage Web
*/ */
class SettingsController extends Controller class SettingsController extends Controller
{ {
/**
* @var array
*/
protected $settings; protected $settings;
/** /**
@ -34,8 +39,8 @@ class SettingsController extends Controller
{ {
parent::init(); parent::init();
$parser = new Parser(); $parser = new Parser();
$yaml = file_get_contents(APPLICATION_PATH . 'PHPCI/config.yml'); $yaml = file_get_contents(APPLICATION_PATH . 'PHPCI/config.yml');
$this->settings = $parser->parse($yaml); $this->settings = $parser->parse($yaml);
} }
@ -45,9 +50,6 @@ class SettingsController extends Controller
*/ */
public function index() public function index()
{ {
$this->requireAdmin();
$this->layout->title = 'Settings';
$this->view->settings = $this->settings; $this->view->settings = $this->settings;
$emailSettings = array(); $emailSettings = array();
@ -60,10 +62,16 @@ class SettingsController extends Controller
$buildSettings = $this->settings['phpci']['build']; $buildSettings = $this->settings['phpci']['build'];
} }
$this->view->github = $this->getGithubForm(); $authenticationSettings = array();
$this->view->emailSettings = $this->getEmailForm($emailSettings); if (isset($this->settings['phpci']['authentication_settings'])) {
$this->view->buildSettings = $this->getBuildForm($buildSettings); $authenticationSettings = $this->settings['phpci']['authentication_settings'];
$this->view->isWriteable = $this->canWriteConfig(); }
$this->view->github = $this->getGithubForm();
$this->view->emailSettings = $this->getEmailForm($emailSettings);
$this->view->buildSettings = $this->getBuildForm($buildSettings);
$this->view->isWriteable = $this->canWriteConfig();
$this->view->authenticationSettings = $this->getAuthenticationForm($authenticationSettings);
if (!empty($this->settings['phpci']['github']['token'])) { if (!empty($this->settings['phpci']['github']['token'])) {
$this->view->githubUser = $this->getGithubUser($this->settings['phpci']['github']['token']); $this->view->githubUser = $this->getGithubUser($this->settings['phpci']['github']['token']);
@ -79,9 +87,9 @@ class SettingsController extends Controller
{ {
$this->requireAdmin(); $this->requireAdmin();
$this->settings['phpci']['github']['id'] = $this->getParam('githubid', ''); $this->settings['phpci']['github']['id'] = $this->getParam('githubid', '');
$this->settings['phpci']['github']['secret'] = $this->getParam('githubsecret', ''); $this->settings['phpci']['github']['secret'] = $this->getParam('githubsecret', '');
$error = $this->storeSettings(); $error = $this->storeSettings();
if ($error) { if ($error) {
header('Location: ' . PHPCI_URL . 'settings?saved=2'); header('Location: ' . PHPCI_URL . 'settings?saved=2');
@ -99,7 +107,7 @@ class SettingsController extends Controller
{ {
$this->requireAdmin(); $this->requireAdmin();
$this->settings['phpci']['email_settings'] = $this->getParams(); $this->settings['phpci']['email_settings'] = $this->getParams();
$this->settings['phpci']['email_settings']['smtp_encryption'] = $this->getParam('smtp_encryption', 0); $this->settings['phpci']['email_settings']['smtp_encryption'] = $this->getParam('smtp_encryption', 0);
$error = $this->storeSettings(); $error = $this->storeSettings();
@ -133,19 +141,40 @@ class SettingsController extends Controller
die; die;
} }
/**
* Handle authentication settings
*/
public function authentication()
{
$this->requireAdmin();
$this->settings['phpci']['authentication_settings']['state'] = $this->getParam('disable_authentication', 0);
$this->settings['phpci']['authentication_settings']['user_id'] = $_SESSION['phpci_user_id'];
$error = $this->storeSettings();
if ($error) {
header('Location: ' . PHPCI_URL . 'settings?saved=2');
} else {
header('Location: ' . PHPCI_URL . 'settings?saved=1');
}
die;
}
/** /**
* Github redirects users back to this URL when t * Github redirects users back to this URL when t
*/ */
public function githubCallback() public function githubCallback()
{ {
$code = $this->getParam('code', null); $code = $this->getParam('code', null);
$github = $this->settings['phpci']['github']; $github = $this->settings['phpci']['github'];
if (!is_null($code)) { if (!is_null($code)) {
$http = new HttpClient(); $http = new HttpClient();
$url = 'https://github.com/login/oauth/access_token'; $url = 'https://github.com/login/oauth/access_token';
$params = array('client_id' => $github['id'], 'client_secret' => $github['secret'], 'code' => $code); $params = array('client_id' => $github['id'], 'client_secret' => $github['secret'], 'code' => $code);
$resp = $http->post($url, $params); $resp = $http->post($url, $params);
if ($resp['success']) { if ($resp['success']) {
parse_str($resp['body'], $resp); parse_str($resp['body'], $resp);
@ -165,12 +194,13 @@ class SettingsController extends Controller
/** /**
* Convert config to yaml and store to file. * Convert config to yaml and store to file.
*
* @return mixed * @return mixed
*/ */
protected function storeSettings() protected function storeSettings()
{ {
$dumper = new Dumper(); $dumper = new Dumper();
$yaml = $dumper->dump($this->settings, 4); $yaml = $dumper->dump($this->settings, 4);
file_put_contents(APPLICATION_PATH . 'PHPCI/config.yml', $yaml); file_put_contents(APPLICATION_PATH . 'PHPCI/config.yml', $yaml);
if (error_get_last()) { if (error_get_last()) {
@ -336,16 +366,52 @@ class SettingsController extends Controller
$field->setClass('form-control'); $field->setClass('form-control');
$field->setContainerClass('form-group'); $field->setContainerClass('form-group');
$field->setOptions([ $field->setOptions([
300 => '5 Minutes', 300 => '5 Minutes',
900 => '15 Minutes', 900 => '15 Minutes',
1800 => '30 Minutes', 1800 => '30 Minutes',
3600 => '1 Hour', 3600 => '1 Hour',
10800 => '3 Hours', 10800 => '3 Hours',
]); ]);
$field->setValue(1800); $field->setValue(1800);
$form->addField($field); $form->addField($field);
$field = new Form\Element\Submit();
$field->setValue('Save &raquo;');
$field->setClass('btn btn-success pull-right');
$form->addField($field);
$form->setValues($values);
return $form;
}
/**
* Form for disabling user authentication while using a default user
*
* @param array $values
* @return Form
*/
protected function getAuthenticationForm($values = array())
{
$form = new Form();
$form->setMethod('POST');
$form->setAction(PHPCI_URL . 'settings/authentication');
$form->addField(new Form\Element\Csrf('csrf'));
$field = new Form\Element\Checkbox('disable_authentication');
$field->setCheckedValue(1);
$field->setRequired(false);
$field->setLabel('Disable Authentication?');
$field->setContainerClass('form-group');
$field->setValue(0);
if (isset($values['state'])) {
$field->setValue((int)$values['state']);
}
$form->addField($field);
$field = new Form\Element\Submit(); $field = new Form\Element\Submit();
$field->setValue('Save &raquo;'); $field->setValue('Save &raquo;');
$field->setClass('btn btn-success pull-right'); $field->setClass('btn btn-success pull-right');

View file

@ -104,4 +104,19 @@
<?php print $emailSettings; ?> <?php print $emailSettings; ?>
</div> </div>
</div> </div>
<div class="box box-primary">
<div class="box-header">
<h3 class="box-title">Authentication Settings</h3>
</div>
<div class="box-body clearfix">
<p class="alert alert-warning clearfix">
Be careful: This setting disables authentication and uses your current admin account for all actions within phpci with admin rights.
</p>
<?php print $authenticationSettings; ?>
</div>
</div>