ResetShell.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. Configure::write('debug', 2);
  41. $this->User->recursive = -1;
  42. $this->User->updateAll(array('User.email' => '\'' . $email . '\''), array('User.email !=' => $email));
  43. $count = $this->User->getAffectedRows();
  44. $this->out($count . ' emails resetted - DONE');
  45. }
  46. /**
  47. * Resets all pwds to a simple pwd (for local development).
  48. *
  49. * @return void
  50. */
  51. public function pwd() {
  52. $components = array('Tools.AuthExt', 'Auth');
  53. foreach ($components as $component) {
  54. if (App::import('Component', $component)) {
  55. $component .= 'Component';
  56. list($plugin, $component) = pluginSplit($component);
  57. $this->Auth = new $component(new ComponentCollection());
  58. break;
  59. }
  60. }
  61. if (!is_object($this->Auth)) {
  62. return $this->error('No Auth Component found');
  63. }
  64. $this->out('Using: ' . get_class($this->Auth) . ' (Abort with STRG+C)');
  65. if (!empty($this->args[0]) && mb_strlen($this->args[0]) >= 2) {
  66. $pwToHash = $this->args[0];
  67. }
  68. while (empty($pwToHash) || mb_strlen($pwToHash) < 2) {
  69. $pwToHash = $this->in('Password to Hash (2 characters at least)');
  70. }
  71. $this->hr();
  72. $this->out('Password:');
  73. $this->out($pwToHash);
  74. if ($authType = Configure::read('Passwordable.authType')) {
  75. list($plugin, $authType) = pluginSplit($authType, true);
  76. $className = $authType . 'PasswordHasher';
  77. App::uses($className, $plugin . 'Controller/Component/Auth');
  78. $passwordHasher = new $className();
  79. $pw = $passwordHasher->hash($pwToHash);
  80. } else {
  81. $pw = $this->Auth->password($pwToHash);
  82. }
  83. $this->hr();
  84. $this->out('Hash:');
  85. $this->out($pw);
  86. $this->hr();
  87. $this->out('resetting...');
  88. $this->User = ClassRegistry::init(CLASS_USER);
  89. if (!$this->User->hasField('password')) {
  90. return $this->error(CLASS_USER . ' model doesnt have a password field!');
  91. }
  92. $newPwd = '\'' . $pw . '\'';
  93. $this->User->recursive = -1;
  94. $this->User->updateAll(array('password' => $newPwd), array('password !=' => $pw));
  95. $count = $this->User->getAffectedRows();
  96. $this->out($count . ' pwds resetted - DONE');
  97. }
  98. /**
  99. * ResetShell::help()
  100. *
  101. * @return void
  102. */
  103. public function help() {
  104. $this->out('-- pwd: Hash and Reset all user passwords with Auth(Ext) Component --');
  105. $this->out('-- email: Reset all user emails --');
  106. }
  107. }