Add the possiblity to regenerate all tokens
This commit is contained in:
parent
d9dfd9d619
commit
99953ab62a
|
|
@ -4,9 +4,19 @@ namespace Controller;
|
|||
|
||||
require_once __DIR__.'/base.php';
|
||||
|
||||
/**
|
||||
* Config controller
|
||||
*
|
||||
* @package controller
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
class Config extends Base
|
||||
{
|
||||
// Settings page
|
||||
/**
|
||||
* Display the settings page
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$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()
|
||||
{
|
||||
$values = $this->request->getValues();
|
||||
|
|
@ -53,18 +67,38 @@ class Config extends Base
|
|||
)));
|
||||
}
|
||||
|
||||
// Download the database
|
||||
/**
|
||||
* Download the Sqlite database
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function downloadDb()
|
||||
{
|
||||
$this->response->forceDownload('db.sqlite.gz');
|
||||
$this->response->binary($this->config->downloadDatabase());
|
||||
}
|
||||
|
||||
// Optimize the database
|
||||
/**
|
||||
* Optimize the Sqlite database
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function optimizeDb()
|
||||
{
|
||||
$this->config->optimizeDatabase();
|
||||
$this->session->flash(t('Database optimization done.'));
|
||||
$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');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -267,4 +267,6 @@ return array(
|
|||
'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',
|
||||
'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.',
|
||||
);
|
||||
|
|
|
|||
|
|
@ -270,4 +270,6 @@ return array(
|
|||
// 'Current password for the user "%s"' => '',
|
||||
// 'The current password is required' => '',
|
||||
// 'Wrong password' => '',
|
||||
// 'Reset all tokens' => '',
|
||||
// 'All tokens have been regenerated.' => '',
|
||||
);
|
||||
|
|
|
|||
|
|
@ -7,16 +7,39 @@ require_once __DIR__.'/base.php';
|
|||
use \SimpleValidator\Validator;
|
||||
use \SimpleValidator\Validators;
|
||||
|
||||
/**
|
||||
* Config model
|
||||
*
|
||||
* @package model
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
class Config extends Base
|
||||
{
|
||||
/**
|
||||
* SQL table name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const TABLE = 'config';
|
||||
|
||||
/**
|
||||
* Get available timezones
|
||||
*
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
public function getTimezones()
|
||||
{
|
||||
$timezones = \timezone_identifiers_list();
|
||||
return array_combine(array_values($timezones), $timezones);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get available languages
|
||||
*
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
public function getLanguages()
|
||||
{
|
||||
$languages = array(
|
||||
|
|
@ -30,6 +53,14 @@ class Config extends Base
|
|||
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 = '')
|
||||
{
|
||||
if (! isset($_SESSION['config'][$name])) {
|
||||
|
|
@ -43,17 +74,35 @@ class Config extends Base
|
|||
return $default_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all settings
|
||||
*
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
public function getAll()
|
||||
{
|
||||
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)
|
||||
{
|
||||
$_SESSION['config'] = $values;
|
||||
return $this->db->table(self::TABLE)->update($values);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reload settings in the session and the translations
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function reload()
|
||||
{
|
||||
$_SESSION['config'] = $this->getAll();
|
||||
|
|
@ -62,10 +111,18 @@ class Config extends Base
|
|||
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)
|
||||
{
|
||||
$v = new Validator($values, array(
|
||||
new Validators\Required('language', t('The language is required')),
|
||||
new Validators\Required('timezone', t('The timezone is required')),
|
||||
));
|
||||
|
||||
return array(
|
||||
|
|
@ -74,18 +131,52 @@ class Config extends Base
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Optimize the Sqlite database
|
||||
*
|
||||
* @access public
|
||||
* @return boolean
|
||||
*/
|
||||
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()
|
||||
{
|
||||
return gzencode(file_get_contents(DB_FILENAME));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Sqlite database size in bytes
|
||||
*
|
||||
* @access public
|
||||
* @return integer
|
||||
*/
|
||||
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()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@
|
|||
</div>
|
||||
<section class="settings">
|
||||
<ul>
|
||||
<li><a href="?controller=config&action=tokens"><?= t('Reset all tokens') ?></a></li>
|
||||
<li>
|
||||
<?= t('Webhooks token:') ?>
|
||||
<strong><?= Helper\escape($values['webhooks_token']) ?></strong>
|
||||
|
|
|
|||
Loading…
Reference in New Issue