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
{
/**
* init
*/
public function init()
{
$request =& $this->request;
@ -44,24 +47,22 @@ class Application extends b8\Application
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 () {
/** $parser = new Parser();
$yaml = file_get_contents(APPLICATION_PATH . 'PHPCI/config.yml');
$settings = $parser->parse($yaml);
if ((!empty($settings['phpci']['authentication_settings']['state'])
&& 1 == (int)$settings['phpci']['authentication_settings']['state'])
&& !empty($settings['phpci']['authentication_settings']['user_id'])
) {
$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($settings['phpci']['authentication_settings']['user_id']);
->getByPrimaryKey($id);
if ($user) {
$_SESSION['user'] = $user;
$_SESSION['phpci_user'] = $user;
return true;
}
}
*/
return false;
};
@ -88,8 +89,11 @@ class Application extends b8\Application
$this->router->clearRoutes();
$this->router->register($route, $opts, $routeHandler);
}
/**
* Handle an incoming web request.
*
* @return b8\b8\Http\Response|Response
*/
public function handleRequest()
{
@ -123,6 +127,10 @@ class Application extends b8\Application
return $this->response;
}
/**
* @param $class
* @return mixed
*/
protected function loadController($class)
{
$controller = parent::loadController($class);
@ -133,6 +141,9 @@ class Application extends b8\Application
return $controller;
}
/**
* @param View $layout
*/
protected function setLayoutVariables(View &$layout)
{
/** @var \PHPCI\Store\ProjectStore $projectStore */

View file

@ -19,14 +19,22 @@ use Symfony\Component\Yaml\Parser;
/**
* Settings Controller
*
* @author Dan Cryer <dan@block8.co.uk>
* @package PHPCI
* @subpackage Web
*/
class SettingsController extends Controller
{
/**
* @var array
*/
protected $settings;
/**
*
*/
public function init()
{
parent::init();
@ -36,6 +44,9 @@ class SettingsController extends Controller
$this->settings = $parser->parse($yaml);
}
/**
* @return string
*/
public function index()
{
$this->view->settings = $this->settings;
@ -50,10 +61,16 @@ class SettingsController extends Controller
$buildSettings = $this->settings['phpci']['build'];
}
$authenticationSettings = array();
if (isset($this->settings['phpci']['authentication_settings'])) {
$authenticationSettings = $this->settings['phpci']['authentication_settings'];
}
$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'])) {
$this->view->githubUser = $this->getGithubUser($this->settings['phpci']['github']['token']);
@ -62,6 +79,9 @@ class SettingsController extends Controller
return $this->view->render();
}
/**
* @throws \PHPCI\ForbiddenException
*/
public function github()
{
$this->requireAdmin();
@ -79,6 +99,9 @@ class SettingsController extends Controller
die;
}
/**
* @throws \PHPCI\ForbiddenException
*/
public function email()
{
$this->requireAdmin();
@ -97,6 +120,9 @@ class SettingsController extends Controller
die;
}
/**
* @throws \PHPCI\ForbiddenException
*/
public function build()
{
$this->requireAdmin();
@ -114,6 +140,29 @@ class SettingsController extends Controller
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
*/
@ -146,6 +195,7 @@ class SettingsController extends Controller
/**
* Convert config to yaml and store to file.
*
* @return mixed
*/
protected function storeSettings()
@ -160,6 +210,9 @@ class SettingsController extends Controller
}
}
/**
* @return Form
*/
protected function getGithubForm()
{
$form = new Form();
@ -199,6 +252,10 @@ class SettingsController extends Controller
return $form;
}
/**
* @param array $values
* @return Form
*/
protected function getEmailForm($values = array())
{
$form = new Form();
@ -269,6 +326,10 @@ class SettingsController extends Controller
return $form;
}
/**
* @param $token
* @return mixed
*/
protected function getGithubUser($token)
{
$http = new HttpClient('https://api.github.com');
@ -277,11 +338,18 @@ class SettingsController extends Controller
return $user['body'];
}
/**
* @return bool
*/
protected function canWriteConfig()
{
return is_writeable(APPLICATION_PATH . 'PHPCI/config.yml');
}
/**
* @param array $values
* @return Form
*/
protected function getBuildForm($values = array())
{
$form = new Form();
@ -314,6 +382,12 @@ class SettingsController extends Controller
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();

View file

@ -106,23 +106,17 @@
</div>
</div>
<div class="box">
<div class="row">
<div class="col-lg-12">
<h3 class="title">Authentication Settings</h3>
<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 preconfigured admin account for all actions within phpci with admin rights.
Be careful: This setting disables authentication and uses your current 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>