Add the possiblity to regenerate all tokens

This commit is contained in:
Frédéric Guillot
2014-03-30 21:21:16 -04:00
parent d9dfd9d619
commit 99953ab62a
5 changed files with 136 additions and 6 deletions

View File

@@ -4,9 +4,19 @@ namespace Controller;
require_once __DIR__.'/base.php'; require_once __DIR__.'/base.php';
/**
* Config controller
*
* @package controller
* @author Frederic Guillot
*/
class Config extends Base class Config extends Base
{ {
// Settings page /**
* Display the settings page
*
* @access public
*/
public function index() public function index()
{ {
$this->response->html($this->template->layout('config_index', array( $this->response->html($this->template->layout('config_index', array(
@@ -22,7 +32,11 @@ class Config extends Base
))); )));
} }
// Validate and save settings /**
* Validate and save settings
*
* @access public
*/
public function save() public function save()
{ {
$values = $this->request->getValues(); $values = $this->request->getValues();
@@ -53,18 +67,38 @@ class Config extends Base
))); )));
} }
// Download the database /**
* Download the Sqlite database
*
* @access public
*/
public function downloadDb() public function downloadDb()
{ {
$this->response->forceDownload('db.sqlite.gz'); $this->response->forceDownload('db.sqlite.gz');
$this->response->binary($this->config->downloadDatabase()); $this->response->binary($this->config->downloadDatabase());
} }
// Optimize the database /**
* Optimize the Sqlite database
*
* @access public
*/
public function optimizeDb() public function optimizeDb()
{ {
$this->config->optimizeDatabase(); $this->config->optimizeDatabase();
$this->session->flash(t('Database optimization done.')); $this->session->flash(t('Database optimization done.'));
$this->response->redirect('?controller=config'); $this->response->redirect('?controller=config');
} }
/**
* Regenerate all application tokens
*
* @access public
*/
public function tokens()
{
$this->config->regenerateTokens();
$this->session->flash(t('All tokens have been regenerated.'));
$this->response->redirect('?controller=config');
}
} }

View File

@@ -267,4 +267,6 @@ return array(
'Current password for the user "%s"' => 'Mot de passe actuel pour l\'utilisateur « %s »', 'Current password for the user "%s"' => 'Mot de passe actuel pour l\'utilisateur « %s »',
'The current password is required' => 'Le mot de passe actuel est obligatoire', 'The current password is required' => 'Le mot de passe actuel est obligatoire',
'Wrong password' => 'Mauvais mot de passe', 'Wrong password' => 'Mauvais mot de passe',
'Reset all tokens' => 'Réinitialiser tous les jetons de sécurité',
'All tokens have been regenerated.' => 'Tous les jetons de sécurité ont été réinitialisés.',
); );

View File

@@ -270,4 +270,6 @@ return array(
// 'Current password for the user "%s"' => '', // 'Current password for the user "%s"' => '',
// 'The current password is required' => '', // 'The current password is required' => '',
// 'Wrong password' => '', // 'Wrong password' => '',
// 'Reset all tokens' => '',
// 'All tokens have been regenerated.' => '',
); );

View File

