ResetShell.php 3.2 KB

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