ResetShell.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. // Enhancement for plugin user model
  3. if (!defined('CLASS_USER')) {
  4. define('CLASS_USER', 'User');
  5. }
  6. App::uses('AppShell', 'Console/Command');
  7. /**
  8. * Reset user data
  9. *
  10. * @author Mark Scherer
  11. * @license http://opensource.org/licenses/mit-license.php MIT
  12. */
  13. class ResetShell extends AppShell {
  14. public $Auth = null;
  15. /**
  16. * ResetShell::main()
  17. *
  18. * @return void
  19. */
  20. public function main() {
  21. $this->help();
  22. }
  23. /**
  24. * Resets all emails - e.g. to your admin email (for local development).
  25. *
  26. * @return void
  27. */
  28. public function email() {
  29. $this->out('Email:');
  30. App::uses('Validation', 'Utility');
  31. while (empty($email) || !Validation::email($email)) {
  32. $email = $this->in('New email address (must have a valid form at least)');
  33. }
  34. $this->User = ClassRegistry::init(CLASS_USER);
  35. if (!$this->User->hasField('email')) {
  36. return $this->error(CLASS_USER . ' model doesnt have an email field!');
  37. }
  38. $this->hr();
  39. $this->out('resetting...');
  40. $this->User->updateAll(['User.email' => '\'' . $email . '\''], ['User.email !=' => $email]);
  41. $count = $this->User->getAffectedRows();
  42. $this->out($count . ' emails resetted - DONE');
  43. }
  44. /**
  45. * Resets all pwds to a simple pwd (for local development).
  46. *
  47. * @return void
  48. */
  49. public function pwd() {
  50. $components = ['Tools.AuthExt', 'Auth'];
  51. foreach ($components as $component) {
  52. if (App::import('Component', $component)) {
  53. $component .= 'Component';
  54. list($plugin, $component) = pluginSplit($component);
  55. $this->Auth = new $component(new ComponentCollection());
  56. break;
  57. }
  58. }
  59. if (!is_object($this->Auth)) {
  60. return $this->error('No Auth Component found');
  61. }
  62. $this->out('Using: ' . get_class($this->Auth) . ' (Abort with STRG+C)');
  63. if (!empty($this->args[0]) && mb_strlen($this->args[0]) >= 2) {
  64. $pwToHash = $this->args[0];
  65. }
  66. while (empty($pwToHash) || mb_strlen($pwToHash) < 2) {
  67. $pwToHash = $this->in('Password to Hash (2 characters at least)');
  68. }
  69. $this->hr();
  70. $this->out('Password:');
  71. $this->out($pwToHash);
  72. if ($authType = Configure::read('Passwordable.authType')) {
  73. list($plugin, $authType) = pluginSplit($authType, true);
  74. $className = $authType . 'PasswordHasher';
  75. App::uses($className, $plugin . 'Controller/Component/Auth');
  76. $passwordHasher = new $className();
  77. $pw = $passwordHasher->hash($pwToHash);
  78. } else {
  79. $pw = $this->Auth->password($pwToHash);
  80. }
  81. $this->hr();
  82. $this->out('Hash:');
  83. $this->out($pw);
  84. $this->hr();
  85. $this->out('resetting...');
  86. $this->User = ClassRegistry::init(CLASS_USER);
  87. if (!$this->User->hasField('password')) {
  88. return $this->error(CLASS_USER . ' model doesnt have a password field!');
  89. }
  90. $newPwd = '\'' . $pw . '\'';
  91. $this->User->updateAll(['password' => $newPwd], ['password !=' => $pw]);
  92. $count = $this->User->getAffectedRows();
  93. $this->out($count . ' pwds resetted - DONE');
  94. }
  95. /**
  96. * ResetShell::help()
  97. *
  98. * @return void
  99. */
  100. public function help() {
  101. $this->out('-- pwd: Hash and Reset all user passwords with Auth(Ext) Component --');
  102. $this->out('-- email: Reset all user emails --');
  103. }
  104. }