@@ -7,16 +7,39 @@ require_once __DIR__.'/base.php';
use \SimpleValidator\Validator; use \SimpleValidator\Validator;
use \SimpleValidator\Validators; use \SimpleValidator\Validators;
/**
* Config model
*
* @package model
* @author Frederic Guillot
*/
class Config extends Base class Config extends Base
{ {
/**
* SQL table name
*
* @var string
*/
const TABLE = 'config'; const TABLE = 'config';
/**
* Get available timezones
*
* @access public
* @return array
*/
public function getTimezones() public function getTimezones()
{ {
$timezones = \timezone_identifiers_list(); $timezones = \timezone_identifiers_list();
return array_combine(array_values($timezones), $timezones); return array_combine(array_values($timezones), $timezones);
} }
/**
* Get available languages
*
* @access public
* @return array
*/
public function getLanguages() public function getLanguages()
{ {
$languages = array( $languages = array(
@@ -30,6 +53,14 @@ class Config extends Base
return $languages; return $languages;
} }
/**
* Get a config variable from the session or the database
*
* @access public
* @param string $name Parameter name
* @param mixed $default_value Default value of the parameter
* @return mixed
*/
public function get($name, $default_value = '') public function get($name, $default_value = '')
{ {
if (! isset($_SESSION['config'][$name])) { if (! isset($_SESSION['config'][$name])) {
@@ -43,17 +74,35 @@ class Config extends Base
return $default_value; return $default_value;
} }
/**
* Get all settings
*
* @access public
* @return array
*/
public function getAll() public function getAll()
{ {
return $this->db->table(self::TABLE)->findOne(); return $this->db->table(self::TABLE)->findOne();
} }
/**
* Save settings in the database
*
* @access public
* @param $values array Settings values
* @return boolean
*/
public function save(array $values) public function save(array $values)
{ {
$_SESSION['config'] = $values; $_SESSION['config'] = $values;
return $this->db->table(self::TABLE)->update($values); return $this->db->table(self::TABLE)->update($values);
} }
/**
* Reload settings in the session and the translations
*
* @access public
*/
public function reload() public function reload()
{ {
$_SESSION['config'] = $this->getAll(); $_SESSION['config'] = $this->getAll();
@@ -62,10 +111,18 @@ class Config extends Base
if ($language !== 'en_US') \Translator\load($language); if ($language !== 'en_US') \Translator\load($language);
} }
/**
* Validate settings modification
*
* @access public
* @param array $values Form values
* @return array $valid, $errors [0] = Success or not, [1] = List of errors
*/
public function validateModification(array $values) public function validateModification(array $values)
{ {
$v = new Validator($values, array( $v = new Validator($values, array(
new Validators\Required('language', t('The language is required')), new Validators\Required('language', t('The language is required')),
new Validators\Required('timezone', t('The timezone is required')),
)); ));
return array( return array(
@@ -74,18 +131,52 @@ class Config extends Base
); );
} }
/**
* Optimize the Sqlite database
*
* @access public
* @return boolean
*/
public function optimizeDatabase() public function optimizeDatabase()
{ {
$this->db->getconnection()->exec("VACUUM"); return $this->db->getconnection()->exec("VACUUM");
} }
/**
* Compress the Sqlite database
*
* @access public
* @return string
*/
public function downloadDatabase() public function downloadDatabase()
{ {
return gzencode(file_get_contents(DB_FILENAME)); return gzencode(file_get_contents(DB_FILENAME));
} }
/**
* Get the Sqlite database size in bytes
*
* @access public
* @return integer
*/
public function getDatabaseSize() public function getDatabaseSize()
{ {
return filesize(DB_FILENAME); return DB_DRIVER === 'sqlite' ? filesize(DB_FILENAME) : 0;
}
/**
* Regenerate all tokens (projects and webhooks)
*
* @access public
*/
public function regenerateTokens()
{
$this->db->table(self::TABLE)->update(array('webhooks_token' => $this->generateToken()));
$projects = $this->db->table(Project::TABLE)->findAllByColumn('id');
foreach ($projects as $project_id) {
$this->db->table(Project::TABLE)->eq('id', $project_id)->update(array('token' => $this->generateToken()));
}
} }
} }

View File

@@ -23,6 +23,7 @@
</div> </div>
<section class="settings"> <section class="settings">
<ul> <ul>
<li><a href="?controller=config&amp;action=tokens"><?= t('Reset all tokens') ?></a></li>
<li> <li>
<?= t('Webhooks token:') ?> <?= t('Webhooks token:') ?>
<strong><?= Helper\escape($values['webhooks_token']) ?></strong> <strong><?= Helper\escape($values['webhooks_token']) ?></strong>