PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/core/Updates/2.0.4-b5.php

https://github.com/CodeYellowBV/piwik
PHP | 92 lines | 67 code | 13 blank | 12 comment | 5 complexity | b2641d44377a742b57ecca2502b756e6 MD5 | raw file
Possible License(s): LGPL-3.0, JSON, MIT, GPL-3.0, LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-2-Clause, BSD-3-Clause
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. *
  8. */
  9. namespace Piwik\Updates;
  10. use Piwik\Common;
  11. use Piwik\Config;
  12. use Piwik\Date;
  13. use Piwik\Db;
  14. use Piwik\Plugins\UsersManager\API as UsersManagerApi;
  15. use Piwik\Updater;
  16. use Piwik\UpdaterErrorException;
  17. use Piwik\Updates;
  18. /**
  19. */
  20. class Updates_2_0_4_b5 extends Updates
  21. {
  22. static function getSql()
  23. {
  24. return array(
  25. // ignore existing column name error (1060)
  26. 'ALTER TABLE ' . Common::prefixTable('user')
  27. . " ADD COLUMN `superuser_access` tinyint(2) unsigned NOT NULL DEFAULT '0' AFTER token_auth" => 1060,
  28. );
  29. }
  30. static function update()
  31. {
  32. Updater::updateDatabase(__FILE__, self::getSql());
  33. try {
  34. self::migrateConfigSuperUserToDb();
  35. } catch (\Exception $e) {
  36. throw new UpdaterErrorException($e->getMessage());
  37. }
  38. }
  39. private static function migrateConfigSuperUserToDb()
  40. {
  41. $config = Config::getInstance();
  42. if (!$config->existsLocalConfig()) {
  43. return;
  44. }
  45. try {
  46. $superUser = $config->superuser;
  47. } catch (\Exception $e) {
  48. $superUser = null;
  49. }
  50. if (!empty($superUser['bridge']) || empty($superUser)) {
  51. // there is a super user which is not from the config but from the bridge, that means we already have
  52. // a super user in the database
  53. return;
  54. }
  55. $userApi = UsersManagerApi::getInstance();
  56. try {
  57. Db::get()->insert(Common::prefixTable('user'), array(
  58. 'login' => $superUser['login'],
  59. 'password' => $superUser['password'],
  60. 'alias' => $superUser['login'],
  61. 'email' => $superUser['email'],
  62. 'token_auth' => $userApi->getTokenAuth($superUser['login'], $superUser['password']),
  63. 'date_registered' => Date::now()->getDatetime(),
  64. 'superuser_access' => 1
  65. )
  66. );
  67. } catch(\Exception $e) {
  68. echo "There was an issue, but we proceed: " . $e->getMessage();
  69. }
  70. if (array_key_exists('salt', $superUser)) {
  71. $salt = $superUser['salt'];
  72. } else {
  73. $salt = Common::generateUniqId();
  74. }
  75. $config->General['salt'] = $salt;
  76. $config->superuser = array();
  77. $config->forceSave();
  78. }
  79. }