reimplemented disable user

This commit is contained in:
Marc Aschmann 2014-12-07 17:51:00 +01:00
parent 2df2ca5cb5
commit effd6909aa
3 changed files with 129 additions and 50 deletions

View file

@ -22,6 +22,9 @@ use PHPCI\Model\Build;
*/ */
class Application extends b8\Application class Application extends b8\Application
{ {
/**
* init
*/
public function init() public function init()
{ {
$request =& $this->request; $request =& $this->request;
@ -44,24 +47,22 @@ class Application extends b8\Application
return false; return false;
}; };
// load settings to check if there's a configured default user and auth disabled // Check settings for disable_authentication enabled and user_id
$skipAuth = function () { $skipAuth = function () {
/** $parser = new Parser(); $config = b8\Config::getInstance();
$yaml = file_get_contents(APPLICATION_PATH . 'PHPCI/config.yml'); $state = (bool)$config->get('phpci.authentication_settings.state', false);
$settings = $parser->parse($yaml); $id = $config->get('phpci.authentication_settings.user_id', 0);
if ((!empty($settings['phpci']['authentication_settings']['state'])
&& 1 == (int)$settings['phpci']['authentication_settings']['state']) if (false !== $state && 0 != (int)$id) {
&& !empty($settings['phpci']['authentication_settings']['user_id'])
) {
$user = b8\Store\Factory::getStore('User') $user = b8\Store\Factory::getStore('User')
->getByPrimaryKey($settings['phpci']['authentication_settings']['user_id']); ->getByPrimaryKey($id);
if ($user) { if ($user) {
$_SESSION['user'] = $user; $_SESSION['phpci_user'] = $user;
return true; return true;
} }
} }
*/
return false; return false;
}; };
@ -88,9 +89,12 @@ class Application extends b8\Application
$this->router->clearRoutes(); $this->router->clearRoutes();
$this->router->register($route, $opts, $routeHandler); $this->router->register($route, $opts, $routeHandler);
} }
/** /**
* Handle an incoming web request. * Handle an incoming web request.
*/ *
* @return b8\b8\Http\Response|Response
*/
public function handleRequest() public function handleRequest()
{ {
try { try {
@ -123,6 +127,10 @@ class Application extends b8\Application
return $this->response; return $this->response;
} }
/**
* @param $class
* @return mixed
*/
protected function loadController($class) protected function loadController($class)
{ {
$controller = parent::loadController($class); $controller = parent::loadController($class);
@ -133,6 +141,9 @@ class Application extends b8\Application
return $controller; return $controller;
} }
/**
* @param View $layout
*/
protected function setLayoutVariables(View &$layout) protected function setLayoutVariables(View &$layout)
{ {
/** @var \PHPCI\Store\ProjectStore $projectStore */ /** @var \PHPCI\Store\ProjectStore $projectStore */

View file

@ -19,23 +19,34 @@ 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;
/**
*
*/
public function init() public function init()
{ {
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);
} }
/**
* @return string
*/
public function index() public function index()
{ {
$this->view->settings = $this->settings; $this->view->settings = $this->settings;
@ -50,10 +61,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']);
@ -62,13 +79,16 @@ class SettingsController extends Controller
return $this->view->render(); return $this->view->render();
} }
/**
* @throws \PHPCI\ForbiddenException
*/
public function github() public function github()
{ {
$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');
@ -79,11 +99,14 @@ class SettingsController extends Controller
die; die;
} }
/**
* @throws \PHPCI\ForbiddenException
*/
public function email() public function email()
{ {
$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();
@ -97,6 +120,9 @@ class SettingsController extends Controller
die; die;
} }
/**
* @throws \PHPCI\ForbiddenException
*/
public function build() public function build()
{ {
$this->requireAdmin(); $this->requireAdmin();
@ -114,19 +140,42 @@ class SettingsController extends Controller
die; die;
} }
/**
* Handle authentication settings
*
* @throws \PHPCI\ForbiddenException
*/
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);
@ -146,12 +195,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()) {
@ -160,6 +210,9 @@ class SettingsController extends Controller
} }
} }
/**
* @return Form
*/
protected function getGithubForm() protected function getGithubForm()
{ {
$form = new Form(); $form = new Form();
@ -199,6 +252,10 @@ class SettingsController extends Controller
return $form; return $form;
} }
/**
* @param array $values
* @return Form
*/
protected function getEmailForm($values = array()) protected function getEmailForm($values = array())
{ {
$form = new Form(); $form = new Form();
@ -269,6 +326,10 @@ class SettingsController extends Controller
return $form; return $form;
} }
/**
* @param $token
* @return mixed
*/
protected function getGithubUser($token) protected function getGithubUser($token)
{ {
$http = new HttpClient('https://api.github.com'); $http = new HttpClient('https://api.github.com');
@ -277,11 +338,18 @@ class SettingsController extends Controller
return $user['body']; return $user['body'];
} }
/**
* @return bool
*/
protected function canWriteConfig() protected function canWriteConfig()
{ {
return is_writeable(APPLICATION_PATH . 'PHPCI/config.yml'); return is_writeable(APPLICATION_PATH . 'PHPCI/config.yml');
} }
/**
* @param array $values
* @return Form
*/
protected function getBuildForm($values = array()) protected function getBuildForm($values = array())
{ {
$form = new Form(); $form = new Form();
@ -294,10 +362,10 @@ 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);
@ -314,6 +382,12 @@ class SettingsController extends Controller
return $form; return $form;
} }
/**
* Form for disabling user authentication while using a default user
*
* @param array $values
* @return Form
*/
protected function getAuthenticationForm($values = array()) protected function getAuthenticationForm($values = array())
{ {
$form = new Form(); $form = new Form();

View file

@ -106,23 +106,17 @@
</div> </div>
</div> </div>
<div class="box"> <div class="box box-primary">
<div class="row"> <div class="box-header">
<div class="col-lg-12"> <h3 class="box-title">Authentication Settings</h3>
<h3 class="title">Authentication Settings</h3>
<p class="alert alert-warning clearfix">
Be careful: This setting disables authentication and uses your preconfigured admin account for all actions within phpci with admin rights.
</p>
</div>
<div class="col-lg-8">
<?php print $authenticationSettings; ?>
</div>
<div class="col-lg-4">
<!-- nothing -->
</div>
</div> </div>
</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>